|
18 | 18 | import org.opensearch.cluster.OpenSearchAllocationTestCase;
|
19 | 19 | import org.opensearch.cluster.metadata.IndexMetadata;
|
20 | 20 | import org.opensearch.cluster.metadata.Metadata;
|
| 21 | +import org.opensearch.cluster.node.DiscoveryNode; |
| 22 | +import org.opensearch.cluster.routing.IndexRoutingTable; |
21 | 23 | import org.opensearch.cluster.routing.IndexShardRoutingTable;
|
| 24 | +import org.opensearch.cluster.routing.RecoverySource; |
22 | 25 | import org.opensearch.cluster.routing.RoutingNodes;
|
23 | 26 | import org.opensearch.cluster.routing.RoutingTable;
|
24 | 27 | import org.opensearch.cluster.routing.ShardRouting;
|
| 28 | +import org.opensearch.cluster.routing.ShardRoutingState; |
| 29 | +import org.opensearch.cluster.routing.TestShardRouting; |
| 30 | +import org.opensearch.cluster.routing.UnassignedInfo; |
25 | 31 | import org.opensearch.cluster.routing.allocation.RoutingAllocation;
|
26 | 32 | import org.opensearch.cluster.routing.allocation.decider.AllocationDeciders;
|
27 | 33 | import org.opensearch.common.collect.Tuple;
|
28 | 34 | import org.opensearch.common.settings.Settings;
|
| 35 | +import org.opensearch.common.util.set.Sets; |
29 | 36 | import org.opensearch.core.index.shard.ShardId;
|
30 | 37 | import org.opensearch.snapshots.SnapshotShardSizeInfo;
|
31 | 38 | import org.opensearch.test.gateway.TestShardBatchGatewayAllocator;
|
@@ -222,6 +229,21 @@ public void testSafelyRemoveShardFromBothBatch() {
|
222 | 229 | assertEquals(0, testShardsBatchGatewayAllocator.getBatchIdToStoreShardBatch().size());
|
223 | 230 | }
|
224 | 231 |
|
| 232 | + public void testDeDuplicationOfReplicaShardsAcrossBatch() { |
| 233 | + final ShardId shardId = new ShardId("test", "_na_", 0); |
| 234 | + final DiscoveryNode node = newNode("node1"); |
| 235 | + // number of replicas is greater than batch size - to ensure shardRouting gets de-duped across batch |
| 236 | + createRoutingWithDifferentUnAssignedInfo(shardId, node, 50); |
| 237 | + testShardsBatchGatewayAllocator = new TestShardBatchGatewayAllocator(10); |
| 238 | + |
| 239 | + // only replica shard should be in the batch |
| 240 | + Set<String> replicaBatches = testShardsBatchGatewayAllocator.createAndUpdateBatches(testAllocation, false); |
| 241 | + assertEquals(1, replicaBatches.size()); |
| 242 | + ShardsBatchGatewayAllocator.ShardsBatch shardsBatch = testShardsBatchGatewayAllocator.getBatchIdToStoreShardBatch() |
| 243 | + .get(replicaBatches.iterator().next()); |
| 244 | + assertEquals(1, shardsBatch.getBatchedShards().size()); |
| 245 | + } |
| 246 | + |
225 | 247 | public void testGetBatchIdExisting() {
|
226 | 248 | createIndexAndUpdateClusterState(2, 1020, 1);
|
227 | 249 | // get all shardsRoutings for test index
|
@@ -345,6 +367,59 @@ private void createIndexAndUpdateClusterState(int count, int numberOfShards, int
|
345 | 367 | );
|
346 | 368 | }
|
347 | 369 |
|
| 370 | + private void createRoutingWithDifferentUnAssignedInfo(ShardId primaryShardId, DiscoveryNode node, int numberOfReplicas) { |
| 371 | + |
| 372 | + ShardRouting primaryShard = TestShardRouting.newShardRouting(primaryShardId, node.getId(), true, ShardRoutingState.STARTED); |
| 373 | + Metadata metadata = Metadata.builder() |
| 374 | + .put( |
| 375 | + IndexMetadata.builder(primaryShardId.getIndexName()) |
| 376 | + .settings(settings(Version.CURRENT)) |
| 377 | + .numberOfShards(1) |
| 378 | + .numberOfReplicas(numberOfReplicas) |
| 379 | + .putInSyncAllocationIds(0, Sets.newHashSet(primaryShard.allocationId().getId())) |
| 380 | + ) |
| 381 | + .build(); |
| 382 | + |
| 383 | + IndexRoutingTable.Builder isd = IndexRoutingTable.builder(primaryShardId.getIndex()) |
| 384 | + .addIndexShard(new IndexShardRoutingTable.Builder(primaryShardId).addShard(primaryShard).build()); |
| 385 | + |
| 386 | + for (int i = 0; i < numberOfReplicas; i++) { |
| 387 | + isd.addShard( |
| 388 | + ShardRouting.newUnassigned( |
| 389 | + primaryShardId, |
| 390 | + false, |
| 391 | + RecoverySource.PeerRecoverySource.INSTANCE, |
| 392 | + new UnassignedInfo( |
| 393 | + UnassignedInfo.Reason.REPLICA_ADDED, |
| 394 | + "message for replica-copy " + i, |
| 395 | + null, |
| 396 | + 0, |
| 397 | + System.nanoTime(), |
| 398 | + System.currentTimeMillis(), |
| 399 | + false, |
| 400 | + UnassignedInfo.AllocationStatus.NO_ATTEMPT, |
| 401 | + Collections.emptySet() |
| 402 | + ) |
| 403 | + ) |
| 404 | + ); |
| 405 | + } |
| 406 | + |
| 407 | + RoutingTable routingTable = RoutingTable.builder().add(isd).build(); |
| 408 | + clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)) |
| 409 | + .metadata(metadata) |
| 410 | + .routingTable(routingTable) |
| 411 | + .build(); |
| 412 | + testAllocation = new RoutingAllocation( |
| 413 | + new AllocationDeciders(Collections.emptyList()), |
| 414 | + new RoutingNodes(clusterState, false), |
| 415 | + clusterState, |
| 416 | + ClusterInfo.EMPTY, |
| 417 | + SnapshotShardSizeInfo.EMPTY, |
| 418 | + System.nanoTime() |
| 419 | + ); |
| 420 | + |
| 421 | + } |
| 422 | + |
348 | 423 | // call this after index creation and update cluster state
|
349 | 424 | private Tuple<Set<String>, Set<String>> createBatchesAndAssert(int expectedBatchSize) {
|
350 | 425 | Set<String> primaryBatches = testShardsBatchGatewayAllocator.createAndUpdateBatches(testAllocation, true);
|
|
0 commit comments