Updated code to match new rules
[integration/test.git] / csit / libraries / ClusterStateLibrary.py
1 import SettingsLibrary
2 from time import sleep
3 import UtilLibrary
4 import json
5 import sys
6
7
8 __author__ = "Basheeruddin Ahmed"
9 __copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
10 __license__ = "New-style BSD"
11 __email__ = "syedbahm@cisco.com"
12
13
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
16
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}
20     """
21     dict = {}
22     for ip in ips:
23         i = 1
24         dict[ip] = None
25         print "numOfShards => " + str(numOfShards)
26         while i <= numOfShards:
27             shardMemberName = "member-" + str(i) + "-" + shardName
28             j = 1
29             print 'j => ' + str(j)
30             print 'numOfTries => ' + str(numOfTries)
31             while int(j) <= int(numOfTries):
32                 print("Try number " + str(j))
33                 try:
34                     print("getting role of " + ip + "  for shardName = " + shardMemberName)
35                     url = SettingsLibrary.getJolokiaURL(ip, str(port), str(i), shardName)
36                     print url
37                     resp = UtilLibrary.get(url)
38                     print(resp)
39                     if resp.status_code != 200:
40                         sleep(sleepBetweenRetriesInSecs)
41                         continue
42                     print(resp.text)
43                     data = json.loads(resp.text)
44                     if 'value' in data:
45                         dataValue = data['value']
46                         print("datavalue RaftState is", dataValue['RaftState'])
47                         dict[ip] = dataValue['RaftState']
48                 except:
49                     e = sys.exc_info()[0]
50                     print("Try" + str(j) + ":An error occurred when finding leader on" + ip +
51                           " for shardName:" + shardMemberName)
52                     print(e)
53                     sleep(sleepBetweenRetriesInSecs)
54                     continue
55                 finally:
56                     j = j + 1
57             if dict[ip] is not None:
58                 break
59             i = i + 1
60     return dict
61
62
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.
68     """
69     ip = getClusterRoles(shardName, numOfShards, numOfRetries, sleepFor, port, ipAddress)
70     print(ip)
71     if ip[ipAddress] == role:
72         return True
73     return False
74
75
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':
82                 return ip
83     return None
84
85
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)
90         result = []
91
92         for ip in dict.keys():
93             if dict[ip] == 'Follower':
94                 result.append(ip)
95         print "i=", i, "result=", result
96         if (len(result) == (len(ips) - 1)):
97             break
98         sleep(1)
99     return result
100
101
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")
105     print(dict)
106
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")
112         else:
113             print(ip + " seems to have value " + str(dict[ip]))
114
115
116 def testGetLeader():
117     leader = getLeader("shard-inventory-config", 3, 1, 1, 8181,
118                        "10.194.126.116", "10.194.126.117", "10.194.126.118")
119     print leader
120     return leader
121
122
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")
126     print(followers)
127     return followers
128
129 # testGetClusterRoles()
130 # testGetLeader()
131 # testGetFollowers()