Skip to content

Commit

Permalink
Use VotingConfiguration#of where possible
Browse files Browse the repository at this point in the history
This resolves a longstanding TODO in the cluster coordination subsystem.

Relates elastic#32006
  • Loading branch information
DaveCTurner committed Mar 31, 2020
1 parent 666719f commit d09853a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
}

public static VotingConfiguration of(DiscoveryNode... nodes) {
// this could be used in many more places - TODO use this where appropriate
return new VotingConfiguration(Arrays.stream(nodes).map(DiscoveryNode::getId).collect(Collectors.toSet()));
}
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,7 @@ public void testCannotSetInitialConfigurationWithoutQuorum() {
assertThat(exceptionMessage, containsString(coordinator.getLocalNode().toString()));

// This is VERY BAD: setting a _different_ initial configuration. Yet it works if the first attempt will never be a quorum.
assertTrue(coordinator.setInitialConfiguration(
new VotingConfiguration(Collections.singleton(coordinator.getLocalNode().getId()))));
assertTrue(coordinator.setInitialConfiguration(VotingConfiguration.of(coordinator.getLocalNode())));
cluster.stabilise();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public void testJoinWithHigherTermElectsLeader() {
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupFakeMasterServiceAndCoordinator(initialTerm, initialState(node0, initialTerm, initialVersion,
new VotingConfiguration(Collections.singleton(randomFrom(node0, node1).getId()))));
VotingConfiguration.of(randomFrom(node0, node1))));
assertFalse(isLocalNodeElectedMaster());
assertNull(coordinator.getStateForMasterService().nodes().getMasterNodeId());
long newTerm = initialTerm + randomLongBetween(1, 10);
Expand All @@ -296,7 +296,7 @@ public void testJoinWithHigherTermButBetterStateGetsRejected() {
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupFakeMasterServiceAndCoordinator(initialTerm, initialState(node0, initialTerm, initialVersion,
new VotingConfiguration(Collections.singleton(node1.getId()))));
VotingConfiguration.of(node1)));
assertFalse(isLocalNodeElectedMaster());
long newTerm = initialTerm + randomLongBetween(1, 10);
long higherVersion = initialVersion + randomLongBetween(1, 10);
Expand All @@ -312,7 +312,7 @@ public void testJoinWithHigherTermButBetterStateStillElectsMasterThroughSelfJoin
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupFakeMasterServiceAndCoordinator(initialTerm, initialState(node0, initialTerm, initialVersion,
new VotingConfiguration(Collections.singleton(node0.getId()))));
VotingConfiguration.of(node0)));
assertFalse(isLocalNodeElectedMaster());
long newTerm = initialTerm + randomLongBetween(1, 10);
long higherVersion = initialVersion + randomLongBetween(1, 10);
Expand All @@ -326,7 +326,7 @@ public void testJoinElectedLeader() {
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupFakeMasterServiceAndCoordinator(initialTerm, initialState(node0, initialTerm, initialVersion,
new VotingConfiguration(Collections.singleton(node0.getId()))));
VotingConfiguration.of(node0)));
assertFalse(isLocalNodeElectedMaster());
long newTerm = initialTerm + randomLongBetween(1, 10);
joinNodeAndRun(new JoinRequest(node0, newTerm, Optional.of(new Join(node0, node0, newTerm, initialTerm, initialVersion))));
Expand All @@ -343,7 +343,7 @@ public void testJoinElectedLeaderWithHigherTerm() {
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupFakeMasterServiceAndCoordinator(initialTerm, initialState(node0, initialTerm, initialVersion,
new VotingConfiguration(Collections.singleton(node0.getId()))));
VotingConfiguration.of(node0)));
long newTerm = initialTerm + randomLongBetween(1, 10);

joinNodeAndRun(new JoinRequest(node0, newTerm, Optional.of(new Join(node0, node0, newTerm, initialTerm, initialVersion))));
Expand All @@ -362,7 +362,7 @@ public void testJoinAccumulation() {
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupFakeMasterServiceAndCoordinator(initialTerm, initialState(node0, initialTerm, initialVersion,
new VotingConfiguration(Collections.singleton(node2.getId()))));
VotingConfiguration.of(node2)));
assertFalse(isLocalNodeElectedMaster());
long newTerm = initialTerm + randomLongBetween(1, 10);
SimpleFuture futNode0 = joinNodeAsync(new JoinRequest(node0, newTerm, Optional.of(
Expand All @@ -389,7 +389,7 @@ public void testJoinFollowerWithHigherTerm() throws Exception {
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupFakeMasterServiceAndCoordinator(initialTerm, initialState(node0, initialTerm, initialVersion,
new VotingConfiguration(Collections.singleton(node0.getId()))));
VotingConfiguration.of(node0)));
long newTerm = initialTerm + randomLongBetween(1, 10);
handleStartJoinFrom(node1, newTerm);
handleFollowerCheckFrom(node1, newTerm);
Expand Down Expand Up @@ -464,7 +464,7 @@ public void testJoinFollowerFails() throws Exception {
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupFakeMasterServiceAndCoordinator(initialTerm, initialState(node0, initialTerm, initialVersion,
new VotingConfiguration(Collections.singleton(node0.getId()))));
VotingConfiguration.of(node0)));
long newTerm = initialTerm + randomLongBetween(1, 10);
handleStartJoinFrom(node1, newTerm);
handleFollowerCheckFrom(node1, newTerm);
Expand All @@ -480,7 +480,7 @@ public void testBecomeFollowerFailsPendingJoin() throws Exception {
long initialTerm = randomLongBetween(1, 10);
long initialVersion = randomLongBetween(1, 10);
setupFakeMasterServiceAndCoordinator(initialTerm, initialState(node0, initialTerm, initialVersion,
new VotingConfiguration(Collections.singleton(node1.getId()))));
VotingConfiguration.of(node1)));
long newTerm = initialTerm + randomLongBetween(1, 10);
SimpleFuture fut = joinNodeAsync(new JoinRequest(node0, newTerm,
Optional.of(new Join(node0, node0, newTerm, initialTerm, initialVersion))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ private void runCollector() {
}

private ClusterState makeClusterState(DiscoveryNode[] votingNodes) {
final VotingConfiguration votingConfiguration
= new VotingConfiguration(Arrays.stream(votingNodes).map(DiscoveryNode::getId).collect(Collectors.toSet()));
final VotingConfiguration votingConfiguration = VotingConfiguration.of(votingNodes);
return CoordinationStateTests.clusterState(lastAcceptedTerm, lastAcceptedVersion, localNode,
votingConfiguration, votingConfiguration, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private void initializeCluster(VotingConfiguration initialConfig) {
}

public void testSimpleClusterStatePublishing() throws InterruptedException {
VotingConfiguration singleNodeConfig = new VotingConfiguration(Sets.newHashSet(n1.getId()));
VotingConfiguration singleNodeConfig = VotingConfiguration.of(n1);
initializeCluster(singleNodeConfig);

AssertingAckListener ackListener = new AssertingAckListener(nodes.size());
Expand Down Expand Up @@ -252,7 +252,7 @@ public void testSimpleClusterStatePublishing() throws InterruptedException {
}

public void testClusterStatePublishingWithFaultyNodeBeforeCommit() throws InterruptedException {
VotingConfiguration singleNodeConfig = new VotingConfiguration(Sets.newHashSet(n1.getId()));
VotingConfiguration singleNodeConfig = VotingConfiguration.of(n1);
initializeCluster(singleNodeConfig);

AssertingAckListener ackListener = new AssertingAckListener(nodes.size());
Expand Down Expand Up @@ -295,7 +295,7 @@ public void testClusterStatePublishingWithFaultyNodeBeforeCommit() throws Interr
}

public void testClusterStatePublishingWithFaultyNodeAfterCommit() throws InterruptedException {
VotingConfiguration singleNodeConfig = new VotingConfiguration(Sets.newHashSet(n1.getId()));
VotingConfiguration singleNodeConfig = VotingConfiguration.of(n1);
initializeCluster(singleNodeConfig);

AssertingAckListener ackListener = new AssertingAckListener(nodes.size());
Expand Down Expand Up @@ -348,7 +348,7 @@ public void testClusterStatePublishingWithFaultyNodeAfterCommit() throws Interru
}

public void testClusterStatePublishingFailsOrTimesOutBeforeCommit() throws InterruptedException {
VotingConfiguration config = new VotingConfiguration(Sets.newHashSet(n1.getId(), n2.getId()));
VotingConfiguration config = VotingConfiguration.of(n1, n2);
initializeCluster(config);

AssertingAckListener ackListener = new AssertingAckListener(nodes.size());
Expand Down Expand Up @@ -387,7 +387,7 @@ public void testClusterStatePublishingFailsOrTimesOutBeforeCommit() throws Inter
}

public void testPublishingToMastersFirst() {
VotingConfiguration singleNodeConfig = new VotingConfiguration(Sets.newHashSet(n1.getId()));
VotingConfiguration singleNodeConfig = VotingConfiguration.of(n1);
initializeCluster(singleNodeConfig);

DiscoveryNodes.Builder discoNodesBuilder = DiscoveryNodes.builder();
Expand All @@ -403,8 +403,7 @@ public void testPublishingToMastersFirst() {
}

public void testClusterStatePublishingTimesOutAfterCommit() throws InterruptedException {
VotingConfiguration config = new VotingConfiguration(randomBoolean() ?
Sets.newHashSet(n1.getId(), n2.getId()) : Sets.newHashSet(n1.getId(), n2.getId(), n3.getId()));
VotingConfiguration config = randomBoolean() ? VotingConfiguration.of(n1, n2): VotingConfiguration.of(n1, n2, n3);
initializeCluster(config);

AssertingAckListener ackListener = new AssertingAckListener(nodes.size());
Expand Down

0 comments on commit d09853a

Please sign in to comment.