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