|
| 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.cluster.metadata; |
| 10 | + |
| 11 | +import org.opensearch.action.admin.cluster.state.ClusterStateResponse; |
| 12 | +import org.opensearch.action.admin.indices.open.OpenIndexResponse; |
| 13 | +import org.opensearch.action.admin.indices.settings.get.GetSettingsResponse; |
| 14 | +import org.opensearch.action.search.SearchResponse; |
| 15 | +import org.opensearch.action.support.WriteRequest; |
| 16 | +import org.opensearch.common.settings.Settings; |
| 17 | +import org.opensearch.common.util.FeatureFlags; |
| 18 | +import org.opensearch.remotestore.RemoteStoreBaseIntegTestCase; |
| 19 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 20 | + |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | + |
| 23 | +import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS; |
| 24 | +import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SEARCH_REPLICAS; |
| 25 | +import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS; |
| 26 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; |
| 27 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertHitCount; |
| 28 | + |
| 29 | +@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0) |
| 30 | +public class MetadataIndexStateServiceIT extends RemoteStoreBaseIntegTestCase { |
| 31 | + |
| 32 | + private static final String TEST_INDEX = "test_open_close_index"; |
| 33 | + |
| 34 | + @Override |
| 35 | + protected Settings featureFlagSettings() { |
| 36 | + return Settings.builder().put(super.featureFlagSettings()).put(FeatureFlags.READER_WRITER_SPLIT_EXPERIMENTAL, Boolean.TRUE).build(); |
| 37 | + } |
| 38 | + |
| 39 | + public void testIndexCloseAndOpen() throws Exception { |
| 40 | + internalCluster().startClusterManagerOnlyNode(); |
| 41 | + internalCluster().startDataOnlyNodes(2); |
| 42 | + |
| 43 | + Settings specificSettings = Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 1).build(); |
| 44 | + |
| 45 | + createIndex(TEST_INDEX, specificSettings); |
| 46 | + ensureGreen(TEST_INDEX); |
| 47 | + |
| 48 | + for (int i = 0; i < 10; i++) { |
| 49 | + client().prepareIndex(TEST_INDEX) |
| 50 | + .setId(Integer.toString(i)) |
| 51 | + .setSource("field1", "value" + i) |
| 52 | + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
| 53 | + .get(); |
| 54 | + } |
| 55 | + |
| 56 | + assertAcked(client().admin().indices().prepareClose(TEST_INDEX).get()); |
| 57 | + |
| 58 | + ClusterStateResponse clusterStateResponse = client().admin().cluster().prepareState().get(); |
| 59 | + IndexMetadata indexMetadata = clusterStateResponse.getState().metadata().index(TEST_INDEX); |
| 60 | + assertEquals(IndexMetadata.State.CLOSE, indexMetadata.getState()); |
| 61 | + |
| 62 | + OpenIndexResponse openIndexResponse = client().admin().indices().prepareOpen(TEST_INDEX).get(); |
| 63 | + |
| 64 | + assertTrue("Open operation should be acknowledged", openIndexResponse.isAcknowledged()); |
| 65 | + assertTrue("Open operation shards should be acknowledged", openIndexResponse.isShardsAcknowledged()); |
| 66 | + |
| 67 | + clusterStateResponse = client().admin().cluster().prepareState().get(); |
| 68 | + indexMetadata = clusterStateResponse.getState().metadata().index(TEST_INDEX); |
| 69 | + assertEquals(IndexMetadata.State.OPEN, indexMetadata.getState()); |
| 70 | + |
| 71 | + assertBusy(() -> { |
| 72 | + SearchResponse searchResponse = client().prepareSearch(TEST_INDEX).get(); |
| 73 | + assertHitCount(searchResponse, 10); |
| 74 | + }, 30, TimeUnit.SECONDS); |
| 75 | + } |
| 76 | + |
| 77 | + public void testIndexCloseAndOpenWithSearchOnlyMode() throws Exception { |
| 78 | + internalCluster().startClusterManagerOnlyNode(); |
| 79 | + internalCluster().startDataOnlyNodes(2); |
| 80 | + internalCluster().startSearchOnlyNodes(1); |
| 81 | + |
| 82 | + Settings specificSettings = Settings.builder() |
| 83 | + .put(SETTING_NUMBER_OF_SHARDS, 1) |
| 84 | + .put(SETTING_NUMBER_OF_REPLICAS, 1) |
| 85 | + .put(SETTING_NUMBER_OF_SEARCH_REPLICAS, 1) |
| 86 | + .build(); |
| 87 | + |
| 88 | + createIndex(TEST_INDEX, specificSettings); |
| 89 | + ensureGreen(TEST_INDEX); |
| 90 | + |
| 91 | + for (int i = 0; i < 10; i++) { |
| 92 | + client().prepareIndex(TEST_INDEX) |
| 93 | + .setId(Integer.toString(i)) |
| 94 | + .setSource("field1", "value" + i) |
| 95 | + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
| 96 | + .get(); |
| 97 | + } |
| 98 | + |
| 99 | + assertAcked(client().admin().indices().prepareScaleSearchOnly(TEST_INDEX, true).get()); |
| 100 | + ensureGreen(TEST_INDEX); |
| 101 | + |
| 102 | + GetSettingsResponse settingsResponse = client().admin().indices().prepareGetSettings(TEST_INDEX).get(); |
| 103 | + assertTrue(settingsResponse.getSetting(TEST_INDEX, IndexMetadata.INDEX_BLOCKS_SEARCH_ONLY_SETTING.getKey()).equals("true")); |
| 104 | + |
| 105 | + assertAcked(client().admin().indices().prepareClose(TEST_INDEX).get()); |
| 106 | + |
| 107 | + ClusterStateResponse clusterStateResponse = client().admin().cluster().prepareState().get(); |
| 108 | + IndexMetadata indexMetadata = clusterStateResponse.getState().metadata().index(TEST_INDEX); |
| 109 | + assertEquals(IndexMetadata.State.CLOSE, indexMetadata.getState()); |
| 110 | + |
| 111 | + OpenIndexResponse openIndexResponse = client().admin().indices().prepareOpen(TEST_INDEX).get(); |
| 112 | + |
| 113 | + assertTrue("Open operation should be acknowledged", openIndexResponse.isAcknowledged()); |
| 114 | + assertTrue("Open operation shards should be acknowledged", openIndexResponse.isShardsAcknowledged()); |
| 115 | + |
| 116 | + clusterStateResponse = client().admin().cluster().prepareState().get(); |
| 117 | + indexMetadata = clusterStateResponse.getState().metadata().index(TEST_INDEX); |
| 118 | + assertEquals(IndexMetadata.State.OPEN, indexMetadata.getState()); |
| 119 | + |
| 120 | + settingsResponse = client().admin().indices().prepareGetSettings(TEST_INDEX).get(); |
| 121 | + assertTrue(settingsResponse.getSetting(TEST_INDEX, IndexMetadata.INDEX_BLOCKS_SEARCH_ONLY_SETTING.getKey()).equals("true")); |
| 122 | + |
| 123 | + assertBusy(() -> { |
| 124 | + SearchResponse searchResponse = client().prepareSearch(TEST_INDEX).get(); |
| 125 | + assertHitCount(searchResponse, 10); |
| 126 | + }, 30, TimeUnit.SECONDS); |
| 127 | + } |
| 128 | +} |
0 commit comments