[sheepdog] [PATCH V2 10/11] collie: update 'collie cluster recover info' commands

Yunkai Zhang yunkai.me at gmail.com
Thu Aug 9 10:43:48 CEST 2012


From: Yunkai Zhang <qiushu.zyk at taobao.com>

Show recovery status and inner joining/leaving nodes to user.
 $ collie cluster recover info
 Status: disable
 Joining nodes in inner temporary list:
 --------------------------------------
 Id               Host:Port
  0               127.0.0.1:7005
  1               127.0.0.1:7008
  2               127.0.0.1:7007
  3               127.0.0.1:7006

 Leaving nodes in inner temporary list:
 --------------------------------------
 Id               Host:Port
  4               127.0.0.1:7004
  5               127.0.0.1:7003

Signed-off-by: Yunkai Zhang <qiushu.zyk at taobao.com>
---
 collie/cluster.c | 39 ++++++++++++++++++++++++++++++---------
 sheep/ops.c      | 16 ++++++++++++++--
 2 files changed, 44 insertions(+), 11 deletions(-)

diff --git a/collie/cluster.c b/collie/cluster.c
index 9302b78..b0b5e95 100644
--- a/collie/cluster.c
+++ b/collie/cluster.c
@@ -13,6 +13,7 @@
 #include <string.h>
 #include <ctype.h>
 #include <sys/time.h>
+#include <assert.h>
 
 #include "collie.h"
 
@@ -350,14 +351,17 @@ Are you sure you want to continue? [yes/no]: "
 
 static int cluster_info_recover(int argc, char **argv)
 {
-	int fd, ret = EXIT_SYSFAIL;
+	int i, fd, ret = EXIT_SYSFAIL;
 	struct sd_req hdr;
 	struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
 	unsigned rlen, wlen;
 	void *buf;
-	int i, nr_nodes;
+	char ipaddr[128];
+	struct sd_node *nodes;
+	size_t nr_nodes, nr_joining_nodes, nr_leaving_nodes, *p;
 	const char *status[] = {"enable", "disable"};
 
+
 	wlen = 0;
 	rlen = SD_MAX_NODES * sizeof(struct sd_node);
 
@@ -387,20 +391,37 @@ static int cluster_info_recover(int argc, char **argv)
 	}
 	nr_nodes = rsp->data_length/sizeof(struct sd_node);
 
+	p = (size_t *)&rsp->__pad[1];
+	nr_joining_nodes = *p++;
+	nr_leaving_nodes = *p;
+
+	assert(nr_nodes == (nr_joining_nodes + nr_leaving_nodes));
+
 	printf("Status: %s\n", status[rsp->__pad[0]]);
 	printf("Joining nodes in inner temporary list:\n"
 	       "--------------------------------------\n"
 	       "Id               Host:Port\n");
-	for (i = 0; i < nr_nodes; i++) {
-		char ipaddr[128];
-		struct sd_node *rnodes;
-
-		rnodes = (struct sd_node *)buf;
-		addr_to_str(ipaddr, sizeof(ipaddr), rnodes[i].nid.addr,
-			    rnodes[i].nid.port);
+	for (i = 0; i < nr_joining_nodes; i++) {
+		nodes = (struct sd_node *)buf;
+		addr_to_str(ipaddr, sizeof(ipaddr), nodes[i].nid.addr,
+			    nodes[i].nid.port);
 		printf("%2d               %s\n", i, ipaddr);
 	}
 
+	printf("\nLeaving nodes in inner temporary list:\n"
+	       "--------------------------------------\n"
+	       "Id               Host:Port\n");
+	for (i = nr_joining_nodes; i < nr_nodes; i++) {
+		nodes = (struct sd_node *)buf;
+
+		/* ignore joined back node */
+		if (!nodes[i].left)
+			continue;
+
+		addr_to_str(ipaddr, sizeof(ipaddr), nodes[i].nid.addr,
+			    nodes[i].nid.port);
+		printf("%2d               %s\n", i, ipaddr);
+	}
 
 out:
 	free(buf);
diff --git a/sheep/ops.c b/sheep/ops.c
index de0dae7..ed8c7d4 100644
--- a/sheep/ops.c
+++ b/sheep/ops.c
@@ -323,10 +323,22 @@ static int cluster_disable_recover(const struct sd_req *req,
 static int local_info_recover(const struct sd_req *req,
 			      struct sd_rsp *rsp, void *data)
 {
+	size_t nr, *p;
+	struct sd_node *nodes = data;
+
 	rsp->__pad[0] = sys->disable_recovery;
-	rsp->data_length = nr_joining_nodes * sizeof(struct sd_node);
-	memcpy(data, joining_nodes, rsp->data_length);
 
+	p = (size_t *)&rsp->__pad[1];
+	memcpy(p, &nr_joining_nodes, sizeof(nr_joining_nodes));
+	memcpy(p + 1, &nr_leaving_nodes, sizeof(nr_leaving_nodes));
+
+	nr = nr_joining_nodes;
+	memcpy(nodes, joining_nodes, nr * sizeof(*nodes));
+
+	nr += nr_leaving_nodes;
+	memcpy(nodes + nr_joining_nodes, leaving_nodes, nr * sizeof(*nodes));
+
+	rsp->data_length = nr * sizeof(*nodes);
 	return SD_RES_SUCCESS;
 }
 
-- 
1.7.11.2




More information about the sheepdog mailing list