[sheepdog] [PATCH 3/4] tools: add kill_zk_session

Kai Zhang kyle at zelin.io
Fri Jun 14 03:46:37 CEST 2013


This is a useful tool for simulating zookeeper session timeout.

Usage:
./kill_zk_session [zk_path]

zk_path should contains the clientid_t content.

Signed-off-by: Kai Zhang <kyle at zelin.io>
---
 Makefile.am             |    2 +-
 configure.ac            |    3 +-
 tools/Makefile.am       |   11 +++++++++
 tools/kill_zk_session.c |   58 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 72 insertions(+), 2 deletions(-)
 create mode 100644 tools/Makefile.am
 create mode 100644 tools/kill_zk_session.c

diff --git a/Makefile.am b/Makefile.am
index 16eb532..adc0784 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -17,7 +17,7 @@ sheepdogsysconfdir	= ${SHEEPDOGCONFDIR}
 
 sheepdogsysconf_DATA	= 
 
-SUBDIRS			= lib collie sheep include script shepherd
+SUBDIRS			= lib collie sheep include script shepherd tools
 
 if BUILD_SHEEPFS
 SUBDIRS			+= sheepfs
diff --git a/configure.ac b/configure.ac
index e06e55c..187fd71 100644
--- a/configure.ac
+++ b/configure.ac
@@ -134,7 +134,8 @@ AC_CONFIG_FILES([Makefile
 		script/Makefile
 		lib/Makefile
 		man/Makefile
-		shepherd/Makefile])
+		shepherd/Makefile
+		tools/Makefile])
 
 ### Local business
 
diff --git a/tools/Makefile.am b/tools/Makefile.am
new file mode 100644
index 0000000..f07a1cc
--- /dev/null
+++ b/tools/Makefile.am
@@ -0,0 +1,11 @@
+MAINTAINERCLEANFILES	= Makefile.in
+
+sbin_PROGRAMS		= kill_zk_session
+
+if BUILD_ZOOKEEPER
+kill_zk_session_SOURCES = kill_zk_session.c
+
+kill_zk_session_LDADD	= -lzookeeper_mt
+endif
+
+noinst_PROGRAMS		= $(sbin_PROGRAMS)
diff --git a/tools/kill_zk_session.c b/tools/kill_zk_session.c
new file mode 100644
index 0000000..831c0a8
--- /dev/null
+++ b/tools/kill_zk_session.c
@@ -0,0 +1,58 @@
+#include <zookeeper/zookeeper.h>
+
+static void print_usage(void)
+{
+	fprintf(stderr,
+		"Usage:\n"
+		"\tkill_zk_session \"zk_path\"\n");
+}
+
+int main(int argc, char **argv)
+{
+	const char *hosts = "127.0.0.1:2181";
+	char *path;
+	clientid_t cid;
+	int len = sizeof(clientid_t), rc;
+	zhandle_t *zh;
+
+	if (argc != 2) {
+		print_usage();
+		exit(1);
+	}
+
+	path = argv[1];
+
+	zoo_set_debug_level(0);
+
+	zh = zookeeper_init(hosts, NULL, 1000, NULL, NULL, 0);
+	if (!zh) {
+		fprintf(stderr, "failed to init zookeeper\n");
+		exit(1);
+	}
+
+	while (zoo_state(zh) != ZOO_CONNECTED_STATE)
+		;
+
+	rc = zoo_get(zh, path, 0, (char *)&cid, &len, NULL);
+	if (rc != ZOK) {
+		fprintf(stderr, "failed to get data, %s\n", zerror(rc));
+		exit(1);
+	}
+
+	zh = zookeeper_init(hosts, NULL, 1000, &cid, NULL, 0);
+
+	if (!zh) {
+		fprintf(stderr, "failed to re-init zookeeper\n");
+		exit(1);
+	}
+
+	while (zoo_state(zh) != ZOO_CONNECTED_STATE)
+		;
+
+	if (zookeeper_close(zh) != ZOK) {
+		fprintf(stderr, "failed to close zookeeper session\n");
+		exit(1);
+	}
+
+	return 0;
+}
-- 
1.7.1




More information about the sheepdog mailing list