-
Notifications
You must be signed in to change notification settings - Fork 1
/
election.py
80 lines (63 loc) · 1.74 KB
/
election.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
from kazoo.client import KazooClient
import time
import random
#todo use logging - soon
#192.168.239.141 2181
from kazoo.client import KazooState
def my_listener(state):
if state == KazooState.LOST:
print "session lost"
elif state == KazooState.SUSPENDED:
print "session suspended"
else:
print "ok here i am"
def election_child_watcher(event):
print "election_child_watcher"
print event
#WTF closure stuff working ?
def prev_path_watcher(event):
if prev_path == None:
print "I won"
else:
print "setting watch on "+ prev_path
def main():
zk = KazooClient(hosts='127.0.0.1:2181', timeout=2.0)
zk.add_listener(my_listener)
zk.start()
if zk.exists("/ELECTION") == None:
zk.ensure_path("/ELECTION")
c = 1
node_pathes = []
while c < 10:
c += 1
node_path = zk.create("/ELECTION/guid-n_", b"a value", ephemeral=True, sequence=True)
node_pathes.append(node_path)
my_path = random.choice(node_pathes)
my_path = my_path.replace("/ELECTION/","")
#print "print my_path", my_path
children = zk.get_children("/ELECTION/", watch=election_child_watcher)
get_next_path = False;
prev_path = None
for child_path in sorted(children):
if child_path == my_path:
break
prev_path = child_path
#I'm the leader
if prev_path == None:
print "OK I'm leader don't have to watch"
return
#fires twice, once on creation ignore
@zk.DataWatch("/ELECTION/"+prev_path)
def watch_node(data, stat):
#only watch for first change
if stat.version == 1:
print("Version: %s, data: %s" % (stat.version, data.decode("utf-8")))
print "setting watch on "+ prev_path
print "my",my_path
zk.set("/ELECTION/"+prev_path, b"some data")
print "boom. watch triggered?"
#time.sleep(10)
print "bye"
zk.stop()
if __name__ == '__main__':
main()