Skip to content

Commit ed3124b

Browse files
authored
Fix primary balance flaky test (#8366)
Signed-off-by: Kunal Kotwani <kkotwani@amazon.com>
1 parent 7b436fb commit ed3124b

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public void testGlobalPrimaryAllocation() throws Exception {
9696
* This test in general passes without primary shard balance as well due to nature of allocation algorithm which
9797
* assigns all primary shards first followed by replica copies.
9898
*/
99-
@AwaitsFix(bugUrl = "https://github.com/opensearch-project/OpenSearch/issues/7751")
10099
public void testPerIndexPrimaryAllocation() throws Exception {
101100
internalCluster().startClusterManagerOnlyNode();
102101
final int maxReplicaCount = 2;
@@ -234,7 +233,7 @@ private void verifyPerIndexPrimaryBalance() throws Exception {
234233
RoutingNodes nodes = currentState.getRoutingNodes();
235234
for (final Map.Entry<String, IndexRoutingTable> index : currentState.getRoutingTable().indicesRouting().entrySet()) {
236235
final int totalPrimaryShards = index.getValue().primaryShardsActive();
237-
final int avgPrimaryShardsPerNode = (int) Math.ceil(totalPrimaryShards * 1f / currentState.getRoutingNodes().size());
236+
final int avgPrimaryShardsPerNode = (int) Math.floor(totalPrimaryShards * 1f / currentState.getRoutingNodes().size());
238237
for (RoutingNode node : nodes) {
239238
final int primaryCount = node.shardsWithState(index.getKey(), STARTED)
240239
.stream()
@@ -250,7 +249,8 @@ private void verifyPerIndexPrimaryBalance() throws Exception {
250249
avgPrimaryShardsPerNode
251250
);
252251
}
253-
assertTrue(primaryCount <= avgPrimaryShardsPerNode);
252+
// Asserts value is within the variance threshold (-1/+1 of the average value).
253+
assertTrue(avgPrimaryShardsPerNode - 1 <= primaryCount && primaryCount <= avgPrimaryShardsPerNode + 1);
254254
}
255255
}
256256
}, 60, TimeUnit.SECONDS);

0 commit comments

Comments
 (0)