8 __author__ = "Basheeruddin Ahmed"
9 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
10 __license__ = "New-style BSD"
11 __email__ = "syedbahm@cisco.com"
14 def getClusterRoles(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesInSecs=3, port=8181, *ips):
15 """Given a shardname (e.g. shard-inventory-config), number of shards and bunch of ips
17 determines what role each ip has in an Akka (Raft based) cluster
18 result would look like
19 {'10.194.126.118':'Leader', '10.194.126.118':'Follower', '10.194.126.117': None}
25 print "numOfShards => " + str(numOfShards)
26 while i <= numOfShards:
27 shardMemberName = "member-" + str(i) + "-" + shardName
29 print 'j => ' + str(j)
30 print 'numOfTries => ' + str(numOfTries)
31 while int(j) <= int(numOfTries):
32 print("Try number " + str(j))
34 print("getting role of " + ip + " for shardName = " + shardMemberName)
35 url = SettingsLibrary.getJolokiaURL(ip, str(port), str(i), shardName)
37 resp = UtilLibrary.get(url)
39 if resp.status_code != 200:
40 sleep(sleepBetweenRetriesInSecs)
43 data = json.loads(resp.text)
45 dataValue = data['value']
46 print("datavalue RaftState is", dataValue['RaftState'])
47 dict[ip] = dataValue['RaftState']
50 print("Try" + str(j) + ":An error occurred when finding leader on" + ip +
51 " for shardName:" + shardMemberName)
53 sleep(sleepBetweenRetriesInSecs)
57 if dict[ip] is not None:
63 def isRole(role, shardName, ipAddress, numOfShards=3, numOfRetries=1, sleepFor=3, port=8181):
64 """Given a role (Leader, Follower, Candidate, or IsolatedLeader),
65 shardname (e.g. shard-inventory-config), controller IP address,
66 and number of shards on the controller,this function determines if the controller,
67 has that role for the specified shard.
69 ip = getClusterRoles(shardName, numOfShards, numOfRetries, sleepFor, port, ipAddress)
71 if ip[ipAddress] == role:
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':
86 def getFollowers(shardName, numOfShards=3, numOfTries=3, sleepBetweenRetriesInSecs=1, port=8181, *ips):
87 """Returns the follower list of a shard given a set of IPs Or []"""
88 for i in range(6): # Try 6 times to find all followers
89 dict = getClusterRoles(shardName, numOfShards, numOfTries, sleepBetweenRetriesInSecs, port, *ips)
92 for ip in dict.keys():
93 if dict[ip] == 'Follower':
95 print "i=", i, "result=", result
96 if (len(result) == (len(ips) - 1)):
102 def testGetClusterRoles():
103 dict = getClusterRoles("shard-inventory-config", 3, 1, 1, 8181,
104 "10.194.126.116", "10.194.126.117", "10.194.126.118")
107 for ip in dict.keys():
108 if isRole("Leader", "shard-inventory-config", 3, 1, 1, 8181, ip):
109 print(ip + " is Leader")
110 elif isRole("Follower", "shard-inventory-config", 3, 1, 1, 8181, ip):
111 print(ip + " is follower")
113 print(ip + " seems to have value " + str(dict[ip]))
117 leader = getLeader("shard-inventory-config", 3, 1, 1, 8181,
118 "10.194.126.116", "10.194.126.117", "10.194.126.118")
123 def testGetFollowers():
124 followers = getFollowers("shard-inventory-config", 3, 1, 1, 8181,
125 "10.194.126.116", "10.194.126.117", "10.194.126.118")
129 # testGetClusterRoles()