def broadCastLeader(self): for k in get_client_map().items(): logger.info("Broadcasting leader node {} to cluster node {}".format(self.leader_id,k[1])) c = self.get_client(k[0]) if not c: continue c.setLeader(self.leader_id)
def connect_neighbours(self): print ("connecting to neighbours") for v in get_client_map().items(): node_details = v[1] try: c =Client(node_details[0], node_details[1]) self.clients[v[0]] = {'client_obj':c,'active':True,'host':node_details[0],'port':node_details[1],'write_capacity_full':False} except: self.clients[v[0]] = {'client_obj':None,'active':False,'host':node_details[0],'port':node_details[1],'write_capacity_full':False} print ("done connecting to neighbours")
def checkClusterForLeader(self): for k in get_client_map().items(): c = self.get_client(k[0]) if not c: continue status = c.getClientStatus() logger.info("Status for client id %d",k[0]) if(status.is_leader): leader_node = status.leader_id leader_client = self.get_client(leader_node) if leader_client.pingInternal(): return leader_node else: logger.info("Unable to ping leader") return 0 return 0
def requestVote(self): logger.info("Node {} contesting election".format(self.id)) self.status = 'CANDIDATE' self.vote_count = 1 for k in get_client_map().items(): c = self.get_client(k[0]) if not c: continue id = k[0] if id == self.id: logger.info("Not requesting vote from myself") continue if (c.requestVote(self.id)).result: self.vote_count+=1 active_nodes = self.getActiveNodes() logger.info("Active nodes count %d",active_nodes) logger.info("No of votes needed %d" ,math.ceil((active_nodes+1)/2)) if self.vote_count >= math.ceil((active_nodes+1)/2): return True else: return False
def getActiveNodes(self): count = 0 for k in get_client_map().items(): if self.get_client(k[0]): count+=1 return count