4242import java .util .HashMap ;
4343import java .util .List ;
4444import java .util .Map ;
45+ import java .util .function .Supplier ;
4546
47+ import static org .elasticsearch .cluster .metadata .IndexMetadata .INDEX_ROUTING_EXCLUDE_GROUP_PREFIX ;
48+ import static org .elasticsearch .cluster .metadata .IndexMetadata .INDEX_ROUTING_INCLUDE_GROUP_PREFIX ;
49+ import static org .elasticsearch .cluster .metadata .IndexMetadata .INDEX_ROUTING_REQUIRE_GROUP_PREFIX ;
4650import static org .elasticsearch .cluster .metadata .IndexMetadata .SETTING_CREATION_DATE ;
4751import static org .elasticsearch .cluster .routing .TestShardRouting .shardRoutingBuilder ;
4852import static org .elasticsearch .cluster .routing .allocation .decider .FilterAllocationDecider .CLUSTER_ROUTING_EXCLUDE_GROUP_PREFIX ;
@@ -77,12 +81,12 @@ public class IndexBalanceAllocationDeciderTests extends ESAllocationTestCase {
7781 private List <RoutingNode > indexTier ;
7882 private List <RoutingNode > searchIier ;
7983
80- private void setup (Settings settings ) {
84+ private void setup (Settings clusterSettings , Supplier < Settings > indexSettings ) {
8185 final String indexName = "IndexBalanceAllocationDeciderIndex" ;
8286 final Map <DiscoveryNode , List <ShardRouting >> nodeToShardRoutings = new HashMap <>();
8387
8488 Settings .Builder builder = Settings .builder ()
85- .put (settings )
89+ .put (clusterSettings )
8690 .put ("stateless.enabled" , "true" )
8791 .put (IndexBalanceConstraintSettings .INDEX_BALANCE_DECIDER_ENABLED_SETTING .getKey (), "true" );
8892
@@ -111,10 +115,9 @@ private void setup(Settings settings) {
111115
112116 indexMetadata = IndexMetadata .builder (indexName )
113117 .settings (
114- indexSettings (IndexVersion .current (), numberOfPrimaryShards , replicationFactor ).put (
115- SETTING_CREATION_DATE ,
116- System .currentTimeMillis ()
117- ).build ()
118+ indexSettings (IndexVersion .current (), numberOfPrimaryShards , replicationFactor ).put (indexSettings .get ())
119+ .put (SETTING_CREATION_DATE , System .currentTimeMillis ())
120+ .build ()
118121 )
119122 .timestampRange (IndexLongFieldRange .UNKNOWN )
120123 .eventIngestedRange (IndexLongFieldRange .UNKNOWN )
@@ -211,8 +214,8 @@ private void setup(Settings settings) {
211214 }
212215
213216 public void testCanAllocateUnderThresholdWithExcessShards () {
214- Settings settings = allowExcessShards (Settings .EMPTY );
215- setup (settings );
217+ Settings clusterSettings = allowExcessShards (Settings .EMPTY );
218+ setup (clusterSettings , () -> Settings . EMPTY );
216219
217220 ShardRouting newIndexShardRouting = TestShardRouting .newShardRouting (
218221 new ShardId ("newIndex" , "uuid" , 1 ),
@@ -279,7 +282,7 @@ private void verifyCanAllocate() {
279282 }
280283
281284 public void testCanAllocateExceedThreshold () {
282- setup (Settings .EMPTY );
285+ setup (Settings .EMPTY , () -> Settings . EMPTY );
283286
284287 int ideal = numberOfPrimaryShards / 2 ;
285288 int current = numberOfPrimaryShards / 2 ;
@@ -316,11 +319,11 @@ public void testCanAllocateExceedThreshold() {
316319 }
317320
318321 public void testCanAllocateHasDiscoveryNodeFilters () {
319- Settings settings = addRandomFilterSetting (Settings .EMPTY );
322+ Settings clusterSettings = addRandomFilterSetting (Settings .EMPTY );
320323 if (randomBoolean ()) {
321- settings = allowExcessShards (settings );
324+ clusterSettings = allowExcessShards (clusterSettings );
322325 }
323- setup (settings );
326+ setup (clusterSettings , () -> Settings . EMPTY );
324327
325328 for (RoutingNode routingNode : indexTier ) {
326329 assertDecisionMatches (
@@ -341,6 +344,28 @@ public void testCanAllocateHasDiscoveryNodeFilters() {
341344 }
342345 }
343346
347+ public void testCanAllocateHasIndexRoutingFilters () {
348+ setup (Settings .EMPTY , this ::addRandomIndexRoutingFilters );
349+
350+ for (RoutingNode routingNode : indexTier ) {
351+ assertDecisionMatches (
352+ "Having DiscoveryNodeFilters disables this decider" ,
353+ indexBalanceAllocationDecider .canAllocate (indexTierShardRouting , routingNode , routingAllocation ),
354+ Decision .Type .YES ,
355+ "Decider is disabled for index level allocation filters."
356+ );
357+ }
358+
359+ for (RoutingNode routingNode : searchIier ) {
360+ assertDecisionMatches (
361+ "Having DiscoveryNodeFilters disables this decider" ,
362+ indexBalanceAllocationDecider .canAllocate (searchTierShardRouting , routingNode , routingAllocation ),
363+ Decision .Type .YES ,
364+ "Decider is disabled for index level allocation filters."
365+ );
366+ }
367+ }
368+
344369 public Settings addRandomFilterSetting (Settings settings ) {
345370 String setting = randomFrom (
346371 CLUSTER_ROUTING_REQUIRE_GROUP_PREFIX ,
@@ -362,4 +387,29 @@ public Settings allowExcessShards(Settings settings) {
362387 .build ();
363388 }
364389
390+ public Settings addRandomIndexRoutingFilters () {
391+ String setting = randomFrom (
392+ INDEX_ROUTING_REQUIRE_GROUP_PREFIX ,
393+ INDEX_ROUTING_INCLUDE_GROUP_PREFIX ,
394+ INDEX_ROUTING_EXCLUDE_GROUP_PREFIX
395+ );
396+ String attribute = randomFrom ("_ip" , "_host" , "_id" );
397+ String ip = randomFrom ("192.168.0.1" , "192.168.0.2" , "192.168.7.1" , "10.17.0.1" );
398+ String id = randomFrom (indexNodeOne .getId (), indexNodeTwo .getId (), searchNodeOne .getId (), searchNodeTwo .getId ());
399+ String hostName = randomFrom (
400+ indexNodeOne .getHostName (),
401+ indexNodeTwo .getHostName (),
402+ searchNodeOne .getHostName (),
403+ searchNodeTwo .getHostName ()
404+ );
405+
406+ String value = switch (attribute ) {
407+ case "_ip" -> ip ;
408+ case "_host" -> hostName ;
409+ case "_id" -> id ;
410+ default -> throw new IllegalStateException ("Unexpected value: " + attribute );
411+ };
412+ return Settings .builder ().put (setting + "." + attribute , value ).build ();
413+ }
414+
365415}
0 commit comments