@Test public void respondToRemoteTerm() throws Exception { for (Server s : raftServer) { s.start(); } raftServer[0].become(CANDIDATE, FOLLOWER); int leaderCount = 0; UUID initialLeader = null; long startTime = System.currentTimeMillis(); while (leaderCount == 0 && System.currentTimeMillis() < startTime + Server.LEADER_HEARTBEAT_TIMEOUT) { for (Server s : raftServer) { if (s.getState() == LEADER) { initialLeader = s.getMyServerID(); leaderCount++; } } } assertEquals(raftServer[0].getMyServerID(), initialLeader); raftServer[0].respondToRemoteTerm(2); Assert.assertEquals(FOLLOWER, raftServer[0].getState()); }
void respondToRemoteTerm(int remoteTerm) { int currentTerm = pState.getCurrentTerm(); if (currentTerm < remoteTerm) { pState.setCurrentTerm(remoteTerm, currentTerm); become(State.FOLLOWER, vState.getState()); } }
Server implements RemoteRaftServer { void respondToRemoteTerm(int remoteTerm) { int currentTerm = pState.getCurrentTerm(); if (currentTerm < remoteTerm) { pState.setCurrentTerm(remoteTerm, currentTerm); become(State.FOLLOWER, vState.getState()); } } }
Server implements RemoteRaftServer { void respondToRemoteTerm(int remoteTerm) { int currentTerm = pState.getCurrentTerm(); if (currentTerm < remoteTerm) { pState.setCurrentTerm(remoteTerm, currentTerm); become(State.FOLLOWER, vState.getState()); } } Server(StateMachineApplier applier); }
Server implements RemoteRaftServer { void respondToRemoteTerm(int remoteTerm) { int currentTerm = pState.getCurrentTerm(); if (currentTerm < remoteTerm) { pState.setCurrentTerm(remoteTerm, currentTerm); become(State.FOLLOWER, vState.getState()); } } Server(StateMachineApplier applier); UUID getMyServerID(); void start(); void stop(); int appendEntries(
            int term,
            UUID leader,
            int prevLogIndex,
            int prevLogTerm,
            List<LogEntry> entries,
            int leaderCommit); int requestVote(int term, UUID candidate, int lastLogIndex, int lastLogTerm); Object applyToStateMachine(Object operation); void addServer(UUID id, RemoteRaftServer server); }
Server implements RemoteRaftServer { void respondToRemoteTerm(int remoteTerm) { int currentTerm = pState.getCurrentTerm(); if (currentTerm < remoteTerm) { pState.setCurrentTerm(remoteTerm, currentTerm); become(State.FOLLOWER, vState.getState()); } } Server(StateMachineApplier applier); UUID getMyServerID(); void start(); void stop(); int appendEntries(
            int term,
            UUID leader,
            int prevLogIndex,
            int prevLogTerm,
            List<LogEntry> entries,
            int leaderCommit); int requestVote(int term, UUID candidate, int lastLogIndex, int lastLogTerm); Object applyToStateMachine(Object operation); void addServer(UUID id, RemoteRaftServer server); static final int LEADER_HEARTBEAT_TIMEOUT; }