Skip to content

Commit 3014d88

Browse files
modularize test set up
1 parent a0e61e0 commit 3014d88

File tree

1 file changed

+123
-50
lines changed

1 file changed

+123
-50
lines changed

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

Lines changed: 123 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,122 @@
3636

3737
public class WriteLoadConstraintDeciderTests extends ESAllocationTestCase {
3838

39-
public void testWriteLoadDecider() {
39+
/**
40+
* Test the write load decider behavior when disabled
41+
*/
42+
public void testWriteLoadDeciderDisabled() {
43+
String indexName = "test-index";
44+
var testHarness = createClusterStateAndRoutingAllocation(indexName);
45+
46+
// The write load decider is disabled by default.
47+
48+
var writeLoadDecider = createWriteLoadConstraintDecider(Settings.builder().build());
49+
50+
assertEquals(
51+
Decision.Type.YES,
52+
writeLoadDecider.canAllocate(
53+
testHarness.shardRouting2,
54+
testHarness.exceedingThresholdRoutingNode,
55+
testHarness.routingAllocation
56+
).type()
57+
);
58+
assertEquals(
59+
Decision.Type.YES,
60+
writeLoadDecider.canAllocate(testHarness.shardRouting1, testHarness.belowThresholdRoutingNode, testHarness.routingAllocation)
61+
.type()
62+
);
63+
assertEquals(
64+
Decision.Type.YES,
65+
writeLoadDecider.canAllocate(testHarness.shardRouting1, testHarness.nearThresholdRoutingNode, testHarness.routingAllocation)
66+
.type()
67+
);
68+
assertEquals(
69+
Decision.Type.YES,
70+
writeLoadDecider.canAllocate(
71+
testHarness.thirdRoutingNoWriteLoad,
72+
testHarness.exceedingThresholdRoutingNode,
73+
testHarness.routingAllocation
74+
).type()
75+
);
76+
77+
assertEquals(
78+
Decision.Type.YES,
79+
writeLoadDecider.canRemain(
80+
testHarness.clusterState.metadata().getProject().index(indexName),
81+
testHarness.shardRouting1,
82+
testHarness.exceedingThresholdRoutingNode,
83+
testHarness.routingAllocation
84+
).type()
85+
);
86+
}
87+
88+
/**
89+
* Test the {@link WriteLoadConstraintDecider#canAllocate} implementation.
90+
*/
91+
public void testWriteLoadDeciderCanAllocate() {
4092
String indexName = "test-index";
93+
var testHarness = createClusterStateAndRoutingAllocation(indexName);
4194

95+
var writeLoadDecider = createWriteLoadConstraintDecider(
96+
Settings.builder()
97+
.put(
98+
WriteLoadConstraintSettings.WRITE_LOAD_DECIDER_ENABLED_SETTING.getKey(),
99+
randomBoolean()
100+
? WriteLoadConstraintSettings.WriteLoadDeciderStatus.ENABLED
101+
: WriteLoadConstraintSettings.WriteLoadDeciderStatus.LOW_THRESHOLD_ONLY
102+
)
103+
.build()
104+
);
105+
assertEquals(
106+
"Assigning a new shard to a node that is above the threshold should fail",
107+
Decision.Type.NO,
108+
writeLoadDecider.canAllocate(
109+
testHarness.shardRouting2,
110+
testHarness.exceedingThresholdRoutingNode,
111+
testHarness.routingAllocation
112+
).type()
113+
);
114+
assertEquals(
115+
"Assigning a new shard to a node that has capacity should succeed",
116+
Decision.Type.YES,
117+
writeLoadDecider.canAllocate(testHarness.shardRouting1, testHarness.belowThresholdRoutingNode, testHarness.routingAllocation)
118+
.type()
119+
);
120+
assertEquals(
121+
"Assigning a new shard without a write load estimate should _not_ be blocked by lack of capacity",
122+
Decision.Type.YES,
123+
writeLoadDecider.canAllocate(
124+
testHarness.thirdRoutingNoWriteLoad,
125+
testHarness.exceedingThresholdRoutingNode,
126+
testHarness.routingAllocation
127+
).type()
128+
);
129+
assertEquals(
130+
"Assigning a new shard that would cause the node to exceed capacity should fail",
131+
Decision.Type.NO,
132+
writeLoadDecider.canAllocate(testHarness.shardRouting1, testHarness.nearThresholdRoutingNode, testHarness.routingAllocation)
133+
.type()
134+
);
135+
}
136+
137+
/**
138+
* Carries all the cluster state objects needed for testing after {@link #createClusterStateAndRoutingAllocation} sets them up.
139+
*/
140+
private record TestHarness(
141+
ClusterState clusterState,
142+
RoutingAllocation routingAllocation,
143+
RoutingNode exceedingThresholdRoutingNode,
144+
RoutingNode belowThresholdRoutingNode,
145+
RoutingNode nearThresholdRoutingNode,
146+
ShardRouting shardRouting1,
147+
ShardRouting shardRouting2,
148+
ShardRouting thirdRoutingNoWriteLoad
149+
) {}
150+
151+
/**
152+
* Creates all the cluster state and objects needed to test the {@link WriteLoadConstraintDecider}.
153+
*/
154+
private TestHarness createClusterStateAndRoutingAllocation(String indexName) {
42155
/**
43156
* Create the ClusterState for multiple nodes and multiple index shards.
44157
*/
@@ -152,55 +265,15 @@ public void testWriteLoadDecider() {
152265
new ShardRouting[] {}
153266
);
154267

155-
/**
156-
* Test the write load decider
157-
*/
158-
159-
// The write load decider is disabled by default.
160-
161-
var writeLoadDecider = createWriteLoadConstraintDecider(Settings.builder().build());
162-
assertEquals(
163-
Decision.Type.YES,
164-
writeLoadDecider.canAllocate(shardRouting2, exceedingThresholdRoutingNode, routingAllocation).type()
165-
);
166-
assertEquals(Decision.Type.YES, writeLoadDecider.canAllocate(shardRouting1, belowThresholdRoutingNode, routingAllocation).type());
167-
assertEquals(Decision.Type.YES, writeLoadDecider.canAllocate(shardRouting1, nearThresholdRoutingNode, routingAllocation).type());
168-
assertEquals(
169-
Decision.Type.YES,
170-
writeLoadDecider.canAllocate(thirdRoutingNoWriteLoad, exceedingThresholdRoutingNode, routingAllocation).type()
171-
);
172-
173-
// Check that the answers change when enabled.
174-
175-
writeLoadDecider = createWriteLoadConstraintDecider(
176-
Settings.builder()
177-
.put(
178-
WriteLoadConstraintSettings.WRITE_LOAD_DECIDER_ENABLED_SETTING.getKey(),
179-
randomBoolean()
180-
? WriteLoadConstraintSettings.WriteLoadDeciderStatus.ENABLED
181-
: WriteLoadConstraintSettings.WriteLoadDeciderStatus.LOW_THRESHOLD_ONLY
182-
)
183-
.build()
184-
);
185-
assertEquals(
186-
"Assigning a new shard to a node that is above the threshold should fail",
187-
Decision.Type.NO,
188-
writeLoadDecider.canAllocate(shardRouting2, exceedingThresholdRoutingNode, routingAllocation).type()
189-
);
190-
assertEquals(
191-
"Assigning a new shard to a node that has capacity should succeed",
192-
Decision.Type.YES,
193-
writeLoadDecider.canAllocate(shardRouting1, belowThresholdRoutingNode, routingAllocation).type()
194-
);
195-
assertEquals(
196-
"Assigning a new shard without a write load estimate should _not_ be blocked by lack of capacity",
197-
Decision.Type.YES,
198-
writeLoadDecider.canAllocate(thirdRoutingNoWriteLoad, exceedingThresholdRoutingNode, routingAllocation).type()
199-
);
200-
assertEquals(
201-
"Assigning a new shard that would cause the node to exceed capacity should fail",
202-
Decision.Type.NO,
203-
writeLoadDecider.canAllocate(shardRouting1, nearThresholdRoutingNode, routingAllocation).type()
268+
return new TestHarness(
269+
clusterState,
270+
routingAllocation,
271+
exceedingThresholdRoutingNode,
272+
belowThresholdRoutingNode,
273+
nearThresholdRoutingNode,
274+
shardRouting1,
275+
shardRouting2,
276+
thirdRoutingNoWriteLoad
204277
);
205278
}
206279

0 commit comments

Comments
 (0)