def test_candidate_server_wins_election(self): board = MemoryBoard() state = Follower() server0 = Server(0, state, [], board, []) board = MemoryBoard() state = Follower() oserver = Server(1, state, [], board, []) board = MemoryBoard() state = Candidate() server = Server(2, state, [], board, [oserver, server0]) server0._neighbors.append(server) oserver._neighbors.append(server) oserver.on_message(oserver._messageBoard.get_message()) server0.on_message(server0._messageBoard.get_message()) server._total_nodes = 3 server.on_message(server._messageBoard.get_message()) server.on_message(server._messageBoard.get_message()) self.assertEqual(type(server._state), Leader)
def setUp(self): board = MemoryBoard() state = Follower() self.oserver = Server(0, state, [], board, []) board = MemoryBoard() state = Follower() self.server = Server(1, state, [], board, [self.oserver])
def test_two_candidates_tie(self): followers = [] for i in range(4): board = MemoryBoard() state = Follower() followers.append(Server(i, state, [], board, [])) board = MemoryBoard() state = Candidate() c0 = Server(5, state, [], board, followers[0:2]) board = MemoryBoard() state = Candidate() c1 = Server(6, state, [], board, followers[2:]) for i in range(2): followers[i]._neighbors.append(c0) followers[i].on_message(followers[i]._messageBoard.get_message()) for i in range(2, 4): followers[i]._neighbors.append(c1) followers[i].on_message(followers[i]._messageBoard.get_message()) c0._total_nodes = 6 c1._total_nodes = 6 for i in range(2): c0.on_message(c0._messageBoard.get_message()) c1.on_message(c1._messageBoard.get_message()) self.assertEqual(type(c0._state), Candidate) self.assertEqual(type(c1._state), Candidate)
def run(self): self._state = Follower() self._state.set_server(self) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind((self._ip_addr, self._listen_port)) sock.listen(4) while True: conn, addr = sock.accept() # spin off thread? print "CONNECTION ADDRESS: {0}".format(addr) message = conn.recv(2048) # process message here; either CLIENT, APPEND_ENTRIES, RESPONSE, or REQUEST_VOTE self.on_message(message)
def setUp(self): board = MemoryBoard() state = Follower() self.oserver = Server(0, state, [], board, []) board = MemoryBoard() state = Candidate() self.server = Server(1, state, [], board, [self.oserver]) self.oserver._neighbors.append(self.server)
def __init__(self): self.peers = [] for IP, port in config['raft']['cluster']: srv = ZeroMQPeer('%s:%s' % (IP, port), host=IP, port=port) self.peers.append(srv) if config['raft']['actor']: self.server = ZeroMQServer('%s:%s' % (config['IP'], config['raft']['port']), Follower(), [], self.peers, host=config['IP'], port=config['raft']['port']) self.server.start() self.client = ZeroMQClient(self.peers)
def setUp(self): followers = [] for i in range(1, 4): board = MemoryBoard() state = Follower() followers.append(Server(i, state, [], board, [])) board = MemoryBoard() state = Leader() self.leader = Server(0, state, [], board, followers) for i in followers: i._neighbors.append(self.leader)
def setUpClass(self): self.servers = [] for i in range(N): if i == 0: state = Leader() else: state = Follower() s = ZeroMQServer("S%d" % i, state, [], MemoryBoard(), [], 6666 + i) if (i == 0): self.leader = s self.servers.append(s) for i in range(N): me = self.servers[i] neighbors = [n for n in self.servers if n != me] me.set_neighbors(neighbors)
import unittest from boards.memory_board import MemoryBoard from messages.append_entries import AppendEntriesMessage from messages.request_vote import RequestVoteMessage from servers.server import Server from states.follower import Follower from states.candidate import Candidate from states.leader import Leader board = MemoryBoard() state = Follower() oserver = Server(0, state, [], board, []) # print(oserver) """ _name: 0, _state: Follower ** _timeout: 500, _timeoutTime: 1581685132.670627, _last_vote: None, _log: [], _messageBoard: board: [], _neighbors: [], _total_nodes: 0, _commitIndex: 0, _currentTerm: 0, _lastApplied: 0, _lastLogIndex: 0, _lastLogTerm: None """ board = MemoryBoard() state = Candidate() # 候选人 # print(state) # Candidate ** _last_vote: None, _votes: None
class Server(object): def __init__(self, server_directory_index): server_info = SERVER_DIRECTORY[server_directory_index] self._name = server_info[SERVER_INFO_NAME_INDEX] self._ip_addr = server_info[SERVER_INFO_IP_ADDR_INDEX] self._listen_port = int(server_info[SERVER_INFO_LISTEN_PORT_INDEX]) self._neighbors = [] # for server_info in SERVER_DIRECTORY.values(): # server_name = server_info[SERVER_INFO_NAME_INDEX] # # if server_name != self._name: # self._neighbors[server_name] = self._log = [] self._commitIndex = 0 self._currentTerm = 0 self._lastApplied = 0 self._lastLogIndex = 0 self._lastLogTerm = None self._state = None self._ftqueue = FTQueue() def run(self): self._state = Follower() self._state.set_server(self) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind((self._ip_addr, self._listen_port)) sock.listen(4) while True: conn, addr = sock.accept() # spin off thread? print "CONNECTION ADDRESS: {0}".format(addr) message = conn.recv(2048) # process message here; either CLIENT, APPEND_ENTRIES, RESPONSE, or REQUEST_VOTE self.on_message(message) def send_message(self, message): for n in self._neighbors: message._receiver = n._name n.post_message(message) def send_message_response(self, message): n = [n for n in self._neighbors if n._name == message.receiver] if (len(n) > 0): n[0].post_message(message) def post_message(self, message): self._messageBoard.post_message(message) def on_message(self, message): state, response = self._state.on_message(message) self._state = state