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=3, 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}
24 print "numOfShards => " + str(numOfShards)
25 while i <= numOfShards:
26 shardMemberName = "member-" + str(i) + "-" + shardName
28 print 'j => ' + str(j)
29 print 'numOfTries => ' + str(numOfTries)
30 while int(j) <= int(numOfTries):
31 print("Try number " + str(j))
33 print("getting role of " + ip + " for shardName = " + shardMemberName)
34 url = SettingsLibrary.getJolokiaURL(ip, str(port), str(i), shardName)
36 resp = UtilLibrary.get(url)
38 if resp.status_code != 200:
39 sleep(sleepBetweenRetriesInSecs)
42 data = json.loads(resp.text)
44 dataValue = data['value']
45 print("datavalue RaftState is", dataValue['RaftState'])
46 dict[ip] = dataValue['RaftState']
49 print("Try" + str(j) + ":An error occurred when finding leader on" + ip +
50 " for shardName:" + shardMemberName)
52 sleep(sleepBetweenRetriesInSecs)
56 if dict[ip] is not None:
62 def isRole(role, shardName, ipAddress, numOfShards=3, numOfRetries=1, sleepFor=3, port=8181):
63 """Given a role (Leader, Follower, Candidate, or IsolatedLeader),
64 shardname (e.g. shard-inventory-config), controller IP address,
65 and number of shards on the controller,this function determines if the controller,
66 has that role for the specified shard.
68 ip = getClusterRoles(shardName, numOfShards, numOfRetries, sleepFor, port, ipAddress)
70 if ip[ipAddress] == role:
75 def getLeader(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesInSecs=1, port=8181, *ips):
76 """Returns the leader of the shard given a set of IPs Or None"""
77 for i in range(3): # Try 3 times to find a leader
78 dict = getClusterRoles(shardName, numOfShards, numOfTries, sleepBetweenRetriesInSecs, port, *ips)
79 for ip in dict.keys():
80 if dict[ip] == 'Leader':
85 def getFollowers(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesInSecs=1, port=8181, *ips):
86 """Returns the follower list of a shard given a set of IPs Or []"""
87 for i in range(6): # Try 6 times to find all followers
88 dict = getClusterRoles(shardName, numOfShards, numOfTries, sleepBetweenRetriesInSecs, port, *ips)
91 for ip in dict.keys():
92 if dict[ip] == 'Follower':
94 print "i=", i, "result=", result
95 if (len(result) == (len(ips) - 1)):
101 def testGetClusterRoles():
102 dict = getClusterRoles("shard-inventory-config", 3, 1, 1, 8181,
103 "10.194.126.116", "10.194.126.117", "10.194.126.118")
106 for ip in dict.keys():
107 if isRole("Leader", "shard-inventory-config", 3, 1, 1, 8181, ip):
108 print(ip + " is Leader")
109 elif isRole("Follower", "shard-inventory-config", 3, 1, 1, 8181, ip):
110 print(ip + " is follower")
112 print(ip + " seems to have value " + str(dict[ip]))
116 leader = getLeader("shard-inventory-config", 3, 1, 1, 8181,
117 "10.194.126.116", "10.194.126.117", "10.194.126.118")
122 def testGetFollowers():
123 followers = getFollowers("shard-inventory-config", 3, 1, 1, 8181,
124 "10.194.126.116", "10.194.126.117", "10.194.126.118")
128 # testGetClusterRoles()