Skip to content

Commit

Permalink
pick more variable term and version
Browse files Browse the repository at this point in the history
  • Loading branch information
ywelsch committed Aug 22, 2018
1 parent 4676116 commit 7dcd656
Showing 1 changed file with 53 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,59 +204,76 @@ private void joinNodeAndRun(final JoinRequest joinRequest) {
public void testJoinWithHigherTermElectsLeader() {
DiscoveryNode node0 = newNode(0, true);
DiscoveryNode node1 = newNode(1, true);
setupFakeMasterServiceAndCoordinator(1, initialState(false, node0, 1, 1,
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupFakeMasterServiceAndCoordinator(initialTerm, initialState(false, node0, initialTerm, initialVersion,
new VotingConfiguration(Collections.singleton(randomFrom(node0, node1).getId()))));
assertFalse(isLocalNodeElectedMaster());
joinNodeAndRun(new JoinRequest(node1, Optional.of(new Join(node1, node0, 2, 1, 1))));
long newTerm = initialTerm + randomLongBetween(1, 10);
joinNodeAndRun(new JoinRequest(node1, Optional.of(new Join(node1, node0, newTerm, initialTerm, initialVersion))));
assertTrue(isLocalNodeElectedMaster());
}

public void testJoinWithHigherTermButBetterStateGetsRejected() {
DiscoveryNode node0 = newNode(0, true);
DiscoveryNode node1 = newNode(1, true);
setupFakeMasterServiceAndCoordinator(1, initialState(false, node0, 1, 1,
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupFakeMasterServiceAndCoordinator(initialTerm, initialState(false, node0, initialTerm, initialVersion,
new VotingConfiguration(Collections.singleton(node1.getId()))));
assertFalse(isLocalNodeElectedMaster());
long newTerm = initialTerm + randomLongBetween(1, 10);
long higherVersion = initialVersion + randomLongBetween(1, 10);
expectThrows(CoordinationStateRejectedException.class,
() -> joinNodeAndRun(new JoinRequest(node1, Optional.of(new Join(node1, node0, 2, 2, 2)))));
() -> joinNodeAndRun(new JoinRequest(node1, Optional.of(new Join(node1, node0, newTerm, initialTerm, higherVersion)))));
assertFalse(isLocalNodeElectedMaster());
}

public void testJoinWithHigherTermButBetterStateStillElectsMasterThroughSelfJoin() {
DiscoveryNode node0 = newNode(0, true);
DiscoveryNode node1 = newNode(1, true);
setupFakeMasterServiceAndCoordinator(1, initialState(false, node0, 1, 1,
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupFakeMasterServiceAndCoordinator(initialTerm, initialState(false, node0, initialTerm, initialVersion,
new VotingConfiguration(Collections.singleton(node0.getId()))));
assertFalse(isLocalNodeElectedMaster());
joinNodeAndRun(new JoinRequest(node1, Optional.of(new Join(node1, node0, 2, 2, 2))));
long newTerm = initialTerm + randomLongBetween(1, 10);
long higherVersion = initialVersion + randomLongBetween(1, 10);
joinNodeAndRun(new JoinRequest(node1, Optional.of(new Join(node1, node0, newTerm, initialTerm, higherVersion))));
assertTrue(isLocalNodeElectedMaster());
}

public void testJoinElectedLeader() {
DiscoveryNode node0 = newNode(0, true);
DiscoveryNode node1 = newNode(1, true);
setupFakeMasterServiceAndCoordinator(1, initialState(false, node0, 1, 1,
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupFakeMasterServiceAndCoordinator(initialTerm, initialState(false, node0, initialTerm, initialVersion,
new VotingConfiguration(Collections.singleton(node0.getId()))));
assertFalse(isLocalNodeElectedMaster());
joinNodeAndRun(new JoinRequest(node0, Optional.of(new Join(node0, node0, 2, 1, 1))));
long newTerm = initialTerm + randomLongBetween(1, 10);
joinNodeAndRun(new JoinRequest(node0, Optional.of(new Join(node0, node0, newTerm, initialTerm, initialVersion))));
assertTrue(isLocalNodeElectedMaster());
assertFalse(clusterStateHasNode(node1));
joinNodeAndRun(new JoinRequest(node1, Optional.of(new Join(node1, node0, 2, 1, 1))));
joinNodeAndRun(new JoinRequest(node1, Optional.of(new Join(node1, node0, newTerm, initialTerm, initialVersion))));
assertTrue(isLocalNodeElectedMaster());
assertTrue(clusterStateHasNode(node1));
}

public void testJoinAccumulation() {
DiscoveryNode node0 = newNode(0, true);
DiscoveryNode node1 = newNode(1, true);
setupFakeMasterServiceAndCoordinator(1, initialState(false, node0, 1, 1,
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupFakeMasterServiceAndCoordinator(initialTerm, initialState(false, node0, initialTerm, initialVersion,
new VotingConfiguration(Collections.singleton(node1.getId()))));
assertFalse(isLocalNodeElectedMaster());
SimpleFuture fut = joinNodeAsync(new JoinRequest(node0, Optional.of(new Join(node0, node0, 2, 1, 1))));
long newTerm = initialTerm + randomLongBetween(1, 10);
SimpleFuture fut = joinNodeAsync(new JoinRequest(node0, Optional.of(new Join(node0, node0, newTerm, initialTerm, initialVersion))));
deterministicTaskQueue.runAllTasks(random());
assertFalse(fut.isDone());
assertFalse(isLocalNodeElectedMaster());
joinNodeAndRun(new JoinRequest(node1, Optional.of(new Join(node1, node0, 2, 1, 1))));
joinNodeAndRun(new JoinRequest(node1, Optional.of(new Join(node1, node0, newTerm, initialTerm, initialVersion))));
assertTrue(isLocalNodeElectedMaster());
assertTrue(clusterStateHasNode(node1));
FutureUtils.get(fut);
Expand All @@ -265,25 +282,32 @@ public void testJoinAccumulation() {
public void testJoinFollowerFails() {
DiscoveryNode node0 = newNode(0, true);
DiscoveryNode node1 = newNode(1, true);
setupFakeMasterServiceAndCoordinator(1, initialState(false, node0, 1, 1,
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupFakeMasterServiceAndCoordinator(initialTerm, initialState(false, node0, initialTerm, initialVersion,
new VotingConfiguration(Collections.singleton(node0.getId()))));
coordinator.coordinationState.get().handleStartJoin(new StartJoinRequest(node1, 2));
long newTerm = initialTerm + randomLongBetween(1, 10);
coordinator.coordinationState.get().handleStartJoin(new StartJoinRequest(node1, newTerm));
synchronized (coordinator.mutex) {
coordinator.becomeFollower("test", node1);
}
assertFalse(isLocalNodeElectedMaster());
assertThat(expectThrows(CoordinationStateRejectedException.class,
() -> joinNodeAndRun(new JoinRequest(node1, Optional.of(new Join(node1, node0, 2, 1, 1))))).getMessage(),
() -> joinNodeAndRun(new JoinRequest(node1,
Optional.of(new Join(node1, node0, newTerm, initialTerm, initialVersion))))).getMessage(),
containsString("join target is a follower"));
assertFalse(isLocalNodeElectedMaster());
}

public void testBecomeFollowerFailsPendingJoin() {
DiscoveryNode node0 = newNode(0, true);
DiscoveryNode node1 = newNode(1, true);
setupFakeMasterServiceAndCoordinator(1, initialState(false, node0, 1, 1,
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupFakeMasterServiceAndCoordinator(initialTerm, initialState(false, node0, initialTerm, initialVersion,
new VotingConfiguration(Collections.singleton(node1.getId()))));
SimpleFuture fut = joinNodeAsync(new JoinRequest(node0, Optional.of(new Join(node0, node0, 2, 1, 1))));
long newTerm = initialTerm + randomLongBetween(1, 10);
SimpleFuture fut = joinNodeAsync(new JoinRequest(node0, Optional.of(new Join(node0, node0, newTerm, initialTerm, initialVersion))));
deterministicTaskQueue.runAllTasks(random());
assertFalse(fut.isDone());
assertFalse(isLocalNodeElectedMaster());
Expand All @@ -307,7 +331,10 @@ public void testConcurrentJoining() {
logger.info("Voting configuration: {}", votingConfiguration);

DiscoveryNode localNode = nodes.get(0);
setupRealMasterServiceAndCoordinator(1, initialState(false, localNode, 1, 1, votingConfiguration));
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupRealMasterServiceAndCoordinator(initialTerm, initialState(false, localNode, initialTerm, initialVersion, votingConfiguration));
long newTerm = initialTerm + randomLongBetween(1, 10);

// we need at least a quorum of voting nodes with a correct term and worse state
List<DiscoveryNode> successfulNodes;
Expand All @@ -319,7 +346,8 @@ public void testConcurrentJoining() {
logger.info("Successful voting nodes: {}", successfulNodes);

List<JoinRequest> correctJoinRequests = successfulNodes.stream().map(
node -> new JoinRequest(node, Optional.of(new Join(node, localNode, 2, 1, 1)))).collect(Collectors.toList());
node -> new JoinRequest(node, Optional.of(new Join(node, localNode, newTerm, initialTerm, initialVersion))))
.collect(Collectors.toList());

List<DiscoveryNode> possiblyUnsuccessfulNodes = new ArrayList<>(nodes);
possiblyUnsuccessfulNodes.removeAll(successfulNodes);
Expand All @@ -329,13 +357,16 @@ public void testConcurrentJoining() {
List<JoinRequest> possiblyFailingJoinRequests = possiblyUnsuccessfulNodes.stream().map(node -> {
if (randomBoolean()) {
// a correct request
return new JoinRequest(node, Optional.of(new Join(node, localNode, 2, 1, 1)));
return new JoinRequest(node, Optional.of(new Join(node, localNode,
newTerm, initialTerm, initialVersion)));
} else if (randomBoolean()) {
// term too low
return new JoinRequest(node, Optional.of(new Join(node, localNode, 1, 1, 1)));
return new JoinRequest(node, Optional.of(new Join(node, localNode,
randomLongBetween(0, initialTerm), initialTerm, initialVersion)));
} else {
// better state
return new JoinRequest(node, Optional.of(new Join(node, localNode, 2, 2, 2)));
return new JoinRequest(node, Optional.of(new Join(node, localNode,
newTerm, initialTerm, initialVersion + randomLongBetween(1, 10))));
}
}).collect(Collectors.toList());

Expand Down

0 comments on commit 7dcd656

Please sign in to comment.