Skip to content

Commit ba64f40

Browse files
authored
Refactor Allocation Decider Test (#138710)
* Refactor Allocation Decider Test
1 parent 6932053 commit ba64f40

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

server/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/IndexBalanceAllocationDeciderTests.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@
5252

5353
public class IndexBalanceAllocationDeciderTests extends ESAllocationTestCase {
5454

55-
public static final String INCLUDE_DISCOVERY_NODE_FILTERS = "include.discovery.node.filters";
56-
public static final String ALLOW_EXCESS_SHARDS = "allow.excess.shards";
57-
5855
private DiscoveryNode indexNodeOne;
5956
private DiscoveryNode indexNodeTwo;
6057
private DiscoveryNode searchNodeOne;
@@ -75,25 +72,19 @@ public class IndexBalanceAllocationDeciderTests extends ESAllocationTestCase {
7572
private IndexMetadata indexMetadata;
7673
private RoutingAllocation routingAllocation;
7774
private IndexBalanceAllocationDecider indexBalanceAllocationDecider;
78-
private int excessShards;
7975
private ShardRouting indexTierShardRouting;
8076
private ShardRouting searchTierShardRouting;
8177
private List<RoutingNode> indexTier;
8278
private List<RoutingNode> searchIier;
8379

8480
private void setup(Settings settings) {
85-
boolean hasDiscoveryNodeFilters = settings.getAsBoolean(INCLUDE_DISCOVERY_NODE_FILTERS, true);
86-
boolean allowExcessShards = settings.getAsBoolean(ALLOW_EXCESS_SHARDS, true);
87-
8881
final String indexName = "IndexBalanceAllocationDeciderIndex";
8982
final Map<DiscoveryNode, List<ShardRouting>> nodeToShardRoutings = new HashMap<>();
9083

91-
excessShards = allowExcessShards ? randomIntBetween(1, 5) : 0;
92-
9384
Settings.Builder builder = Settings.builder()
85+
.put(settings)
9486
.put("stateless.enabled", "true")
95-
.put(IndexBalanceConstraintSettings.INDEX_BALANCE_DECIDER_ENABLED_SETTING.getKey(), "true")
96-
.put(IndexBalanceConstraintSettings.INDEX_BALANCE_DECIDER_EXCESS_SHARDS.getKey(), excessShards);
87+
.put(IndexBalanceConstraintSettings.INDEX_BALANCE_DECIDER_ENABLED_SETTING.getKey(), "true");
9788

9889
numberOfPrimaryShards = randomIntBetween(2, 10) * 2;
9990
replicationFactor = 2;
@@ -108,18 +99,6 @@ private void setup(Settings settings) {
10899
.build();
109100
allNodes = List.of(indexNodeOne, indexNodeTwo, searchNodeOne, searchNodeTwo, masterNode, machineLearningNode);
110101

111-
if (hasDiscoveryNodeFilters) {
112-
String setting = randomFrom(
113-
CLUSTER_ROUTING_REQUIRE_GROUP_PREFIX,
114-
CLUSTER_ROUTING_INCLUDE_GROUP_PREFIX,
115-
CLUSTER_ROUTING_EXCLUDE_GROUP_PREFIX
116-
);
117-
String attribute = randomFrom("_value", "name");
118-
String name = randomFrom("indexNodeOne", "indexNodeTwo", "searchNodeOne", "searchNodeTwo");
119-
String ip = randomFrom("192.168.0.1", "192.168.0.2", "192.168.7.1", "10.17.0.1");
120-
builder.put(setting + "." + attribute, attribute.equals("name") ? name : ip);
121-
}
122-
123102
DiscoveryNodes.Builder discoveryNodeBuilder = DiscoveryNodes.builder();
124103
for (DiscoveryNode node : allNodes) {
125104
discoveryNodeBuilder.add(node);
@@ -232,8 +211,9 @@ private void setup(Settings settings) {
232211
}
233212

234213
public void testCanAllocateUnderThresholdWithExcessShards() {
235-
Settings testSettings = Settings.builder().put(INCLUDE_DISCOVERY_NODE_FILTERS, false).put(ALLOW_EXCESS_SHARDS, true).build();
236-
setup(testSettings);
214+
Settings settings = allowExcessShards(Settings.EMPTY);
215+
setup(settings);
216+
237217
ShardRouting newIndexShardRouting = TestShardRouting.newShardRouting(
238218
new ShardId("newIndex", "uuid", 1),
239219
indexNodeTwo.getId(),
@@ -299,8 +279,7 @@ private void verifyCanAllocate() {
299279
}
300280

301281
public void testCanAllocateExceedThreshold() {
302-
Settings testSettings = Settings.builder().put(INCLUDE_DISCOVERY_NODE_FILTERS, false).put(ALLOW_EXCESS_SHARDS, false).build();
303-
setup(testSettings);
282+
setup(Settings.EMPTY);
304283

305284
int ideal = numberOfPrimaryShards / 2;
306285
int current = numberOfPrimaryShards / 2;
@@ -337,11 +316,11 @@ public void testCanAllocateExceedThreshold() {
337316
}
338317

339318
public void testCanAllocateHasDiscoveryNodeFilters() {
340-
Settings testSettings = Settings.builder()
341-
.put(INCLUDE_DISCOVERY_NODE_FILTERS, true)
342-
.put(ALLOW_EXCESS_SHARDS, randomBoolean())
343-
.build();
344-
setup(testSettings);
319+
Settings settings = addRandomFilterSetting(Settings.EMPTY);
320+
if (randomBoolean()) {
321+
settings = allowExcessShards(settings);
322+
}
323+
setup(settings);
345324

346325
for (RoutingNode routingNode : indexTier) {
347326
assertDecisionMatches(
@@ -362,4 +341,25 @@ public void testCanAllocateHasDiscoveryNodeFilters() {
362341
}
363342
}
364343

344+
public Settings addRandomFilterSetting(Settings settings) {
345+
String setting = randomFrom(
346+
CLUSTER_ROUTING_REQUIRE_GROUP_PREFIX,
347+
CLUSTER_ROUTING_INCLUDE_GROUP_PREFIX,
348+
CLUSTER_ROUTING_EXCLUDE_GROUP_PREFIX
349+
);
350+
String attribute = randomFrom("_value", "name");
351+
String name = randomFrom("indexNodeOne", "indexNodeTwo", "searchNodeOne", "searchNodeTwo");
352+
String ip = randomFrom("192.168.0.1", "192.168.0.2", "192.168.7.1", "10.17.0.1");
353+
return Settings.builder().put(settings).put(setting + "." + attribute, attribute.equals("name") ? name : ip).build();
354+
}
355+
356+
public Settings allowExcessShards(Settings settings) {
357+
int excessShards = randomIntBetween(1, 5);
358+
359+
return Settings.builder()
360+
.put(settings)
361+
.put(IndexBalanceConstraintSettings.INDEX_BALANCE_DECIDER_EXCESS_SHARDS.getKey(), excessShards)
362+
.build();
363+
}
364+
365365
}

0 commit comments

Comments
 (0)