Skip to content

Commit 593c706

Browse files
authored
Remove test usages of DataStream#getDefaultBackingIndexName in ILM integration tests (#124319) (#124465)
* Incorporate review comments
1 parent d8686a3 commit 593c706

File tree

9 files changed

+73
-39
lines changed

9 files changed

+73
-39
lines changed

test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequestBuilder;
5050
import org.elasticsearch.action.bulk.BulkRequestBuilder;
5151
import org.elasticsearch.action.bulk.BulkResponse;
52+
import org.elasticsearch.action.datastreams.GetDataStreamAction;
5253
import org.elasticsearch.action.index.IndexRequestBuilder;
5354
import org.elasticsearch.action.ingest.DeletePipelineRequest;
5455
import org.elasticsearch.action.ingest.DeletePipelineTransportAction;
@@ -81,6 +82,7 @@
8182
import org.elasticsearch.cluster.InternalClusterInfoService;
8283
import org.elasticsearch.cluster.coordination.ElasticsearchNodeCommand;
8384
import org.elasticsearch.cluster.health.ClusterHealthStatus;
85+
import org.elasticsearch.cluster.metadata.DataStream;
8486
import org.elasticsearch.cluster.metadata.IndexMetadata;
8587
import org.elasticsearch.cluster.metadata.Metadata;
8688
import org.elasticsearch.cluster.node.DiscoveryNode;
@@ -846,6 +848,22 @@ private static Settings.Builder getExcludeSettings(int num, Settings.Builder bui
846848
return builder;
847849
}
848850

851+
/**
852+
* Returns a list of the data stream's backing index names.
853+
*/
854+
public List<String> getDataStreamBackingIndexNames(String dataStreamName) {
855+
GetDataStreamAction.Response response = safeGet(
856+
client().execute(
857+
GetDataStreamAction.INSTANCE,
858+
new GetDataStreamAction.Request(TEST_REQUEST_TIMEOUT, new String[] { dataStreamName })
859+
)
860+
);
861+
assertThat(response.getDataStreams().size(), equalTo(1));
862+
DataStream dataStream = response.getDataStreams().getFirst().getDataStream();
863+
assertThat(dataStream.getName(), equalTo(dataStreamName));
864+
return dataStream.getIndices().stream().map(Index::getName).toList();
865+
}
866+
849867
/**
850868
* Waits until all nodes have no pending tasks.
851869
*/

test/framework/src/main/java/org/elasticsearch/test/rest/ESRestTestCase.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,6 +2024,20 @@ protected static boolean aliasExists(String index, String alias) throws IOExcept
20242024
return RestStatus.OK.getStatus() == response.getStatusLine().getStatusCode();
20252025
}
20262026

2027+
/**
2028+
* Returns a list of the data stream's backing index names.
2029+
*/
2030+
@SuppressWarnings("unchecked")
2031+
protected static List<String> getDataStreamBackingIndexNames(String dataStreamName) throws IOException {
2032+
Map<String, Object> response = getAsMap(client(), "/_data_stream/" + dataStreamName);
2033+
List<?> dataStreams = (List<?>) response.get("data_streams");
2034+
assertThat(dataStreams.size(), equalTo(1));
2035+
Map<?, ?> dataStream = (Map<?, ?>) dataStreams.getFirst();
2036+
assertThat(dataStream.get("name"), equalTo(dataStreamName));
2037+
List<?> indices = (List<?>) dataStream.get("indices");
2038+
return indices.stream().map(index -> ((Map<String, String>) index).get("index_name")).toList();
2039+
}
2040+
20272041
@SuppressWarnings("unchecked")
20282042
protected static Map<String, Object> getAlias(final String index, final String alias) throws IOException {
20292043
String endpoint = "/_alias";

x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/LifecycleLicenseIT.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.elasticsearch.client.Request;
1212
import org.elasticsearch.client.Response;
1313
import org.elasticsearch.client.ResponseException;
14-
import org.elasticsearch.cluster.metadata.DataStream;
1514
import org.elasticsearch.cluster.metadata.Template;
1615
import org.elasticsearch.common.Strings;
1716
import org.elasticsearch.common.settings.Settings;
@@ -30,6 +29,7 @@
3029
import org.junit.Before;
3130

3231
import java.io.IOException;
32+
import java.util.List;
3333
import java.util.Locale;
3434
import java.util.Map;
3535
import java.util.concurrent.TimeUnit;
@@ -41,6 +41,7 @@
4141
import static org.elasticsearch.xpack.TimeSeriesRestDriver.indexDocument;
4242
import static org.elasticsearch.xpack.TimeSeriesRestDriver.rolloverMaxOneDocCondition;
4343
import static org.hamcrest.CoreMatchers.containsStringIgnoringCase;
44+
import static org.hamcrest.Matchers.equalTo;
4445
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
4546
import static org.hamcrest.Matchers.is;
4647

@@ -107,7 +108,9 @@ public void testSearchableSnapshotActionErrorsOnInvalidLicense() throws Exceptio
107108
// rolling over the data stream so we can apply the searchable snapshot policy to a backing index that's not the write index
108109
rolloverMaxOneDocCondition(client(), dataStream);
109110

110-
String backingIndexName = DataStream.getDefaultBackingIndexName(dataStream, 1L);
111+
List<String> backingIndices = getDataStreamBackingIndexNames(dataStream);
112+
assertThat(backingIndices.size(), equalTo(2));
113+
String backingIndexName = backingIndices.getFirst();
111114
// the searchable_snapshot action should start failing (and retrying) due to invalid license
112115
assertBusy(() -> {
113116
Map<String, Object> explainIndex = explainIndex(client(), backingIndexName);

x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeSeriesDataStreamsIT.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import static org.elasticsearch.xpack.TimeSeriesRestDriver.createNewSingletonPolicy;
4141
import static org.elasticsearch.xpack.TimeSeriesRestDriver.createSnapshotRepo;
4242
import static org.elasticsearch.xpack.TimeSeriesRestDriver.explainIndex;
43-
import static org.elasticsearch.xpack.TimeSeriesRestDriver.getBackingIndices;
4443
import static org.elasticsearch.xpack.TimeSeriesRestDriver.getOnlyIndexSettings;
4544
import static org.elasticsearch.xpack.TimeSeriesRestDriver.getStepKeyForIndex;
4645
import static org.elasticsearch.xpack.TimeSeriesRestDriver.getTemplate;
@@ -81,12 +80,12 @@ public void testRolloverAction() throws Exception {
8180
indexDocument(client(), dataStream, true);
8281

8382
assertBusy(() -> {
84-
final var backingIndices = getBackingIndices(client(), dataStream);
83+
final var backingIndices = getDataStreamBackingIndexNames(dataStream);
8584
assertEquals(2, backingIndices.size());
8685
assertTrue(Boolean.parseBoolean((String) getIndexSettingsAsMap(backingIndices.getLast()).get("index.hidden")));
8786
});
8887
assertBusy(() -> {
89-
final var backingIndices = getBackingIndices(client(), dataStream);
88+
final var backingIndices = getDataStreamBackingIndexNames(dataStream);
9089
assertEquals(PhaseCompleteStep.finalStep("hot").getKey(), getStepKeyForIndex(client(), backingIndices.getFirst()));
9190
});
9291
}
@@ -98,7 +97,7 @@ public void testRolloverIsSkippedOnManualDataStreamRollover() throws Exception {
9897

9998
indexDocument(client(), dataStream, true);
10099

101-
String firstGenerationIndex = getBackingIndices(client(), dataStream).getFirst();
100+
String firstGenerationIndex = getDataStreamBackingIndexNames(dataStream).getFirst();
102101
assertBusy(
103102
() -> assertThat(getStepKeyForIndex(client(), firstGenerationIndex).name(), equalTo(WaitForRolloverReadyStep.NAME)),
104103
30,
@@ -107,7 +106,7 @@ public void testRolloverIsSkippedOnManualDataStreamRollover() throws Exception {
107106

108107
rolloverMaxOneDocCondition(client(), dataStream);
109108
assertBusy(() -> {
110-
final var backingIndices = getBackingIndices(client(), dataStream);
109+
final var backingIndices = getDataStreamBackingIndexNames(dataStream);
111110
assertEquals(2, backingIndices.size());
112111
}, 30, TimeUnit.SECONDS);
113112

@@ -125,7 +124,7 @@ public void testShrinkActionInPolicyWithoutHotPhase() throws Exception {
125124
createComposableTemplate(client(), template, dataStream + "*", getTemplate(policyName));
126125
indexDocument(client(), dataStream, true);
127126

128-
String backingIndexName = getBackingIndices(client(), dataStream).getFirst();
127+
String backingIndexName = getDataStreamBackingIndexNames(dataStream).getFirst();
129128
assertBusy(
130129
() -> assertThat(
131130
"original index must wait in the " + CheckNotDataStreamWriteIndexStep.NAME + " until it is not the write index anymore",
@@ -140,7 +139,7 @@ public void testShrinkActionInPolicyWithoutHotPhase() throws Exception {
140139
rolloverMaxOneDocCondition(client(), dataStream);
141140
// Wait for rollover to happen
142141
assertBusy(
143-
() -> assertEquals("the rollover action created the rollover index", 2, getBackingIndices(client(), dataStream).size()),
142+
() -> assertEquals("the rollover action created the rollover index", 2, getDataStreamBackingIndexNames(dataStream).size()),
144143
30,
145144
TimeUnit.SECONDS
146145
);
@@ -159,7 +158,7 @@ public void testSearchableSnapshotAction() throws Exception {
159158
createComposableTemplate(client(), template, dataStream + "*", getTemplate(policyName));
160159
indexDocument(client(), dataStream, true);
161160

162-
String backingIndexName = getBackingIndices(client(), dataStream).getFirst();
161+
String backingIndexName = getDataStreamBackingIndexNames(dataStream).getFirst();
163162
String restoredIndexName = SearchableSnapshotAction.FULL_RESTORED_INDEX_PREFIX + backingIndexName;
164163

165164
assertBusy(
@@ -190,7 +189,7 @@ public void testReadOnlyAction() throws Exception {
190189
createComposableTemplate(client(), template, dataStream + "*", getTemplate(policyName));
191190
indexDocument(client(), dataStream, true);
192191

193-
String backingIndexName = getBackingIndices(client(), dataStream).getFirst();
192+
String backingIndexName = getDataStreamBackingIndexNames(dataStream).getFirst();
194193
assertBusy(
195194
() -> assertThat(
196195
"index must wait in the " + CheckNotDataStreamWriteIndexStep.NAME + " until it is not the write index anymore",
@@ -225,7 +224,7 @@ public void testFreezeAction() throws Exception {
225224
indexDocument(client(), dataStream, true);
226225

227226
// The freeze action is a noop action with only noop steps and should pass through to complete the phase asap.
228-
String backingIndexName = getBackingIndices(client(), dataStream).getFirst();
227+
String backingIndexName = getDataStreamBackingIndexNames(dataStream).getFirst();
229228
assertBusy(() -> {
230229
try {
231230
assertThat(explainIndex(client(), backingIndexName).get("step"), is(PhaseCompleteStep.NAME));
@@ -247,7 +246,7 @@ public void checkForceMergeAction(String codec) throws Exception {
247246
createComposableTemplate(client(), template, dataStream + "*", getTemplate(policyName));
248247
indexDocument(client(), dataStream, true);
249248

250-
String backingIndexName = getBackingIndices(client(), dataStream).getFirst();
249+
String backingIndexName = getDataStreamBackingIndexNames(dataStream).getFirst();
251250
assertBusy(
252251
() -> assertThat(
253252
"index must wait in the " + CheckNotDataStreamWriteIndexStep.NAME + " until it is not the write index anymore",
@@ -321,7 +320,7 @@ public void testDataStreamWithMultipleIndicesAndWriteIndexInDeletePhase() throws
321320
client().performRequest(new Request("POST", dataStream + "/_rollover"));
322321
indexDocument(client(), dataStream, true);
323322

324-
String secondGenerationIndex = getBackingIndices(client(), dataStream).get(1);
323+
String secondGenerationIndex = getDataStreamBackingIndexNames(dataStream).get(1);
325324
assertBusy(() -> {
326325
Request explainRequest = new Request("GET", "/_data_stream/" + dataStream);
327326
Response response = client().performRequest(explainRequest);

x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/TimeSeriesLifecycleActionsIT.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.elasticsearch.client.Response;
1616
import org.elasticsearch.client.ResponseException;
1717
import org.elasticsearch.client.WarningFailureException;
18-
import org.elasticsearch.cluster.metadata.DataStream;
1918
import org.elasticsearch.cluster.metadata.IndexMetadata;
2019
import org.elasticsearch.common.Strings;
2120
import org.elasticsearch.common.settings.Settings;
@@ -1231,7 +1230,7 @@ private void assertHistoryIsPresent(
12311230
}
12321231

12331232
// Finally, check that the history index is in a good state
1234-
String historyIndexName = DataStream.getDefaultBackingIndexName("ilm-history-7", 1);
1233+
String historyIndexName = getDataStreamBackingIndexNames("ilm-history-7").getFirst();
12351234
Response explainHistoryIndex = client().performRequest(new Request("GET", historyIndexName + "/_lifecycle/explain"));
12361235
Map<String, Object> responseMap;
12371236
try (InputStream is = explainHistoryIndex.getEntity().getContent()) {

x-pack/plugin/ilm/qa/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/ilm/actions/DownsampleActionIT.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.elasticsearch.client.Response;
1212
import org.elasticsearch.client.ResponseException;
1313
import org.elasticsearch.client.RestClient;
14-
import org.elasticsearch.cluster.metadata.DataStream;
1514
import org.elasticsearch.cluster.metadata.IndexMetadata;
1615
import org.elasticsearch.cluster.metadata.IndexMetadata.DownsampleTaskStatus;
1716
import org.elasticsearch.common.Strings;
@@ -49,7 +48,6 @@
4948
import static org.elasticsearch.xpack.TimeSeriesRestDriver.createIndexWithSettings;
5049
import static org.elasticsearch.xpack.TimeSeriesRestDriver.createNewSingletonPolicy;
5150
import static org.elasticsearch.xpack.TimeSeriesRestDriver.explainIndex;
52-
import static org.elasticsearch.xpack.TimeSeriesRestDriver.getBackingIndices;
5351
import static org.elasticsearch.xpack.TimeSeriesRestDriver.getOnlyIndexSettings;
5452
import static org.elasticsearch.xpack.TimeSeriesRestDriver.getStepKeyForIndex;
5553
import static org.elasticsearch.xpack.TimeSeriesRestDriver.index;
@@ -318,7 +316,7 @@ public void testTsdbDataStreams() throws Exception {
318316

319317
index(client(), dataStream, true, null, "@timestamp", "2020-01-01T05:10:00Z", "volume", 11.0, "metricset", randomAlphaOfLength(5));
320318

321-
String backingIndexName = DataStream.getDefaultBackingIndexName(dataStream, 1);
319+
String backingIndexName = getDataStreamBackingIndexNames(dataStream).getFirst();
322320
assertBusy(
323321
() -> assertThat(
324322
"index must wait in the " + CheckNotDataStreamWriteIndexStep.NAME + " until it is not the write index anymore",
@@ -365,7 +363,7 @@ public void testILMWaitsForTimeSeriesEndTimeToLapse() throws Exception {
365363
String now = DateFormatter.forPattern(FormatNames.STRICT_DATE_OPTIONAL_TIME.getName()).format(Instant.now());
366364
index(client(), dataStream, true, null, "@timestamp", now, "volume", 11.0, "metricset", randomAlphaOfLength(5));
367365

368-
String backingIndexName = getBackingIndices(client(), dataStream).get(0);
366+
String backingIndexName = getDataStreamBackingIndexNames(dataStream).getFirst();
369367
assertBusy(
370368
() -> assertThat(
371369
"index must wait in the " + CheckNotDataStreamWriteIndexStep.NAME + " until it is not the write index anymore",
@@ -459,7 +457,7 @@ public void testDownsampleTwice() throws Exception {
459457

460458
index(client(), dataStream, true, null, "@timestamp", "2020-01-01T05:10:00Z", "volume", 11.0, "metricset", randomAlphaOfLength(5));
461459

462-
String firstBackingIndex = DataStream.getDefaultBackingIndexName(dataStream, 1);
460+
String firstBackingIndex = getDataStreamBackingIndexNames(dataStream).getFirst();
463461
logger.info("--> firstBackingIndex: {}", firstBackingIndex);
464462
assertBusy(
465463
() -> assertThat(
@@ -540,7 +538,7 @@ public void testDownsampleTwiceSameInterval() throws Exception {
540538

541539
index(client(), dataStream, true, null, "@timestamp", "2020-01-01T05:10:00Z", "volume", 11.0, "metricset", randomAlphaOfLength(5));
542540

543-
String firstBackingIndex = getBackingIndices(client(), dataStream).get(0);
541+
String firstBackingIndex = getDataStreamBackingIndexNames(dataStream).getFirst();
544542
logger.info("--> firstBackingIndex: {}", firstBackingIndex);
545543
assertBusy(
546544
() -> assertThat(

0 commit comments

Comments
 (0)