|
| 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.remotemigration; |
| 10 | + |
| 11 | +import org.opensearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; |
| 12 | +import org.opensearch.action.admin.indices.close.CloseIndexRequest; |
| 13 | +import org.opensearch.action.support.ActiveShardCount; |
| 14 | +import org.opensearch.cluster.ClusterState; |
| 15 | +import org.opensearch.cluster.metadata.IndexMetadata; |
| 16 | +import org.opensearch.cluster.metadata.MetadataIndexStateService; |
| 17 | +import org.opensearch.common.settings.Settings; |
| 18 | +import org.opensearch.indices.replication.common.ReplicationType; |
| 19 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 20 | + |
| 21 | +import java.util.concurrent.ExecutionException; |
| 22 | + |
| 23 | +import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_REPLICATION_TYPE; |
| 24 | +import static org.opensearch.node.remotestore.RemoteStoreNodeService.MIGRATION_DIRECTION_SETTING; |
| 25 | +import static org.opensearch.node.remotestore.RemoteStoreNodeService.REMOTE_STORE_COMPATIBILITY_MODE_SETTING; |
| 26 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; |
| 27 | + |
| 28 | +@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0) |
| 29 | +public class CloseIndexMigrationTestCase extends MigrationBaseTestCase { |
| 30 | + private static final String TEST_INDEX = "ind"; |
| 31 | + private final static String REMOTE_STORE_DIRECTION = "remote_store"; |
| 32 | + private final static String MIXED_MODE = "mixed"; |
| 33 | + |
| 34 | + /* |
| 35 | + * This test will verify the close request failure, when cluster mode is mixed |
| 36 | + * and migration to remote store is in progress. |
| 37 | + * */ |
| 38 | + public void testFailCloseIndexWhileDocRepToRemoteStoreMigration() { |
| 39 | + setAddRemote(false); |
| 40 | + // create a docrep cluster |
| 41 | + internalCluster().startClusterManagerOnlyNode(); |
| 42 | + internalCluster().validateClusterFormed(); |
| 43 | + |
| 44 | + // add a non-remote node |
| 45 | + String nonRemoteNodeName = internalCluster().startDataOnlyNode(); |
| 46 | + internalCluster().validateClusterFormed(); |
| 47 | + |
| 48 | + // create index in cluster |
| 49 | + Settings.Builder builder = Settings.builder().put(SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT); |
| 50 | + internalCluster().client() |
| 51 | + .admin() |
| 52 | + .indices() |
| 53 | + .prepareCreate(TEST_INDEX) |
| 54 | + .setSettings( |
| 55 | + builder.put("index.number_of_shards", 2) |
| 56 | + .put("index.number_of_replicas", 0) |
| 57 | + .put("index.routing.allocation.include._name", nonRemoteNodeName) |
| 58 | + ) |
| 59 | + .setWaitForActiveShards(ActiveShardCount.ALL) |
| 60 | + .execute() |
| 61 | + .actionGet(); |
| 62 | + |
| 63 | + // set mixed mode |
| 64 | + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); |
| 65 | + updateSettingsRequest.persistentSettings(Settings.builder().put(REMOTE_STORE_COMPATIBILITY_MODE_SETTING.getKey(), MIXED_MODE)); |
| 66 | + assertAcked(internalCluster().client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); |
| 67 | + |
| 68 | + // add a remote node |
| 69 | + addRemote = true; |
| 70 | + internalCluster().startDataOnlyNode(); |
| 71 | + internalCluster().validateClusterFormed(); |
| 72 | + |
| 73 | + // set remote store migration direction |
| 74 | + updateSettingsRequest.persistentSettings(Settings.builder().put(MIGRATION_DIRECTION_SETTING.getKey(), REMOTE_STORE_DIRECTION)); |
| 75 | + assertAcked(internalCluster().client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); |
| 76 | + |
| 77 | + ensureGreen(TEST_INDEX); |
| 78 | + |
| 79 | + // Try closing the index, expecting failure. |
| 80 | + ExecutionException ex = expectThrows( |
| 81 | + ExecutionException.class, |
| 82 | + () -> internalCluster().client().admin().indices().close(new CloseIndexRequest(TEST_INDEX)).get() |
| 83 | + |
| 84 | + ); |
| 85 | + assertEquals("Cannot close index while remote migration is ongoing", ex.getCause().getMessage()); |
| 86 | + } |
| 87 | + |
| 88 | + /* |
| 89 | + * Verify that index closes if compatibility mode is MIXED, and direction is set to NONE |
| 90 | + * */ |
| 91 | + public void testCloseIndexRequestWithMixedCompatibilityModeAndNoDirection() { |
| 92 | + setAddRemote(false); |
| 93 | + // create a docrep cluster |
| 94 | + internalCluster().startClusterManagerOnlyNode(); |
| 95 | + internalCluster().validateClusterFormed(); |
| 96 | + |
| 97 | + // add a non-remote node |
| 98 | + String nonRemoteNodeName = internalCluster().startDataOnlyNode(); |
| 99 | + internalCluster().validateClusterFormed(); |
| 100 | + |
| 101 | + // create index in cluster |
| 102 | + Settings.Builder builder = Settings.builder().put(SETTING_REPLICATION_TYPE, ReplicationType.DOCUMENT); |
| 103 | + internalCluster().client() |
| 104 | + .admin() |
| 105 | + .indices() |
| 106 | + .prepareCreate(TEST_INDEX) |
| 107 | + .setSettings( |
| 108 | + builder.put("index.number_of_shards", 2) |
| 109 | + .put("index.number_of_replicas", 0) |
| 110 | + .put("index.routing.allocation.include._name", nonRemoteNodeName) |
| 111 | + ) |
| 112 | + .setWaitForActiveShards(ActiveShardCount.ALL) |
| 113 | + .execute() |
| 114 | + .actionGet(); |
| 115 | + |
| 116 | + // set mixed mode |
| 117 | + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); |
| 118 | + updateSettingsRequest.persistentSettings(Settings.builder().put(REMOTE_STORE_COMPATIBILITY_MODE_SETTING.getKey(), MIXED_MODE)); |
| 119 | + assertAcked(internalCluster().client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); |
| 120 | + |
| 121 | + ensureGreen(TEST_INDEX); |
| 122 | + |
| 123 | + // perform close action |
| 124 | + assertAcked(internalCluster().client().admin().indices().close(new CloseIndexRequest(TEST_INDEX)).actionGet()); |
| 125 | + |
| 126 | + // verify that index has been closed |
| 127 | + final ClusterState clusterState = client().admin().cluster().prepareState().get().getState(); |
| 128 | + |
| 129 | + final IndexMetadata indexMetadata = clusterState.metadata().indices().get(TEST_INDEX); |
| 130 | + assertEquals(IndexMetadata.State.CLOSE, indexMetadata.getState()); |
| 131 | + final Settings indexSettings = indexMetadata.getSettings(); |
| 132 | + assertTrue(indexSettings.hasValue(MetadataIndexStateService.VERIFIED_BEFORE_CLOSE_SETTING.getKey())); |
| 133 | + assertEquals(true, indexSettings.getAsBoolean(MetadataIndexStateService.VERIFIED_BEFORE_CLOSE_SETTING.getKey(), false)); |
| 134 | + assertNotNull(clusterState.routingTable().index(TEST_INDEX)); |
| 135 | + assertTrue(clusterState.blocks().hasIndexBlock(TEST_INDEX, MetadataIndexStateService.INDEX_CLOSED_BLOCK)); |
| 136 | + |
| 137 | + } |
| 138 | +} |
0 commit comments