Skip to content

Commit 640bf4e

Browse files
authored
Correctly retrieve data stream write index in DataStreamsUpgradeIT (#133433)
The `testUpgradeDataStream` test configures an ILM policy with force merge on a data stream, which is then reindexed after a cluster upgrade. The test used to determine the write index of the data stream by looking at the backing index of the data stream in the cluster state metadata and retrieving the index that was created last. While this may have worked for this test suite thusfar, this is generally not the right way to determine the write index of a data stream, as newer indices can be put inside the data stream either manually or by ILM. The latter is the case with the upcoming force merge improvements, where we perform force merge on a cloned index, meaning the cloned index might be newer than the write index, resulting in a test failure because the real write index is not skipped in the `checkILMPhase` method and thus fails on the ILM `phase` assertion. We also improve the assertion message to include some more information to aid test failure investigation.
1 parent a8d1f08 commit 640bf4e

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/DataStreamsUpgradeIT.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -208,19 +208,21 @@ public void testUpgradeDataStream() throws Exception {
208208
createDataStreamFromNonDataStreamIndices(dataStreamFromNonDataStreamIndices);
209209
} else if (CLUSTER_TYPE == ClusterType.UPGRADED) {
210210
Map<String, Map<String, Object>> oldIndicesMetadata = getIndicesMetadata(dataStreamName);
211+
String oldWriteIndex = getDataStreamBackingIndexNames(dataStreamName).getLast();
211212
upgradeDataStream(dataStreamName, numRollovers, numRollovers + 1, 0, ilmEnabled);
212213
cancelReindexTask(dataStreamName);
213214
upgradeDataStream(dataStreamFromNonDataStreamIndices, 0, 1, 0, ilmEnabled);
214215
cancelReindexTask(dataStreamFromNonDataStreamIndices);
215216
Map<String, Map<String, Object>> upgradedIndicesMetadata = getIndicesMetadata(dataStreamName);
217+
String newWriteIndex = getDataStreamBackingIndexNames(dataStreamName).getLast();
216218

217219
if (ilmEnabled) {
218-
checkILMPhase(dataStreamName, upgradedIndicesMetadata);
220+
checkILMPhase(dataStreamName, newWriteIndex);
219221
// Delete the data streams to avoid ILM continuously running cluster state tasks, see
220222
// https://github.com/elastic/elasticsearch/issues/129097#issuecomment-3016122739
221223
deleteDataStream(dataStreamName);
222224
} else {
223-
compareIndexMetadata(oldIndicesMetadata, upgradedIndicesMetadata);
225+
compareIndexMetadata(oldIndicesMetadata, oldWriteIndex, upgradedIndicesMetadata);
224226
}
225227
}
226228
}
@@ -262,9 +264,9 @@ private void cancelReindexTask(String dataStreamName) throws IOException {
262264

263265
private void compareIndexMetadata(
264266
Map<String, Map<String, Object>> oldIndicesMetadata,
267+
String oldWriteIndex,
265268
Map<String, Map<String, Object>> upgradedIndicesMetadata
266269
) {
267-
String oldWriteIndex = getWriteIndexFromDataStreamIndexMetadata(oldIndicesMetadata);
268270
for (Map.Entry<String, Map<String, Object>> upgradedIndexEntry : upgradedIndicesMetadata.entrySet()) {
269271
String upgradedIndexName = upgradedIndexEntry.getKey();
270272
if (upgradedIndexName.startsWith(".migrated-")) {
@@ -287,10 +289,8 @@ private void compareIndexMetadata(
287289
}
288290

289291
@SuppressWarnings("unchecked")
290-
private void checkILMPhase(String dataStreamName, Map<String, Map<String, Object>> upgradedIndicesMetadata) throws Exception {
291-
var writeIndex = getWriteIndexFromDataStreamIndexMetadata(upgradedIndicesMetadata);
292+
private void checkILMPhase(String dataStreamName, String writeIndex) throws Exception {
292293
assertBusy(() -> {
293-
294294
Request request = new Request("GET", dataStreamName + "/_ilm/explain");
295295
Response response = client().performRequest(request);
296296
Map<String, Object> responseMap = XContentHelper.convertToMap(
@@ -302,21 +302,12 @@ private void checkILMPhase(String dataStreamName, Map<String, Map<String, Object
302302
for (var index : indices.keySet()) {
303303
if (index.equals(writeIndex) == false) {
304304
Map<String, Object> ilmInfo = (Map<String, Object>) indices.get(index);
305-
assertThat("Index has not moved to cold ILM phase", ilmInfo.get("phase"), equalTo("cold"));
305+
assertThat("Index [" + index + "] has not moved to cold ILM phase, " + indices, ilmInfo.get("phase"), equalTo("cold"));
306306
}
307307
}
308308
}, 30, TimeUnit.SECONDS);
309309
}
310310

311-
private String getWriteIndexFromDataStreamIndexMetadata(Map<String, Map<String, Object>> indexMetadataForDataStream) {
312-
return indexMetadataForDataStream.entrySet()
313-
.stream()
314-
.sorted((o1, o2) -> Long.compare(getCreationDate(o2.getValue()), getCreationDate(o1.getValue())))
315-
.map(Map.Entry::getKey)
316-
.findFirst()
317-
.get();
318-
}
319-
320311
private void startILM() throws IOException {
321312
setILMInterval();
322313
var request = new Request("POST", "/_ilm/start");

0 commit comments

Comments
 (0)