Added library to determine Shard Cluster Roles
authorBasheeruddin Ahmed <syedbahm@cisco.com>
Wed, 10 Sep 2014 21:54:27 +0000 (14:54 -0700)
committerBasheeruddin Ahmed <syedbahm@cisco.com>
Fri, 19 Sep 2014 17:20:52 +0000 (10:20 -0700)
patch 2: added getLeader and getFollowers API

Change-Id: Ie0acd69d83d0e7a37fd4ccfd0089029eb7722120
Signed-off-by: Basheeruddin Ahmed <syedbahm@cisco.com>
test/csit/libraries/ClusterStateLibrary.py [new file with mode: 0644]
test/csit/libraries/SettingsLibrary.py
test/csit/libraries/UtilLibrary.py

diff --git a/test/csit/libraries/ClusterStateLibrary.py b/test/csit/libraries/ClusterStateLibrary.py
new file mode 100644 (file)
index 0000000..fa0b9cd
--- /dev/null
@@ -0,0 +1,147 @@
+__author__ = "Basheeruddin Ahmed"
+__copyright__ = "Copyright(c) 2014, Cisco Systems, Inc."
+__license__ = "New-style BSD"
+__email__ = "syedbahm@cisco.com"
+
+import SettingsLibrary
+from time import sleep
+import UtilLibrary
+import json
+import sys
+
+#
+# Given a shardname (e.g. shard-inventory-config), number of shards and bunch of ips
+# determines what role each ip has in an Akka (Raft based) cluster
+# result would look like
+# {'Leader':10.194.126.118', 'Follower':10.194.126.118':, '10.194.126.117': None}
+#
+
+def getClusterRoles(shardName,numOfShards=3,numOfTries=3,sleepBetweenRetriesInSecs=1,port=8181,*ips):
+    dict={}
+    for ip in ips:
+      i=1
+      dict[ip]=None
+      bFollower = 0
+      while i <= numOfShards:
+        shardMemberName = "member-"+str(i)+"-"+shardName;
+        j=1
+
+        while j <= numOfTries:
+            print "Try number "+str(j)
+            try:
+                print "finding if"+ ip +"is leader for shardName ="+shardMemberName
+                url = SettingsLibrary.getJolokiaURL(ip,str(port),str(i),shardName)
+                resp = UtilLibrary.get(url)
+                print resp
+                if(resp.status_code != 200):
+                    continue
+                data = json.loads(resp.text)
+                if('value' in data):
+                    dataValue = data['value']
+                    if(dataValue['RaftState']=='Follower'):
+                        dict[ip]='Follower'
+                        break;
+                    elif(dataValue['RaftState']=='Leader'):
+                        dict[ip]='Leader'
+            except:
+                e = sys.exc_info()[0]
+                print "Try"+str(j)+":An error occurred when finding leader on"+ip+" for shardName:" +shardMemberName
+                print e
+                sleep(sleepBetweenRetriesInSecs)
+                continue
+            finally:
+                j=j+1
+
+        if(dict[ip]!=None):
+            break;
+        i=i+1
+
+    return dict
+
+#
+#Given a shardname (e.g. shard-inventory-config), number of shards and an ip
+#determines if its a leader
+#
+#
+def isLeader(shardName,numOfShards,numOfRetries,sleepFor,port,ipAddress):
+    ip = getClusterRoles(shardName,numOfShards,numOfRetries,sleepFor,port,ipAddress)
+    print ip
+    if( ip[ipAddress] == 'Leader'):
+         return True
+
+    return False
+
+#
+# Returns the leader of the shard given a set of IPs
+# Or None
+#
+def getLeader(shardName,numOfShards=3,numOfTries=3,sleepBetweenRetriesInSecs=1,port=8181,*ips):
+    dict = getClusterRoles(shardName,numOfShards,numOfTries,sleepBetweenRetriesInSecs,port,*ips)
+    for ip in dict.keys():
+        if(dict[ip]=='Leader'):
+             return ip
+
+    return None
+
+#
+# Returns the follower list of a shard given a set of IPs
+# Or []
+#
+def getFollowers (shardName,numOfShards=3,numOfTries=3,sleepBetweenRetriesInSecs=1,port=8181,*ips):
+    dict = getClusterRoles(shardName,numOfShards,numOfTries,sleepBetweenRetriesInSecs,port,*ips)
+    result = []
+
+    for ip in dict.keys():
+        if(dict[ip]=='Follower'):
+            result.append(ip)
+
+    return result
+#
+#Given a shardname (e.g. shard-inventory-config), number of shards and an ip
+#determines if its a leader
+#
+#
+def isFollower(shardName,numOfShards,numOfRetries,sleepFor,port,ipAddress):
+    ip = getClusterRoles(shardName,numOfShards,numOfRetries,sleepFor,port,ipAddress)
+    print ip
+    if( ip[ipAddress] == 'Follower'):
+        return True
+
+    return False
+
+
+def testGetClusterRoles():
+    dict = getClusterRoles("shard-inventory-config",3,1,1,8181,"10.194.126.116","10.194.126.117","10.194.126.118")
+    print dict
+
+    for ip in dict.keys():
+        if(isLeader("shard-inventory-config",3,1,1,8181,ip)):
+            print ( ip + " is Leader")
+        elif (isFollower("shard-inventory-config",3,1,1,8181,ip)):
+            print (ip + " is follower")
+        else:
+            print (ip + " seems to have value "+ str(dict[ip]))
+
+def testGetLeader ():
+  leader =  getLeader("shard-inventory-config",3,1,1,8181,"10.194.126.116","10.194.126.117","10.194.126.118")
+  print leader
+  return leader
+
+def testGetFollowers():
+   followers = getFollowers("shard-inventory-config",3,1,1,8181,"10.194.126.116","10.194.126.117","10.194.126.118")
+   print followers
+   return followers
+
+#testGetClusterRoles()
+#testGetLeader()
+#testGetFollowers()
+
+
+
+
+
+
+
+
+
+
index cab12a3e6b384ec6fb166a811a2e1ce9c0a913b2..d98a263219a553ea4b7f0f38cfcbc379cbedcea9 100644 (file)
@@ -6,7 +6,7 @@ __email__ = "syedbahm@cisco.com"
 from string import Template
 
 # helps in taking the hostname entered by the user
-#global hostname 
+#global hostname
 #global port
 
 #def setHostname(host):
@@ -48,6 +48,10 @@ def getAddCarPersonUrl(hostname,port):
 def getBuyCarRpcUrl(hostname,port):
     return "http://"+hostname+":"+port+"/restconf/operations/car-purchase:buy-car"
 
+#GET URL for jolokia
+def getJolokiaURL(hostname,port, shardIndex,shardName):
+    return "http://"+ hostname + ":"+ port+"/jolokia/read/org.opendaylight.controller:Category=Shards,name=member-"+shardIndex+"-"+shardName+",type=DistributedConfigDatastore"
+
 
 # Template for Car resource payload
 add_car_payload_template = Template( '{\"car:cars\":{'
@@ -111,3 +115,5 @@ buy_car_rpc_template = Template ( '{'
 
 
 
+
+
index 00d1bff83bd2289143a49c76d68758210198090d..793fff4d9c5e4b491964a8cb46aef3a80760bb8c 100644 (file)
@@ -11,7 +11,7 @@ from SSHLibrary import SSHLibrary
 #Helps in making GET REST calls
 #
 
-def get(url, userId, password):
+def get(url, userId=None, password=None):
 
     headers = {}
     headers['Accept']= 'application/xml'