Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
Signed-off-by: Poojita Raj <[email protected]>
  • Loading branch information
Poojita-Raj committed Aug 29, 2023
1 parent fb88eca commit b71276e
Showing 1 changed file with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.opensearch.cluster.routing.allocation.decider.ClusterRebalanceAllocationDecider;
import org.opensearch.common.settings.Settings;
import org.opensearch.indices.cluster.ClusterStateChanges;
import org.opensearch.indices.replication.common.ReplicationType;
import org.opensearch.test.VersionUtils;
import org.opensearch.threadpool.TestThreadPool;
import org.opensearch.threadpool.ThreadPool;
Expand All @@ -69,6 +70,7 @@

import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_REPLICAS;
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_NUMBER_OF_SHARDS;
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_REPLICATION_TYPE;
import static org.opensearch.cluster.routing.ShardRoutingState.INITIALIZING;
import static org.opensearch.cluster.routing.ShardRoutingState.STARTED;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -137,7 +139,15 @@ public void testSimpleFailedNodeTest() {
}
}

public void testRandomClusterPromotesOldestReplica() throws InterruptedException {
testRandomClusterPromotesReplica(true);
}

public void testRandomClusterPromotesNewestReplica() throws InterruptedException {
testRandomClusterPromotesReplica(false);
}

void testRandomClusterPromotesReplica(boolean isSegmentReplicationEnabled) throws InterruptedException {

ThreadPool threadPool = new TestThreadPool(getClass().getName());
ClusterStateChanges cluster = new ClusterStateChanges(xContentRegistry(), threadPool);
Expand All @@ -164,6 +174,9 @@ public void testRandomClusterPromotesNewestReplica() throws InterruptedException
Settings.Builder settingsBuilder = Settings.builder()
.put(SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 4))
.put(SETTING_NUMBER_OF_REPLICAS, randomIntBetween(2, 4));
if (isSegmentReplicationEnabled) {
settingsBuilder.put(SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT);
}
CreateIndexRequest request = new CreateIndexRequest(name, settingsBuilder.build()).waitForActiveShards(ActiveShardCount.NONE);
state = cluster.createIndex(state, request);
assertTrue(state.metadata().hasIndex(name));
Expand Down Expand Up @@ -206,13 +219,23 @@ public void testRandomClusterPromotesNewestReplica() throws InterruptedException
Version candidateVer = getNodeVersion(sr, compareState);
if (candidateVer != null) {
logger.info("--> candidate on {} node; shard routing: {}", candidateVer, sr);
assertTrue(
"candidate was not on the newest version, new primary is on "
+ newPrimaryVersion
+ " and there is a candidate on "
+ candidateVer,
candidateVer.onOrBefore(newPrimaryVersion)
);
if (isSegmentReplicationEnabled) {
assertTrue(
"candidate was not on the oldest version, new primary is on "
+ newPrimaryVersion
+ " and there is a candidate on "
+ candidateVer,
candidateVer.onOrAfter(newPrimaryVersion)
);
} else {
assertTrue(
"candidate was not on the newest version, new primary is on "
+ newPrimaryVersion
+ " and there is a candidate on "
+ candidateVer,
candidateVer.onOrBefore(newPrimaryVersion)
);
}
}
});
});
Expand Down

0 comments on commit b71276e

Please sign in to comment.