Skip to content

Commit

Permalink
Refactor more test uses of DiscoveryNode constructor (#100265)
Browse files Browse the repository at this point in the history
Convert them to use DiscoveryNodeUtils builder class
  • Loading branch information
thecoop authored Oct 4, 2023
1 parent 99313b0 commit 22a0ae1
Show file tree
Hide file tree
Showing 16 changed files with 327 additions and 362 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.elasticsearch.cluster.metadata.MetadataIndexStateService;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
import org.elasticsearch.cluster.node.DiscoveryNodeUtils;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.cluster.routing.RoutingTable;
import org.elasticsearch.cluster.routing.allocation.AllocationService;
Expand Down Expand Up @@ -80,7 +81,6 @@
import java.time.Instant;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -1294,17 +1294,12 @@ private static DiscoveryNodes.Builder buildNodes(String nodeId) {
}

private static DiscoveryNode getNode(String nodeId) {
return new DiscoveryNode(
nodeId,
nodeId,
nodeId,
"host",
"host_address",
buildNewFakeTransportAddress(),
Collections.emptyMap(),
Set.of(DiscoveryNodeRole.MASTER_ROLE, DiscoveryNodeRole.DATA_HOT_NODE_ROLE, DiscoveryNodeRole.DATA_CONTENT_NODE_ROLE),
null
);
return DiscoveryNodeUtils.builder(nodeId)
.name(nodeId)
.ephemeralId(nodeId)
.address("host", "host_address", buildNewFakeTransportAddress())
.roles(Set.of(DiscoveryNodeRole.MASTER_ROLE, DiscoveryNodeRole.DATA_HOT_NODE_ROLE, DiscoveryNodeRole.DATA_CONTENT_NODE_ROLE))
.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodeRole;
import org.elasticsearch.cluster.node.DiscoveryNodeUtils;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.cluster.routing.RoutingTable;
import org.elasticsearch.common.UUIDs;
Expand Down Expand Up @@ -457,17 +458,12 @@ private static DiscoveryNodes createDiscoveryNodes(final int numNodes, final boo

// Create a new DiscoveryNode
private static DiscoveryNode newNode(final String nodeId, Set<DiscoveryNodeRole> roles) {
return new DiscoveryNode(
nodeId,
nodeId,
nodeId,
"host",
"host_address",
buildNewFakeTransportAddress(),
Collections.emptyMap(),
roles,
null
);
return DiscoveryNodeUtils.builder(nodeId)
.name(nodeId)
.ephemeralId(nodeId)
.address("host", "host_address", buildNewFakeTransportAddress())
.roles(roles)
.build();
}

// Create the metadata for a cluster state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,14 @@ public void testJoinReasonService() {
matchesNeedingGuidance("joining, removed [0ms] ago with reason [second test removal], [2] total removals")
);

final DiscoveryNode rebootedNode = new DiscoveryNode(
discoveryNode.getName(),
discoveryNode.getId(),
UUIDs.randomBase64UUID(random()),
discoveryNode.getHostName(),
discoveryNode.getHostAddress(),
discoveryNode.getAddress(),
discoveryNode.getAttributes(),
discoveryNode.getRoles(),
discoveryNode.getVersionInformation()
);
final DiscoveryNode rebootedNode = DiscoveryNodeUtils.builder(discoveryNode.getId())
.name(discoveryNode.getName())
.ephemeralId(UUIDs.randomBase64UUID(random()))
.address(discoveryNode.getHostName(), discoveryNode.getHostAddress(), discoveryNode.getAddress())
.attributes(discoveryNode.getAttributes())
.roles(discoveryNode.getRoles())
.version(discoveryNode.getVersionInformation())
.build();

assertThat(
joinReasonService.getJoinReason(rebootedNode, LEADER),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,14 @@ public void testUpdatesNodeWithNewRoles() throws Exception {
final DiscoveryNode masterNode = DiscoveryNodeUtils.create(UUIDs.base64UUID());

final DiscoveryNode actualNode = DiscoveryNodeUtils.create(UUIDs.base64UUID());
final DiscoveryNode bwcNode = new DiscoveryNode(
actualNode.getName(),
actualNode.getId(),
actualNode.getEphemeralId(),
actualNode.getHostName(),
actualNode.getHostAddress(),
actualNode.getAddress(),
actualNode.getAttributes(),
new HashSet<>(randomSubsetOf(actualNode.getRoles())),
actualNode.getVersionInformation()
);
final DiscoveryNode bwcNode = DiscoveryNodeUtils.builder(actualNode.getId())
.name(actualNode.getName())
.ephemeralId(actualNode.getEphemeralId())
.address(actualNode.getHostName(), actualNode.getHostAddress(), actualNode.getAddress())
.attributes(actualNode.getAttributes())
.roles(new HashSet<>(randomSubsetOf(actualNode.getRoles())))
.version(actualNode.getVersionInformation())
.build();
final ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT)
.nodes(DiscoveryNodes.builder().add(masterNode).localNodeId(masterNode.getId()).masterNodeId(masterNode.getId()).add(bwcNode))
.build();
Expand Down Expand Up @@ -395,17 +392,14 @@ public void testRemovesOlderNodeInstancesWhenBecomingMaster() throws Exception {

final var masterNode = DiscoveryNodeUtils.create(UUIDs.randomBase64UUID(random()));
final var otherNodeOld = DiscoveryNodeUtils.create(UUIDs.randomBase64UUID(random()));
final var otherNodeNew = new DiscoveryNode(
otherNodeOld.getName(),
otherNodeOld.getId(),
UUIDs.randomBase64UUID(random()),
otherNodeOld.getHostName(),
otherNodeOld.getHostAddress(),
otherNodeOld.getAddress(),
otherNodeOld.getAttributes(),
otherNodeOld.getRoles(),
otherNodeOld.getVersionInformation()
);
final var otherNodeNew = DiscoveryNodeUtils.builder(otherNodeOld.getId())
.name(otherNodeOld.getName())
.ephemeralId(UUIDs.randomBase64UUID(random()))
.address(otherNodeOld.getHostName(), otherNodeOld.getHostAddress(), otherNodeOld.getAddress())
.attributes(otherNodeOld.getAttributes())
.roles(otherNodeOld.getRoles())
.version(otherNodeOld.getVersionInformation())
.build();

final var afterElectionClusterState = ClusterStateTaskExecutorUtils.executeAndAssertSuccessful(
ClusterState.builder(ClusterName.DEFAULT)
Expand Down
Loading

0 comments on commit 22a0ae1

Please sign in to comment.