From 4625fc9fceb3c44e2887e5d3b1f1031f322c7e62 Mon Sep 17 00:00:00 2001 From: Bukhtawar Khan Date: Sun, 4 Aug 2024 14:05:46 +0530 Subject: [PATCH] Commented out wiring and adding tests Signed-off-by: Bukhtawar Khan --- .../routing/RoutingTableDiffTests.java | 60 +++++++++++++++++ .../cluster/routing/RoutingTableTests.java | 67 ++++--------------- 2 files changed, 72 insertions(+), 55 deletions(-) create mode 100644 server/src/test/java/org/opensearch/cluster/routing/RoutingTableDiffTests.java diff --git a/server/src/test/java/org/opensearch/cluster/routing/RoutingTableDiffTests.java b/server/src/test/java/org/opensearch/cluster/routing/RoutingTableDiffTests.java new file mode 100644 index 0000000000000..6187a2a56f5cf --- /dev/null +++ b/server/src/test/java/org/opensearch/cluster/routing/RoutingTableDiffTests.java @@ -0,0 +1,60 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.cluster.routing; + +import org.opensearch.cluster.ClusterState; +import org.opensearch.cluster.Diff; + +import static org.hamcrest.Matchers.is; + +public class RoutingTableDiffTests extends RoutingTableTests { + + public void testRoutingTableShardsWithState() { + assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.UNASSIGNED).size(), is(this.totalNumberOfShards)); + + initPrimaries(); + assertThat( + clusterState.routingTable().shardsWithState(ShardRoutingState.UNASSIGNED).size(), + is(this.totalNumberOfShards - 2 * this.numberOfShards) + ); + assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.INITIALIZING).size(), is(2 * this.numberOfShards)); + + startInitializingShards(TEST_INDEX_1); + assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.STARTED).size(), is(this.numberOfShards)); + int initializingExpected = this.numberOfShards + this.numberOfShards * this.numberOfReplicas; + assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.INITIALIZING).size(), is(initializingExpected)); + assertThat( + clusterState.routingTable().shardsWithState(ShardRoutingState.UNASSIGNED).size(), + is(this.totalNumberOfShards - initializingExpected - this.numberOfShards) + ); + + startInitializingShards(TEST_INDEX_2); + assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.STARTED).size(), is(2 * this.numberOfShards)); + initializingExpected = 2 * this.numberOfShards * this.numberOfReplicas; + assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.INITIALIZING).size(), is(initializingExpected)); + assertThat( + clusterState.routingTable().shardsWithState(ShardRoutingState.UNASSIGNED).size(), + is(this.totalNumberOfShards - initializingExpected - 2 * this.numberOfShards) + ); + ClusterState oldClusterState = clusterState; + // now start all replicas too + //startInitializingShards(TEST_INDEX_1); + clusterState = startRandomInitializingShard(clusterState, ALLOCATION_SERVICE); + //startInitializingShards(TEST_INDEX_2); + //assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.STARTED).size(), is(this.totalNumberOfShards)); + Diff diff = clusterState.routingTable().diff(oldClusterState.getRoutingTable()); + Diff incrementalDiff = clusterState.routingTable().incrementalDiff(oldClusterState.getRoutingTable()); + RoutingTable newRoutingTable = incrementalDiff.apply(oldClusterState.getRoutingTable()); + for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) { + assertEquals(clusterState.routingTable().version(), newRoutingTable.version()); + assertEquals(indexRoutingTable, newRoutingTable.index(indexRoutingTable.getIndex())); + } + } + +} diff --git a/server/src/test/java/org/opensearch/cluster/routing/RoutingTableTests.java b/server/src/test/java/org/opensearch/cluster/routing/RoutingTableTests.java index 8082423f8222e..756644f8a3eb7 100644 --- a/server/src/test/java/org/opensearch/cluster/routing/RoutingTableTests.java +++ b/server/src/test/java/org/opensearch/cluster/routing/RoutingTableTests.java @@ -74,15 +74,15 @@ public class RoutingTableTests extends OpenSearchAllocationTestCase { - private static final String TEST_INDEX_1 = "test1"; - private static final String TEST_INDEX_2 = "test2"; - private RoutingTable emptyRoutingTable; - private int numberOfShards; - private int numberOfReplicas; - private int shardsPerIndex; - private int totalNumberOfShards; - private static final Settings DEFAULT_SETTINGS = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build(); - private final AllocationService ALLOCATION_SERVICE = createAllocationService( + protected static final String TEST_INDEX_1 = "test1"; + protected static final String TEST_INDEX_2 = "test2"; + protected RoutingTable emptyRoutingTable; + protected int numberOfShards; + protected int numberOfReplicas; + protected int shardsPerIndex; + protected int totalNumberOfShards; + protected static final Settings DEFAULT_SETTINGS = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build(); + protected final AllocationService ALLOCATION_SERVICE = createAllocationService( Settings.builder() .put("cluster.routing.allocation.node_concurrent_recoveries", Integer.MAX_VALUE) // don't limit recoveries .put("cluster.routing.allocation.node_initial_primaries_recoveries", Integer.MAX_VALUE) @@ -92,7 +92,7 @@ public class RoutingTableTests extends OpenSearchAllocationTestCase { ) .build() ); - private ClusterState clusterState; + protected ClusterState clusterState; @Override @Before @@ -122,7 +122,7 @@ public void setUp() throws Exception { /** * puts primary shard indexRoutings into initializing state */ - private void initPrimaries() { + protected void initPrimaries() { logger.info("adding {} nodes and performing rerouting", this.numberOfReplicas + 1); Builder discoBuilder = DiscoveryNodes.builder(); for (int i = 0; i < this.numberOfReplicas + 1; i++) { @@ -134,7 +134,7 @@ private void initPrimaries() { this.clusterState = rerouteResult; } - private void startInitializingShards(String index) { + protected void startInitializingShards(String index) { logger.info("start primary shards for index {}", index); clusterState = startInitializingShardsAndReroute(ALLOCATION_SERVICE, clusterState, index); } @@ -608,49 +608,6 @@ private Map getIndexShardRoutingTableMap(Index return indexShardRoutingTableMap; } - public void testRoutingTableShardsWithState() { - assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.UNASSIGNED).size(), is(this.totalNumberOfShards)); - - initPrimaries(); - assertThat( - clusterState.routingTable().shardsWithState(ShardRoutingState.UNASSIGNED).size(), - is(this.totalNumberOfShards - 2 * this.numberOfShards) - ); - assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.INITIALIZING).size(), is(2 * this.numberOfShards)); - - startInitializingShards(TEST_INDEX_1); - assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.STARTED).size(), is(this.numberOfShards)); - int initializingExpected = this.numberOfShards + this.numberOfShards * this.numberOfReplicas; - assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.INITIALIZING).size(), is(initializingExpected)); - assertThat( - clusterState.routingTable().shardsWithState(ShardRoutingState.UNASSIGNED).size(), - is(this.totalNumberOfShards - initializingExpected - this.numberOfShards) - ); - - startInitializingShards(TEST_INDEX_2); - assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.STARTED).size(), is(2 * this.numberOfShards)); - initializingExpected = 2 * this.numberOfShards * this.numberOfReplicas; - assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.INITIALIZING).size(), is(initializingExpected)); - assertThat( - clusterState.routingTable().shardsWithState(ShardRoutingState.UNASSIGNED).size(), - is(this.totalNumberOfShards - initializingExpected - 2 * this.numberOfShards) - ); - ClusterState oldClusterState = clusterState; - // now start all replicas too - //startInitializingShards(TEST_INDEX_1); - clusterState = startRandomInitializingShard(clusterState, ALLOCATION_SERVICE); - //startInitializingShards(TEST_INDEX_2); - //assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.STARTED).size(), is(this.totalNumberOfShards)); - Diff diff = clusterState.routingTable().diff(oldClusterState.getRoutingTable()); - Diff incrementalDiff = clusterState.routingTable().incrementalDiff(oldClusterState.getRoutingTable()); - RoutingTable newRoutingTable = incrementalDiff.apply(oldClusterState.getRoutingTable()); - for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) { - assertEquals(clusterState.routingTable().version(), newRoutingTable.version()); - assertEquals(indexRoutingTable, newRoutingTable.index(indexRoutingTable.getIndex())); - } - System.out.println(diff); - } - public void testAddAsRemoteStoreRestoreAllUnassigned() { int numberOfReplicas = randomIntBetween(0, 5); final IndexMetadata indexMetadata = createIndexMetadata(TEST_INDEX_1).state(IndexMetadata.State.OPEN)