Skip to content

Commit

Permalink
Refactor 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 4625fc9 commit 4cc04c6
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,98 @@

package org.opensearch.cluster.routing;

import org.junit.Before;
import org.opensearch.Version;
import org.opensearch.cluster.ClusterState;
import org.opensearch.cluster.Diff;
import org.opensearch.cluster.OpenSearchAllocationTestCase;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.cluster.metadata.Metadata;
import org.opensearch.cluster.node.DiscoveryNodes;
import org.opensearch.cluster.routing.allocation.AllocationService;
import org.opensearch.cluster.routing.allocation.decider.ThrottlingAllocationDecider;
import org.opensearch.common.settings.Settings;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.equalTo;

public class RoutingTableDiffTests extends RoutingTableTests {
public class RoutingTableDiffTests extends OpenSearchAllocationTestCase {

public void testRoutingTableShardsWithState() {
assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.UNASSIGNED).size(), is(this.totalNumberOfShards));
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(
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)
.put(
ThrottlingAllocationDecider.CLUSTER_ROUTING_ALLOCATION_NODE_INITIAL_REPLICAS_RECOVERIES_SETTING.getKey(),
Integer.MAX_VALUE
)
.build()
);
private ClusterState clusterState;

@Override
@Before
public void setUp() throws Exception {
super.setUp();
this.numberOfShards = randomIntBetween(1, 5);
this.numberOfReplicas = randomIntBetween(1, 5);
this.shardsPerIndex = this.numberOfShards * (this.numberOfReplicas + 1);
this.totalNumberOfShards = this.shardsPerIndex * 2;
logger.info("Setup test with {} shards and {} replicas.", this.numberOfShards, this.numberOfReplicas);
this.emptyRoutingTable = new RoutingTable.Builder().build();
Metadata metadata = Metadata.builder().put(createIndexMetadata(TEST_INDEX_1)).put(createIndexMetadata(TEST_INDEX_2)).build();

RoutingTable testRoutingTable = new RoutingTable.Builder().add(
new IndexRoutingTable.Builder(metadata.index(TEST_INDEX_1).getIndex()).initializeAsNew(metadata.index(TEST_INDEX_1)).build()
)
.add(
new IndexRoutingTable.Builder(metadata.index(TEST_INDEX_2).getIndex()).initializeAsNew(metadata.index(TEST_INDEX_2)).build()
)
.build();
this.clusterState = ClusterState.builder(org.opensearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY))
.metadata(metadata)
.routingTable(testRoutingTable)
.build();
}

/**
* puts primary shard indexRoutings into initializing state
*/
private void initPrimaries() {
logger.info("adding {} nodes and performing rerouting", this.numberOfReplicas + 1);
DiscoveryNodes.Builder discoBuilder = DiscoveryNodes.builder();
for (int i = 0; i < this.numberOfReplicas + 1; i++) {
discoBuilder = discoBuilder.add(newNode("node" + i));
}
this.clusterState = ClusterState.builder(clusterState).nodes(discoBuilder).build();
ClusterState rerouteResult = ALLOCATION_SERVICE.reroute(clusterState, "reroute");
assertThat(rerouteResult, not(equalTo(this.clusterState)));
this.clusterState = rerouteResult;
}

private void startInitializingShards(String index) {
logger.info("start primary shards for index {}", index);
clusterState = startInitializingShardsAndReroute(ALLOCATION_SERVICE, clusterState, index);
}

private IndexMetadata.Builder createIndexMetadata(String indexName) {
return new IndexMetadata.Builder(indexName).settings(DEFAULT_SETTINGS)
.numberOfReplicas(this.numberOfReplicas)
.numberOfShards(this.numberOfShards);
}


public void testRoutingTableDiffsWithStartedState() {
assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.UNASSIGNED).size(), is(this.totalNumberOfShards));
initPrimaries();
assertThat(
clusterState.routingTable().shardsWithState(ShardRoutingState.UNASSIGNED).size(),
Expand All @@ -43,18 +125,22 @@ public void testRoutingTableShardsWithState() {
is(this.totalNumberOfShards - initializingExpected - 2 * this.numberOfShards)
);
ClusterState oldClusterState = clusterState;
// now start all replicas too
//startInitializingShards(TEST_INDEX_1);
// start a random replica to change a single shard routing
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());
assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.INITIALIZING).size(), is(initializingExpected - 1));
assertThat(clusterState.routingTable().shardsWithState(ShardRoutingState.STARTED).size(), is(2 * this.numberOfShards + 1));
Diff<RoutingTable> fullDiff = 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()));
}
RoutingTable newRoutingTableWithFullDiff = fullDiff.apply(oldClusterState.getRoutingTable());
for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) {
assertEquals(clusterState.routingTable().version(), newRoutingTableWithFullDiff.version());
assertEquals(indexRoutingTable, newRoutingTableWithFullDiff.index(indexRoutingTable.getIndex()));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import org.opensearch.Version;
import org.opensearch.cluster.ClusterState;
import org.opensearch.cluster.Diff;
import org.opensearch.cluster.OpenSearchAllocationTestCase;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.cluster.metadata.Metadata;
Expand Down Expand Up @@ -74,15 +73,15 @@

public class RoutingTableTests extends OpenSearchAllocationTestCase {

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(
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(
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 +91,7 @@ public class RoutingTableTests extends OpenSearchAllocationTestCase {
)
.build()
);
protected ClusterState clusterState;
private ClusterState clusterState;

@Override
@Before
Expand Down Expand Up @@ -122,7 +121,7 @@ public void setUp() throws Exception {
/**
* puts primary shard indexRoutings into initializing state
*/
protected void initPrimaries() {
private 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 +133,7 @@ protected void initPrimaries() {
this.clusterState = rerouteResult;
}

protected void startInitializingShards(String index) {
private void startInitializingShards(String index) {
logger.info("start primary shards for index {}", index);
clusterState = startInitializingShardsAndReroute(ALLOCATION_SERVICE, clusterState, index);
}
Expand Down

0 comments on commit 4cc04c6

Please sign in to comment.