[sheepdog] [PATCH v1 2/2] sheep: avoid dead loop when zookeeper session is expired

Robin Dong robin.k.dong at gmail.com
Wed Feb 19 02:51:23 CET 2014


From: Robin Dong <sanbai at taobao.com>

When zookeeper session is expired, zk_get_least_seq() will return
ZNONODE, but we have not check the return value of zk_get_least_seq()
so the routine will fall in dead loop.

To avoid it, we add check for return value of zk_get_least_seq() and
jump to recreate sequential-node if find out the ZNONODE event.

Signed-off-by: Robin Dong <sanbai at taobao.com>
---
 sheep/cluster/zookeeper.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/sheep/cluster/zookeeper.c b/sheep/cluster/zookeeper.c
index 68d592d..5c21665 100644
--- a/sheep/cluster/zookeeper.c
+++ b/sheep/cluster/zookeeper.c
@@ -1259,6 +1259,7 @@ static void zk_lock(uint64_t lock_id)
 	 */
 	snprintf(parent_node, MAX_NODE_STR_LEN, LOCK_ZNODE "/%"PRIu64,
 		 cluster_lock->id);
+create_seq_node:
 	/* compete owner of lock is just like zk_compete_master() */
 	while (true) {
 		rc = zk_create_node(parent, node_to_str(&this_node.node),
@@ -1283,9 +1284,13 @@ static void zk_lock(uint64_t lock_id)
 
 	/* create node ok now */
 	while (true) {
-		zk_get_least_seq(parent_node, lowest_seq_path, MAX_NODE_STR_LEN,
-				 owner_name, &len);
-
+		rc = zk_get_least_seq(parent_node, lowest_seq_path,
+				      MAX_NODE_STR_LEN, owner_name, &len);
+		/* may be expired */
+		if (rc == ZNONODE) {
+			sd_info("Recreate seq node");
+			goto create_seq_node;
+		}
 		/* I got the lock */
 		if (!strncmp(lowest_seq_path, my_path, strlen(my_path))) {
 			sd_debug("I am master now. %s", lowest_seq_path);
-- 
1.7.12.4



More information about the sheepdog mailing list