Skip to content

Commit

Permalink
simplify cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ywelsch committed Aug 22, 2018
1 parent 7dcd656 commit bd242ed
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,23 @@ private void handleJoinRequestUnderLock(JoinRequest joinRequest, JoinCallback jo
}
}

if (prevElectionWon == false && coordState.electionWon()) {
joinHelper.addPendingJoin(joinRequest, joinCallback);
becomeLeader("handleJoin");
joinHelper.clearAndSubmitPendingJoins();
} else if (mode == Mode.LEADER) {
joinHelper.joinLeader(joinRequest, joinCallback);
} else if (mode == Mode.FOLLOWER) {
joinCallback.onFailure(new CoordinationStateRejectedException("join target is a follower"));
} else {
assert mode == Mode.CANDIDATE;
joinHelper.addPendingJoin(joinRequest, joinCallback);
switch (mode) {
case LEADER:
joinHelper.joinLeader(joinRequest, joinCallback);
break;
case FOLLOWER:
assert joinRequest.getOptionalJoin().isPresent() == false : "follower should not have solicited join " + joinRequest;
joinCallback.onFailure(new CoordinationStateRejectedException("join target is a follower"));
break;
case CANDIDATE:
joinHelper.addPendingJoin(joinRequest, joinCallback);
if (prevElectionWon == false && coordState.electionWon()) {
becomeLeader("handleJoin");
joinHelper.clearAndSubmitPendingJoins();
}
break;
default:
throw new AssertionError("unexpected mode when handling join: " + mode);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,25 @@ public void testJoinAccumulation() {
FutureUtils.get(fut);
}

public void testJoinFollowerWithHigherTerm() {
DiscoveryNode node0 = newNode(0, true);
DiscoveryNode node1 = newNode(1, true);
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupFakeMasterServiceAndCoordinator(initialTerm, initialState(false, node0, initialTerm, initialVersion,
new VotingConfiguration(Collections.singleton(node0.getId()))));
long newTerm = initialTerm + randomLongBetween(1, 10);
coordinator.coordinationState.get().handleStartJoin(new StartJoinRequest(node1, newTerm));
synchronized (coordinator.mutex) {
coordinator.becomeFollower("test", node1);
}
assertFalse(isLocalNodeElectedMaster());
long newerTerm = newTerm + randomLongBetween(1, 10);
joinNodeAndRun(new JoinRequest(node1,
Optional.of(new Join(node1, node0, newerTerm, initialTerm, initialVersion))));
assertTrue(isLocalNodeElectedMaster());
}

public void testJoinFollowerFails() {
DiscoveryNode node0 = newNode(0, true);
DiscoveryNode node1 = newNode(1, true);
Expand All @@ -293,8 +312,7 @@ public void testJoinFollowerFails() {
}
assertFalse(isLocalNodeElectedMaster());
assertThat(expectThrows(CoordinationStateRejectedException.class,
() -> joinNodeAndRun(new JoinRequest(node1,
Optional.of(new Join(node1, node0, newTerm, initialTerm, initialVersion))))).getMessage(),
() -> joinNodeAndRun(new JoinRequest(node1, Optional.empty()))).getMessage(),
containsString("join target is a follower"));
assertFalse(isLocalNodeElectedMaster());
}
Expand Down

0 comments on commit bd242ed

Please sign in to comment.