浏览代码

Further changes to remove ImmutableOpenMap from server tests (#89939)

Simon Cooper 3 年之前
父节点
当前提交
dc1d2dd1a7

+ 2 - 6
server/src/test/java/org/elasticsearch/action/admin/cluster/coordination/ClusterFormationInfoActionTests.java

@@ -16,7 +16,6 @@ import org.elasticsearch.cluster.coordination.CoordinationMetadata;
 import org.elasticsearch.cluster.coordination.Coordinator;
 import org.elasticsearch.cluster.node.DiscoveryNode;
 import org.elasticsearch.cluster.service.ClusterService;
-import org.elasticsearch.common.collect.ImmutableOpenMap;
 import org.elasticsearch.common.settings.Settings;
 import org.elasticsearch.monitor.StatusInfo;
 import org.elasticsearch.test.ESTestCase;
@@ -134,15 +133,12 @@ public class ClusterFormationInfoActionTests extends ESTestCase {
 
     private ClusterFormationFailureHelper.ClusterFormationState getClusterFormationState() {
         Map<String, DiscoveryNode> masterEligibleNodesMap = getMasterEligibleNodes();
-        List<String> initialMasterNodesSetting = Arrays.stream(generateRandomStringArray(7, 30, false, false)).toList();
+        List<String> initialMasterNodesSetting = Arrays.asList(generateRandomStringArray(7, 30, false, false));
         DiscoveryNode localNode = masterEligibleNodesMap.values().stream().findAny().get();
-        ImmutableOpenMap.Builder<String, DiscoveryNode> masterEligibleNodesBuilder = new ImmutableOpenMap.Builder<>();
-        masterEligibleNodesMap.forEach(masterEligibleNodesBuilder::put);
-        ImmutableOpenMap<String, DiscoveryNode> masterEligibleNodes = masterEligibleNodesBuilder.build();
         return new ClusterFormationFailureHelper.ClusterFormationState(
             initialMasterNodesSetting,
             localNode,
-            masterEligibleNodes,
+            Map.copyOf(masterEligibleNodesMap),
             randomLong(),
             randomLong(),
             new CoordinationMetadata.VotingConfiguration(Collections.emptySet()),

+ 2 - 6
server/src/test/java/org/elasticsearch/cluster/coordination/CoordinationDiagnosticsServiceTests.java

@@ -18,7 +18,6 @@ import org.elasticsearch.cluster.node.DiscoveryNodeRole;
 import org.elasticsearch.cluster.node.DiscoveryNodes;
 import org.elasticsearch.cluster.routing.RoutingTable;
 import org.elasticsearch.cluster.service.ClusterService;
-import org.elasticsearch.common.collect.ImmutableOpenMap;
 import org.elasticsearch.common.settings.Settings;
 import org.elasticsearch.common.util.concurrent.DeterministicTaskQueue;
 import org.elasticsearch.core.TimeValue;
@@ -964,16 +963,13 @@ public class CoordinationDiagnosticsServiceTests extends AbstractCoordinatorTest
         boolean hasDiscoveredQuorum
     ) {
         Map<String, DiscoveryNode> masterEligibleNodesMap = Map.of("node1", node1, "node2", node2, "node3", node3);
-        List<String> initialMasterNodesSetting = Arrays.stream(generateRandomStringArray(7, 30, false, false)).toList();
+        List<String> initialMasterNodesSetting = Arrays.asList(generateRandomStringArray(7, 30, false, false));
         DiscoveryNode localNode = masterEligibleNodesMap.values().stream().findAny().get();
-        ImmutableOpenMap.Builder<String, DiscoveryNode> masterEligibleNodesBuilder = new ImmutableOpenMap.Builder<>();
-        masterEligibleNodesMap.forEach(masterEligibleNodesBuilder::put);
-        ImmutableOpenMap<String, DiscoveryNode> masterEligibleNodes = masterEligibleNodesBuilder.build();
         List<DiscoveryNode> allMasterEligibleNodes = List.of(node1, node2, node3);
         return new ClusterFormationFailureHelper.ClusterFormationState(
             initialMasterNodesSetting,
             localNode,
-            masterEligibleNodes,
+            Map.copyOf(masterEligibleNodesMap),
             randomLong(),
             randomLong(),
             new CoordinationMetadata.VotingConfiguration(Collections.emptySet()),

+ 20 - 35
server/src/test/java/org/elasticsearch/snapshots/SnapshotsServiceTests.java

@@ -24,7 +24,6 @@ import org.elasticsearch.cluster.routing.ShardRoutingState;
 import org.elasticsearch.cluster.routing.TestShardRouting;
 import org.elasticsearch.cluster.service.ClusterStateTaskExecutorUtils;
 import org.elasticsearch.common.UUIDs;
-import org.elasticsearch.common.collect.ImmutableOpenMap;
 import org.elasticsearch.common.settings.Settings;
 import org.elasticsearch.common.unit.ByteSizeValue;
 import org.elasticsearch.index.Index;
@@ -41,7 +40,6 @@ import java.util.HashSet;
 import java.util.Map;
 import java.util.function.Function;
 import java.util.stream.Collectors;
-import java.util.stream.StreamSupport;
 
 import static java.util.Collections.singleton;
 import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_VERSION_CREATED;
@@ -53,7 +51,7 @@ public class SnapshotsServiceTests extends ESTestCase {
     public void testNoopShardStateUpdates() throws Exception {
         final String repoName = "test-repo";
         final Snapshot snapshot = snapshot(repoName, "snapshot-1");
-        final SnapshotsInProgress.Entry snapshotNoShards = snapshotEntry(snapshot, Collections.emptyMap(), ImmutableOpenMap.of());
+        final SnapshotsInProgress.Entry snapshotNoShards = snapshotEntry(snapshot, Collections.emptyMap(), Collections.emptyMap());
 
         final String indexName1 = "index-1";
         final ShardId shardId1 = new ShardId(index(indexName1), 0);
@@ -66,7 +64,7 @@ public class SnapshotsServiceTests extends ESTestCase {
             final IndexId indexId = indexId(indexName1);
             final ClusterState state = stateWithSnapshots(
                 repoName,
-                snapshotEntry(snapshot, Collections.singletonMap(indexId.getName(), indexId), shardsMap(shardId1, initShardStatus(uuid())))
+                snapshotEntry(snapshot, Map.of(indexId.getName(), indexId), Map.of(shardId1, initShardStatus(uuid())))
             );
             final SnapshotsService.ShardSnapshotUpdate shardCompletion = successUpdate(
                 snapshot("other-repo", snapshot.getSnapshotId().getName()),
@@ -87,7 +85,7 @@ public class SnapshotsServiceTests extends ESTestCase {
         final SnapshotsInProgress.Entry snapshotSingleShard = snapshotEntry(
             sn1,
             Collections.singletonMap(indexId1.getName(), indexId1),
-            shardsMap(shardId1, initShardStatus(dataNodeId))
+            Map.of(shardId1, initShardStatus(dataNodeId))
         );
 
         assertThat(snapshotSingleShard.state(), is(SnapshotsInProgress.State.STARTED));
@@ -113,7 +111,7 @@ public class SnapshotsServiceTests extends ESTestCase {
         final SnapshotsInProgress.Entry snapshotSingleShard = snapshotEntry(
             sn1,
             Collections.singletonMap(indexId1.getName(), indexId1),
-            ImmutableOpenMap.builder(shardsMap(shardId1, shardInitStatus)).fPut(shardId2, shardInitStatus).build()
+            Map.of(shardId1, shardInitStatus, shardId2, shardInitStatus)
         );
 
         assertThat(snapshotSingleShard.state(), is(SnapshotsInProgress.State.STARTED));
@@ -137,7 +135,7 @@ public class SnapshotsServiceTests extends ESTestCase {
         final SnapshotsInProgress.Entry cloneSingleShard = cloneEntry(
             targetSnapshot,
             sourceSnapshot.getSnapshotId(),
-            clonesMap(shardId1, initShardStatus(dataNodeId))
+            Map.of(shardId1, initShardStatus(dataNodeId))
         );
 
         assertThat(cloneSingleShard.state(), is(SnapshotsInProgress.State.STARTED));
@@ -163,7 +161,7 @@ public class SnapshotsServiceTests extends ESTestCase {
         final SnapshotsInProgress.Entry cloneMultipleShards = cloneEntry(
             targetSnapshot,
             sourceSnapshot.getSnapshotId(),
-            ImmutableOpenMap.builder(clonesMap(shardId1, shardInitStatus)).fPut(shardId2, shardInitStatus).build()
+            Map.of(shardId1, shardInitStatus, shardId2, shardInitStatus)
         );
 
         assertThat(cloneMultipleShards.state(), is(SnapshotsInProgress.State.STARTED));
@@ -188,7 +186,7 @@ public class SnapshotsServiceTests extends ESTestCase {
         final SnapshotsInProgress.Entry cloneSingleShard = cloneEntry(
             targetSnapshot,
             sourceSnapshot.getSnapshotId(),
-            clonesMap(shardId1, shardInitStatus)
+            Map.of(shardId1, shardInitStatus)
         );
 
         final ClusterState stateWithIndex = stateWithUnassignedIndices(indexName1);
@@ -197,7 +195,7 @@ public class SnapshotsServiceTests extends ESTestCase {
         final SnapshotsInProgress.Entry snapshotSingleShard = snapshotEntry(
             plainSnapshot,
             Collections.singletonMap(indexId1.getName(), indexId1),
-            shardsMap(routingShardId1, SnapshotsInProgress.ShardSnapshotStatus.UNASSIGNED_QUEUED)
+            Map.of(routingShardId1, SnapshotsInProgress.ShardSnapshotStatus.UNASSIGNED_QUEUED)
         );
 
         assertThat(cloneSingleShard.state(), is(SnapshotsInProgress.State.STARTED));
@@ -289,7 +287,7 @@ public class SnapshotsServiceTests extends ESTestCase {
         final SnapshotsInProgress.Entry cloneSingleShard = cloneEntry(
             targetSnapshot,
             sourceSnapshot.getSnapshotId(),
-            clonesMap(repositoryShardId, SnapshotsInProgress.ShardSnapshotStatus.UNASSIGNED_QUEUED)
+            Map.of(repositoryShardId, SnapshotsInProgress.ShardSnapshotStatus.UNASSIGNED_QUEUED)
         );
 
         final ClusterState stateWithIndex = stateWithUnassignedIndices(indexName);
@@ -298,7 +296,7 @@ public class SnapshotsServiceTests extends ESTestCase {
         final SnapshotsInProgress.Entry snapshotSingleShard = snapshotEntry(
             plainSnapshot,
             Collections.singletonMap(indexId1.getName(), indexId1),
-            shardsMap(routingShardId, initShardStatus(dataNodeId))
+            Map.of(routingShardId, initShardStatus(dataNodeId))
         );
 
         assertThat(cloneSingleShard.state(), is(SnapshotsInProgress.State.STARTED));
@@ -332,14 +330,14 @@ public class SnapshotsServiceTests extends ESTestCase {
         final SnapshotsInProgress.Entry snapshotSingleShard = snapshotEntry(
             plainSnapshot,
             Collections.singletonMap(indexId1.getName(), indexId1),
-            shardsMap(routingShardId, initShardStatus(dataNodeId))
+            Map.of(routingShardId, initShardStatus(dataNodeId))
         );
 
         final Snapshot queuedSnapshot = snapshot(repoName, "test-snapshot-2");
         final SnapshotsInProgress.Entry queuedSnapshotSingleShard = snapshotEntry(
             queuedSnapshot,
             Collections.singletonMap(indexId1.getName(), indexId1),
-            shardsMap(routingShardId, SnapshotsInProgress.ShardSnapshotStatus.UNASSIGNED_QUEUED)
+            Map.of(routingShardId, SnapshotsInProgress.ShardSnapshotStatus.UNASSIGNED_QUEUED)
         );
 
         final SnapshotsService.ShardSnapshotUpdate completeShard = successUpdate(plainSnapshot, routingShardId, dataNodeId);
@@ -370,14 +368,14 @@ public class SnapshotsServiceTests extends ESTestCase {
         final SnapshotsInProgress.Entry cloneSingleShard = cloneEntry(
             targetSnapshot,
             sourceSnapshot.getSnapshotId(),
-            clonesMap(shardId1, initShardStatus(masterNodeId))
+            Map.of(shardId1, initShardStatus(masterNodeId))
         );
 
         final Snapshot queuedTargetSnapshot = snapshot(repoName, "test-snapshot");
         final SnapshotsInProgress.Entry queuedClone = cloneEntry(
             queuedTargetSnapshot,
             sourceSnapshot.getSnapshotId(),
-            clonesMap(shardId1, SnapshotsInProgress.ShardSnapshotStatus.UNASSIGNED_QUEUED)
+            Map.of(shardId1, SnapshotsInProgress.ShardSnapshotStatus.UNASSIGNED_QUEUED)
         );
 
         assertThat(cloneSingleShard.state(), is(SnapshotsInProgress.State.STARTED));
@@ -409,7 +407,7 @@ public class SnapshotsServiceTests extends ESTestCase {
             cloneEntry(
                 snapshot(repoName, "target-snapshot"),
                 snapshot(repoName, "source-snapshot").getSnapshotId(),
-                clonesMap(new RepositoryShardId(indexId(indexName), 0), initShardStatus(uuid()))
+                Map.of(new RepositoryShardId(indexId(indexName), 0), initShardStatus(uuid()))
             )
         );
 
@@ -434,20 +432,6 @@ public class SnapshotsServiceTests extends ESTestCase {
             .build();
     }
 
-    private static ImmutableOpenMap<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shardsMap(
-        ShardId shardId,
-        SnapshotsInProgress.ShardSnapshotStatus shardStatus
-    ) {
-        return ImmutableOpenMap.<ShardId, SnapshotsInProgress.ShardSnapshotStatus>builder().fPut(shardId, shardStatus).build();
-    }
-
-    private static ImmutableOpenMap<RepositoryShardId, SnapshotsInProgress.ShardSnapshotStatus> clonesMap(
-        RepositoryShardId shardId,
-        SnapshotsInProgress.ShardSnapshotStatus shardStatus
-    ) {
-        return ImmutableOpenMap.<RepositoryShardId, SnapshotsInProgress.ShardSnapshotStatus>builder().fPut(shardId, shardStatus).build();
-    }
-
     private static SnapshotsService.ShardSnapshotUpdate successUpdate(Snapshot snapshot, ShardId shardId, String nodeId) {
         return new SnapshotsService.ShardSnapshotUpdate(
             snapshot,
@@ -518,7 +502,7 @@ public class SnapshotsServiceTests extends ESTestCase {
     private static SnapshotsInProgress.Entry snapshotEntry(
         Snapshot snapshot,
         Map<String, IndexId> indexIds,
-        ImmutableOpenMap<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shards
+        Map<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shards
     ) {
         return SnapshotsInProgress.startedEntry(
             snapshot,
@@ -538,10 +522,11 @@ public class SnapshotsServiceTests extends ESTestCase {
     private static SnapshotsInProgress.Entry cloneEntry(
         Snapshot snapshot,
         SnapshotId source,
-        ImmutableOpenMap<RepositoryShardId, SnapshotsInProgress.ShardSnapshotStatus> clones
+        Map<RepositoryShardId, SnapshotsInProgress.ShardSnapshotStatus> clones
     ) {
-        final Map<String, IndexId> indexIds = StreamSupport.stream(clones.keySet().spliterator(), false)
-            .map(k -> k.index())
+        final Map<String, IndexId> indexIds = clones.keySet()
+            .stream()
+            .map(RepositoryShardId::index)
             .distinct()
             .collect(Collectors.toMap(IndexId::getName, Function.identity()));
         return SnapshotsInProgress.startClone(snapshot, source, indexIds, 1L, randomNonNegativeLong(), Version.CURRENT).withClones(clones);