1 __author__ = "Basheeruddin Ahmed"
2 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
3 __license__ = "New-style BSD"
4 __email__ = "syedbahm@cisco.com"
13 def getClusterRoles(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesInSecs=1, port=8181, *ips):
14 """Given a shardname (e.g. shard-inventory-config), number of shards and bunch of ips
16 determines what role each ip has in an Akka (Raft based) cluster
17 result would look like
18 {'10.194.126.118':'Leader', '10.194.126.118':'Follower', '10.194.126.117': None}
25 while i <= numOfShards:
26 shardMemberName = "member-" + str(i) + "-" + shardName
29 while j <= numOfTries:
30 print("Try number " + str(j))
32 print("finding if" + ip + "is leader for shardName =" + shardMemberName)
33 url = SettingsLibrary.getJolokiaURL(ip, str(port), str(i), shardName)
34 resp = UtilLibrary.get(url)
36 if resp.status_code != 200:
37 sleep(sleepBetweenRetriesInSecs)
40 data = json.loads(resp.text)
42 dataValue = data['value']
43 print("datavalue RaftState is", dataValue['RaftState'])
44 if dataValue['RaftState'] == 'Follower':
47 elif dataValue['RaftState'] == 'Leader':
51 print("Try" + str(j) + ":An error occurred when finding leader on" + ip +
52 " for shardName:" + shardMemberName)
54 sleep(sleepBetweenRetriesInSecs)
59 if dict[ip] is not None:
66 def isLeader(shardName, numOfShards, numOfRetries, sleepFor, port, ipAddress):
67 """Given a shardname (e.g. shard-inventory-config), number of shards and an ip determines if its a leader"""
68 ip = getClusterRoles(shardName, numOfShards, numOfRetries, sleepFor, port, ipAddress)
70 if ip[ipAddress] == 'Leader':
76 def getLeader(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesInSecs=1, port=8181, *ips):
77 """Returns the leader of the shard given a set of IPs Or None"""
78 for i in range(3): # Try 3 times to find a leader
79 dict = getClusterRoles(shardName, numOfShards, numOfTries, sleepBetweenRetriesInSecs, port, *ips)
80 for ip in dict.keys():
81 if dict[ip] == 'Leader':
87 def getFollowers(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesInSecs=1, port=8181, *ips):
88 """Returns the follower list of a shard given a set of IPs Or []"""
89 for i in range(6): # Try 6 times to find all followers
90 dict = getClusterRoles(shardName, numOfShards, numOfTries, sleepBetweenRetriesInSecs, port, *ips)
93 for ip in dict.keys():
94 if dict[ip] == 'Follower':
96 print "i=", i, "result=", result
97 if (len(result) == (len(ips) - 1)):
104 def isFollower(shardName, numOfShards, numOfRetries, sleepFor, port, ipAddress):
105 """Given a shardname (e.g. shard-inventory-config), number of shards and an ip determines if its a leader"""
106 ip = getClusterRoles(shardName, numOfShards, numOfRetries, sleepFor, port, ipAddress)
108 if ip[ipAddress] == 'Follower':
114 def testGetClusterRoles():
115 dict = getClusterRoles("shard-inventory-config", 3, 1, 1, 8181,
116 "10.194.126.116", "10.194.126.117", "10.194.126.118")
119 for ip in dict.keys():
120 if isLeader("shard-inventory-config", 3, 1, 1, 8181, ip):
121 print(ip + " is Leader")
122 elif isFollower("shard-inventory-config", 3, 1, 1, 8181, ip):
123 print(ip + " is follower")
125 print(ip + " seems to have value " + str(dict[ip]))
129 leader = getLeader("shard-inventory-config", 3, 1, 1, 8181,
130 "10.194.126.116", "10.194.126.117", "10.194.126.118")
135 def testGetFollowers():
136 followers = getFollowers("shard-inventory-config", 3, 1, 1, 8181,
137 "10.194.126.116", "10.194.126.117", "10.194.126.118")
141 # testGetClusterRoles()