def _send_response_message(self, msg, yes=True): """ AppendEntries: 0, RequestVote: 1, RequestVoteResponse: 2, Response: 3, _timestamp: 1581652434, _sender: 0, _receiver: 1, _data: {}, _term: 2 _name: 1, _state: <states.follower.Follower object at 0x10ef77048>, _log: [], _messageBoard: board: [], _neighbors: [<servers.server.Server object at 0x10ef71cf8>], _total_nodes: 0, _commitIndex: 0, _currentTerm: 0, _lastApplied: 0, _lastLogIndex: 0, _lastLogTerm: None """ response = ResponseMessage( self._server._name, msg.sender, msg.term, { "response": yes, "currentTerm": self._server._currentTerm } ) # print(response) self._server.send_message_response(response)
def _send_response_message(self, msg, yes=True): response = ResponseMessage(sender=self.server.name, receiver=msg.sender, term=self.server.current_term, data={ "response": yes, "current_term": self.server.current_term, }) self.server.send_message_response(response)
def _send_response_message(self, msg, yes=True, txn_id=None, reply_heartbeat=False): response = ResponseMessage( self._server._name, msg.sender, msg.term, { "response": yes, "currentTerm": self._server._currentTerm, 'txn_id': txn_id, 'reply_heartbeat': reply_heartbeat }) self._server.send_message(response)
def on_message(self, message): """This method is called when a message is received, and calls one of the other corrosponding methods that this state reacts to. """ if ClientRequestMessage.is_client_message(message): message = ClientRequestMessage.from_message_string(message) print "NOT SURE WHAT TO DO NOW..." if (message.term > self._server._currentTerm): self._server._currentTerm = message.term # Is the messages.term < ours? If so we need to tell # them this so they don't get left behind. elif (message.term < self._server._currentTerm): self._send_response_message(message, yes=False) return self, None if AppendEntriesMessage.is_append_entries_message(): message = AppendEntriesMessage.from_message_string(message) return self.on_append_entries(message) elif RequestVoteMessage.is_request_vote_message(message): message = RequestVoteMessage.from_message_string(message) return self.on_vote_request(message) elif RequestVoteResponseMessage.is_request_vote_response_message( message): message = RequestVoteResponseMessage.from_message_string(message) return self.on_vote_received(message) elif ResponseMessage.is_response_message(message): message = ResponseMessage.from_message_string(message) return self.on_response_received(message)
def on_response_received(self, message): # Was the last AppendEntries good? if (not message.data["response"]): # No, so lets back up the log for this node self._nextIndexes[message.sender] = \ min(self._nextIndexes[message.sender], self._server._log.size()-1) if not message.data['reply_heartbeat']: self._nextIndexes[message.sender] -= 1 # Get the next log entry to send to the client. previousIndex = max(0, self._nextIndexes[message.sender] - 1) previous = self._server._log.get(previousIndex) self._server.logger.info(str(self._nextIndexes[message.sender])) current = self._server._log.get(self._nextIndexes[message.sender]) # Send the new log to the client and wait for it to respond. appendEntry = AppendEntriesMessage( self._server._name, message.sender, self._server._currentTerm, { "leaderId": self._server._name, "prevLogIndex": previousIndex, "prevLogTerm": previous.get_term(), "entries": [current], "leaderCommit": self._server._commitIndex, }) self._server.send_message(appendEntry) else: # The last append was good so increase their index. self._server.logger.info('append an entry to follower') self._nextIndexes[message.sender] += 1 # Are they caught up? if (self._nextIndexes[message.sender] > self._server._lastLogIndex + 1): self._nextIndexes[ message.sender] = self._server._lastLogIndex + 1 self._matchIndex[ message.sender] = self._nextIndexes[message.sender] - 1 commit_count = 1 for _ in self._matchIndex: #print(self._matchIndex[_], self._nextIndexes[_]) if self._matchIndex[_] > self._server._commitIndex: commit_count += 1 if commit_count > (self._server._total_nodes - 1) / 2: self._server.logger.info('commitIndex add 1') self._server._commitIndex += 1 self._server._log.set_commitIndex(self._server._commitIndex) commit_log = self._server._log.get(self._server._commitIndex) log_data = commit_log.txns for txn in log_data: #print(txn) if len(txn.split('\t')) == 1: continue txn_id = txn.split('\t')[0] receiver = txn_id.split('_')[0].replace(' ', '\t') response = ResponseMessage( self._server._name, receiver, None, { "response": True, "currentTerm": self._server._currentTerm, 'txn_id': txn_id }) self._server.send_message(response) return self, None
def send_message(queue: Queue, message: ResponseMessage) -> None: body = message.to_json() queue.send_message(MessageBody=body)
def _send_response_message(self, msg, yes=True): response = ResponseMessage(self._server._name, msg.sender, msg.term, { "response": yes, "currentTerm": self._server._currentTerm, }) self._server.send_message_response(response)