1255623ac73d77568092d9d9c4ddd0d4a2116eca
[integration/test.git] / test / 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 # Given a shardname (e.g. shard-inventory-config), number of shards and bunch of ips
14 # determines what role each ip has in an Akka (Raft based) cluster
15 # result would look like
16 # {'10.194.126.118':'Leader', '10.194.126.118':'Follower', '10.194.126.117': None}
17 #
18
19 def getClusterRoles(shardName,numOfShards=3,numOfTries=3,sleepBetweenRetriesInSecs=1,port=8181,*ips):
20     dict={}
21     for ip in ips:
22       i=1
23       dict[ip]=None
24       bFollower = 0
25       while i <= numOfShards:
26         shardMemberName = "member-"+str(i)+"-"+shardName;
27         j=1
28
29         while j <= numOfTries:
30             print "Try number "+str(j)
31             try:
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)
35                 print resp
36                 if(resp.status_code != 200):
37                     continue
38                 data = json.loads(resp.text)
39                 if('value' in data):
40                     dataValue = data['value']
41                     if(dataValue['RaftState']=='Follower'):
42                         dict[ip]='Follower'
43                         break;
44                     elif(dataValue['RaftState']=='Leader'):
45                         dict[ip]='Leader'
46             except:
47                 e = sys.exc_info()[0]
48                 print "Try"+str(j)+":An error occurred when finding leader on"+ip+" for shardName:" +shardMemberName
49                 print e
50                 sleep(sleepBetweenRetriesInSecs)
51                 continue
52             finally:
53                 j=j+1
54
55         if(dict[ip]!=None):
56             break;
57         i=i+1
58
59     return dict
60
61 #
62 #Given a shardname (e.g. shard-inventory-config), number of shards and an ip
63 #determines if its a leader
64 #
65 #
66 def isLeader(shardName,numOfShards,numOfRetries,sleepFor,port,ipAddress):
67     ip = getClusterRoles(shardName,numOfShards,numOfRetries,sleepFor,port,ipAddress)
68     print ip
69     if( ip[ipAddress] == 'Leader'):
70          return True
71
72     return False
73
74 #
75 # Returns the leader of the shard given a set of IPs
76 # Or None
77 #
78 def getLeader(shardName,numOfShards=3,numOfTries=3,sleepBetweenRetriesInSecs=1,port=8181,*ips):
79     dict = getClusterRoles(shardName,numOfShards,numOfTries,sleepBetweenRetriesInSecs,port,*ips)
80     for ip in dict.keys():
81         if(dict[ip]=='Leader'):
82              return ip
83
84     return None
85
86 #
87 # Returns the follower list of a shard given a set of IPs
88 # Or []
89 #
90 def getFollowers (shardName,numOfShards=3,numOfTries=3,sleepBetweenRetriesInSecs=1,port=8181,*ips):
91     dict = getClusterRoles(shardName,numOfShards,numOfTries,sleepBetweenRetriesInSecs,port,*ips)
92     result = []
93
94     for ip in dict.keys():
95         if(dict[ip]=='Follower'):
96             result.append(ip)
97
98     return result
99 #
100 #Given a shardname (e.g. shard-inventory-config), number of shards and an ip
101 #determines if its a leader
102 #
103 #
104 def isFollower(shardName,numOfShards,numOfRetries,sleepFor,port,ipAddress):
105     ip = getClusterRoles(shardName,numOfShards,numOfRetries,sleepFor,port,ipAddress)
106     print ip
107     if( ip[ipAddress] == 'Follower'):
108         return True
109
110     return False
111
112
113 def testGetClusterRoles():
114     dict = getClusterRoles("shard-inventory-config",3,1,1,8181,"10.194.126.116","10.194.126.117","10.194.126.118")
115     print dict
116
117     for ip in dict.keys():
118         if(isLeader("shard-inventory-config",3,1,1,8181,ip)):
119             print ( ip + " is Leader")
120         elif (isFollower("shard-inventory-config",3,1,1,8181,ip)):
121             print (ip + " is follower")
122         else:
123             print (ip + " seems to have value "+ str(dict[ip]))
124
125 def testGetLeader ():
126   leader =  getLeader("shard-inventory-config",3,1,1,8181,"10.194.126.116","10.194.126.117","10.194.126.118")
127   print leader
128   return leader
129
130 def testGetFollowers():
131    followers = getFollowers("shard-inventory-config",3,1,1,8181,"10.194.126.116","10.194.126.117","10.194.126.118")
132    print followers
133    return followers
134
135 #testGetClusterRoles()
136 #testGetLeader()
137 #testGetFollowers()
138
139
140
141
142
143
144
145
146
147