diff --git a/CHANGELOG.md b/CHANGELOG.md index df9b743b7ab3d..bdad8014abe7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Add successfulSearchShardIndices in searchRequestContext ([#15967](https://github.com/opensearch-project/OpenSearch/pull/15967)) - Remove identity-related feature flagged code from the RestController ([#15430](https://github.com/opensearch-project/OpenSearch/pull/15430)) - Add support for msearch API to pass search pipeline name - ([#15923](https://github.com/opensearch-project/OpenSearch/pull/15923)) -- Add support for restoring from snapshot with search replicas - ([](https://github.com/opensearch-project/OpenSearch/pull/)) +- Add support for restoring from snapshot with search replicas - ([#16111](https://github.com/opensearch-project/OpenSearch/pull/16111)) ### Dependencies - Bump `com.azure:azure-identity` from 1.13.0 to 1.13.2 ([#15578](https://github.com/opensearch-project/OpenSearch/pull/15578)) diff --git a/server/src/internalClusterTest/java/org/opensearch/indices/replication/SearchReplicaRestoreIT.java b/server/src/internalClusterTest/java/org/opensearch/indices/replication/SearchReplicaRestoreIT.java index 6c57d33b42bf8..d0cdd9a162c3c 100644 --- a/server/src/internalClusterTest/java/org/opensearch/indices/replication/SearchReplicaRestoreIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/indices/replication/SearchReplicaRestoreIT.java @@ -35,83 +35,56 @@ public class SearchReplicaRestoreIT extends AbstractSnapshotIntegTestCase { @Override protected Settings featureFlagSettings() { - return Settings.builder() - .put(super.featureFlagSettings()) - .put(FeatureFlags.READER_WRITER_SPLIT_EXPERIMENTAL, true) - .build(); - } - - public void testSearchReplicaRestore_WhenSnapshotOnSegRep_RestoredWithSameSettings() throws Exception { - bootstrapIndexWithOutSearchNodes(ReplicationType.SEGMENT); - createRepoAndSnapshot(REPOSITORY_NAME, FS_REPOSITORY_TYPE, SNAPSHOT_NAME, INDEX_NAME); - - restoreSnapshot(REPOSITORY_NAME, SNAPSHOT_NAME, INDEX_NAME, RESTORED_INDEX_NAME, null); - - ensureGreen(RESTORED_INDEX_NAME); - SearchResponse resp = client().prepareSearch(RESTORED_INDEX_NAME).setQuery(QueryBuilders.matchAllQuery()).get(); - assertHitCount(resp, DOC_COUNT); - } - - public void testSearchReplicaRestore_WhenSnapshotOnDocRep_RestoredWithSameSettings() throws Exception { - bootstrapIndexWithOutSearchNodes(ReplicationType.DOCUMENT); - createRepoAndSnapshot(REPOSITORY_NAME, FS_REPOSITORY_TYPE, SNAPSHOT_NAME, INDEX_NAME); - - restoreSnapshot(REPOSITORY_NAME, SNAPSHOT_NAME, INDEX_NAME, RESTORED_INDEX_NAME, null); - - ensureGreen(RESTORED_INDEX_NAME); - SearchResponse resp = client().prepareSearch(RESTORED_INDEX_NAME).setQuery(QueryBuilders.matchAllQuery()).get(); - assertHitCount(resp, DOC_COUNT); - } - - public void testSearchReplicaRestore_WhenSnapshotOnDocRep_RestoreOnDocRep() throws Exception { - bootstrapIndexWithOutSearchNodes(ReplicationType.DOCUMENT); - createRepoAndSnapshot(REPOSITORY_NAME, FS_REPOSITORY_TYPE, SNAPSHOT_NAME, INDEX_NAME); - - restoreSnapshot(REPOSITORY_NAME, SNAPSHOT_NAME, INDEX_NAME, RESTORED_INDEX_NAME, - Settings.builder() - .put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.DOCUMENT) - .build() - ); - ensureGreen(RESTORED_INDEX_NAME); - - SearchResponse resp = client().prepareSearch(RESTORED_INDEX_NAME).setQuery(QueryBuilders.matchAllQuery()).get(); - assertHitCount(resp, DOC_COUNT); + return Settings.builder().put(super.featureFlagSettings()).put(FeatureFlags.READER_WRITER_SPLIT_EXPERIMENTAL, true).build(); } public void testSearchReplicaRestore_WhenSnapshotOnDocRep_RestoreOnDocRepWithSearchReplica() throws Exception { - bootstrapIndexWithOutSearchNodes(ReplicationType.DOCUMENT); + bootstrapIndexWithOutSearchReplicas(ReplicationType.DOCUMENT); createRepoAndSnapshot(REPOSITORY_NAME, FS_REPOSITORY_TYPE, SNAPSHOT_NAME, INDEX_NAME); - SnapshotRestoreException exception = expectThrows(SnapshotRestoreException.class, - () -> restoreSnapshot(REPOSITORY_NAME, SNAPSHOT_NAME, INDEX_NAME, RESTORED_INDEX_NAME, + SnapshotRestoreException exception = expectThrows( + SnapshotRestoreException.class, + () -> restoreSnapshot( + REPOSITORY_NAME, + SNAPSHOT_NAME, + INDEX_NAME, + RESTORED_INDEX_NAME, Settings.builder() .put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.DOCUMENT) .put(IndexMetadata.SETTING_NUMBER_OF_SEARCH_REPLICAS, 1) .build() - )); + ) + ); assertTrue(exception.getMessage().contains(getSnapshotExceptionMessage(ReplicationType.DOCUMENT, ReplicationType.DOCUMENT))); } public void testSearchReplicaRestore_WhenSnapshotOnDocRep_RestoreOnSegRep() throws Exception { - bootstrapIndexWithOutSearchNodes(ReplicationType.DOCUMENT); + bootstrapIndexWithOutSearchReplicas(ReplicationType.DOCUMENT); createRepoAndSnapshot(REPOSITORY_NAME, FS_REPOSITORY_TYPE, SNAPSHOT_NAME, INDEX_NAME); - restoreSnapshot(REPOSITORY_NAME, SNAPSHOT_NAME, INDEX_NAME, RESTORED_INDEX_NAME, - Settings.builder() - .put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT) - .build() + restoreSnapshot( + REPOSITORY_NAME, + SNAPSHOT_NAME, + INDEX_NAME, + RESTORED_INDEX_NAME, + Settings.builder().put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT).build() ); ensureGreen(RESTORED_INDEX_NAME); + assertEquals(0, getNumShards(RESTORED_INDEX_NAME).numSearchReplicas); SearchResponse resp = client().prepareSearch(RESTORED_INDEX_NAME).setQuery(QueryBuilders.matchAllQuery()).get(); assertHitCount(resp, DOC_COUNT); } public void testSearchReplicaRestore_WhenSnapshotOnDocRep_RestoreOnSegRepWithSearchReplica() throws Exception { - bootstrapIndexWithOutSearchNodes(ReplicationType.DOCUMENT); + bootstrapIndexWithOutSearchReplicas(ReplicationType.DOCUMENT); createRepoAndSnapshot(REPOSITORY_NAME, FS_REPOSITORY_TYPE, SNAPSHOT_NAME, INDEX_NAME); - restoreSnapshot(REPOSITORY_NAME, SNAPSHOT_NAME, INDEX_NAME, RESTORED_INDEX_NAME, + restoreSnapshot( + REPOSITORY_NAME, + SNAPSHOT_NAME, + INDEX_NAME, + RESTORED_INDEX_NAME, Settings.builder() .put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT) .put(IndexMetadata.SETTING_NUMBER_OF_SEARCH_REPLICAS, 1) @@ -120,130 +93,137 @@ public void testSearchReplicaRestore_WhenSnapshotOnDocRep_RestoreOnSegRepWithSea ensureYellowAndNoInitializingShards(RESTORED_INDEX_NAME); internalCluster().startDataOnlyNode(); ensureGreen(RESTORED_INDEX_NAME); + assertEquals(1, getNumShards(RESTORED_INDEX_NAME).numSearchReplicas); SearchResponse resp = client().prepareSearch(RESTORED_INDEX_NAME).setQuery(QueryBuilders.matchAllQuery()).get(); assertHitCount(resp, DOC_COUNT); } public void testSearchReplicaRestore_WhenSnapshotOnSegRep_RestoreOnDocRep() throws Exception { - bootstrapIndexWithOutSearchNodes(ReplicationType.SEGMENT); + bootstrapIndexWithOutSearchReplicas(ReplicationType.SEGMENT); createRepoAndSnapshot(REPOSITORY_NAME, FS_REPOSITORY_TYPE, SNAPSHOT_NAME, INDEX_NAME); - restoreSnapshot(REPOSITORY_NAME, SNAPSHOT_NAME, INDEX_NAME, RESTORED_INDEX_NAME, - Settings.builder() - .put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.DOCUMENT) - .build() + restoreSnapshot( + REPOSITORY_NAME, + SNAPSHOT_NAME, + INDEX_NAME, + RESTORED_INDEX_NAME, + Settings.builder().put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.DOCUMENT).build() ); ensureGreen(RESTORED_INDEX_NAME); + assertEquals(0, getNumShards(RESTORED_INDEX_NAME).numSearchReplicas); SearchResponse resp = client().prepareSearch(RESTORED_INDEX_NAME).setQuery(QueryBuilders.matchAllQuery()).get(); assertHitCount(resp, DOC_COUNT); } public void testSearchReplicaRestore_WhenSnapshotOnSegRep_RestoreOnDocRepWithSearchReplica() throws Exception { - bootstrapIndexWithOutSearchNodes(ReplicationType.SEGMENT); + bootstrapIndexWithOutSearchReplicas(ReplicationType.SEGMENT); createRepoAndSnapshot(REPOSITORY_NAME, FS_REPOSITORY_TYPE, SNAPSHOT_NAME, INDEX_NAME); - SnapshotRestoreException exception = expectThrows(SnapshotRestoreException.class, - () -> restoreSnapshot(REPOSITORY_NAME, SNAPSHOT_NAME, INDEX_NAME, RESTORED_INDEX_NAME, + SnapshotRestoreException exception = expectThrows( + SnapshotRestoreException.class, + () -> restoreSnapshot( + REPOSITORY_NAME, + SNAPSHOT_NAME, + INDEX_NAME, + RESTORED_INDEX_NAME, Settings.builder() .put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.DOCUMENT) .put(IndexMetadata.SETTING_NUMBER_OF_SEARCH_REPLICAS, 1) .build() - )); - assertTrue(exception.getMessage().contains(getSnapshotExceptionMessage(ReplicationType.SEGMENT, ReplicationType.DOCUMENT))); - } - - public void testSearchReplicaRestore_WhenSnapshotOnSegRep_RestoreOnSegRep() throws Exception { - bootstrapIndexWithOutSearchNodes(ReplicationType.SEGMENT); - createRepoAndSnapshot(REPOSITORY_NAME, FS_REPOSITORY_TYPE, SNAPSHOT_NAME, INDEX_NAME); - - restoreSnapshot(REPOSITORY_NAME, SNAPSHOT_NAME, INDEX_NAME, RESTORED_INDEX_NAME, - Settings.builder() - .put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT) - .build() + ) ); - ensureGreen(RESTORED_INDEX_NAME); - - SearchResponse resp = client().prepareSearch(RESTORED_INDEX_NAME).setQuery(QueryBuilders.matchAllQuery()).get(); - assertHitCount(resp, DOC_COUNT); + assertTrue(exception.getMessage().contains(getSnapshotExceptionMessage(ReplicationType.SEGMENT, ReplicationType.DOCUMENT))); } public void testSearchReplicaRestore_WhenSnapshotOnSegRep_RestoreOnSegRepWithSearchReplica() throws Exception { - bootstrapIndexWithOutSearchNodes(ReplicationType.SEGMENT); + bootstrapIndexWithOutSearchReplicas(ReplicationType.SEGMENT); createRepoAndSnapshot(REPOSITORY_NAME, FS_REPOSITORY_TYPE, SNAPSHOT_NAME, INDEX_NAME); - restoreSnapshot(REPOSITORY_NAME, SNAPSHOT_NAME, INDEX_NAME, RESTORED_INDEX_NAME, - Settings.builder() - .put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT) - .put(IndexMetadata.SETTING_NUMBER_OF_SEARCH_REPLICAS, 1) - .build() + restoreSnapshot( + REPOSITORY_NAME, + SNAPSHOT_NAME, + INDEX_NAME, + RESTORED_INDEX_NAME, + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SEARCH_REPLICAS, 1).build() ); ensureYellowAndNoInitializingShards(RESTORED_INDEX_NAME); internalCluster().startDataOnlyNode(); ensureGreen(RESTORED_INDEX_NAME); + assertEquals(1, getNumShards(RESTORED_INDEX_NAME).numSearchReplicas); SearchResponse resp = client().prepareSearch(RESTORED_INDEX_NAME).setQuery(QueryBuilders.matchAllQuery()).get(); assertHitCount(resp, DOC_COUNT); } public void testSearchReplicaRestore_WhenSnapshotOnSegRepWithSearchReplica_RestoreOnDocRep() throws Exception { - bootstrapIndexWithSearchNodes(ReplicationType.SEGMENT); + bootstrapIndexWithSearchReplicas(); createRepoAndSnapshot(REPOSITORY_NAME, FS_REPOSITORY_TYPE, SNAPSHOT_NAME, INDEX_NAME); - SnapshotRestoreException exception = expectThrows(SnapshotRestoreException.class, - () -> restoreSnapshot(REPOSITORY_NAME, SNAPSHOT_NAME, INDEX_NAME, RESTORED_INDEX_NAME, - Settings.builder() - .put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.DOCUMENT) - .build() - )); + SnapshotRestoreException exception = expectThrows( + SnapshotRestoreException.class, + () -> restoreSnapshot( + REPOSITORY_NAME, + SNAPSHOT_NAME, + INDEX_NAME, + RESTORED_INDEX_NAME, + Settings.builder().put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.DOCUMENT).build() + ) + ); assertTrue(exception.getMessage().contains(getSnapshotExceptionMessage(ReplicationType.SEGMENT, ReplicationType.DOCUMENT))); } public void testSearchReplicaRestore_WhenSnapshotOnSegRepWithSearchReplica_RestoreOnDocRepWithNoSearchReplica() throws Exception { - bootstrapIndexWithSearchNodes(ReplicationType.SEGMENT); + bootstrapIndexWithSearchReplicas(); createRepoAndSnapshot(REPOSITORY_NAME, FS_REPOSITORY_TYPE, SNAPSHOT_NAME, INDEX_NAME); - restoreSnapshot(REPOSITORY_NAME, SNAPSHOT_NAME, INDEX_NAME, RESTORED_INDEX_NAME, + restoreSnapshot( + REPOSITORY_NAME, + SNAPSHOT_NAME, + INDEX_NAME, + RESTORED_INDEX_NAME, Settings.builder() .put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.DOCUMENT) .put(IndexMetadata.SETTING_NUMBER_OF_SEARCH_REPLICAS, 0) .build() ); ensureGreen(RESTORED_INDEX_NAME); + assertEquals(0, getNumShards(RESTORED_INDEX_NAME).numSearchReplicas); SearchResponse resp = client().prepareSearch(RESTORED_INDEX_NAME).setQuery(QueryBuilders.matchAllQuery()).get(); assertHitCount(resp, DOC_COUNT); } - public void testSearchReplicaRestore_WhenSnapshotOnSegRepWithSearchReplica_RestoreOnSegRep() throws Exception { - bootstrapIndexWithSearchNodes(ReplicationType.SEGMENT); - createRepoAndSnapshot(REPOSITORY_NAME, FS_REPOSITORY_TYPE, SNAPSHOT_NAME, INDEX_NAME); - - restoreSnapshot(REPOSITORY_NAME, SNAPSHOT_NAME, INDEX_NAME, RESTORED_INDEX_NAME, - Settings.builder() - .put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT) - .build() - ); - ensureGreen(RESTORED_INDEX_NAME); + private void bootstrapIndexWithOutSearchReplicas(ReplicationType replicationType) throws InterruptedException { + startCluster(2); - SearchResponse resp = client().prepareSearch(RESTORED_INDEX_NAME).setQuery(QueryBuilders.matchAllQuery()).get(); - assertHitCount(resp, DOC_COUNT); - } + Settings settings = Settings.builder() + .put(super.indexSettings()) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_SEARCH_REPLICAS, 0) + .put(IndexMetadata.SETTING_REPLICATION_TYPE, replicationType) + .build(); - private void bootstrapIndexWithOutSearchNodes(ReplicationType replicationType) throws InterruptedException { - startCluster(2); - createIndex(INDEX_NAME, getIndexSettings(1, 1, 0, - replicationType)); + createIndex(INDEX_NAME, settings); indexRandomDocs(INDEX_NAME, DOC_COUNT); refresh(INDEX_NAME); ensureGreen(INDEX_NAME); } - private void bootstrapIndexWithSearchNodes(ReplicationType replicationType) throws InterruptedException { + private void bootstrapIndexWithSearchReplicas() throws InterruptedException { startCluster(3); - createIndex(INDEX_NAME, getIndexSettings(1, 1, 1, - replicationType)); + + Settings settings = Settings.builder() + .put(super.indexSettings()) + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1) + .put(IndexMetadata.SETTING_NUMBER_OF_SEARCH_REPLICAS, 1) + .put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT) + .build(); + + createIndex(INDEX_NAME, settings); indexRandomDocs(INDEX_NAME, DOC_COUNT); refresh(INDEX_NAME); ensureGreen(INDEX_NAME); @@ -251,33 +231,23 @@ private void bootstrapIndexWithSearchNodes(ReplicationType replicationType) thro private void startCluster(int numOfNodes) { internalCluster().startClusterManagerOnlyNode(); - for(int i = 0; i< numOfNodes; i++) { - internalCluster().startDataOnlyNode(); - } + internalCluster().startDataOnlyNodes(numOfNodes); } - private void createRepoAndSnapshot(String repositoryName, String repositoryType, - String snapshotName, String indexName) { + private void createRepoAndSnapshot(String repositoryName, String repositoryType, String snapshotName, String indexName) { createRepository(repositoryName, repositoryType, randomRepoPath().toAbsolutePath()); createSnapshot(repositoryName, snapshotName, List.of(indexName)); assertAcked(client().admin().indices().prepareDelete(INDEX_NAME)); assertFalse("index [" + INDEX_NAME + "] should have been deleted", indexExists(INDEX_NAME)); } - private Settings getIndexSettings(int numOfShards, int numOfReplicas, int numOfSearchReplicas, - ReplicationType replicationType) { - return Settings.builder() - .put(super.indexSettings()) - .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numOfShards) - .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numOfReplicas) - .put(IndexMetadata.SETTING_NUMBER_OF_SEARCH_REPLICAS, numOfSearchReplicas) - .put(IndexMetadata.SETTING_REPLICATION_TYPE, replicationType) - .build(); - } - private String getSnapshotExceptionMessage(ReplicationType snapshotReplicationType, ReplicationType restoreReplicationType) { - return "snapshot was created with [index.replication.type] as [" + snapshotReplicationType + "]. " + - "To restore with [index.replication.type] as [" + restoreReplicationType + "], " + - "[index.number_of_search_only_replicas] must be set to [0]"; + return "snapshot was created with [index.replication.type] as [" + + snapshotReplicationType + + "]. " + + "To restore with [index.replication.type] as [" + + restoreReplicationType + + "], " + + "[index.number_of_search_only_replicas] must be set to [0]"; } } diff --git a/server/src/main/java/org/opensearch/snapshots/RestoreService.java b/server/src/main/java/org/opensearch/snapshots/RestoreService.java index 04518051616b5..d127671981bcf 100644 --- a/server/src/main/java/org/opensearch/snapshots/RestoreService.java +++ b/server/src/main/java/org/opensearch/snapshots/RestoreService.java @@ -107,7 +107,6 @@ import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.function.Function; @@ -405,11 +404,7 @@ public ClusterState execute(ClusterState currentState) { ignoreSettingsInternal ); - validateReplicationTypeRestoreSettings( - metadata.index(index).getSettings().get(SETTING_REPLICATION_TYPE), - snapshotIndexMetadata.getSettings().get(SETTING_REPLICATION_TYPE), - snapshotIndexMetadata.getSettings().getAsInt(SETTING_NUMBER_OF_SEARCH_REPLICAS, 0) - ); + validateReplicationTypeRestoreSettings(metadata.index(index), snapshotIndexMetadata); if (isRemoteSnapshot) { snapshotIndexMetadata = addSnapshotToIndexSettings(snapshotIndexMetadata, snapshot, snapshotIndexId); @@ -661,17 +656,27 @@ public ClusterState execute(ClusterState currentState) { return allocationService.reroute(updatedState, "restored snapshot [" + snapshot + "]"); } - private void validateReplicationTypeRestoreSettings(String snapshotReplicationType, - String restoreReplicationType, - int restoreNumberOfSearchReplicas) { - if(Objects.equals(restoreReplicationType, ReplicationType.DOCUMENT.toString())) { - if(restoreNumberOfSearchReplicas > 0) { + private void validateReplicationTypeRestoreSettings(IndexMetadata snapshottedMetadata, IndexMetadata updatedMetadata) { + String restoreReplicationType = updatedMetadata.getSettings().get(SETTING_REPLICATION_TYPE); + int restoreNumberOfSearchReplicas = updatedMetadata.getSettings().getAsInt(SETTING_NUMBER_OF_SEARCH_REPLICAS, 0); + + if (ReplicationType.DOCUMENT.toString().equals(restoreReplicationType)) { + if (restoreNumberOfSearchReplicas > 0) { throw new SnapshotRestoreException( snapshot, - "snapshot was created with [" + SETTING_REPLICATION_TYPE + "]" - + " as ["+ snapshotReplicationType + "]." - + " To restore with [" + SETTING_REPLICATION_TYPE + "]" + " as [" - + ReplicationType.DOCUMENT + "], [" + SETTING_NUMBER_OF_SEARCH_REPLICAS + "snapshot was created with [" + + SETTING_REPLICATION_TYPE + + "]" + + " as [" + + snapshottedMetadata.getSettings().get(SETTING_REPLICATION_TYPE) + + "]." + + " To restore with [" + + SETTING_REPLICATION_TYPE + + "]" + + " as [" + + ReplicationType.DOCUMENT + + "], [" + + SETTING_NUMBER_OF_SEARCH_REPLICAS + "] must be set to [0]" ); } diff --git a/server/src/test/java/org/opensearch/cluster/routing/SearchOnlyReplicaRestoreTests.java b/server/src/test/java/org/opensearch/cluster/routing/SearchOnlyReplicaRestoreTests.java index 85f31d27bb6be..d0effe9484533 100644 --- a/server/src/test/java/org/opensearch/cluster/routing/SearchOnlyReplicaRestoreTests.java +++ b/server/src/test/java/org/opensearch/cluster/routing/SearchOnlyReplicaRestoreTests.java @@ -21,8 +21,6 @@ import org.opensearch.test.OpenSearchTestCase; import java.util.HashSet; -import java.util.List; -import java.util.stream.Collectors; public class SearchOnlyReplicaRestoreTests extends OpenSearchTestCase { @@ -45,20 +43,17 @@ public void testSearchOnlyReplicasRestored() { new IndexId("test", UUIDs.randomBase64UUID(random())) ); - RoutingTable routingTable = RoutingTable.builder() - .addAsNewRestore(indexMetadata, snapshotRecoverySource, new HashSet<>()) - .build(); + RoutingTable routingTable = RoutingTable.builder().addAsNewRestore(indexMetadata, snapshotRecoverySource, new HashSet<>()).build(); ClusterState clusterState = ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)) .metadata(metadata) .routingTable(routingTable) .build(); - List shardRoutings = clusterState.routingTable().index("test").shard(0).getShards(); - List searchOnlyShards = shardRoutings.stream().filter(ShardRouting::isSearchOnly).collect(Collectors.toList()); + IndexShardRoutingTable indexShardRoutingTable = clusterState.routingTable().index("test").shard(0); assertEquals(1, clusterState.routingTable().index("test").shards().size()); - assertEquals(3, shardRoutings.size()); - assertEquals(1, searchOnlyShards.size()); + assertEquals(3, indexShardRoutingTable.getShards().size()); + assertEquals(1, indexShardRoutingTable.searchOnlyReplicas().size()); } } diff --git a/test/framework/src/main/java/org/opensearch/snapshots/AbstractSnapshotIntegTestCase.java b/test/framework/src/main/java/org/opensearch/snapshots/AbstractSnapshotIntegTestCase.java index 8b89a3c899e87..9bf725766069c 100644 --- a/test/framework/src/main/java/org/opensearch/snapshots/AbstractSnapshotIntegTestCase.java +++ b/test/framework/src/main/java/org/opensearch/snapshots/AbstractSnapshotIntegTestCase.java @@ -508,10 +508,14 @@ protected SnapshotInfo createSnapshot(String repositoryName, String snapshot, Li return snapshotInfo; } - protected void restoreSnapshot(String repositoryName, String snapshotName, String indexName, - String restoredIndexName, Settings indexSettings) { - logger.info("--> restoring snapshot [{}] of {} in [{}] to [{}]", - snapshotName, indexName, repositoryName, restoredIndexName); + protected void restoreSnapshot( + String repositoryName, + String snapshotName, + String indexName, + String restoredIndexName, + Settings indexSettings + ) { + logger.info("--> restoring snapshot [{}] of {} in [{}] to [{}]", snapshotName, indexName, repositoryName, restoredIndexName); RestoreSnapshotRequestBuilder builder = client().admin() .cluster() .prepareRestoreSnapshot(repositoryName, snapshotName) diff --git a/test/framework/src/main/java/org/opensearch/test/OpenSearchIntegTestCase.java b/test/framework/src/main/java/org/opensearch/test/OpenSearchIntegTestCase.java index 68a2b8086a92e..4530870b252f9 100644 --- a/test/framework/src/main/java/org/opensearch/test/OpenSearchIntegTestCase.java +++ b/test/framework/src/main/java/org/opensearch/test/OpenSearchIntegTestCase.java @@ -215,6 +215,7 @@ import java.util.stream.Collectors; import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS; +import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SEARCH_REPLICAS; import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS; import static org.opensearch.common.unit.TimeValue.timeValueMillis; import static org.opensearch.core.common.util.CollectionUtils.eagerPartition; @@ -2264,7 +2265,8 @@ protected NumShards getNumShards(String index) { assertThat(metadata.hasIndex(index), equalTo(true)); int numShards = Integer.valueOf(metadata.index(index).getSettings().get(SETTING_NUMBER_OF_SHARDS)); int numReplicas = Integer.valueOf(metadata.index(index).getSettings().get(SETTING_NUMBER_OF_REPLICAS)); - return new NumShards(numShards, numReplicas); + int numSearchReplicas = Integer.valueOf(metadata.index(index).getSettings().get(SETTING_NUMBER_OF_SEARCH_REPLICAS)); + return new NumShards(numShards, numReplicas, numSearchReplicas); } /** @@ -2305,13 +2307,15 @@ public void assertSortedSegments(String indexName, Sort expectedIndexSort) { protected static class NumShards { public final int numPrimaries; public final int numReplicas; + public final int numSearchReplicas; public final int totalNumShards; public final int dataCopies; - private NumShards(int numPrimaries, int numReplicas) { + private NumShards(int numPrimaries, int numReplicas, int numSearchReplicas) { this.numPrimaries = numPrimaries; this.numReplicas = numReplicas; - this.dataCopies = numReplicas + 1; + this.numSearchReplicas = numSearchReplicas; + this.dataCopies = numReplicas + numSearchReplicas + 1; this.totalNumShards = numPrimaries * dataCopies; } }