Skip to content

Commit

Permalink
Transition Raft servers to candidate/leader immediately if single nod…
Browse files Browse the repository at this point in the history
…e cluster (#1005)

* Transition Raft servers to candidate/leader immediately if single node cluster.

Co-authored-by: Johno Crawford <johno@hellface.com>
  • Loading branch information
kuujo and johnou committed Apr 26, 2020
1 parent 9bc9745 commit 568639e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public RaftServer.Role role() {

@Override
public synchronized CompletableFuture<RaftRole> start() {
if (raft.getCluster().getActiveMemberStates().isEmpty()) {
log.debug("Single member cluster. Transitioning directly to leader.");
raft.setTerm(raft.getTerm() + 1);
raft.setLastVotedFor(raft.getCluster().getMember().memberId());
raft.transition(RaftServer.Role.LEADER);
return CompletableFuture.completedFuture(this);
}
return super.start().thenRun(this::startElection).thenApply(v -> this);
}

Expand Down Expand Up @@ -91,13 +98,6 @@ private void sendVoteRequests() {
final AtomicBoolean complete = new AtomicBoolean();
final Set<DefaultRaftMember> votingMembers = new HashSet<>(raft.getCluster().getActiveMemberStates().stream().map(RaftMemberContext::getMember).collect(Collectors.toList()));

// If there are no other members in the cluster, immediately transition to leader.
if (votingMembers.isEmpty()) {
log.debug("Single member cluster. Transitioning directly to leader.", raft.getCluster().getMember().memberId());
raft.transition(RaftServer.Role.LEADER);
return;
}

// Send vote requests to all nodes. The vote request that is sent
// to this node will be automatically successful.
// First check if the quorum is null. If the quorum isn't null then that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public RaftServer.Role role() {

@Override
public synchronized CompletableFuture<RaftRole> start() {
if (raft.getCluster().getActiveMemberStates().isEmpty()) {
log.debug("Single member cluster. Transitioning directly to candidate.");
raft.transition(RaftServer.Role.CANDIDATE);
return CompletableFuture.completedFuture(this);
}
raft.getMembershipService().addListener(clusterListener);
return super.start().thenRun(this::resetHeartbeatTimeout).thenApply(v -> this);
}
Expand Down

0 comments on commit 568639e

Please sign in to comment.