[sheepdog] [PATCH v6 4/6] tools: add kill_zk_session

Kai Zhang kyle at zelin.io
Fri Jun 21 14:34:42 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       |   13 +++++++++
 tools/kill_zk_session.c |   71 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 87 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 bbd3296..078c80b 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 6d9eaf9..9e611eb 100644
--- a/configure.ac
+++ b/configure.ac
@@ -137,7 +137,8 @@ AC_CONFIG_FILES([Makefile
 		shepherd/Makefile
 		tests/unit/Makefile
 		tests/unit/collie/Makefile
-		tests/unit/sheep/Makefile])
+		tests/unit/sheep/Makefile
+		tools/Makefile])
 
 ### Local business
 
diff --git a/tools/Makefile.am b/tools/Makefile.am
new file mode 100644
index 0000000..3656da8
--- /dev/null
+++ b/tools/Makefile.am
@@ -0,0 +1,13 @@
+MAINTAINERCLEANFILES	= Makefile.in
+
+sbin_PROGRAMS		=
+
+if BUILD_ZOOKEEPER
+sbin_PROGRAMS		+= kill_zk_session
+
+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..dc0e6a8
--- /dev/null
+++ b/tools/kill_zk_session.c
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2013 Zelin.io
+ *
+ * Kai Zhang <kyle at zelin.io>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#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.9.5




More information about the sheepdog mailing list