Skip to content

Commit

Permalink
Remove ImmutableOpenMap from most xpack tests (#87334)
Browse files Browse the repository at this point in the history
Many tests in xpack create ImmutableOpenMap for building mock cluster
states. However, since most of the API signatures for the cluster state
have changed to be Map, it is no longer necessary to build
ImmutableOpenMap. This commit converts most tests in xpack to build Maps
for their mock cluster states.

relates #86239
  • Loading branch information
rjernst committed Jun 2, 2022
1 parent d67ea4c commit 204696f
Show file tree
Hide file tree
Showing 23 changed files with 183 additions and 298 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.elasticsearch.cluster.routing.ShardRoutingState;
import org.elasticsearch.cluster.routing.TestShardRouting;
import org.elasticsearch.cluster.routing.allocation.DataTier;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.PathUtils;
import org.elasticsearch.index.Index;
Expand Down Expand Up @@ -85,14 +84,13 @@ public void testTierIndices() {
IndexMetadata coldIndex2 = indexMetadata("cold-2", 1, 0, DataTier.DATA_COLD, DataTier.DATA_WARM); // Prefers cold over warm
IndexMetadata nonTiered = indexMetadata("non-tier", 1, 0); // No tier

ImmutableOpenMap.Builder<String, IndexMetadata> indicesBuilder = ImmutableOpenMap.builder();
indicesBuilder.put("hot-1", hotIndex1);
indicesBuilder.put("hot-2", hotIndex2);
indicesBuilder.put("warm-1", warmIndex1);
indicesBuilder.put("cold-1", coldIndex1);
indicesBuilder.put("cold-2", coldIndex2);
indicesBuilder.put("non-tier", nonTiered);
ImmutableOpenMap<String, IndexMetadata> indices = indicesBuilder.build();
Map<String, IndexMetadata> indices = new HashMap<>();
indices.put("hot-1", hotIndex1);
indices.put("hot-2", hotIndex2);
indices.put("warm-1", warmIndex1);
indices.put("cold-1", coldIndex1);
indices.put("cold-2", coldIndex2);
indices.put("non-tier", nonTiered);

Map<String, String> tiers = DataTiersUsageTransportAction.tierIndices(indices);
assertThat(tiers.size(), equalTo(5));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.elasticsearch.cluster.routing.TestShardRouting;
import org.elasticsearch.cluster.routing.UnassignedInfo;
import org.elasticsearch.cluster.routing.UnassignedInfo.Reason;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.Index;
Expand Down Expand Up @@ -154,13 +153,12 @@ public void testClusterExcludeFiltersConditionMetOnlyOneCopyAllocated() {
.numberOfShards(1)
.numberOfReplicas(1)
.build();
ImmutableOpenMap.Builder<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder()
.fPut(index.getName(), indexMetadata);
Map<String, IndexMetadata> indices = Map.of(index.getName(), indexMetadata);

Settings clusterSettings = Settings.builder().put("cluster.routing.allocation.exclude._id", "node1").build();
Settings.Builder nodeSettingsBuilder = Settings.builder();
ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE)
.metadata(Metadata.builder().indices(indices.build()).transientSettings(clusterSettings))
.metadata(Metadata.builder().indices(indices).transientSettings(clusterSettings))
.nodes(
DiscoveryNodes.builder()
.add(
Expand Down Expand Up @@ -527,11 +525,10 @@ private void assertAllocateStatus(
.numberOfShards(shards)
.numberOfReplicas(replicas)
.build();
ImmutableOpenMap.Builder<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder()
.fPut(index.getName(), indexMetadata);
Map<String, IndexMetadata> indices = Map.of(index.getName(), indexMetadata);

ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE)
.metadata(Metadata.builder().indices(indices.build()))
.metadata(Metadata.builder().indices(indices))
.nodes(
DiscoveryNodes.builder()
.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.elasticsearch.cluster.routing.TestShardRouting;
import org.elasticsearch.cluster.routing.UnassignedInfo;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.Index;
Expand Down Expand Up @@ -453,8 +452,7 @@ public void testStepCompletableIfAllShardsActive() {
.numberOfShards(1)
.numberOfReplicas(1)
.build();
ImmutableOpenMap.Builder<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder()
.fPut(index.getName(), indexMetadata);
Map<String, IndexMetadata> indices = Map.of(index.getName(), indexMetadata);

final SingleNodeShutdownMetadata.Type type = randomFrom(
SingleNodeShutdownMetadata.Type.REMOVE,
Expand All @@ -464,7 +462,7 @@ public void testStepCompletableIfAllShardsActive() {
ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE)
.metadata(
Metadata.builder()
.indices(indices.build())
.indices(indices)
.putCustom(
NodesShutdownMetadata.TYPE,
new NodesShutdownMetadata(
Expand Down Expand Up @@ -531,8 +529,7 @@ public void testStepBecomesUncompletable() {
.numberOfShards(1)
.numberOfReplicas(1)
.build();
ImmutableOpenMap.Builder<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder()
.fPut(index.getName(), indexMetadata);
Map<String, IndexMetadata> indices = Map.of(index.getName(), indexMetadata);

final SingleNodeShutdownMetadata.Type type = randomFrom(
SingleNodeShutdownMetadata.Type.REMOVE,
Expand All @@ -542,7 +539,7 @@ public void testStepBecomesUncompletable() {
ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE)
.metadata(
Metadata.builder()
.indices(indices.build())
.indices(indices)
.putCustom(
NodesShutdownMetadata.TYPE,
new NodesShutdownMetadata(
Expand Down Expand Up @@ -604,11 +601,10 @@ private void assertAllocateStatus(
.numberOfShards(shards)
.numberOfReplicas(replicas)
.build();
ImmutableOpenMap.Builder<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder()
.fPut(index.getName(), indexMetadata);
Map<String, IndexMetadata> indices = Map.of(index.getName(), indexMetadata);

ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE)
.metadata(Metadata.builder().indices(indices.build()))
.metadata(Metadata.builder().indices(indices))
.nodes(
DiscoveryNodes.builder()
.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.elasticsearch.cluster.routing.RoutingTable;
import org.elasticsearch.cluster.routing.ShardRoutingState;
import org.elasticsearch.cluster.routing.TestShardRouting;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.Settings.Builder;
import org.elasticsearch.common.transport.TransportAddress;
Expand Down Expand Up @@ -207,12 +206,11 @@ public void testPerformActionWithClusterExcludeFilters() throws IOException {
);

Settings clusterSettings = Settings.builder().put("cluster.routing.allocation.exclude._id", "node_id_0").build();
ImmutableOpenMap.Builder<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder()
.fPut(index.getName(), indexMetadata);
Map<String, IndexMetadata> indices = Map.of(index.getName(), indexMetadata);
IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index)
.addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node_id_0", true, ShardRoutingState.STARTED));
ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE)
.metadata(Metadata.builder().indices(indices.build()).transientSettings(clusterSettings))
.metadata(Metadata.builder().indices(indices).transientSettings(clusterSettings))
.nodes(nodes)
.routingTable(RoutingTable.builder().add(indexRoutingTable).build())
.build();
Expand Down Expand Up @@ -299,12 +297,11 @@ public void testPerformActionAttrsRequestFails() {
validNodeIds.add(nodeId);
}

ImmutableOpenMap.Builder<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder()
.fPut(index.getName(), indexMetadata);
Map<String, IndexMetadata> indices = Map.of(index.getName(), indexMetadata);
IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index)
.addShard(TestShardRouting.newShardRouting(new ShardId(index, 0), "node_id_0", true, ShardRoutingState.STARTED));
ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE)
.metadata(Metadata.builder().indices(indices.build()))
.metadata(Metadata.builder().indices(indices))
.nodes(nodes)
.routingTable(RoutingTable.builder().add(indexRoutingTable).build())
.build();
Expand Down Expand Up @@ -372,11 +369,10 @@ public void testPerformActionAttrsNoShard() {
nodes.add(DiscoveryNode.createLocal(nodeSettings, new TransportAddress(TransportAddress.META_ADDRESS, nodePort), nodeId));
}

ImmutableOpenMap.Builder<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder()
.fPut(index.getName(), indexMetadata);
Map<String, IndexMetadata> indices = Map.of(index.getName(), indexMetadata);
IndexRoutingTable.Builder indexRoutingTable = IndexRoutingTable.builder(index);
ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE)
.metadata(Metadata.builder().indices(indices.build()))
.metadata(Metadata.builder().indices(indices))
.nodes(nodes)
.routingTable(RoutingTable.builder().add(indexRoutingTable).build())
.build();
Expand Down Expand Up @@ -611,10 +607,9 @@ private void assertNodeSelected(
DiscoveryNodes nodes,
IndexRoutingTable indexRoutingTable
) throws Exception {
ImmutableOpenMap.Builder<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder()
.fPut(index.getName(), indexMetadata);
Map<String, IndexMetadata> indices = Map.of(index.getName(), indexMetadata);
ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE)
.metadata(Metadata.builder().indices(indices.build()))
.metadata(Metadata.builder().indices(indices))
.nodes(nodes)
.routingTable(RoutingTable.builder().add(indexRoutingTable).build())
.build();
Expand Down Expand Up @@ -652,10 +647,9 @@ private void assertNoValidNode(IndexMetadata indexMetadata, Index index, Discove

private void assertNoValidNode(IndexMetadata indexMetadata, Index index, DiscoveryNodes nodes, IndexRoutingTable indexRoutingTable) {

ImmutableOpenMap.Builder<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder()
.fPut(index.getName(), indexMetadata);
Map<String, IndexMetadata> indices = Map.of(index.getName(), indexMetadata);
ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE)
.metadata(Metadata.builder().indices(indices.build()))
.metadata(Metadata.builder().indices(indices))
.nodes(nodes)
.routingTable(RoutingTable.builder().add(indexRoutingTable).build())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.xpack.core.ilm.Step.StepKey;
import org.mockito.Mockito;

import java.util.Collections;
import java.util.Map;

import static org.elasticsearch.cluster.metadata.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY;
import static org.elasticsearch.xpack.core.ilm.ShrinkIndexNameSupplier.SHRUNKEN_INDEX_PREFIX;
Expand Down Expand Up @@ -153,11 +153,8 @@ public void testPerformActionShrunkenIndexExists() throws Exception {
.numberOfShards(1)
.numberOfReplicas(0)
.build();
ImmutableOpenMap.Builder<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder()
.fPut(generatedShrunkenIndexName, indexMetadata);
ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE)
.metadata(Metadata.builder().indices(indices.build()))
.build();
Map<String, IndexMetadata> indices = Map.of(generatedShrunkenIndexName, indexMetadata);
ClusterState clusterState = ClusterState.builder(ClusterState.EMPTY_STATE).metadata(Metadata.builder().indices(indices)).build();

step.performAction(sourceIndexMetadata, clusterState, null, new ActionListener<>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.elasticsearch.cluster.metadata.LifecycleExecutionState;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.xpack.core.slm.SnapshotInvocationRecord;
import org.elasticsearch.xpack.core.slm.SnapshotLifecycleMetadata;
import org.elasticsearch.xpack.core.slm.SnapshotLifecyclePolicy;
Expand Down Expand Up @@ -57,9 +56,8 @@ public void testNoSlmPolicies() {
.numberOfShards(randomIntBetween(1, 5))
.numberOfReplicas(randomIntBetween(0, 5))
.build();
ImmutableOpenMap.Builder<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder()
.fPut(indexMetadata.getIndex().getName(), indexMetadata);
Metadata.Builder meta = Metadata.builder().indices(indices.build());
Map<String, IndexMetadata> indices = Map.of(indexMetadata.getIndex().getName(), indexMetadata);
Metadata.Builder meta = Metadata.builder().indices(indices);
ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(meta).build();
WaitForSnapshotStep instance = createRandomInstance();
IllegalStateException e = expectThrows(
Expand Down Expand Up @@ -87,9 +85,8 @@ public void testSlmPolicyNotExecuted() throws IOException {
.numberOfShards(randomIntBetween(1, 5))
.numberOfReplicas(randomIntBetween(0, 5))
.build();
ImmutableOpenMap.Builder<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder()
.fPut(indexMetadata.getIndex().getName(), indexMetadata);
Metadata.Builder meta = Metadata.builder().indices(indices.build()).putCustom(SnapshotLifecycleMetadata.TYPE, smlMetadata);
Map<String, IndexMetadata> indices = Map.of(indexMetadata.getIndex().getName(), indexMetadata);
Metadata.Builder meta = Metadata.builder().indices(indices).putCustom(SnapshotLifecycleMetadata.TYPE, smlMetadata);
ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(meta).build();
ClusterStateWaitStep.Result result = instance.isConditionMet(indexMetadata.getIndex(), clusterState);
assertFalse(result.isComplete());
Expand Down Expand Up @@ -139,9 +136,8 @@ private void assertSlmPolicyExecuted(boolean startTimeAfterPhaseTime, boolean fi
.numberOfShards(randomIntBetween(1, 5))
.numberOfReplicas(randomIntBetween(0, 5))
.build();
ImmutableOpenMap.Builder<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder()
.fPut(indexMetadata.getIndex().getName(), indexMetadata);
Metadata.Builder meta = Metadata.builder().indices(indices.build()).putCustom(SnapshotLifecycleMetadata.TYPE, smlMetadata);
Map<String, IndexMetadata> indices = Map.of(indexMetadata.getIndex().getName(), indexMetadata);
Metadata.Builder meta = Metadata.builder().indices(indices).putCustom(SnapshotLifecycleMetadata.TYPE, smlMetadata);
ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(meta).build();
ClusterStateWaitStep.Result result = instance.isConditionMet(indexMetadata.getIndex(), clusterState);
if (startTimeAfterPhaseTime) {
Expand Down Expand Up @@ -174,9 +170,8 @@ public void testNullStartTime() throws IOException {
.numberOfShards(randomIntBetween(1, 5))
.numberOfReplicas(randomIntBetween(0, 5))
.build();
ImmutableOpenMap.Builder<String, IndexMetadata> indices = ImmutableOpenMap.<String, IndexMetadata>builder()
.fPut(indexMetadata.getIndex().getName(), indexMetadata);
Metadata.Builder meta = Metadata.builder().indices(indices.build()).putCustom(SnapshotLifecycleMetadata.TYPE, smlMetadata);
Map<String, IndexMetadata> indices = Map.of(indexMetadata.getIndex().getName(), indexMetadata);
Metadata.Builder meta = Metadata.builder().indices(indices).putCustom(SnapshotLifecycleMetadata.TYPE, smlMetadata);
ClusterState clusterState = ClusterState.builder(ClusterName.DEFAULT).metadata(meta).build();
IllegalStateException e = expectThrows(
IllegalStateException.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.node.DiscoveryNodes;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.util.concurrent.ThreadContext;
Expand Down Expand Up @@ -411,13 +410,7 @@ private static ClusterState createClusterState(
.add(new DiscoveryNode("bar", new TransportAddress(inetAddress2, 9202), minNodeVersion))
.build()
)
.metadata(
Metadata.builder()
.indices(ImmutableOpenMap.<String, IndexMetadata>builder().putAllFromMap(indices).build())
.templates(ImmutableOpenMap.<String, IndexTemplateMetadata>builder().putAllFromMap(legacyTemplates).build())
.indexTemplates(composableTemplates)
.build()
)
.metadata(Metadata.builder().indices(indices).templates(legacyTemplates).indexTemplates(composableTemplates).build())
.build();
}

Expand Down

0 comments on commit 204696f

Please sign in to comment.