|
| 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.shrink.ResizeType; |
| 13 | +import org.opensearch.action.support.ActiveShardCount; |
| 14 | +import org.opensearch.client.Client; |
| 15 | +import org.opensearch.common.settings.Settings; |
| 16 | +import org.opensearch.indices.replication.common.ReplicationType; |
| 17 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_REPLICATION_TYPE; |
| 22 | +import static org.opensearch.node.remotestore.RemoteStoreNodeService.MIGRATION_DIRECTION_SETTING; |
| 23 | +import static org.opensearch.node.remotestore.RemoteStoreNodeService.REMOTE_STORE_COMPATIBILITY_MODE_SETTING; |
| 24 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; |
| 25 | + |
| 26 | +@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.TEST, numDataNodes = 0, autoManageMasterNodes = false) |
| 27 | +public class ResizeIndexMigrationTestCase extends MigrationBaseTestCase { |
| 28 | + private static final String TEST_INDEX = "test_index"; |
| 29 | + private final static String REMOTE_STORE_DIRECTION = "remote_store"; |
| 30 | + private final static String DOC_REP_DIRECTION = "docrep"; |
| 31 | + private final static String NONE_DIRECTION = "none"; |
| 32 | + private final static String STRICT_MODE = "strict"; |
| 33 | + private final static String MIXED_MODE = "mixed"; |
| 34 | + |
| 35 | + /* |
| 36 | + * This test will verify the resize request failure, when cluster mode is mixed |
| 37 | + * and index is on DocRep node, and migration to remote store is in progress. |
| 38 | + * */ |
| 39 | + public void testFailResizeIndexWhileDocRepToRemoteStoreMigration() throws Exception { |
| 40 | + |
| 41 | + internalCluster().setBootstrapClusterManagerNodeIndex(0); |
| 42 | + List<String> cmNodes = internalCluster().startNodes(1); |
| 43 | + Client client = internalCluster().client(cmNodes.get(0)); |
| 44 | + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); |
| 45 | + updateSettingsRequest.persistentSettings(Settings.builder().put(REMOTE_STORE_COMPATIBILITY_MODE_SETTING.getKey(), MIXED_MODE)); |
| 46 | + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); |
| 47 | + |
| 48 | + // Adding a non remote and a remote node |
| 49 | + addRemote = false; |
| 50 | + String nonRemoteNodeName = internalCluster().startNode(); |
| 51 | + |
| 52 | + addRemote = true; |
| 53 | + String remoteNodeName = internalCluster().startNode(); |
| 54 | + |
| 55 | + logger.info("-->Create index on non-remote node and SETTING_REMOTE_STORE_ENABLED is false. Resize should not happen"); |
| 56 | + Settings.Builder builder = Settings.builder().put(SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT); |
| 57 | + client.admin() |
| 58 | + .indices() |
| 59 | + .prepareCreate(TEST_INDEX) |
| 60 | + .setSettings( |
| 61 | + builder.put("index.number_of_shards", 10) |
| 62 | + .put("index.number_of_replicas", 0) |
| 63 | + .put("index.routing.allocation.include._name", nonRemoteNodeName) |
| 64 | + .put("index.routing.allocation.exclude._name", remoteNodeName) |
| 65 | + ) |
| 66 | + .setWaitForActiveShards(ActiveShardCount.ALL) |
| 67 | + .execute() |
| 68 | + .actionGet(); |
| 69 | + |
| 70 | + updateSettingsRequest.persistentSettings(Settings.builder().put(MIGRATION_DIRECTION_SETTING.getKey(), REMOTE_STORE_DIRECTION)); |
| 71 | + assertAcked(client.admin().cluster().updateSettings(updateSettingsRequest).actionGet()); |
| 72 | + |
| 73 | + ResizeType resizeType; |
| 74 | + int resizeShardsNum; |
| 75 | + String cause; |
| 76 | + switch (randomIntBetween(0, 2)) { |
| 77 | + case 0: |
| 78 | + resizeType = ResizeType.SHRINK; |
| 79 | + resizeShardsNum = 5; |
| 80 | + cause = "shrink_index"; |
| 81 | + break; |
| 82 | + case 1: |
| 83 | + resizeType = ResizeType.SPLIT; |
| 84 | + resizeShardsNum = 20; |
| 85 | + cause = "split_index"; |
| 86 | + break; |
| 87 | + default: |
| 88 | + resizeType = ResizeType.CLONE; |
| 89 | + resizeShardsNum = 10; |
| 90 | + cause = "clone_index"; |
| 91 | + } |
| 92 | + |
| 93 | + client.admin() |
| 94 | + .indices() |
| 95 | + .prepareUpdateSettings(TEST_INDEX) |
| 96 | + .setSettings(Settings.builder().put("index.blocks.write", true)) |
| 97 | + .execute() |
| 98 | + .actionGet(); |
| 99 | + |
| 100 | + ensureGreen(TEST_INDEX); |
| 101 | + |
| 102 | + Settings.Builder resizeSettingsBuilder = Settings.builder() |
| 103 | + .put("index.number_of_replicas", 0) |
| 104 | + .put("index.number_of_shards", resizeShardsNum) |
| 105 | + .putNull("index.blocks.write"); |
| 106 | + |
| 107 | + IllegalStateException ex = expectThrows( |
| 108 | + IllegalStateException.class, |
| 109 | + () -> client().admin() |
| 110 | + .indices() |
| 111 | + .prepareResizeIndex(TEST_INDEX, "first_split") |
| 112 | + .setResizeType(resizeType) |
| 113 | + .setSettings(resizeSettingsBuilder.build()) |
| 114 | + .get() |
| 115 | + ); |
| 116 | + assertEquals( |
| 117 | + ex.getMessage(), |
| 118 | + "Index " + resizeType + " is not allowed as remote migration mode is mixed" + " and index is remote store disabled" |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + /* |
| 123 | + * This test will verify the resize request failure, when cluster mode is mixed |
| 124 | + * and index is on Remote Store node, and migration to DocRep node is in progress. |
| 125 | + * */ |
| 126 | + public void testFailResizeIndexWhileRemoteStoreToDocRepMigration() throws Exception { |
| 127 | + |
| 128 | + addRemote = true; |
| 129 | + internalCluster().setBootstrapClusterManagerNodeIndex(0); |
| 130 | + List<String> cmNodes = internalCluster().startNodes(1); |
| 131 | + Client client = internalCluster().client(cmNodes.get(0)); |
| 132 | + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); |
| 133 | + updateSettingsRequest.persistentSettings(Settings.builder().put(REMOTE_STORE_COMPATIBILITY_MODE_SETTING.getKey(), MIXED_MODE)); |
| 134 | + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); |
| 135 | + |
| 136 | + // Adding a non remote and a remote node |
| 137 | + String remoteNodeName = internalCluster().startNode(); |
| 138 | + |
| 139 | + addRemote = false; |
| 140 | + String nonRemoteNodeName = internalCluster().startNode(); |
| 141 | + |
| 142 | + logger.info("-->Create index on remote node and SETTING_REMOTE_STORE_ENABLED is true. Resize should not happen"); |
| 143 | + Settings.Builder builder = Settings.builder().put(SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT); |
| 144 | + client.admin() |
| 145 | + .indices() |
| 146 | + .prepareCreate(TEST_INDEX) |
| 147 | + .setSettings( |
| 148 | + builder.put("index.number_of_shards", 10) |
| 149 | + .put("index.number_of_replicas", 0) |
| 150 | + .put("index.routing.allocation.include._name", remoteNodeName) |
| 151 | + .put("index.routing.allocation.exclude._name", nonRemoteNodeName) |
| 152 | + ) |
| 153 | + .setWaitForActiveShards(ActiveShardCount.ALL) |
| 154 | + .execute() |
| 155 | + .actionGet(); |
| 156 | + |
| 157 | + updateSettingsRequest.persistentSettings(Settings.builder().put(MIGRATION_DIRECTION_SETTING.getKey(), DOC_REP_DIRECTION)); |
| 158 | + assertAcked(client.admin().cluster().updateSettings(updateSettingsRequest).actionGet()); |
| 159 | + |
| 160 | + ResizeType resizeType; |
| 161 | + int resizeShardsNum; |
| 162 | + String cause; |
| 163 | + switch (randomIntBetween(0, 2)) { |
| 164 | + case 0: |
| 165 | + resizeType = ResizeType.SHRINK; |
| 166 | + resizeShardsNum = 5; |
| 167 | + cause = "shrink_index"; |
| 168 | + break; |
| 169 | + case 1: |
| 170 | + resizeType = ResizeType.SPLIT; |
| 171 | + resizeShardsNum = 20; |
| 172 | + cause = "split_index"; |
| 173 | + break; |
| 174 | + default: |
| 175 | + resizeType = ResizeType.CLONE; |
| 176 | + resizeShardsNum = 10; |
| 177 | + cause = "clone_index"; |
| 178 | + } |
| 179 | + |
| 180 | + client.admin() |
| 181 | + .indices() |
| 182 | + .prepareUpdateSettings(TEST_INDEX) |
| 183 | + .setSettings(Settings.builder().put("index.blocks.write", true)) |
| 184 | + .execute() |
| 185 | + .actionGet(); |
| 186 | + |
| 187 | + ensureGreen(TEST_INDEX); |
| 188 | + |
| 189 | + Settings.Builder resizeSettingsBuilder = Settings.builder() |
| 190 | + .put("index.number_of_replicas", 0) |
| 191 | + .put("index.number_of_shards", resizeShardsNum) |
| 192 | + .putNull("index.blocks.write"); |
| 193 | + |
| 194 | + IllegalStateException ex = expectThrows( |
| 195 | + IllegalStateException.class, |
| 196 | + () -> client().admin() |
| 197 | + .indices() |
| 198 | + .prepareResizeIndex(TEST_INDEX, "first_split") |
| 199 | + .setResizeType(resizeType) |
| 200 | + .setSettings(resizeSettingsBuilder.build()) |
| 201 | + .get() |
| 202 | + ); |
| 203 | + assertEquals( |
| 204 | + ex.getMessage(), |
| 205 | + "Index " + resizeType + " is not allowed as remote migration mode is mixed" + " and index is remote store enabled" |
| 206 | + ); |
| 207 | + } |
| 208 | +} |
0 commit comments