Skip to content

Commit

Permalink
Commented out wiring and adding tests
Browse files Browse the repository at this point in the history
Signed-off-by: Bukhtawar Khan <[email protected]>
  • Loading branch information
Bukhtawar committed Aug 4, 2024
1 parent dd25e86 commit 4625fc9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -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<RoutingTable> diff = clusterState.routingTable().diff(oldClusterState.getRoutingTable());
Diff<RoutingTable> 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()));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -92,7 +92,7 @@ public class RoutingTableTests extends OpenSearchAllocationTestCase {
)
.build()
);
private ClusterState clusterState;
protected ClusterState clusterState;

@Override
@Before
Expand Down Expand Up @@ -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++) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -608,49 +608,6 @@ private Map<ShardId, IndexShardRoutingTable> 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<RoutingTable> diff = clusterState.routingTable().diff(oldClusterState.getRoutingTable());
Diff<RoutingTable> 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)
Expand Down

0 comments on commit 4625fc9

Please sign in to comment.