[Sheepdog] [zookeeper][PATCH 4/5] Speed up get_nodes function in zookeeper driver

Yunkai Zhang yunkai.me at gmail.com
Tue Mar 6 19:10:35 CET 2012


Signed-off-by: Yunkai Zhang <qiushu.zyk at taobao.com>
---
 sheep/cluster/zookeeper.c |   34 ++++++++++++++++++++++++++--------
 1 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/sheep/cluster/zookeeper.c b/sheep/cluster/zookeeper.c
index c2592d2..6f53ac4 100644
--- a/sheep/cluster/zookeeper.c
+++ b/sheep/cluster/zookeeper.c
@@ -268,23 +268,41 @@ static enum cluster_join_result (*zk_check_join_cb)(
 /* get node list from the last pushed data */
 static size_t get_nodes(zhandle_t *zh, struct sd_node *nodes)
 {
-	int rc, len;
+	int rc, len, pos;
 	struct zk_event ev;
-	struct String_vector strs;
-	char path[256], max[256] = "";
+	char path[256];
 
-	FOR_EACH_ZNODE(zh, QUEUE_ZNODE, path, &strs) {
-		if (strcmp(max, path) < 0)
-			strcpy(max, path);
+	zk_lock(zh);
+
+	if (queue_pos>0) 
+		pos = queue_pos -1;
+	else 
+		pos = 0;
+
+	for (;;) {
+		sprintf(path, QUEUE_ZNODE "/%010d", pos);
+		dprintf("feach path:%s\n", path);
+		rc = zoo_exists(zh, path, 0, NULL);
+
+		if (rc != ZOK){
+			break;
+		}
+		pos++;
 	}
 
-	if (max[0] == '\0')
+	/* nothing to be found */
+	if (pos == 0 || pos < queue_pos){
+		zk_unlock(zh);
 		return 0;
+	}
 
 	len = sizeof(ev);
-	rc = zoo_get(zh, max, 1, (char *)&ev, &len, NULL);
+	sprintf(path, QUEUE_ZNODE "/%010d", --pos);
+	rc = zoo_get(zh, path, 1, (char *)&ev, &len, NULL);
 	assert(rc == ZOK);
 
+	zk_unlock(zh);
+
 	memcpy(nodes, ev.nodes, sizeof(ev.nodes));
 
 	return ev.nr_nodes;
-- 
1.7.7.6




More information about the sheepdog mailing list