[sheepdog] [PATCH stable-0.8 4/6] sheep/cluster: delete znodes when leaving cluster

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Tue Mar 18 07:30:57 CET 2014


From: Robin Dong <sanbai at taobao.com>

A sheep node call zk_lock() to create a znode in zookeeper and become the
owner of the lock, then if all disks under this sheep node is broken
suddenly, the node will have to leave cluster.Now, the znode will stay
in "/lock" and make other sheep nodes waiting it forever.

So we should delete znodes which created by this sheep node when leaving
cluster.

Signed-off-by: Robin Dong <sanbai at taobao.com>
Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 sheep/cluster/zookeeper.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/sheep/cluster/zookeeper.c b/sheep/cluster/zookeeper.c
index 660ca0c..3c812c1 100644
--- a/sheep/cluster/zookeeper.c
+++ b/sheep/cluster/zookeeper.c
@@ -418,6 +418,38 @@ static void lock_table_lookup_release(uint64_t lock_id)
 	sd_mutex_unlock(table_locks + hval);
 }
 
+/*
+ * If this node leave the cluster, we need to delete the znode which created
+ * for distributed lock. Otherwise, the lock will never be released.
+ */
+static void lock_table_remove_znodes(void)
+{
+	uint64_t hval;
+	int rc;
+	struct hlist_node *iter;
+	struct cluster_lock *lock;
+
+	for (hval = 0; hval < HASH_BUCKET_NR; hval++) {
+		sd_mutex_lock(table_locks + hval);
+		hlist_for_each_entry(lock, iter, cluster_locks_table + hval,
+				     hnode) {
+			while (true) {
+				rc = zk_delete_node(lock->lock_path, -1);
+				if (rc == ZOK || rc == ZNONODE) {
+					sd_debug("delete path: %s ok",
+						 lock->lock_path);
+					break;
+				}
+				sd_err("Failed to delete path: %s %s",
+				       lock->lock_path,
+				       zerror(rc));
+				zk_wait();
+			}
+		}
+		sd_mutex_unlock(table_locks + hval);
+	}
+}
+
 /* ZooKeeper-based queue give us an totally ordered events */
 static int efd;
 static int32_t queue_pos;
@@ -958,6 +990,7 @@ static int zk_leave(void)
 	snprintf(path, sizeof(path), MEMBER_ZNODE"/%s",
 		 node_to_str(&this_node.node));
 	add_event(EVENT_LEAVE, &this_node, NULL, 0);
+	lock_table_remove_znodes();
 	zk_delete_node(path, -1);
 	return 0;
 }
-- 
1.8.1.2




More information about the sheepdog mailing list