3737import java .util .List ;
3838import java .util .Map ;
3939import java .util .Set ;
40+ import java .util .concurrent .TimeUnit ;
4041
4142import static org .opensearch .cluster .metadata .IndexMetadata .SETTING_NUMBER_OF_REPLICAS ;
4243import static org .opensearch .cluster .metadata .IndexMetadata .SETTING_NUMBER_OF_SHARDS ;
@@ -233,8 +234,16 @@ public void testIndexSpecificShardLimit() {
233234 }
234235 }
235236
236- public void testCombinedClusterAndIndexSpecificShardLimits () {
237+ public void testCombinedClusterAndIndexSpecificShardLimits () throws Exception {
237238 startTestNodes (3 );
239+
240+ // Keep disk thresholds from interfering (use typed Setting, not raw String)
241+ client ().admin ()
242+ .cluster ()
243+ .prepareUpdateSettings ()
244+ .setTransientSettings (Settings .builder ().put ("cluster.routing.allocation.disk.threshold_enabled" , false ).build ())
245+ .get ();
246+
238247 // Set the cluster-wide shard limit to 6
239248 updateClusterSetting (getShardsPerNodeKey (false ), 6 );
240249
@@ -247,6 +256,24 @@ public void testCombinedClusterAndIndexSpecificShardLimits() {
247256 .build ();
248257 createIndex ("test1" , indexSettingsWithLimit );
249258
259+ // Ensure test1 primaries are placed before adding other indices (prevents starvation)
260+ assertBusy (() -> {
261+ ClusterState s = client ().admin ().cluster ().prepareState ().get ().getState ();
262+ int primariesStarted = 0 , unassigned = 0 ;
263+ for (IndexRoutingTable irt : s .getRoutingTable ()) {
264+ if (irt .getIndex ().getName ().equals ("test1" )) {
265+ for (IndexShardRoutingTable isrt : irt ) {
266+ for (ShardRouting sr : isrt ) {
267+ if (sr .primary () && sr .started ()) primariesStarted ++;
268+ if (sr .unassigned ()) unassigned ++;
269+ }
270+ }
271+ }
272+ }
273+ assertEquals (3 , primariesStarted ); // 3 primaries started
274+ assertEquals (3 , unassigned ); // 3 unassigned (the replicas)
275+ });
276+
250277 // Create the second index with 4 shards and 1 replica
251278 createIndex (
252279 "test2" ,
@@ -309,12 +336,11 @@ public void testCombinedClusterAndIndexSpecificShardLimits() {
309336 // Check shard distribution across nodes
310337 List <Integer > shardCounts = new ArrayList <>(nodeShardCounts .values ());
311338 Collections .sort (shardCounts , Collections .reverseOrder ());
339+
312340 assertEquals ("Two nodes should have 6 shards" , 6 , shardCounts .get (0 ).intValue ());
313341 assertEquals ("Two nodes should have 6 shards" , 6 , shardCounts .get (1 ).intValue ());
314342 assertEquals ("One node should have 5 shards" , 5 , shardCounts .get (2 ).intValue ());
315-
316- // Check that all nodes have only one shard of the first index
317- });
343+ }, 30 , TimeUnit .SECONDS );
318344 } catch (Exception e ) {
319345 throw new RuntimeException (e );
320346 }
0 commit comments