Skip to content

Commit 8b691db

Browse files
authored
Fix data stream retrieval in ExplainDataStreamLifecycleIT (#125611)
These tests had the potential to fail when two consecutive GET data streams requests would hit two different nodes, where one node already had the cluster state that contained the new backing index and the other node didn't yet. Caused by #122852 Fixes #124882 Fixes #124885
1 parent 7a610c3 commit 8b691db

File tree

2 files changed

+31
-80
lines changed

2 files changed

+31
-80
lines changed

modules/data-streams/src/internalClusterTest/java/org/elasticsearch/datastreams/lifecycle/ExplainDataStreamLifecycleIT.java

Lines changed: 31 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.elasticsearch.action.bulk.BulkRequest;
1919
import org.elasticsearch.action.bulk.BulkResponse;
2020
import org.elasticsearch.action.datastreams.CreateDataStreamAction;
21-
import org.elasticsearch.action.datastreams.GetDataStreamAction;
2221
import org.elasticsearch.action.datastreams.lifecycle.ExplainDataStreamLifecycleAction;
2322
import org.elasticsearch.action.datastreams.lifecycle.ExplainIndexDataStreamLifecycle;
2423
import org.elasticsearch.action.index.IndexRequest;
@@ -31,7 +30,6 @@
3130
import org.elasticsearch.core.Nullable;
3231
import org.elasticsearch.core.TimeValue;
3332
import org.elasticsearch.datastreams.DataStreamsPlugin;
34-
import org.elasticsearch.index.Index;
3533
import org.elasticsearch.index.mapper.DateFieldMapper;
3634
import org.elasticsearch.plugins.Plugin;
3735
import org.elasticsearch.rest.RestStatus;
@@ -99,29 +97,16 @@ public void testExplainLifecycle() throws Exception {
9997

10098
indexDocs(dataStreamName, 1);
10199

102-
assertBusy(() -> {
103-
GetDataStreamAction.Request getDataStreamRequest = new GetDataStreamAction.Request(
104-
TEST_REQUEST_TIMEOUT,
105-
new String[] { dataStreamName }
106-
);
107-
GetDataStreamAction.Response getDataStreamResponse = client().execute(GetDataStreamAction.INSTANCE, getDataStreamRequest)
108-
.actionGet();
109-
assertThat(getDataStreamResponse.getDataStreams().size(), equalTo(1));
110-
assertThat(getDataStreamResponse.getDataStreams().get(0).getDataStream().getName(), equalTo(dataStreamName));
111-
List<Index> backingIndices = getDataStreamResponse.getDataStreams().get(0).getDataStream().getIndices();
112-
assertThat(backingIndices.size(), equalTo(2));
113-
String backingIndex = backingIndices.get(0).getName();
114-
assertThat(backingIndex, backingIndexEqualTo(dataStreamName, 1));
115-
String writeIndex = backingIndices.get(1).getName();
116-
assertThat(writeIndex, backingIndexEqualTo(dataStreamName, 2));
117-
});
100+
List<String> backingIndices = waitForDataStreamBackingIndices(dataStreamName, 2);
101+
String firstGenerationIndex = backingIndices.get(0);
102+
assertThat(firstGenerationIndex, backingIndexEqualTo(dataStreamName, 1));
103+
String secondGenerationIndex = backingIndices.get(1);
104+
assertThat(secondGenerationIndex, backingIndexEqualTo(dataStreamName, 2));
118105

119106
{
120107
ExplainDataStreamLifecycleAction.Request explainIndicesRequest = new ExplainDataStreamLifecycleAction.Request(
121108
TEST_REQUEST_TIMEOUT,
122-
new String[] {
123-
DataStream.getDefaultBackingIndexName(dataStreamName, 1),
124-
DataStream.getDefaultBackingIndexName(dataStreamName, 2) }
109+
new String[] { firstGenerationIndex, secondGenerationIndex }
125110
);
126111
ExplainDataStreamLifecycleAction.Response response = client().execute(
127112
ExplainDataStreamLifecycleAction.INSTANCE,
@@ -140,18 +125,16 @@ public void testExplainLifecycle() throws Exception {
140125
assertThat(explainIndex.getError(), nullValue());
141126
}
142127

143-
if (explainIndex.getIndex().equals(DataStream.getDefaultBackingIndexName(dataStreamName, 1))) {
128+
if (explainIndex.getIndex().equals(firstGenerationIndex)) {
144129
// first generation index was rolled over
145-
assertThat(explainIndex.getIndex(), is(DataStream.getDefaultBackingIndexName(dataStreamName, 1)));
146130
assertThat(explainIndex.getRolloverDate(), notNullValue());
147131
assertThat(explainIndex.getTimeSinceRollover(System::currentTimeMillis), notNullValue());
148132
assertThat(explainIndex.getGenerationTime(System::currentTimeMillis), notNullValue());
149133
} else {
150134
// the write index has not been rolled over yet
151-
assertThat(explainIndex.getGenerationTime(System::currentTimeMillis), nullValue());
152-
assertThat(explainIndex.getIndex(), is(DataStream.getDefaultBackingIndexName(dataStreamName, 2)));
153135
assertThat(explainIndex.getRolloverDate(), nullValue());
154136
assertThat(explainIndex.getTimeSinceRollover(System::currentTimeMillis), nullValue());
137+
assertThat(explainIndex.getGenerationTime(System::currentTimeMillis), nullValue());
155138
}
156139
}
157140
}
@@ -160,9 +143,7 @@ public void testExplainLifecycle() throws Exception {
160143
// let's also explain with include_defaults=true
161144
ExplainDataStreamLifecycleAction.Request explainIndicesRequest = new ExplainDataStreamLifecycleAction.Request(
162145
TEST_REQUEST_TIMEOUT,
163-
new String[] {
164-
DataStream.getDefaultBackingIndexName(dataStreamName, 1),
165-
DataStream.getDefaultBackingIndexName(dataStreamName, 2) },
146+
new String[] { firstGenerationIndex, secondGenerationIndex },
166147
true
167148
);
168149
ExplainDataStreamLifecycleAction.Response response = client().execute(
@@ -195,18 +176,16 @@ public void testExplainLifecycle() throws Exception {
195176
assertThat(explainIndex.getLifecycle(), notNullValue());
196177
assertThat(explainIndex.getLifecycle().dataRetention(), nullValue());
197178

198-
if (explainIndex.getIndex().equals(DataStream.getDefaultBackingIndexName(dataStreamName, 1))) {
179+
if (explainIndex.getIndex().equals(firstGenerationIndex)) {
199180
// first generation index was rolled over
200-
assertThat(explainIndex.getIndex(), is(DataStream.getDefaultBackingIndexName(dataStreamName, 1)));
201181
assertThat(explainIndex.getRolloverDate(), notNullValue());
202182
assertThat(explainIndex.getTimeSinceRollover(System::currentTimeMillis), notNullValue());
203183
assertThat(explainIndex.getGenerationTime(System::currentTimeMillis), notNullValue());
204184
} else {
205185
// the write index has not been rolled over yet
206-
assertThat(explainIndex.getGenerationTime(System::currentTimeMillis), nullValue());
207-
assertThat(explainIndex.getIndex(), is(DataStream.getDefaultBackingIndexName(dataStreamName, 2)));
208186
assertThat(explainIndex.getRolloverDate(), nullValue());
209187
assertThat(explainIndex.getTimeSinceRollover(System::currentTimeMillis), nullValue());
188+
assertThat(explainIndex.getGenerationTime(System::currentTimeMillis), nullValue());
210189
}
211190
}
212191
}
@@ -228,28 +207,15 @@ public void testSystemExplainLifecycle() throws Exception {
228207

229208
indexDocs(dataStreamName, 1);
230209

231-
assertBusy(() -> {
232-
GetDataStreamAction.Request getDataStreamRequest = new GetDataStreamAction.Request(
233-
TEST_REQUEST_TIMEOUT,
234-
new String[] { dataStreamName }
235-
);
236-
GetDataStreamAction.Response getDataStreamResponse = client().execute(GetDataStreamAction.INSTANCE, getDataStreamRequest)
237-
.actionGet();
238-
assertThat(getDataStreamResponse.getDataStreams().size(), equalTo(1));
239-
assertThat(getDataStreamResponse.getDataStreams().get(0).getDataStream().getName(), equalTo(dataStreamName));
240-
List<Index> backingIndices = getDataStreamResponse.getDataStreams().get(0).getDataStream().getIndices();
241-
assertThat(backingIndices.size(), equalTo(2));
242-
String backingIndex = backingIndices.get(0).getName();
243-
assertThat(backingIndex, backingIndexEqualTo(dataStreamName, 1));
244-
String writeIndex = backingIndices.get(1).getName();
245-
assertThat(writeIndex, backingIndexEqualTo(dataStreamName, 2));
246-
});
210+
List<String> backingIndices = waitForDataStreamBackingIndices(dataStreamName, 2);
211+
String firstGenerationIndex = backingIndices.get(0);
212+
assertThat(firstGenerationIndex, backingIndexEqualTo(dataStreamName, 1));
213+
String secondGenerationIndex = backingIndices.get(1);
214+
assertThat(secondGenerationIndex, backingIndexEqualTo(dataStreamName, 2));
247215

248216
ExplainDataStreamLifecycleAction.Request explainIndicesRequest = new ExplainDataStreamLifecycleAction.Request(
249217
TEST_REQUEST_TIMEOUT,
250-
new String[] {
251-
DataStream.getDefaultBackingIndexName(dataStreamName, 1),
252-
DataStream.getDefaultBackingIndexName(dataStreamName, 2) }
218+
new String[] { firstGenerationIndex, secondGenerationIndex }
253219
);
254220
ExplainDataStreamLifecycleAction.Response response = client().execute(
255221
ExplainDataStreamLifecycleAction.INSTANCE,
@@ -289,33 +255,21 @@ public void testExplainLifecycleForIndicesWithErrors() throws Exception {
289255
indexDocs(dataStreamName, 1);
290256

291257
// let's allow one rollover to go through
292-
assertBusy(() -> {
293-
GetDataStreamAction.Request getDataStreamRequest = new GetDataStreamAction.Request(
294-
TEST_REQUEST_TIMEOUT,
295-
new String[] { dataStreamName }
296-
);
297-
GetDataStreamAction.Response getDataStreamResponse = client().execute(GetDataStreamAction.INSTANCE, getDataStreamRequest)
298-
.actionGet();
299-
assertThat(getDataStreamResponse.getDataStreams().size(), equalTo(1));
300-
assertThat(getDataStreamResponse.getDataStreams().get(0).getDataStream().getName(), equalTo(dataStreamName));
301-
List<Index> backingIndices = getDataStreamResponse.getDataStreams().get(0).getDataStream().getIndices();
302-
assertThat(backingIndices.size(), equalTo(2));
303-
String backingIndex = backingIndices.get(0).getName();
304-
assertThat(backingIndex, backingIndexEqualTo(dataStreamName, 1));
305-
String writeIndex = backingIndices.get(1).getName();
306-
assertThat(writeIndex, backingIndexEqualTo(dataStreamName, 2));
307-
});
258+
List<String> backingIndices = waitForDataStreamBackingIndices(dataStreamName, 2);
259+
String firstGenerationIndex = backingIndices.get(0);
260+
assertThat(firstGenerationIndex, backingIndexEqualTo(dataStreamName, 1));
261+
String secondGenerationIndex = backingIndices.get(1);
262+
assertThat(secondGenerationIndex, backingIndexEqualTo(dataStreamName, 2));
308263

309264
// prevent new indices from being created (ie. future rollovers)
310265
updateClusterSettings(Settings.builder().put(SETTING_CLUSTER_MAX_SHARDS_PER_NODE.getKey(), 1));
311266

312267
indexDocs(dataStreamName, 1);
313268

314-
String writeIndexName = DataStream.getDefaultBackingIndexName(dataStreamName, 2);
315269
assertBusy(() -> {
316270
ExplainDataStreamLifecycleAction.Request explainIndicesRequest = new ExplainDataStreamLifecycleAction.Request(
317271
TEST_REQUEST_TIMEOUT,
318-
new String[] { writeIndexName }
272+
new String[] { secondGenerationIndex }
319273
);
320274
ExplainDataStreamLifecycleAction.Response response = client().execute(
321275
ExplainDataStreamLifecycleAction.INSTANCE,
@@ -325,7 +279,7 @@ public void testExplainLifecycleForIndicesWithErrors() throws Exception {
325279
// we requested the explain for indices with the default include_details=false
326280
assertThat(response.getRolloverConfiguration(), nullValue());
327281
for (ExplainIndexDataStreamLifecycle explainIndex : response.getIndices()) {
328-
assertThat(explainIndex.getIndex(), is(writeIndexName));
282+
assertThat(explainIndex.getIndex(), is(secondGenerationIndex));
329283
assertThat(explainIndex.isManagedByLifecycle(), is(true));
330284
assertThat(explainIndex.getIndexCreationDate(), notNullValue());
331285
assertThat(explainIndex.getLifecycle(), notNullValue());
@@ -347,7 +301,7 @@ public void testExplainLifecycleForIndicesWithErrors() throws Exception {
347301
assertBusy(() -> {
348302
ExplainDataStreamLifecycleAction.Request explainIndicesRequest = new ExplainDataStreamLifecycleAction.Request(
349303
TEST_REQUEST_TIMEOUT,
350-
new String[] { writeIndexName }
304+
new String[] { secondGenerationIndex }
351305
);
352306
ExplainDataStreamLifecycleAction.Response response = client().execute(
353307
ExplainDataStreamLifecycleAction.INSTANCE,
@@ -385,11 +339,14 @@ public void testExplainDataStreamLifecycleForUnmanagedIndices() throws Exception
385339

386340
indexDocs(dataStreamName, 4);
387341

388-
String writeIndexName = DataStream.getDefaultBackingIndexName(dataStreamName, 1);
342+
List<String> backingIndices = waitForDataStreamBackingIndices(dataStreamName, 1);
343+
String firstGenerationIndex = backingIndices.get(0);
344+
assertThat(firstGenerationIndex, backingIndexEqualTo(dataStreamName, 1));
345+
389346
assertBusy(() -> {
390347
ExplainDataStreamLifecycleAction.Request explainIndicesRequest = new ExplainDataStreamLifecycleAction.Request(
391348
TEST_REQUEST_TIMEOUT,
392-
new String[] { writeIndexName }
349+
new String[] { firstGenerationIndex }
393350
);
394351
ExplainDataStreamLifecycleAction.Response response = client().execute(
395352
ExplainDataStreamLifecycleAction.INSTANCE,
@@ -399,7 +356,7 @@ public void testExplainDataStreamLifecycleForUnmanagedIndices() throws Exception
399356
assertThat(response.getRolloverConfiguration(), nullValue());
400357
for (ExplainIndexDataStreamLifecycle explainIndex : response.getIndices()) {
401358
assertThat(explainIndex.isManagedByLifecycle(), is(false));
402-
assertThat(explainIndex.getIndex(), is(writeIndexName));
359+
assertThat(explainIndex.getIndex(), is(firstGenerationIndex));
403360
assertThat(explainIndex.getIndexCreationDate(), nullValue());
404361
assertThat(explainIndex.getLifecycle(), nullValue());
405362
assertThat(explainIndex.getGenerationTime(System::currentTimeMillis), nullValue());

muted-tests.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,6 @@ tests:
336336
- class: org.elasticsearch.xpack.ilm.DataStreamAndIndexLifecycleMixingTests
337337
method: testUpdateIndexTemplateFromILMtoBothILMAndDataStreamLifecycle
338338
issue: https://github.com/elastic/elasticsearch/issues/124850
339-
- class: org.elasticsearch.datastreams.lifecycle.ExplainDataStreamLifecycleIT
340-
method: testExplainLifecycle
341-
issue: https://github.com/elastic/elasticsearch/issues/124882
342-
- class: org.elasticsearch.datastreams.lifecycle.ExplainDataStreamLifecycleIT
343-
method: testSystemExplainLifecycle
344-
issue: https://github.com/elastic/elasticsearch/issues/124885
345339
- class: org.elasticsearch.xpack.ilm.DataStreamAndIndexLifecycleMixingTests
346340
method: testIndexTemplateSwapsILMForDataStreamLifecycle
347341
issue: https://github.com/elastic/elasticsearch/issues/124886

0 commit comments

Comments
 (0)