[sheepdog] [PATCH] collie: clean up and fix 'vdi track'

Liu Yuan namei.unix at gmail.com
Fri Nov 23 06:28:29 CET 2012


From: Liu Yuan <tailai.ly at taobao.com>

- fix one error handling
- fix nr_copies calculation
- add some comments
- rename it as do_track_object() because it basically does more than print
- refactor vdi_track()

Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
 collie/vdi.c |   81 ++++++++++++++++++++++++++++------------------------------
 1 file changed, 39 insertions(+), 42 deletions(-)

diff --git a/collie/vdi.c b/collie/vdi.c
index 99e2ea1..108cb1d 100644
--- a/collie/vdi.c
+++ b/collie/vdi.c
@@ -864,7 +864,7 @@ static int vdi_object(int argc, char **argv)
 	return EXIT_SUCCESS;
 }
 
-static int print_obj_epoch(uint64_t oid, uint8_t nr_copies)
+static int do_track_object(uint64_t oid, uint8_t nr_copies)
 {
 	int i, j, fd, ret;
 	struct sd_req hdr;
@@ -876,17 +876,7 @@ static int print_obj_epoch(uint64_t oid, uint8_t nr_copies)
 	char host[128];
 
 	log_length = sd_epoch * sizeof(struct epoch_log);
-again:
-	logs = malloc(log_length);
-	if (!logs) {
-		if (log_length < 10) {
-			fprintf(stderr, "No memory to allocate.\n");
-			return EXIT_SYSFAIL;
-		}
-		log_length /= 2;
-		goto again;
-	}
-
+	logs = xmalloc(log_length);
 	fd = connect_to(sdhost, sdport);
 	if (fd < 0)
 		goto error;
@@ -900,14 +890,21 @@ again:
 	if (ret != 0)
 		goto error;
 
-	if (rsp->result != SD_RES_SUCCESS)
+	if (rsp->result != SD_RES_SUCCESS) {
 		printf("%s\n", sd_strerror(rsp->result));
+		goto error;
+	}
 
 	nr_logs = rsp->data_length / sizeof(struct epoch_log);
 	for (i = nr_logs - 1; i >= 0; i--) {
 		printf("\nobj %"PRIx64" locations at epoch %d, copies = %d\n",
 		       oid, logs[i].epoch, nr_copies);
 		printf("---------------------------------------------------\n");
+
+		/*
+		 * When # of nodes is less than nr_copies, we only print
+		 * remaining nodes that holds all the remaining copies.
+		 */
 		if (logs[i].nr_nodes < nr_copies) {
 			for (j = 0; j < logs[i].nr_nodes; j++) {
 				addr_to_str(host, sizeof(host),
@@ -919,9 +916,8 @@ again:
 		}
 		vnodes_nr = nodes_to_vnodes(logs[i].nodes,
 					    logs[i].nr_nodes, vnodes);
-		oid_to_vnodes(vnodes, vnodes_nr, oid, logs[i].nr_copies,
-			      vnode_buf);
-		for (j = 0; j < logs[i].nr_copies; j++) {
+		oid_to_vnodes(vnodes, vnodes_nr, oid, nr_copies, vnode_buf);
+		for (j = 0; j < nr_copies; j++) {
 			addr_to_str(host, sizeof(host), vnode_buf[j]->nid.addr,
 				    vnode_buf[j]->nid.port);
 			printf("%s\n", host);
@@ -940,6 +936,7 @@ static int vdi_track(int argc, char **argv)
 	const char *vdiname = argv[optind];
 	unsigned idx = vdi_cmd_data.index;
 	struct get_vdi_info info;
+	struct get_data_oid_info oid_info = {0};
 	uint32_t vid;
 	uint8_t nr_copies;
 
@@ -962,37 +959,37 @@ static int vdi_track(int argc, char **argv)
 	if (idx == ~0) {
 		printf("Tracking the inode object 0x%" PRIx32 " with %d nodes\n",
 		       vid, sd_nodes_nr);
-		print_obj_epoch(vid_to_vdi_oid(vid), nr_copies);
-	} else {
-		struct get_data_oid_info oid_info = {0};
+		return do_track_object(vid_to_vdi_oid(vid), nr_copies);
+	}
 
-		oid_info.success = false;
-		oid_info.idx = idx;
+	oid_info.success = false;
+	oid_info.idx = idx;
 
-		if (idx >= MAX_DATA_OBJS) {
-			printf("The offset is too large!\n");
-			exit(EXIT_FAILURE);
-		}
-
-		parse_objs(vid_to_vdi_oid(vid), get_data_oid,
-					&oid_info, SD_DATA_OBJ_SIZE);
+	if (idx >= MAX_DATA_OBJS) {
+		printf("The offset is too large!\n");
+		goto err;
+	}
 
-		if (oid_info.success) {
-			if (oid_info.data_oid) {
-				printf("Tracking the object 0x%" PRIx64
-				       " (the inode vid 0x%" PRIx32 " idx %u)"
-					   " with %d nodes\n",
-				       oid_info.data_oid, vid, idx, sd_nodes_nr);
-				print_obj_epoch(oid_info.data_oid, nr_copies);
+	parse_objs(vid_to_vdi_oid(vid), get_data_oid,
+		   &oid_info, SD_DATA_OBJ_SIZE);
 
-			} else
-				printf("The inode object 0x%" PRIx32 " idx %u is not allocated\n",
-				       vid, idx);
-		} else
-			fprintf(stderr, "Failed to read the inode object 0x%"PRIx32"\n", vid);
+	if (!oid_info.success) {
+		fprintf(stderr, "Failed to read the inode object 0x%"PRIx32"\n",
+			vid);
+		goto err;
 	}
-
-	return EXIT_SUCCESS;
+	if (!oid_info.data_oid) {
+		printf("The inode object 0x%"PRIx32" idx %u is not allocated\n",
+		       vid, idx);
+		goto err;
+	}
+	printf("Tracking the object 0x%" PRIx64
+	       " (the inode vid 0x%" PRIx32 " idx %u)"
+	       " with %d nodes\n",
+	       oid_info.data_oid, vid, idx, sd_nodes_nr);
+	return do_track_object(oid_info.data_oid, nr_copies);
+err:
+	return EXIT_FAILURE;
 }
 
 static int find_vdi_attr_oid(const char *vdiname, const char *tag, uint32_t snapid,
-- 
1.7.9.5




More information about the sheepdog mailing list