|
42 | 42 | import java.util.HashMap; |
43 | 43 | import java.util.List; |
44 | 44 | import java.util.Map; |
| 45 | +import java.util.function.Supplier; |
45 | 46 |
|
| 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; |
46 | 50 | import static org.elasticsearch.cluster.metadata.IndexMetadata.SETTING_CREATION_DATE; |
47 | 51 | import static org.elasticsearch.cluster.routing.TestShardRouting.shardRoutingBuilder; |
48 | 52 | import static org.elasticsearch.cluster.routing.allocation.decider.FilterAllocationDecider.CLUSTER_ROUTING_EXCLUDE_GROUP_PREFIX; |
@@ -77,12 +81,12 @@ public class IndexBalanceAllocationDeciderTests extends ESAllocationTestCase { |
77 | 81 | private List<RoutingNode> indexTier; |
78 | 82 | private List<RoutingNode> searchIier; |
79 | 83 |
|
80 | | - private void setup(Settings settings) { |
| 84 | + private void setup(Settings clusterSettings, Supplier<Settings> indexSettings) { |
81 | 85 | final String indexName = "IndexBalanceAllocationDeciderIndex"; |
82 | 86 | final Map<DiscoveryNode, List<ShardRouting>> nodeToShardRoutings = new HashMap<>(); |
83 | 87 |
|
84 | 88 | Settings.Builder builder = Settings.builder() |
85 | | - .put(settings) |
| 89 | + .put(clusterSettings) |
86 | 90 | .put("stateless.enabled", "true") |
87 | 91 | .put(IndexBalanceConstraintSettings.INDEX_BALANCE_DECIDER_ENABLED_SETTING.getKey(), "true"); |
88 | 92 |
|
@@ -111,10 +115,9 @@ private void setup(Settings settings) { |
111 | 115 |
|
112 | 116 | indexMetadata = IndexMetadata.builder(indexName) |
113 | 117 | .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() |
118 | 121 | ) |
119 | 122 | .timestampRange(IndexLongFieldRange.UNKNOWN) |
120 | 123 | .eventIngestedRange(IndexLongFieldRange.UNKNOWN) |
@@ -211,8 +214,8 @@ private void setup(Settings settings) { |
211 | 214 | } |
212 | 215 |
|
213 | 216 | public void testCanAllocateUnderThresholdWithExcessShards() { |
214 | | - Settings settings = allowExcessShards(Settings.EMPTY); |
215 | | - setup(settings); |
| 217 | + Settings clusterSettings = allowExcessShards(Settings.EMPTY); |
| 218 | + setup(clusterSettings, () -> Settings.EMPTY); |
216 | 219 |
|
217 | 220 | ShardRouting newIndexShardRouting = TestShardRouting.newShardRouting( |
218 | 221 | new ShardId("newIndex", "uuid", 1), |
@@ -279,7 +282,7 @@ private void verifyCanAllocate() { |
279 | 282 | } |
280 | 283 |
|
281 | 284 | public void testCanAllocateExceedThreshold() { |
282 | | - setup(Settings.EMPTY); |
| 285 | + setup(Settings.EMPTY, () -> Settings.EMPTY); |
283 | 286 |
|
284 | 287 | int ideal = numberOfPrimaryShards / 2; |
285 | 288 | int current = numberOfPrimaryShards / 2; |
@@ -316,11 +319,33 @@ public void testCanAllocateExceedThreshold() { |
316 | 319 | } |
317 | 320 |
|
318 | 321 | public void testCanAllocateHasDiscoveryNodeFilters() { |
319 | | - Settings settings = addRandomFilterSetting(Settings.EMPTY); |
| 322 | + Settings clusterSettings = addRandomFilterSetting(Settings.EMPTY); |
320 | 323 | if (randomBoolean()) { |
321 | | - settings = allowExcessShards(settings); |
| 324 | + clusterSettings = allowExcessShards(clusterSettings); |
| 325 | + } |
| 326 | + setup(clusterSettings, () -> Settings.EMPTY); |
| 327 | + |
| 328 | + for (RoutingNode routingNode : indexTier) { |
| 329 | + assertDecisionMatches( |
| 330 | + "Having DiscoveryNodeFilters disables this decider", |
| 331 | + indexBalanceAllocationDecider.canAllocate(indexTierShardRouting, routingNode, routingAllocation), |
| 332 | + Decision.Type.YES, |
| 333 | + "Decider is disabled." |
| 334 | + ); |
| 335 | + } |
| 336 | + |
| 337 | + for (RoutingNode routingNode : searchIier) { |
| 338 | + assertDecisionMatches( |
| 339 | + "Having DiscoveryNodeFilters disables this decider", |
| 340 | + indexBalanceAllocationDecider.canAllocate(searchTierShardRouting, routingNode, routingAllocation), |
| 341 | + Decision.Type.YES, |
| 342 | + "Decider is disabled." |
| 343 | + ); |
322 | 344 | } |
323 | | - setup(settings); |
| 345 | + } |
| 346 | + |
| 347 | + public void testCanAllocateHasIndexRoutingFilters() { |
| 348 | + setup(Settings.EMPTY, this::addRandomIndexRoutingFilters); |
324 | 349 |
|
325 | 350 | for (RoutingNode routingNode : indexTier) { |
326 | 351 | assertDecisionMatches( |
@@ -362,4 +387,29 @@ public Settings allowExcessShards(Settings settings) { |
362 | 387 | .build(); |
363 | 388 | } |
364 | 389 |
|
| 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 | + |
365 | 415 | } |
0 commit comments