Skip to content

Commit e397903

Browse files
authored
Fix flaky tests fromSegmentReplicationAllocationIT (opensearch-project#17429)
* Fix flaky tests in SegmentReplicationAllocationIT Signed-off-by: Lakshya Taragi <lakshya.taragi@gmail.com> * Remove extra logs Signed-off-by: Lakshya Taragi <lakshya.taragi@gmail.com> * Account for replicas as well Signed-off-by: Lakshya Taragi <lakshya.taragi@gmail.com> * Reduce upper limit on no. of indices Signed-off-by: Lakshya Taragi <lakshya.taragi@gmail.com> * Only verified changes Signed-off-by: Lakshya Taragi <lakshya.taragi@gmail.com> * Fix testSingleIndexShardAllocation Signed-off-by: Lakshya Taragi <lakshya.taragi@gmail.com> --------- Signed-off-by: Lakshya Taragi <lakshya.taragi@gmail.com>
1 parent 7e2d243 commit e397903

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

server/src/internalClusterTest/java/org/opensearch/indices/replication/SegmentReplicationAllocationIT.java

+20-9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.opensearch.test.junit.annotations.TestLogging;
2626

2727
import java.util.ArrayList;
28+
import java.util.Arrays;
2829
import java.util.List;
2930
import java.util.Map;
3031
import java.util.concurrent.TimeUnit;
@@ -169,14 +170,16 @@ public void testSingleIndexShardAllocation() throws Exception {
169170

170171
// Remove a node
171172
internalCluster().stopRandomNode(InternalTestCluster.nameFilter(nodeNames.get(0)));
172-
ensureGreen(TimeValue.timeValueSeconds(60));
173+
internalCluster().validateClusterFormed();
174+
ensureGreen(TimeValue.timeValueSeconds(100));
173175
state = client().admin().cluster().prepareState().execute().actionGet().getState();
174176
logger.info(ShardAllocations.printShardDistribution(state));
175177
verifyPerIndexPrimaryBalance();
176178

177179
// Add a new node
178180
internalCluster().startDataOnlyNode();
179-
ensureGreen(TimeValue.timeValueSeconds(60));
181+
internalCluster().validateClusterFormed();
182+
ensureGreen(TimeValue.timeValueSeconds(100));
180183
state = client().admin().cluster().prepareState().execute().actionGet().getState();
181184
logger.info(ShardAllocations.printShardDistribution(state));
182185
verifyPerIndexPrimaryBalance();
@@ -250,24 +253,32 @@ public void testAllocationAndRebalanceWithDisruption() throws Exception {
250253
internalCluster().startClusterManagerOnlyNode();
251254
final int maxReplicaCount = 2;
252255
final int maxShardCount = 2;
253-
// Create higher number of nodes than number of shards to reduce chances of SameShardAllocationDecider kicking-in
256+
final int numberOfIndices = randomIntBetween(1, 3);
257+
final int maxPossibleShards = numberOfIndices * maxShardCount * (1 + maxReplicaCount);
258+
259+
List<List<Integer>> shardAndReplicaCounts = new ArrayList<>();
260+
int shardCount, replicaCount, totalShards = 0;
261+
for (int i = 0; i < numberOfIndices; i++) {
262+
shardCount = randomIntBetween(1, maxShardCount);
263+
replicaCount = randomIntBetween(1, maxReplicaCount);
264+
shardAndReplicaCounts.add(Arrays.asList(shardCount, replicaCount));
265+
totalShards += shardCount * (1 + replicaCount);
266+
}
267+
// Create a strictly higher number of nodes than the number of shards to reduce chances of SameShardAllocationDecider kicking-in
254268
// and preventing primary relocations
255-
final int nodeCount = randomIntBetween(5, 10);
256-
final int numberOfIndices = randomIntBetween(1, 10);
269+
final int nodeCount = randomIntBetween(totalShards, maxPossibleShards) + 1;
257270
final float buffer = randomIntBetween(1, 4) * 0.10f;
258-
259271
logger.info("--> Creating {} nodes", nodeCount);
260272
final List<String> nodeNames = new ArrayList<>();
261273
for (int i = 0; i < nodeCount; i++) {
262274
nodeNames.add(internalCluster().startNode());
263275
}
264276
setAllocationRelocationStrategy(true, true, buffer);
265277

266-
int shardCount, replicaCount;
267278
ClusterState state;
268279
for (int i = 0; i < numberOfIndices; i++) {
269-
shardCount = randomIntBetween(1, maxShardCount);
270-
replicaCount = randomIntBetween(1, maxReplicaCount);
280+
shardCount = shardAndReplicaCounts.get(i).get(0);
281+
replicaCount = shardAndReplicaCounts.get(i).get(1);
271282
logger.info("--> Creating index test{} with primary {} and replica {}", i, shardCount, replicaCount);
272283
createIndex("test" + i, shardCount, replicaCount, i % 2 == 0);
273284
ensureGreen(TimeValue.timeValueSeconds(60));

0 commit comments

Comments
 (0)