-
Notifications
You must be signed in to change notification settings - Fork 6
/
zk-watch.py
executable file
·173 lines (140 loc) · 5.77 KB
/
zk-watch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
import datetime, time, os
from optparse import OptionParser
import zkclient
from zkclient import ZKClient, CountingWatcher, zookeeper
usage = "usage: %prog [options]"
parser = OptionParser(usage=usage)
parser.add_option("", "--cluster", dest="cluster",
default=None, help="comma separated list of host:port, test as a cluster")
parser.add_option("", "--timeout", dest="timeout", type="int",
default=5000, help="session timeout in milliseconds (default %default)")
parser.add_option("", "--root_znode", dest="root_znode",
default="/zk-latencies", help="root for the test, default /zk-latencies")
parser.add_option("", "--znode_count", dest="znode_count", default=10000, type="int",
help="the number of znodes to operate on in each performance section (default %default)")
parser.add_option("", "--watch_session", dest="watch_session", default=1, type="int",
help="number of watch sessions to put on each znode (default %default)")
parser.add_option("", "--znode_size", dest="znode_size", type="int",
default=25, help="data size when creating/setting znodes (default %default)")
parser.add_option("", "--next_watch", dest="next_watch", action="store_true",
default=False, help="continue to watch (default %default)")
parser.add_option("-v", "--verbose",
action="store_true", dest="verbose", default=False,
help="verbose output, include more detail")
parser.add_option("-q", "--quiet",
action="store_true", dest="quiet", default=False,
help="quiet output, basically just success/failure")
(options, args) = parser.parse_args()
zkclient.options = options
zookeeper.set_log_stream(open("cli_log_%d.txt" % (os.getpid()),"w"))
class SmokeError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
def print_elap(start, msg, count):
elapms = (time.time() - start) * 1000
if int(elapms) != 0:
print("%s in %6d ms (%f ms/op %f/sec)"
% (msg, int(elapms), elapms/count, count/(elapms/1000.0)))
else:
print("%s in %6d ms (included in prior)" % (msg, int(elapms)))
def timer(ops, msg, count=options.znode_count):
start = time.time()
for op in ops:
pass
print_elap(start, msg, count)
def timer2(func, msg, count=options.znode_count):
start = time.time()
func()
print_elap(start, msg, count)
def child_path(i):
return "%s/session_%d" % (options.root_znode, i)
def create_nodes(s):
def func():
callbacks = []
for j in xrange(options.znode_count):
cb = zkclient.CreateCallback()
cb.cv.acquire()
s.acreate(child_path(j), cb, data)
callbacks.append(cb)
for j, cb in enumerate(callbacks):
cb.waitForSuccess()
if cb.path != child_path(j):
raise SmokeError("invalid path %s for operation %d on handle %d" %
(cb.path, j, cb.handle))
timer2(func, "created %7d permanent znodes " % (options.znode_count))
def add_wathches(s):
# async operation in a watcher causes dead lock ?
def watch(handle, typ, state, path):
print "watch event (%s)" % path
cb = zkclient.GetCallback()
cb.cv.acquire()
s.aget(path, cb, watch)
cb.waitForSuccess()
def watch_sync(handle, typ, state, path):
if options.verbose:
print "watch event (%s)" % path
if options.next_watch and s.exists(path):
s.get(path, watch_sync)
def func():
callbacks = []
for j in xrange(options.znode_count):
cb = zkclient.GetCallback()
cb.cv.acquire()
s.aget(child_path(j), cb, watch_sync)
def watch2(h, t, s, p):
pass
s.get(child_path(j), watch2)
callbacks.append(cb)
for cb in callbacks:
cb.waitForSuccess()
if cb.value != data:
raise SmokeError("invalid data %s for operation %d on handle %d" %
(cb.value, j, cb.handle))
timer2(func, "get %7d znodes " % (options.znode_count))
def set_nodes(s, data):
def func():
callbacks = []
for j in xrange(options.znode_count):
cb = zkclient.SetCallback()
cb.cv.acquire()
s.aset(child_path(j), cb, data)
callbacks.append(cb)
for cb in callbacks:
cb.waitForSuccess()
timer2(func, "set %7d znodes " % (options.znode_count))
def delete_nodes(s):
def func():
callbacks = []
for j in xrange(options.znode_count):
cb = zkclient.DeleteCallback()
cb.cv.acquire()
s.adelete(child_path(j), cb)
callbacks.append(cb)
for cb in callbacks:
cb.waitForSuccess()
timer2(func, "deleted %7d permanent znodes " % (options.znode_count))
def get_zk_servers():
return options.cluster
if __name__ == '__main__':
data = options.znode_size * "x"
servers = get_zk_servers()
session_gets = []
for x in xrange(options.watch_session):
session_gets.append(ZKClient(servers, options.timeout))
session_set = ZKClient(servers, options.timeout)
session_set.create(options.root_znode,
"smoketest root, delete after test done, created %s" % (datetime.datetime.now().ctime()))
create_nodes(session_set)
for s in session_gets:
add_wathches(s)
set_nodes(session_set, data)
delete_nodes(session_set)
session_set.delete(options.root_znode)
time.sleep(10)
for s in session_gets:
s.close()
session_set.close()
print 'watch test complete'