def server_loop(self, serverID, socket): recent_LogIdx = None while True: clientID, msg = socket.recv_multipart() if b'Connection Request' == msg[:18]: _globals._print("From server:", serverID, "|", msg) time.sleep(2.1) socket.send_multipart([clientID, b'Connection ACK']) continue else: if recent_LogIdx is not None and recent_LogIdx == self.log.get_entry_logIndex( msg): continue recent_LogIdx = self.log.get_entry_logIndex(msg) with _globals.print_lock: print("Reporting from server:", serverID) print("Received a message from leader:", ord(clientID)) logger.print_entry_buffer(self.log, msg) success = True followerTerm = self.term RESP = msgpack.packb([success, followerTerm]) delay = random.uniform(0.3, 0.8) time.sleep(delay) socket.send_multipart([clientID, RESP])
def listen_loop(self): while self.runThread: msg = self.pipe_fetch.recv() if msg[0] == 'fetchRaftAttributes': attributes = msg[1] attribute_dict = self.fetchRaftAttributes(attributes) self.pipe_fetch.send(attribute_dict) elif msg[0] == 'fetchLogAttributes': attributes = msg[1] attribute_dict = self.fetchLogAttributes(attributes) self.pipe_fetch.send(attribute_dict) elif msg[0] == 'printRaftAttributes': self.print_RaftServer_attributes(mode=0) self.pipe_fetch.send('ACK') elif msg[0] == 'printLogAttributes': self.print_Log_attributes(mode=0) self.pipe_fetch.send('ACK') elif msg[0] == 'printComManAttributes': self.print_ComMan_attributes(procIdx=msg[1], mode=2) self.pipe_fetch.send('ACK') elif msg[0] == 'terminate': _globals._print("From TestKit class: terminating") break else: _globals._print( "Wrong command - {0}: From TestKit.listen_loop".format( msg[0]))
def createSocket(self, IP, port, serverID): context = zmq.Context() socket = context.socket(zmq.ROUTER) if IP == 'localhost': addr = "tcp://*:{0}".format(port) else: addr = "tcp://{0}:{1}".format(IP, port) socket.bind(addr) self.sockets[serverID] = socket _globals._print("From Server:", serverID, "Created Socket:", addr)
def processRequest(self, req, attr): mode = attr['mode'] # _globals._print("HELLLL_1") if req == 'printRaftAttributes': self.print_RaftServer_attributes(mode=mode) elif req == 'printLogAttributes': self.print_Log_attributes(mode=mode) elif req == 'printComManAttributes': # _globals._print("HELLLL_2") procIdx = attr['procIdx'] self.print_ComMan_attributes(procIdx, mode=mode) else: _globals._print( "Wrong command - {0}: From TestKit.processRequest".format(req))
def test3(): all_raftParams = [] all_raftParams.append( Namespace(**run_path(dummy_params2_follower1_path)).raftParams) all_raftParams.append( Namespace(**run_path(dummy_params2_follower2_path)).raftParams) all_raftParams.append( Namespace(**run_path(dummy_params2_leader_path)).raftParams) router_instance = router.createRouter(all_raftParams) router_instance.start() rl_leader = RaftLauncher(dummy_params2_leader_path, testMode=True) rl_follower1 = RaftLauncher(dummy_params2_follower1_path, testMode=True) rl_follower2 = RaftLauncher(dummy_params2_follower2_path, testMode=True) SM_leader, testkit_leader = rl_leader.start() testkit_leader.setStateMachine(SM_leader) SM_follower1, testkit_follower1 = rl_follower1.start() testkit_follower1.setStateMachine(SM_follower1) SM_follower2, testkit_follower2 = rl_follower2.start() testkit_follower2.setStateMachine(SM_follower2) all_stateMachines = [SM_leader, SM_follower1, SM_follower2] testkit_leader.name = 'leader' testkit_follower1.name = 'follower1' testkit_follower2.name = 'follower2' time.sleep(1) _globals._print("*** RAFT_TEST: Raft System Initialized") # PORT = utils.findFreePort() PORT = 6000 print("*** RAFT_TEST: Listening on Port:", PORT) addr = "tcp://*:" + str(PORT) socket = zmq.Context().socket(zmq.ROUTER) socket.bind(addr) cmd_id = 0 while True: _, received_bin = socket.recv_multipart() received = msgpack.unpackb(received_bin) data = received.split('-') cmd_type = data[0] if cmd_type == 'exit': break elif cmd_type == 'printGlobalStates': testkit_leader.print_SM_globalState() testkit_follower1.print_SM_globalState() testkit_follower2.print_SM_globalState() elif cmd_type == 'entry': try: print(data) func, args = eval(data[1]), eval(data[2]) cmd_id += 1 for SM in all_stateMachines: if SM.executeCommand(func, args, cmd_id): break except Exception as e: print("Failed to receive from testClient") print(str(e)) continue else: print("Invalid cmd_type:", received) continue testkit_leader.exit() testkit_follower1.exit() testkit_follower2.exit() rl_leader.exit() rl_follower1.exit() rl_follower2.exit() router_instance.terminate()