@Test public void become() throws Exception { raftServer[0].start(); raftServer[0].become(FOLLOWER, FOLLOWER); assert (raftServer[0].getState() == FOLLOWER); raftServer[0].become(CANDIDATE, FOLLOWER); assert (raftServer[0].getState() == CANDIDATE); raftServer[0].become(LEADER, CANDIDATE); assert (raftServer[0].getState() == LEADER); }
synchronized State become(State newState, State preconditionState) { if (vState.getState() == preconditionState) { switch (vState.getState()) { case NONE: break; case LEADER: leader.stop(); break; case FOLLOWER: follower.stop(); break; case CANDIDATE: candidate.stop(); } switch (newState) { case NONE: vState.setState(State.NONE, vState.getState()); break; case LEADER: leader.start(); break; case FOLLOWER: follower.start(); break; case CANDIDATE: candidate.start(); } } return vState.getState(); }
Server implements RemoteRaftServer { synchronized State become(State newState, State preconditionState) { if (vState.getState() == preconditionState) { switch (vState.getState()) { case NONE: break; case LEADER: leader.stop(); break; case FOLLOWER: follower.stop(); break; case CANDIDATE: candidate.stop(); } switch (newState) { case NONE: vState.setState(State.NONE, vState.getState()); break; case LEADER: leader.start(); break; case FOLLOWER: follower.start(); break; case CANDIDATE: candidate.start(); } } return vState.getState(); } }
Server implements RemoteRaftServer { synchronized State become(State newState, State preconditionState) { if (vState.getState() == preconditionState) { switch (vState.getState()) { case NONE: break; case LEADER: leader.stop(); break; case FOLLOWER: follower.stop(); break; case CANDIDATE: candidate.stop(); } switch (newState) { case NONE: vState.setState(State.NONE, vState.getState()); break; case LEADER: leader.start(); break; case FOLLOWER: follower.start(); break; case CANDIDATE: candidate.start(); } } return vState.getState(); } Server(StateMachineApplier applier); }
Server implements RemoteRaftServer { synchronized State become(State newState, State preconditionState) { if (vState.getState() == preconditionState) { switch (vState.getState()) { case NONE: break; case LEADER: leader.stop(); break; case FOLLOWER: follower.stop(); break; case CANDIDATE: candidate.stop(); } switch (newState) { case NONE: vState.setState(State.NONE, vState.getState()); break; case LEADER: leader.start(); break; case FOLLOWER: follower.start(); break; case CANDIDATE: candidate.start(); } } return 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 { synchronized State become(State newState, State preconditionState) { if (vState.getState() == preconditionState) { switch (vState.getState()) { case NONE: break; case LEADER: leader.stop(); break; case FOLLOWER: follower.stop(); break; case CANDIDATE: candidate.stop(); } switch (newState) { case NONE: vState.setState(State.NONE, vState.getState()); break; case LEADER: leader.start(); break; case FOLLOWER: follower.start(); break; case CANDIDATE: candidate.start(); } } return 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; }