|
45 | 45 | import org.opensearch.cluster.routing.allocation.decider.AwarenessAllocationDecider; |
46 | 46 | import org.opensearch.common.Priority; |
47 | 47 | import org.opensearch.common.settings.Settings; |
| 48 | +import org.opensearch.test.InternalTestCluster; |
48 | 49 | import org.opensearch.test.OpenSearchIntegTestCase; |
49 | 50 | import org.opensearch.test.OpenSearchIntegTestCase.ClusterScope; |
50 | 51 |
|
| 52 | +import java.util.ArrayList; |
51 | 53 | import java.util.Arrays; |
52 | 54 | import java.util.List; |
53 | 55 | import java.util.concurrent.TimeUnit; |
54 | 56 | import java.util.stream.Collectors; |
55 | 57 |
|
| 58 | +import static org.opensearch.cluster.routing.ShardRoutingState.STARTED; |
56 | 59 | import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; |
57 | 60 | import static org.hamcrest.Matchers.anyOf; |
58 | 61 | import static org.hamcrest.Matchers.empty; |
@@ -351,4 +354,140 @@ public void testAwarenessZonesIncrementalNodes() { |
351 | 354 | assertThat(counts.get(B_1), equalTo(2)); |
352 | 355 | assertThat(counts.get(noZoneNode), equalTo(2)); |
353 | 356 | } |
| 357 | + |
| 358 | + public void testThreeZoneOneReplicaWithForceZoneValueAndLoadAwareness() throws Exception { |
| 359 | + int nodeCountPerAZ = 5; |
| 360 | + int numOfShards = 30; |
| 361 | + int numOfReplica = 1; |
| 362 | + Settings commonSettings = Settings.builder() |
| 363 | + .put("cluster.routing.allocation.awareness.attributes", "zone") |
| 364 | + .put("cluster.routing.allocation.awareness.force.zone.values", "a,b,c") |
| 365 | + .put("cluster.routing.allocation.load_awareness.skew_factor", "0.0") |
| 366 | + .put("cluster.routing.allocation.load_awareness.provisioned_capacity", Integer.toString(nodeCountPerAZ * 3)) |
| 367 | + .build(); |
| 368 | + |
| 369 | + logger.info("--> starting 15 nodes on zones 'a' & 'b' & 'c'"); |
| 370 | + List<String> nodes_in_zone_a = internalCluster().startNodes( |
| 371 | + nodeCountPerAZ, |
| 372 | + Settings.builder().put(commonSettings).put("node.attr.zone", "a").build() |
| 373 | + ); |
| 374 | + List<String> nodes_in_zone_b = internalCluster().startNodes( |
| 375 | + nodeCountPerAZ, |
| 376 | + Settings.builder().put(commonSettings).put("node.attr.zone", "b").build() |
| 377 | + ); |
| 378 | + List<String> nodes_in_zone_c = internalCluster().startNodes( |
| 379 | + nodeCountPerAZ, |
| 380 | + Settings.builder().put(commonSettings).put("node.attr.zone", "c").build() |
| 381 | + ); |
| 382 | + |
| 383 | + // Creating index with 30 primary and 1 replica |
| 384 | + createIndex( |
| 385 | + "test-1", |
| 386 | + Settings.builder() |
| 387 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numOfShards) |
| 388 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numOfReplica) |
| 389 | + .build() |
| 390 | + ); |
| 391 | + |
| 392 | + ClusterHealthResponse health = client().admin() |
| 393 | + .cluster() |
| 394 | + .prepareHealth() |
| 395 | + .setIndices("test-1") |
| 396 | + .setWaitForEvents(Priority.LANGUID) |
| 397 | + .setWaitForGreenStatus() |
| 398 | + .setWaitForNodes(Integer.toString(nodeCountPerAZ * 3)) |
| 399 | + .setWaitForNoRelocatingShards(true) |
| 400 | + .setWaitForNoInitializingShards(true) |
| 401 | + .execute() |
| 402 | + .actionGet(); |
| 403 | + assertFalse(health.isTimedOut()); |
| 404 | + |
| 405 | + ClusterState clusterState = client().admin().cluster().prepareState().execute().actionGet().getState(); |
| 406 | + ObjectIntHashMap<String> counts = new ObjectIntHashMap<>(); |
| 407 | + |
| 408 | + for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) { |
| 409 | + for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) { |
| 410 | + for (ShardRouting shardRouting : indexShardRoutingTable) { |
| 411 | + counts.addTo(clusterState.nodes().get(shardRouting.currentNodeId()).getName(), 1); |
| 412 | + } |
| 413 | + } |
| 414 | + } |
| 415 | + |
| 416 | + assertThat(counts.size(), equalTo(nodeCountPerAZ * 3)); |
| 417 | + // All shards should be started |
| 418 | + assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(numOfShards * (numOfReplica + 1))); |
| 419 | + |
| 420 | + // stopping half nodes in zone a |
| 421 | + int nodesToStop = nodeCountPerAZ / 2; |
| 422 | + List<Settings> nodeDataPathSettings = new ArrayList<>(); |
| 423 | + for (int i = 0; i < nodesToStop; i++) { |
| 424 | + nodeDataPathSettings.add(internalCluster().dataPathSettings(nodes_in_zone_a.get(i))); |
| 425 | + internalCluster().stopRandomNode(InternalTestCluster.nameFilter(nodes_in_zone_a.get(i))); |
| 426 | + } |
| 427 | + |
| 428 | + client().admin().cluster().prepareReroute().setRetryFailed(true).get(); |
| 429 | + health = client().admin() |
| 430 | + .cluster() |
| 431 | + .prepareHealth() |
| 432 | + .setIndices("test-1") |
| 433 | + .setWaitForEvents(Priority.LANGUID) |
| 434 | + .setWaitForNodes(Integer.toString(nodeCountPerAZ * 3 - nodesToStop)) |
| 435 | + .setWaitForNoRelocatingShards(true) |
| 436 | + .setWaitForNoInitializingShards(true) |
| 437 | + .execute() |
| 438 | + .actionGet(); |
| 439 | + assertFalse(health.isTimedOut()); |
| 440 | + |
| 441 | + // Creating another index with 30 primary and 1 replica |
| 442 | + createIndex( |
| 443 | + "test-2", |
| 444 | + Settings.builder() |
| 445 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numOfShards) |
| 446 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numOfReplica) |
| 447 | + .build() |
| 448 | + ); |
| 449 | + |
| 450 | + health = client().admin() |
| 451 | + .cluster() |
| 452 | + .prepareHealth() |
| 453 | + .setIndices("test-1", "test-2") |
| 454 | + .setWaitForEvents(Priority.LANGUID) |
| 455 | + .setWaitForNodes(Integer.toString(nodeCountPerAZ * 3 - nodesToStop)) |
| 456 | + .setWaitForNoRelocatingShards(true) |
| 457 | + .setWaitForNoInitializingShards(true) |
| 458 | + .execute() |
| 459 | + .actionGet(); |
| 460 | + assertFalse(health.isTimedOut()); |
| 461 | + |
| 462 | + // Restarting the nodes back |
| 463 | + for (int i = 0; i < nodesToStop; i++) { |
| 464 | + internalCluster().startNode( |
| 465 | + Settings.builder() |
| 466 | + .put("node.name", nodes_in_zone_a.get(i)) |
| 467 | + .put(nodeDataPathSettings.get(i)) |
| 468 | + .put(commonSettings) |
| 469 | + .put("node.attr.zone", "a") |
| 470 | + .build() |
| 471 | + ); |
| 472 | + } |
| 473 | + client().admin().cluster().prepareReroute().setRetryFailed(true).get(); |
| 474 | + |
| 475 | + health = client().admin() |
| 476 | + .cluster() |
| 477 | + .prepareHealth() |
| 478 | + .setIndices("test-1", "test-2") |
| 479 | + .setWaitForEvents(Priority.LANGUID) |
| 480 | + .setWaitForNodes(Integer.toString(nodeCountPerAZ * 3)) |
| 481 | + .setWaitForGreenStatus() |
| 482 | + .setWaitForActiveShards(2 * numOfShards * (numOfReplica + 1)) |
| 483 | + .setWaitForNoRelocatingShards(true) |
| 484 | + .setWaitForNoInitializingShards(true) |
| 485 | + .execute() |
| 486 | + .actionGet(); |
| 487 | + clusterState = client().admin().cluster().prepareState().execute().actionGet().getState(); |
| 488 | + |
| 489 | + // All shards should be started now and cluster health should be green |
| 490 | + assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(2 * numOfShards * (numOfReplica + 1))); |
| 491 | + assertThat(health.isTimedOut(), equalTo(false)); |
| 492 | + } |
354 | 493 | } |
0 commit comments