Skip to content

Commit

Permalink
Further changes to remove ImmutableOpenMap from server tests (#89939)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecoop authored Sep 9, 2022
1 parent 258833f commit dc1d2dd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
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;
Expand Down Expand Up @@ -134,15 +133,12 @@ private Map<String, DiscoveryNode> getMasterEligibleNodes() {

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()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
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;
Expand Down Expand Up @@ -964,16 +963,13 @@ private ClusterFormationFailureHelper.ClusterFormationState getClusterFormationS
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()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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;
Expand All @@ -41,7 +40,6 @@
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;
Expand All @@ -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);
Expand All @@ -66,7 +64,7 @@ public void testNoopShardStateUpdates() throws Exception {
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()),
Expand All @@ -87,7 +85,7 @@ public void testUpdateSnapshotToSuccess() throws Exception {
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));
Expand All @@ -113,7 +111,7 @@ public void testUpdateSnapshotMultipleShards() throws Exception {
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));
Expand All @@ -137,7 +135,7 @@ public void testUpdateCloneToSuccess() throws Exception {
final SnapshotsInProgress.Entry cloneSingleShard = cloneEntry(
targetSnapshot,
sourceSnapshot.getSnapshotId(),
clonesMap(shardId1, initShardStatus(dataNodeId))
Map.of(shardId1, initShardStatus(dataNodeId))
);

assertThat(cloneSingleShard.state(), is(SnapshotsInProgress.State.STARTED));
Expand All @@ -163,7 +161,7 @@ public void testUpdateCloneMultipleShards() throws Exception {
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));
Expand All @@ -188,7 +186,7 @@ public void testCompletedCloneStartsSnapshot() throws Exception {
final SnapshotsInProgress.Entry cloneSingleShard = cloneEntry(
targetSnapshot,
sourceSnapshot.getSnapshotId(),
clonesMap(shardId1, shardInitStatus)
Map.of(shardId1, shardInitStatus)
);

final ClusterState stateWithIndex = stateWithUnassignedIndices(indexName1);
Expand All @@ -197,7 +195,7 @@ public void testCompletedCloneStartsSnapshot() throws Exception {
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));
Expand Down Expand Up @@ -289,7 +287,7 @@ public void testCompletedSnapshotStartsClone() throws Exception {
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);
Expand All @@ -298,7 +296,7 @@ public void testCompletedSnapshotStartsClone() throws Exception {
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));
Expand Down Expand Up @@ -332,14 +330,14 @@ public void testCompletedSnapshotStartsNextSnapshot() throws Exception {
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);
Expand Down Expand Up @@ -370,14 +368,14 @@ public void testCompletedCloneStartsNextClone() throws Exception {
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));
Expand Down Expand Up @@ -409,7 +407,7 @@ public void testSnapshottingIndicesExcludesClones() {
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()))
)
);

Expand All @@ -434,20 +432,6 @@ private static DiscoveryNodes discoveryNodes(String localNodeId) {
.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,
Expand Down Expand Up @@ -518,7 +502,7 @@ private static ClusterState applyUpdates(ClusterState state, SnapshotsService.Sh
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,
Expand All @@ -538,10 +522,11 @@ private static SnapshotsInProgress.Entry snapshotEntry(
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);
Expand Down

0 comments on commit dc1d2dd

Please sign in to comment.