[sheepdog] [PATCH v3 04/13] collie: fix 'node info'

Liu Yuan namei.unix at gmail.com
Mon May 27 13:36:07 CEST 2013


Always return the actual size instead of from sys->disk_space

Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 collie/node.c       |   27 +++++++++++++++++----------
 sheep/md.c          |   25 +++++++++++++++++++------
 sheep/ops.c         |   35 ++++++++---------------------------
 sheep/sheep_priv.h  |    1 +
 tests/016.out       |   10 +++++-----
 tests/042           |    2 +-
 tests/042.out       |   48 ++++++++++++++++++++++++------------------------
 tests/044.out       |   10 +++++-----
 tests/058.out       |   20 ++++++++++----------
 tests/common.filter |    7 ++++++-
 10 files changed, 96 insertions(+), 89 deletions(-)

diff --git a/collie/node.c b/collie/node.c
index 3fe1108..0fc645c 100644
--- a/collie/node.c
+++ b/collie/node.c
@@ -58,16 +58,19 @@ static int node_info(int argc, char **argv)
 {
 	int i, ret, success = 0;
 	uint64_t total_size = 0, total_avail = 0, total_vdi_size = 0;
-	char total_str[UINT64_DECIMAL_SIZE], avail_str[UINT64_DECIMAL_SIZE], vdi_size_str[UINT64_DECIMAL_SIZE];
+	char total_str[UINT64_DECIMAL_SIZE], use_str[UINT64_DECIMAL_SIZE],
+	     avail_str[UINT64_DECIMAL_SIZE], vdi_size_str[UINT64_DECIMAL_SIZE];
 
 	if (!raw_output)
-		printf("Id\tSize\tUsed\tUse%%\n");
+		printf("Id\tSize\tUsed\tAvail\tUse%%\n");
 
 	for (i = 0; i < sd_nodes_nr; i++) {
 		char host[128];
 		struct sd_req req;
 		struct sd_rsp *rsp = (struct sd_rsp *)&req;
-		char store_str[UINT64_DECIMAL_SIZE], free_str[UINT64_DECIMAL_SIZE];
+		char store_str[UINT64_DECIMAL_SIZE],
+		     used_str[UINT64_DECIMAL_SIZE],
+		     free_str[UINT64_DECIMAL_SIZE];
 
 		addr_to_str(host, sizeof(host), sd_nodes[i].nid.addr, 0);
 
@@ -76,14 +79,16 @@ static int node_info(int argc, char **argv)
 		ret = send_light_req(&req, host, sd_nodes[i].nid.port);
 
 		size_to_str(rsp->node.store_size, store_str, sizeof(store_str));
+		size_to_str(rsp->node.store_free, free_str, sizeof(free_str));
 		size_to_str(rsp->node.store_size - rsp->node.store_free,
-			    free_str, sizeof(free_str));
+			    used_str, sizeof(used_str));
 		if (!ret) {
 			int ratio = (int)(((double)(rsp->node.store_size -
 						    rsp->node.store_free) /
 					   rsp->node.store_size) * 100);
-			printf(raw_output ? "%d %s %s %d%%\n" : "%2d\t%s\t%s\t%3d%%\n",
-			       i, store_str, free_str,
+			printf(raw_output ? "%d %s %s %s %d%%\n" :
+					"%2d\t%s\t%s\t%s\t%3d%%\n",
+			       i, store_str, used_str, free_str,
 			       rsp->node.store_size == 0 ? 0 : ratio);
 			success++;
 		}
@@ -102,11 +107,13 @@ static int node_info(int argc, char **argv)
 		return EXIT_SYSFAIL;
 
 	size_to_str(total_size, total_str, sizeof(total_str));
-	size_to_str(total_size - total_avail, avail_str, sizeof(avail_str));
+	size_to_str(total_avail, avail_str, sizeof(avail_str));
+	size_to_str(total_size - total_avail, use_str, sizeof(use_str));
 	size_to_str(total_vdi_size, vdi_size_str, sizeof(vdi_size_str));
-	printf(raw_output ? "Total %s %s %d%% %s\n"
-			  : "Total\t%s\t%s\t%3d%%\n\nTotal virtual image size\t%s\n",
-	       total_str, avail_str,
+	printf(raw_output ? "Total %s %s %s %d%% %s\n"
+			  : "Total\t%s\t%s\t%s\t%3d%%\n\n"
+			  "Total virtual image size\t%s\n",
+	       total_str, use_str, avail_str,
 	       (int)(((double)(total_size - total_avail) / total_size) * 100),
 	       vdi_size_str);
 
diff --git a/sheep/md.c b/sheep/md.c
index 357f410..1d034cd 100644
--- a/sheep/md.c
+++ b/sheep/md.c
@@ -241,7 +241,7 @@ static int for_each_object_in_path(char *path,
 	return ret;
 }
 
-static uint64_t get_path_size(char *path, uint64_t *used)
+static uint64_t get_path_free_size(char *path, uint64_t *used)
 {
 	struct statvfs fs;
 	uint64_t size;
@@ -293,7 +293,7 @@ static uint64_t init_path_space(char *path)
 
 	return size;
 create:
-	size = get_path_size(path, NULL);
+	size = get_path_free_size(path, NULL);
 	if (!size)
 		goto broken_path;
 	if (setxattr(path, MDNAME, &size, MDSIZE, 0) < 0) {
@@ -439,7 +439,7 @@ static void md_do_recover(struct work *work)
 		/* Just ignore the duplicate EIO of the same path */
 		goto out;
 	remove_disk(idx);
-	sys->disk_space = md_init_space();
+	md_init_space();
 	nr = md_nr_disks;
 out:
 	pthread_rwlock_unlock(&md_lock);
@@ -613,8 +613,8 @@ uint32_t md_get_info(struct sd_md_info *info)
 		info->disk[i].idx = i;
 		pstrcpy(info->disk[i].path, PATH_MAX, md_disks[i].path);
 		/* FIXME: better handling failure case. */
-		info->disk[i].size = get_path_size(info->disk[i].path,
-						   &info->disk[i].used);
+		info->disk[i].size = get_path_free_size(info->disk[i].path,
+							&info->disk[i].used);
 	}
 	info->nr = md_nr_disks;
 	pthread_rwlock_unlock(&md_lock);
@@ -653,7 +653,7 @@ static int do_plug_unplug(char *disks, bool plug)
 	if (old_nr == md_nr_disks)
 		goto out;
 
-	sys->disk_space = md_init_space();
+	md_init_space();
 	cur_nr = md_nr_disks;
 
 	ret = SD_RES_SUCCESS;
@@ -680,3 +680,16 @@ int md_unplug_disks(char *disks)
 {
 	return do_plug_unplug(disks, false);
 }
+
+uint64_t md_get_size(uint64_t *used)
+{
+	uint64_t fsize = 0;
+	*used = 0;
+
+	pthread_rwlock_rdlock(&md_lock);
+	for (int i = 0; i < md_nr_disks; i++)
+		fsize += get_path_free_size(md_disks[i].path, used);
+	pthread_rwlock_unlock(&md_lock);
+
+	return fsize + *used;
+}
diff --git a/sheep/ops.c b/sheep/ops.c
index 23b482e..3b416ba 100644
--- a/sheep/ops.c
+++ b/sheep/ops.c
@@ -61,38 +61,19 @@ struct sd_op_template {
 	int (*process_main)(const struct sd_req *req, struct sd_rsp *rsp, void *data);
 };
 
-static int get_total_object_size(uint64_t oid, char *wd, void *total)
-{
-	uint64_t *t = total;
-	struct stat s;
-	char path[PATH_MAX];
-
-	snprintf(path, PATH_MAX, "%s/%016" PRIx64, wd, oid);
-	if (stat(path, &s) == 0)
-		*t += s.st_blocks * SECTOR_SIZE;
-	else
-		*t += get_objsize(oid);
-
-	return SD_RES_SUCCESS;
-}
-
 static int stat_sheep(uint64_t *store_size, uint64_t *store_free,
 		      uint32_t epoch)
 {
-	uint64_t used = 0;
-	int ret;
+	uint64_t used;
 
-	ret = for_each_object_in_wd(get_total_object_size, false, &used);
-	if (ret != SD_RES_SUCCESS)
-		goto out;
-
-	*store_size = sys->disk_space;
-	if (sys->gateway_only)
+	if (sys->gateway_only) {
+		*store_size = 0;
 		*store_free = 0;
-	else
-		*store_free = sys->disk_space - used;
-out:
-	return ret;
+	} else {
+		*store_size = md_get_size(&used);
+		*store_free = *store_size - used;
+	}
+	return SD_RES_SUCCESS;
 }
 
 static int cluster_new_vdi(struct request *req)
diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index f3fb09e..febc9b9 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -413,5 +413,6 @@ int md_get_stale_path(uint64_t oid, uint32_t epoch, char *path);
 uint32_t md_get_info(struct sd_md_info *info);
 int md_plug_disks(char *disks);
 int md_unplug_disks(char *disks);
+uint64_t md_get_size(uint64_t *used);
 
 #endif
diff --git a/tests/016.out b/tests/016.out
index 271e028..88c5afd 100644
--- a/tests/016.out
+++ b/tests/016.out
@@ -3,10 +3,10 @@ using backend plain store
 there should be no vdi
   Name        Id    Size    Used  Shared    Creation time   VDI id  Copies  Tag
 there should be no object
-Id	Size	Used	Use%
-0 MASKED GB 12 MB MASKED
-1 MASKED GB 12 MB MASKED
-2 MASKED GB 12 MB MASKED
-Total MASKED GB 36 MB MASKED
+Id	Size	Used	Avail	Use%
+0 MASKED GB 12 MB MASKED GB MASTERD
+1 MASKED GB 12 MB MASKED GB MASTERD
+2 MASKED GB 12 MB MASKED GB MASTERD
+Total MASKED GB 36 MB MASKED GB MASTERD
 
 Total virtual image size	0.0 MB
diff --git a/tests/042 b/tests/042
index 7cbcb04..46f1a07 100755
--- a/tests/042
+++ b/tests/042
@@ -54,7 +54,7 @@ $COLLIE vdi create test2 100M -p 7000
 $COLLIE vdi create test3 100M -p 7002
 
 for i in `seq 0 3`; do
-    $COLLIE node info -p 700$i
+    $COLLIE node info -p 700$i | _filter_node_info
     $COLLIE node list -p 700$i
 done
 ls $STORE/*/obj/* | _filter_store | sort
diff --git a/tests/042.out b/tests/042.out
index 7dfd635..a51153f 100644
--- a/tests/042.out
+++ b/tests/042.out
@@ -46,12 +46,12 @@ Failed to write object fd32fc0000000a: Server has no space for new objects
 Failed to write VDI
 Failed to create VDI test2: Failed to write to requested VDI
 Failed to create VDI test3: Failed to write to requested VDI
-Id	Size	Used	Use%
- 0	1007 MB	104 MB	 10%
- 1	1007 MB	104 MB	 10%
- 2	19 MB	4.0 MB	 20%
- 3	19 MB	4.0 MB	 20%
-Total	2.0 GB	216 MB	 10%
+Id	Size	Used	Avail	Use%
+0 MASKED MB 104 MB MASKED MB MASTERD
+1 MASKED MB 104 MB MASKED MB MASTERD
+2 MASKED MB 4.0 MB MASKED MB MASTERD
+3 MASKED MB 4.0 MB MASKED MB MASTERD
+Total MASKED GB 216 MB MASKED GB MASTERD
 
 Total virtual image size	200 MB
 M   Id   Host:Port         V-Nodes       Zone
@@ -59,12 +59,12 @@ M   Id   Host:Port         V-Nodes       Zone
 -    1   127.0.0.1:7001      	126          1
 -    2   127.0.0.1:7002      	 2          2
 -    3   127.0.0.1:7003      	 2          3
-Id	Size	Used	Use%
- 0	1007 MB	104 MB	 10%
- 1	1007 MB	104 MB	 10%
- 2	19 MB	4.0 MB	 20%
- 3	19 MB	4.0 MB	 20%
-Total	2.0 GB	216 MB	 10%
+Id	Size	Used	Avail	Use%
+0 MASKED MB 104 MB MASKED MB MASTERD
+1 MASKED MB 104 MB MASKED MB MASTERD
+2 MASKED MB 4.0 MB MASKED MB MASTERD
+3 MASKED MB 4.0 MB MASKED MB MASTERD
+Total MASKED GB 216 MB MASKED GB MASTERD
 
 Total virtual image size	200 MB
 M   Id   Host:Port         V-Nodes       Zone
@@ -72,12 +72,12 @@ M   Id   Host:Port         V-Nodes       Zone
 -    1   127.0.0.1:7001      	126          1
 -    2   127.0.0.1:7002      	 2          2
 -    3   127.0.0.1:7003      	 2          3
-Id	Size	Used	Use%
- 0	1007 MB	104 MB	 10%
- 1	1007 MB	104 MB	 10%
- 2	19 MB	4.0 MB	 20%
- 3	19 MB	4.0 MB	 20%
-Total	2.0 GB	216 MB	 10%
+Id	Size	Used	Avail	Use%
+0 MASKED MB 104 MB MASKED MB MASTERD
+1 MASKED MB 104 MB MASKED MB MASTERD
+2 MASKED MB 4.0 MB MASKED MB MASTERD
+3 MASKED MB 4.0 MB MASKED MB MASTERD
+Total MASKED GB 216 MB MASKED GB MASTERD
 
 Total virtual image size	200 MB
 M   Id   Host:Port         V-Nodes       Zone
@@ -85,12 +85,12 @@ M   Id   Host:Port         V-Nodes       Zone
 -    1   127.0.0.1:7001      	126          1
 -    2   127.0.0.1:7002      	 2          2
 -    3   127.0.0.1:7003      	 2          3
-Id	Size	Used	Use%
- 0	1007 MB	104 MB	 10%
- 1	1007 MB	104 MB	 10%
- 2	19 MB	4.0 MB	 20%
- 3	19 MB	4.0 MB	 20%
-Total	2.0 GB	216 MB	 10%
+Id	Size	Used	Avail	Use%
+0 MASKED MB 104 MB MASKED MB MASTERD
+1 MASKED MB 104 MB MASKED MB MASTERD
+2 MASKED MB 4.0 MB MASKED MB MASTERD
+3 MASKED MB 4.0 MB MASKED MB MASTERD
+Total MASKED GB 216 MB MASKED GB MASTERD
 
 Total virtual image size	200 MB
 M   Id   Host:Port         V-Nodes       Zone
diff --git a/tests/044.out b/tests/044.out
index 49c19d4..f1b0106 100644
--- a/tests/044.out
+++ b/tests/044.out
@@ -3,10 +3,10 @@ using backend plain store
 there should be no vdi
   Name        Id    Size    Used  Shared    Creation time   VDI id  Copies  Tag
 there should be no object
-Id	Size	Used	Use%
-0 MASKED GB 144 MB MASKED
-1 MASKED GB 144 MB MASKED
-2 MASKED GB 144 MB MASKED
-Total MASKED GB 432 MB MASKED
+Id	Size	Used	Avail	Use%
+0 MASKED GB 144 MB MASKED GB MASTERD
+1 MASKED GB 144 MB MASKED GB MASTERD
+2 MASKED GB 144 MB MASKED GB MASTERD
+Total MASKED GB 433 MB MASKED GB MASTERD
 
 Total virtual image size	0.0 MB
diff --git a/tests/058.out b/tests/058.out
index 1243a8d..75cc1ee 100644
--- a/tests/058.out
+++ b/tests/058.out
@@ -1,10 +1,10 @@
 QA output created by 058
 using backend plain store
-Id	Size	Used	Use%
-0 MASKED GB 104 MB MASKED
-1 MASKED GB 104 MB MASKED
-2 MASKED GB 104 MB MASKED
-Total MASKED GB 312 MB MASKED
+Id	Size	Used	Avail	Use%
+0 MASKED GB 104 MB MASKED GB MASTERD
+1 MASKED GB 104 MB MASKED GB MASTERD
+2 MASKED GB 104 MB MASKED GB MASTERD
+Total MASKED GB 312 MB MASKED GB MASTERD
 
 Total virtual image size	100 MB
 discard 104857600/104857600 bytes at offset 0
@@ -35,10 +35,10 @@ The inode object 0x7c2b25 idx 21 is not allocated
 The inode object 0x7c2b25 idx 22 is not allocated
 The inode object 0x7c2b25 idx 23 is not allocated
 The inode object 0x7c2b25 idx 24 is not allocated
-Id	Size	Used	Use%
-0 MASKED GB 4.0 MB MASKED
-1 MASKED GB 4.0 MB MASKED
-2 MASKED GB 4.0 MB MASKED
-Total MASKED GB 12 MB MASKED
+Id	Size	Used	Avail	Use%
+0 MASKED GB 4.0 MB MASKED GB MASTERD
+1 MASKED GB 4.0 MB MASKED GB MASTERD
+2 MASKED GB 4.0 MB MASKED GB MASTERD
+Total MASKED GB 12 MB MASKED GB MASTERD
 
 Total virtual image size	100 MB
diff --git a/tests/common.filter b/tests/common.filter
index 13ceb9e..f71b5a2 100644
--- a/tests/common.filter
+++ b/tests/common.filter
@@ -163,6 +163,11 @@ _filter_store()
     sed -e "s|$STORE|STORE|g"
 }
 
+_filter_info()
+{
+	awk '{if ($2 ~ /^[0-9.]+$/) {$2="MASKED";$6="MASKED";$8="MASTERD"};print $0}'
+}
+
 _filter_md_info()
 {
 	awk '{if ($4 ~ /^[0-9.]+$/) $4="MASKED";print $0}' | _filter_store
@@ -170,7 +175,7 @@ _filter_md_info()
 
 _filter_node_info()
 {
-	awk '{if ($2 ~ /^[0-9.]+$/) {$2="MASKED";$6="MASKED"};print $0}'
+	_filter_info
 }
 
 # make sure this script returns success
-- 
1.7.9.5




More information about the sheepdog mailing list