|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.remotestore; |
| 10 | + |
| 11 | +import com.carrotsearch.randomizedtesting.RandomizedTest; |
| 12 | +import org.opensearch.action.admin.indices.close.CloseIndexResponse; |
| 13 | +import org.opensearch.cluster.ClusterState; |
| 14 | +import org.opensearch.cluster.metadata.IndexMetadata; |
| 15 | +import org.opensearch.cluster.node.DiscoveryNode; |
| 16 | +import org.opensearch.cluster.routing.IndexShardRoutingTable; |
| 17 | +import org.opensearch.cluster.routing.ShardRouting; |
| 18 | +import org.opensearch.common.settings.Settings; |
| 19 | +import org.opensearch.test.BackgroundIndexer; |
| 20 | +import org.opensearch.test.InternalTestCluster; |
| 21 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 22 | + |
| 23 | +import java.util.Locale; |
| 24 | + |
| 25 | +import static org.hamcrest.Matchers.equalTo; |
| 26 | +import static org.hamcrest.Matchers.is; |
| 27 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; |
| 28 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount; |
| 29 | + |
| 30 | +@OpenSearchIntegTestCase.ClusterScope(numDataNodes = 0) |
| 31 | +public class ReplicaToPrimaryPromotionIT extends RemoteStoreBaseIT { |
| 32 | + private int shard_count = 5; |
| 33 | + |
| 34 | + @Override |
| 35 | + public Settings indexSettings() { |
| 36 | + return Settings.builder() |
| 37 | + .put(super.indexSettings()) |
| 38 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, shard_count) |
| 39 | + .put(IndexMetadata.SETTING_REMOTE_TRANSLOG_STORE_ENABLED, true) |
| 40 | + .put(IndexMetadata.SETTING_REMOTE_TRANSLOG_STORE_REPOSITORY, REPOSITORY_NAME) |
| 41 | + .build(); |
| 42 | + } |
| 43 | + |
| 44 | + public void testPromoteReplicaToPrimary() throws Exception { |
| 45 | + internalCluster().startNode(); |
| 46 | + internalCluster().startNode(); |
| 47 | + final String indexName = randomAlphaOfLength(5).toLowerCase(Locale.ROOT); |
| 48 | + shard_count = scaledRandomIntBetween(1, 5); |
| 49 | + createIndex(indexName); |
| 50 | + int numOfDocs = 0; |
| 51 | + int numIter = scaledRandomIntBetween(0, 10); |
| 52 | + for (int i = 0; i < numIter; i++) { |
| 53 | + final int numOfDoc = scaledRandomIntBetween(0, 200); |
| 54 | + logger.info("num of docs in iter {} {}", numOfDoc, i); |
| 55 | + if (numOfDoc > 0) { |
| 56 | + try ( |
| 57 | + BackgroundIndexer indexer = new BackgroundIndexer( |
| 58 | + indexName, |
| 59 | + "_doc", |
| 60 | + client(), |
| 61 | + numOfDoc, |
| 62 | + RandomizedTest.scaledRandomIntBetween(2, 5), |
| 63 | + false, |
| 64 | + null |
| 65 | + ) |
| 66 | + ) { |
| 67 | + indexer.setUseAutoGeneratedIDs(true); |
| 68 | + indexer.start(numOfDoc); |
| 69 | + waitForIndexed(numOfDoc, indexer); |
| 70 | + numOfDocs += numOfDoc; |
| 71 | + indexer.stopAndAwaitStopped(); |
| 72 | + if (random().nextBoolean()) { |
| 73 | + // 90% refresh + 10% flush |
| 74 | + if (random().nextInt(10) != 0) { |
| 75 | + refresh(indexName); |
| 76 | + } else { |
| 77 | + flush(indexName); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + ensureGreen(indexName); |
| 85 | + |
| 86 | + // sometimes test with a closed index |
| 87 | + final IndexMetadata.State indexState = randomFrom(IndexMetadata.State.OPEN, IndexMetadata.State.CLOSE); |
| 88 | + if (indexState == IndexMetadata.State.CLOSE) { |
| 89 | + CloseIndexResponse closeIndexResponse = client().admin().indices().prepareClose(indexName).get(); |
| 90 | + assertThat("close index not acked - " + closeIndexResponse, closeIndexResponse.isAcknowledged(), equalTo(true)); |
| 91 | + ensureGreen(indexName); |
| 92 | + } |
| 93 | + |
| 94 | + // pick up a data node that contains a random primary shard |
| 95 | + ClusterState state = client(internalCluster().getClusterManagerName()).admin().cluster().prepareState().get().getState(); |
| 96 | + final int numShards = state.metadata().index(indexName).getNumberOfShards(); |
| 97 | + final ShardRouting primaryShard = state.routingTable().index(indexName).shard(randomIntBetween(0, numShards - 1)).primaryShard(); |
| 98 | + final DiscoveryNode randomNode = state.nodes().resolveNode(primaryShard.currentNodeId()); |
| 99 | + |
| 100 | + // stop the random data node, all remaining shards are promoted to primaries |
| 101 | + internalCluster().stopRandomNode(InternalTestCluster.nameFilter(randomNode.getName())); |
| 102 | + ensureYellowAndNoInitializingShards(indexName); |
| 103 | + |
| 104 | + state = client(internalCluster().getClusterManagerName()).admin().cluster().prepareState().get().getState(); |
| 105 | + for (IndexShardRoutingTable shardRoutingTable : state.routingTable().index(indexName)) { |
| 106 | + for (ShardRouting shardRouting : shardRoutingTable.activeShards()) { |
| 107 | + assertThat(shardRouting + " should be promoted as a primary", shardRouting.primary(), is(true)); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (indexState == IndexMetadata.State.CLOSE) { |
| 112 | + assertAcked(client().admin().indices().prepareOpen(indexName)); |
| 113 | + ensureYellowAndNoInitializingShards(indexName); |
| 114 | + } |
| 115 | + refresh(indexName); |
| 116 | + assertHitCount(client().prepareSearch(indexName).setSize(0).get(), numOfDocs); |
| 117 | + } |
| 118 | +} |
0 commit comments