Skip to content

Commit 4fd1bb2

Browse files
authored
Adapt more tests suites to closed indices (#39186)
* Adapt more tests suites to closed indices Similarly to #38631, this pull request modifies multiple test suites so that they runs the tests with opened or closed indices. The suites are testing: - shard allocation filtering - shard allocation awereness - Reroute API Relates to #33888
1 parent 05debeb commit 4fd1bb2

File tree

4 files changed

+182
-72
lines changed

4 files changed

+182
-72
lines changed

server/src/test/java/org/elasticsearch/cluster/allocation/AwarenessAllocationIT.java

Lines changed: 85 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.apache.logging.log4j.Logger;
2525
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
2626
import org.elasticsearch.cluster.ClusterState;
27+
import org.elasticsearch.cluster.metadata.IndexMetaData;
28+
import org.elasticsearch.cluster.metadata.IndexMetaData.State;
2729
import org.elasticsearch.cluster.routing.IndexRoutingTable;
2830
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
2931
import org.elasticsearch.cluster.routing.ShardRouting;
@@ -33,9 +35,11 @@
3335
import org.elasticsearch.test.ESIntegTestCase;
3436
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
3537

38+
import java.util.Arrays;
3639
import java.util.List;
3740
import java.util.concurrent.TimeUnit;
3841

42+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
3943
import static org.hamcrest.Matchers.anyOf;
4044
import static org.hamcrest.Matchers.equalTo;
4145

@@ -54,7 +58,6 @@ public void testSimpleAwareness() throws Exception {
5458
.put("cluster.routing.allocation.awareness.attributes", "rack_id")
5559
.build();
5660

57-
5861
logger.info("--> starting 2 nodes on the same rack");
5962
internalCluster().startNodes(2, Settings.builder().put(commonSettings).put("node.attr.rack_id", "rack_1").build());
6063

@@ -68,21 +71,33 @@ public void testSimpleAwareness() throws Exception {
6871

6972
ensureGreen();
7073

74+
final List<String> indicesToClose = randomSubsetOf(Arrays.asList("test1", "test2"));
75+
indicesToClose.forEach(indexToClose -> assertAcked(client().admin().indices().prepareClose(indexToClose).get()));
76+
7177
logger.info("--> starting 1 node on a different rack");
7278
final String node3 = internalCluster().startNode(Settings.builder().put(commonSettings).put("node.attr.rack_id", "rack_2").build());
7379

7480
// On slow machines the initial relocation might be delayed
7581
assertThat(awaitBusy(
7682
() -> {
7783
logger.info("--> waiting for no relocation");
78-
ClusterHealthResponse clusterHealth = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID)
79-
.setWaitForGreenStatus().setWaitForNodes("3").setWaitForNoRelocatingShards(true).get();
84+
ClusterHealthResponse clusterHealth = client().admin().cluster().prepareHealth()
85+
.setIndices("test1", "test2")
86+
.setWaitForEvents(Priority.LANGUID)
87+
.setWaitForGreenStatus()
88+
.setWaitForNodes("3")
89+
.setWaitForNoRelocatingShards(true)
90+
.get();
8091
if (clusterHealth.isTimedOut()) {
8192
return false;
8293
}
8394

8495
logger.info("--> checking current state");
8596
ClusterState clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
97+
// check that closed indices are effectively closed
98+
if (indicesToClose.stream().anyMatch(index -> clusterState.metaData().index(index).getState() != State.CLOSE)) {
99+
return false;
100+
}
86101
// verify that we have all the primaries on node3
87102
ObjectIntHashMap<String> counts = new ObjectIntHashMap<>();
88103
for (IndexRoutingTable indexRoutingTable : clusterState.routingTable()) {
@@ -99,7 +114,7 @@ public void testSimpleAwareness() throws Exception {
99114
), equalTo(true));
100115
}
101116

102-
public void testAwarenessZones() throws Exception {
117+
public void testAwarenessZones() {
103118
Settings commonSettings = Settings.builder()
104119
.put(AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_FORCE_GROUP_SETTING.getKey() + "zone.values", "a,b")
105120
.put(AwarenessAllocationDecider.CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING.getKey(), "zone")
@@ -121,12 +136,20 @@ public void testAwarenessZones() throws Exception {
121136
ClusterHealthResponse health = client().admin().cluster().prepareHealth().setWaitForNodes("4").execute().actionGet();
122137
assertThat(health.isTimedOut(), equalTo(false));
123138

124-
client().admin().indices().prepareCreate("test")
125-
.setSettings(Settings.builder().put("index.number_of_shards", 5)
126-
.put("index.number_of_replicas", 1)).execute().actionGet();
139+
createIndex("test", Settings.builder()
140+
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 5)
141+
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1)
142+
.build());
143+
144+
if (randomBoolean()) {
145+
assertAcked(client().admin().indices().prepareClose("test"));
146+
}
127147

128148
logger.info("--> waiting for shards to be allocated");
129-
health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus()
149+
health = client().admin().cluster().prepareHealth()
150+
.setIndices("test")
151+
.setWaitForEvents(Priority.LANGUID)
152+
.setWaitForGreenStatus()
130153
.setWaitForNoRelocatingShards(true).execute().actionGet();
131154
assertThat(health.isTimedOut(), equalTo(false));
132155

@@ -146,7 +169,7 @@ public void testAwarenessZones() throws Exception {
146169
assertThat(counts.get(B_0), anyOf(equalTo(2),equalTo(3)));
147170
}
148171

149-
public void testAwarenessZonesIncrementalNodes() throws Exception {
172+
public void testAwarenessZonesIncrementalNodes() {
150173
Settings commonSettings = Settings.builder()
151174
.put("cluster.routing.allocation.awareness.force.zone.values", "a,b")
152175
.put("cluster.routing.allocation.awareness.attributes", "zone")
@@ -159,11 +182,23 @@ public void testAwarenessZonesIncrementalNodes() throws Exception {
159182
);
160183
String A_0 = nodes.get(0);
161184
String B_0 = nodes.get(1);
162-
client().admin().indices().prepareCreate("test")
163-
.setSettings(Settings.builder().put("index.number_of_shards", 5)
164-
.put("index.number_of_replicas", 1)).execute().actionGet();
165-
ClusterHealthResponse health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID)
166-
.setWaitForGreenStatus().setWaitForNodes("2").setWaitForNoRelocatingShards(true).execute().actionGet();
185+
186+
createIndex("test", Settings.builder()
187+
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 5)
188+
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1)
189+
.build());
190+
191+
if (randomBoolean()) {
192+
assertAcked(client().admin().indices().prepareClose("test"));
193+
}
194+
195+
ClusterHealthResponse health = client().admin().cluster().prepareHealth()
196+
.setIndices("test")
197+
.setWaitForEvents(Priority.LANGUID)
198+
.setWaitForGreenStatus()
199+
.setWaitForNodes("2")
200+
.setWaitForNoRelocatingShards(true)
201+
.execute().actionGet();
167202
assertThat(health.isTimedOut(), equalTo(false));
168203
ClusterState clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
169204
ObjectIntHashMap<String> counts = new ObjectIntHashMap<>();
@@ -180,12 +215,22 @@ public void testAwarenessZonesIncrementalNodes() throws Exception {
180215
logger.info("--> starting another node in zone 'b'");
181216

182217
String B_1 = internalCluster().startNode(Settings.builder().put(commonSettings).put("node.attr.zone", "b").build());
183-
health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus()
184-
.setWaitForNodes("3").execute().actionGet();
218+
health = client().admin().cluster().prepareHealth()
219+
.setIndices("test")
220+
.setWaitForEvents(Priority.LANGUID)
221+
.setWaitForGreenStatus()
222+
.setWaitForNodes("3")
223+
.execute().actionGet();
185224
assertThat(health.isTimedOut(), equalTo(false));
186225
client().admin().cluster().prepareReroute().get();
187-
health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus()
188-
.setWaitForNodes("3").setWaitForActiveShards(10).setWaitForNoRelocatingShards(true).execute().actionGet();
226+
health = client().admin().cluster().prepareHealth()
227+
.setIndices("test")
228+
.setWaitForEvents(Priority.LANGUID)
229+
.setWaitForGreenStatus()
230+
.setWaitForNodes("3")
231+
.setWaitForActiveShards(10)
232+
.setWaitForNoRelocatingShards(true)
233+
.execute().actionGet();
189234

190235
assertThat(health.isTimedOut(), equalTo(false));
191236
clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
@@ -204,12 +249,22 @@ public void testAwarenessZonesIncrementalNodes() throws Exception {
204249
assertThat(counts.get(B_1), equalTo(2));
205250

206251
String noZoneNode = internalCluster().startNode();
207-
health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus()
208-
.setWaitForNodes("4").execute().actionGet();
252+
health = client().admin().cluster().prepareHealth()
253+
.setIndices("test")
254+
.setWaitForEvents(Priority.LANGUID)
255+
.setWaitForGreenStatus()
256+
.setWaitForNodes("4")
257+
.execute().actionGet();
209258
assertThat(health.isTimedOut(), equalTo(false));
210259
client().admin().cluster().prepareReroute().get();
211-
health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus()
212-
.setWaitForNodes("4").setWaitForActiveShards(10).setWaitForNoRelocatingShards(true).execute().actionGet();
260+
health = client().admin().cluster().prepareHealth()
261+
.setIndices("test")
262+
.setWaitForEvents(Priority.LANGUID)
263+
.setWaitForGreenStatus()
264+
.setWaitForNodes("4")
265+
.setWaitForActiveShards(10)
266+
.setWaitForNoRelocatingShards(true)
267+
.execute().actionGet();
213268

214269
assertThat(health.isTimedOut(), equalTo(false));
215270
clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();
@@ -231,8 +286,14 @@ public void testAwarenessZonesIncrementalNodes() throws Exception {
231286
client().admin().cluster().prepareUpdateSettings()
232287
.setTransientSettings(Settings.builder().put("cluster.routing.allocation.awareness.attributes", "").build()).get();
233288

234-
health = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus()
235-
.setWaitForNodes("4").setWaitForActiveShards(10).setWaitForNoRelocatingShards(true).execute().actionGet();
289+
health = client().admin().cluster().prepareHealth()
290+
.setIndices("test")
291+
.setWaitForEvents(Priority.LANGUID)
292+
.setWaitForGreenStatus()
293+
.setWaitForNodes("4")
294+
.setWaitForActiveShards(10)
295+
.setWaitForNoRelocatingShards(true)
296+
.execute().actionGet();
236297

237298
assertThat(health.isTimedOut(), equalTo(false));
238299
clusterState = client().admin().cluster().prepareState().execute().actionGet().getState();

server/src/test/java/org/elasticsearch/cluster/allocation/ClusterRerouteIT.java

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.apache.logging.log4j.Level;
2323
import org.apache.logging.log4j.LogManager;
2424
import org.apache.logging.log4j.Logger;
25-
import org.elasticsearch.core.internal.io.IOUtils;
2625
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
2726
import org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse;
2827
import org.elasticsearch.action.admin.cluster.reroute.TransportClusterRerouteAction;
@@ -34,6 +33,7 @@
3433
import org.elasticsearch.cluster.node.DiscoveryNode;
3534
import org.elasticsearch.cluster.routing.ShardRouting;
3635
import org.elasticsearch.cluster.routing.ShardRoutingState;
36+
import org.elasticsearch.cluster.routing.UnassignedInfo;
3737
import org.elasticsearch.cluster.routing.allocation.RerouteExplanation;
3838
import org.elasticsearch.cluster.routing.allocation.RoutingExplanations;
3939
import org.elasticsearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand;
@@ -48,6 +48,7 @@
4848
import org.elasticsearch.common.logging.Loggers;
4949
import org.elasticsearch.common.settings.Settings;
5050
import org.elasticsearch.common.unit.TimeValue;
51+
import org.elasticsearch.core.internal.io.IOUtils;
5152
import org.elasticsearch.env.NodeEnvironment;
5253
import org.elasticsearch.index.Index;
5354
import org.elasticsearch.index.shard.ShardId;
@@ -102,6 +103,10 @@ private void rerouteWithCommands(Settings commonSettings) throws Exception {
102103
.setSettings(Settings.builder().put("index.number_of_shards", 1))
103104
.execute().actionGet();
104105

106+
if (randomBoolean()) {
107+
client().admin().indices().prepareClose("test").get();
108+
}
109+
105110
ClusterState state = client().admin().cluster().prepareState().execute().actionGet().getState();
106111
assertThat(state.getRoutingNodes().unassigned().size(), equalTo(2));
107112

@@ -128,8 +133,11 @@ private void rerouteWithCommands(Settings commonSettings) throws Exception {
128133
assertThat(state.getRoutingNodes().node(state.nodes().resolveNode(node_1).getId()).iterator().next().state(),
129134
equalTo(ShardRoutingState.INITIALIZING));
130135

131-
ClusterHealthResponse healthResponse = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID)
132-
.setWaitForYellowStatus().execute().actionGet();
136+
ClusterHealthResponse healthResponse = client().admin().cluster().prepareHealth()
137+
.setIndices("test")
138+
.setWaitForEvents(Priority.LANGUID)
139+
.setWaitForYellowStatus()
140+
.execute().actionGet();
133141
assertThat(healthResponse.isTimedOut(), equalTo(false));
134142

135143
logger.info("--> get the state, verify shard 1 primary allocated");
@@ -149,9 +157,12 @@ private void rerouteWithCommands(Settings commonSettings) throws Exception {
149157
assertThat(state.getRoutingNodes().node(state.nodes().resolveNode(node_2).getId()).iterator().next().state(),
150158
equalTo(ShardRoutingState.INITIALIZING));
151159

152-
153-
healthResponse = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForYellowStatus()
154-
.setWaitForNoRelocatingShards(true).execute().actionGet();
160+
healthResponse = client().admin().cluster().prepareHealth()
161+
.setIndices("test")
162+
.setWaitForEvents(Priority.LANGUID)
163+
.setWaitForYellowStatus()
164+
.setWaitForNoRelocatingShards(true)
165+
.execute().actionGet();
155166
assertThat(healthResponse.isTimedOut(), equalTo(false));
156167

157168
logger.info("--> get the state, verify shard 1 primary moved from node1 to node2");
@@ -193,11 +204,15 @@ public void testDelayWithALargeAmountOfShards() throws Exception {
193204

194205
logger.info("--> create indices");
195206
for (int i = 0; i < 25; i++) {
196-
client().admin().indices().prepareCreate("test" + i)
197-
.setSettings(Settings.builder()
198-
.put("index.number_of_shards", 5).put("index.number_of_replicas", 1)
199-
.put("index.unassigned.node_left.delayed_timeout", randomIntBetween(250, 1000) + "ms"))
200-
.execute().actionGet();
207+
final String indexName = "test" + i;
208+
createIndex(indexName, Settings.builder()
209+
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 5)
210+
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1)
211+
.put(UnassignedInfo.INDEX_DELAYED_NODE_LEFT_TIMEOUT_SETTING.getKey(), randomIntBetween(250, 1000) + "ms")
212+
.build());
213+
if (randomBoolean()) {
214+
assertAcked(client().admin().indices().prepareClose(indexName));
215+
}
201216
}
202217

203218
ensureGreen(TimeValue.timeValueMinutes(1));
@@ -294,10 +309,14 @@ public void testRerouteExplain() {
294309
assertThat(healthResponse.isTimedOut(), equalTo(false));
295310

296311
logger.info("--> create an index with 1 shard");
297-
client().admin().indices().prepareCreate("test")
298-
.setSettings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0))
299-
.execute().actionGet();
312+
createIndex("test", Settings.builder()
313+
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
314+
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
315+
.build());
300316

317+
if (randomBoolean()) {
318+
assertAcked(client().admin().indices().prepareClose("test"));
319+
}
301320
ensureGreen("test");
302321

303322
logger.info("--> disable allocation");
@@ -403,12 +422,18 @@ public void testMessageLogging() throws Exception{
403422
Loggers.removeAppender(actionLogger, allocateMockLog);
404423
}
405424

406-
public void testClusterRerouteWithBlocks() throws Exception {
425+
public void testClusterRerouteWithBlocks() {
407426
List<String> nodesIds = internalCluster().startNodes(2);
408427

409428
logger.info("--> create an index with 1 shard and 0 replicas");
410-
assertAcked(prepareCreate("test-blocks").setSettings(Settings.builder().put("index.number_of_shards", 1)
411-
.put("index.number_of_replicas", 0)));
429+
createIndex("test-blocks", Settings.builder()
430+
.put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)
431+
.put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)
432+
.build());
433+
434+
if (randomBoolean()) {
435+
assertAcked(client().admin().indices().prepareClose("test-blocks"));
436+
}
412437
ensureGreen("test-blocks");
413438

414439
logger.info("--> check that the index has 1 shard");
@@ -432,11 +457,14 @@ public void testClusterRerouteWithBlocks() throws Exception {
432457
SETTING_READ_ONLY_ALLOW_DELETE)) {
433458
try {
434459
enableIndexBlock("test-blocks", blockSetting);
435-
assertAcked(client().admin().cluster().prepareReroute().add(new MoveAllocationCommand("test-blocks", 0,
436-
nodesIds.get(toggle % 2), nodesIds.get(++toggle % 2))));
460+
assertAcked(client().admin().cluster().prepareReroute()
461+
.add(new MoveAllocationCommand("test-blocks", 0, nodesIds.get(toggle % 2), nodesIds.get(++toggle % 2))));
437462

438-
ClusterHealthResponse healthResponse = client().admin().cluster().prepareHealth().setWaitForYellowStatus()
439-
.setWaitForNoRelocatingShards(true).execute().actionGet();
463+
ClusterHealthResponse healthResponse = client().admin().cluster().prepareHealth()
464+
.setIndices("test-blocks")
465+
.setWaitForYellowStatus()
466+
.setWaitForNoRelocatingShards(true)
467+
.execute().actionGet();
440468
assertThat(healthResponse.isTimedOut(), equalTo(false));
441469
} finally {
442470
disableIndexBlock("test-blocks", blockSetting);

0 commit comments

Comments
 (0)