|
34 | 34 |
|
35 | 35 | import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
|
36 | 36 |
|
| 37 | +import org.opensearch.action.admin.cluster.health.ClusterHealthResponse; |
37 | 38 | import org.opensearch.action.admin.cluster.node.stats.NodeStats;
|
38 | 39 | import org.opensearch.action.admin.cluster.node.stats.NodesStatsResponse;
|
39 | 40 | import org.opensearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest;
|
|
43 | 44 | import org.opensearch.action.search.SearchResponse;
|
44 | 45 | import org.opensearch.action.search.SearchType;
|
45 | 46 | import org.opensearch.client.Client;
|
| 47 | +import org.opensearch.cluster.ClusterState; |
46 | 48 | import org.opensearch.cluster.metadata.IndexMetadata;
|
| 49 | +import org.opensearch.cluster.routing.allocation.command.MoveAllocationCommand; |
| 50 | +import org.opensearch.cluster.routing.allocation.decider.EnableAllocationDecider; |
47 | 51 | import org.opensearch.common.settings.Settings;
|
48 | 52 | import org.opensearch.common.time.DateFormatter;
|
49 | 53 | import org.opensearch.common.unit.TimeValue;
|
50 | 54 | import org.opensearch.common.util.FeatureFlags;
|
| 55 | +import org.opensearch.core.index.Index; |
| 56 | +import org.opensearch.core.index.shard.ShardId; |
| 57 | +import org.opensearch.env.NodeEnvironment; |
51 | 58 | import org.opensearch.index.IndexNotFoundException;
|
52 | 59 | import org.opensearch.index.cache.request.RequestCacheStats;
|
53 | 60 | import org.opensearch.index.query.QueryBuilders;
|
|
58 | 65 | import org.opensearch.test.ParameterizedOpenSearchIntegTestCase;
|
59 | 66 | import org.opensearch.test.hamcrest.OpenSearchAssertions;
|
60 | 67 |
|
| 68 | +import java.nio.file.Files; |
| 69 | +import java.nio.file.Path; |
61 | 70 | import java.time.ZoneId;
|
62 | 71 | import java.time.ZoneOffset;
|
63 | 72 | import java.time.ZonedDateTime;
|
|
69 | 78 |
|
70 | 79 | import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS;
|
71 | 80 | import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS;
|
| 81 | +import static org.opensearch.cluster.routing.allocation.decider.EnableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING; |
72 | 82 | import static org.opensearch.indices.IndicesRequestCache.INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING;
|
73 | 83 | import static org.opensearch.indices.IndicesService.INDICES_CACHE_CLEANUP_INTERVAL_SETTING_KEY;
|
74 | 84 | import static org.opensearch.search.SearchService.CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING;
|
@@ -1240,6 +1250,101 @@ public void testStaleKeysCleanupWithMultipleIndices() throws Exception {
|
1240 | 1250 | }, cacheCleanIntervalInMillis * 2, TimeUnit.MILLISECONDS);
|
1241 | 1251 | }
|
1242 | 1252 |
|
| 1253 | + public void testDeleteAndCreateSameIndexShardOnSameNode() throws Exception { |
| 1254 | + String node_1 = internalCluster().startNode(Settings.builder().build()); |
| 1255 | + Client client = client(node_1); |
| 1256 | + |
| 1257 | + logger.info("Starting a node in the cluster"); |
| 1258 | + |
| 1259 | + assertThat(cluster().size(), equalTo(1)); |
| 1260 | + ClusterHealthResponse healthResponse = client().admin().cluster().prepareHealth().setWaitForNodes("1").execute().actionGet(); |
| 1261 | + assertThat(healthResponse.isTimedOut(), equalTo(false)); |
| 1262 | + |
| 1263 | + String indexName = "test"; |
| 1264 | + |
| 1265 | + logger.info("Creating an index: {} with 2 shards", indexName); |
| 1266 | + createIndex( |
| 1267 | + indexName, |
| 1268 | + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).build() |
| 1269 | + ); |
| 1270 | + |
| 1271 | + ensureGreen(indexName); |
| 1272 | + |
| 1273 | + logger.info("Writing few docs and searching those which will cache items in RequestCache"); |
| 1274 | + indexRandom(true, client.prepareIndex(indexName).setSource("k", "hello")); |
| 1275 | + indexRandom(true, client.prepareIndex(indexName).setSource("y", "hello again")); |
| 1276 | + SearchResponse resp = client.prepareSearch(indexName).setRequestCache(true).setQuery(QueryBuilders.termQuery("k", "hello")).get(); |
| 1277 | + assertSearchResponse(resp); |
| 1278 | + resp = client.prepareSearch(indexName).setRequestCache(true).setQuery(QueryBuilders.termQuery("y", "hello")).get(); |
| 1279 | + |
| 1280 | + RequestCacheStats stats = getNodeCacheStats(client); |
| 1281 | + assertTrue(stats.getMemorySizeInBytes() > 0); |
| 1282 | + |
| 1283 | + logger.info("Disabling allocation"); |
| 1284 | + Settings newSettings = Settings.builder() |
| 1285 | + .put(CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), EnableAllocationDecider.Allocation.NONE.name()) |
| 1286 | + .build(); |
| 1287 | + client().admin().cluster().prepareUpdateSettings().setTransientSettings(newSettings).execute().actionGet(); |
| 1288 | + |
| 1289 | + logger.info("Starting a second node"); |
| 1290 | + String node_2 = internalCluster().startDataOnlyNode(Settings.builder().build()); |
| 1291 | + assertThat(cluster().size(), equalTo(2)); |
| 1292 | + healthResponse = client().admin().cluster().prepareHealth().setWaitForNodes("2").execute().actionGet(); |
| 1293 | + assertThat(healthResponse.isTimedOut(), equalTo(false)); |
| 1294 | + |
| 1295 | + logger.info("Moving the shard:{} from node:{} to node:{}", indexName + "#0", node_1, node_2); |
| 1296 | + MoveAllocationCommand cmd = new MoveAllocationCommand(indexName, 0, node_1, node_2); |
| 1297 | + internalCluster().client().admin().cluster().prepareReroute().add(cmd).get(); |
| 1298 | + ClusterHealthResponse clusterHealth = client().admin() |
| 1299 | + .cluster() |
| 1300 | + .prepareHealth() |
| 1301 | + .setWaitForNoRelocatingShards(true) |
| 1302 | + .setWaitForNoInitializingShards(true) |
| 1303 | + .get(); |
| 1304 | + assertThat(clusterHealth.isTimedOut(), equalTo(false)); |
| 1305 | + |
| 1306 | + ClusterState state = client().admin().cluster().prepareState().get().getState(); |
| 1307 | + final Index index = state.metadata().index(indexName).getIndex(); |
| 1308 | + |
| 1309 | + assertBusy(() -> { |
| 1310 | + assertThat(Files.exists(shardDirectory(node_1, index, 0)), equalTo(false)); |
| 1311 | + assertThat(Files.exists(shardDirectory(node_2, index, 0)), equalTo(true)); |
| 1312 | + }); |
| 1313 | + |
| 1314 | + logger.info("Moving the shard: {} again from node:{} to node:{}", indexName + "#0", node_2, node_1); |
| 1315 | + cmd = new MoveAllocationCommand(indexName, 0, node_2, node_1); |
| 1316 | + internalCluster().client().admin().cluster().prepareReroute().add(cmd).get(); |
| 1317 | + clusterHealth = client().admin() |
| 1318 | + .cluster() |
| 1319 | + .prepareHealth() |
| 1320 | + .setWaitForNoRelocatingShards(true) |
| 1321 | + .setWaitForNoInitializingShards(true) |
| 1322 | + .get(); |
| 1323 | + assertThat(clusterHealth.isTimedOut(), equalTo(false)); |
| 1324 | + assertThat(Files.exists(shardDirectory(node_1, index, 0)), equalTo(true)); |
| 1325 | + |
| 1326 | + assertBusy(() -> { |
| 1327 | + assertThat(Files.exists(shardDirectory(node_1, index, 0)), equalTo(true)); |
| 1328 | + assertThat(Files.exists(shardDirectory(node_2, index, 0)), equalTo(false)); |
| 1329 | + }); |
| 1330 | + |
| 1331 | + logger.info("Clearing the cache for index:{}. And verify the request stats doesn't go negative", indexName); |
| 1332 | + ClearIndicesCacheRequest clearIndicesCacheRequest = new ClearIndicesCacheRequest(indexName); |
| 1333 | + client.admin().indices().clearCache(clearIndicesCacheRequest).actionGet(); |
| 1334 | + |
| 1335 | + stats = getNodeCacheStats(client(node_1)); |
| 1336 | + assertTrue(stats.getMemorySizeInBytes() == 0); |
| 1337 | + stats = getNodeCacheStats(client(node_2)); |
| 1338 | + assertTrue(stats.getMemorySizeInBytes() == 0); |
| 1339 | + } |
| 1340 | + |
| 1341 | + private Path shardDirectory(String server, Index index, int shard) { |
| 1342 | + NodeEnvironment env = internalCluster().getInstance(NodeEnvironment.class, server); |
| 1343 | + final Path[] paths = env.availableShardPaths(new ShardId(index, shard)); |
| 1344 | + assert paths.length == 1; |
| 1345 | + return paths[0]; |
| 1346 | + } |
| 1347 | + |
1243 | 1348 | private void setupIndex(Client client, String index) throws Exception {
|
1244 | 1349 | assertAcked(
|
1245 | 1350 | client.admin()
|
|
0 commit comments