From 0560caaeaea451e576031916be9489fcd922037e Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Fri, 28 May 2021 13:17:33 +0200 Subject: [PATCH] Make DataStreamsSnapshotsIT resilient to failures because of local time. (#73516) Backing index names contain a date component. Instead of defining what the expected backing index names are before creating the data streams, resolve these expected backing index names after the data streams are created. This way we avoid failures that can occur around midnight (local time), where the expected names are created before midnight and the data streams are created after midnight. Closes #73510 --- .../datastreams/DataStreamsSnapshotsIT.java | 50 +++++++++++-------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/x-pack/plugin/data-streams/src/internalClusterTest/java/org/elasticsearch/datastreams/DataStreamsSnapshotsIT.java b/x-pack/plugin/data-streams/src/internalClusterTest/java/org/elasticsearch/datastreams/DataStreamsSnapshotsIT.java index 8444c773673b4..d1479b80259f7 100644 --- a/x-pack/plugin/data-streams/src/internalClusterTest/java/org/elasticsearch/datastreams/DataStreamsSnapshotsIT.java +++ b/x-pack/plugin/data-streams/src/internalClusterTest/java/org/elasticsearch/datastreams/DataStreamsSnapshotsIT.java @@ -68,13 +68,13 @@ public class DataStreamsSnapshotsIT extends AbstractSnapshotIntegTestCase { - private static final String DS_BACKING_INDEX_NAME = DataStream.getDefaultBackingIndexName("ds", 1); - private static final String DS2_BACKING_INDEX_NAME = DataStream.getDefaultBackingIndexName("ds2", 1); private static final Map DOCUMENT_SOURCE = Collections.singletonMap("@timestamp", 123); public static final String REPO = "repo"; public static final String SNAPSHOT = "snap"; private Client client; + private String dsBackingIndexName; + private String ds2BackingIndexName; private String id; @Override @@ -98,6 +98,14 @@ public void setup() throws Exception { response = client.execute(CreateDataStreamAction.INSTANCE, request).get(); assertTrue(response.isAcknowledged()); + // Resolve backing index names after data streams have been created: + // (these names have a date component, and running around midnight could lead to test failures otherwise) + GetDataStreamAction.Request getDataStreamRequest = new GetDataStreamAction.Request(new String[] { "*" }); + GetDataStreamAction.Response getDataStreamResponse = client.execute(GetDataStreamAction.INSTANCE, getDataStreamRequest).actionGet(); + dsBackingIndexName = getDataStreamResponse.getDataStreams().get(0).getDataStream().getIndices().get(0).getName(); + // Will be used in some tests, to test renaming while restoring a snapshot: + ds2BackingIndexName = dsBackingIndexName.replace("-ds-", "-ds2-"); + IndexResponse indexResponse = client.prepareIndex("ds").setOpType(DocWriteRequest.OpType.CREATE).setSource(DOCUMENT_SOURCE).get(); assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult()); id = indexResponse.getId(); @@ -129,7 +137,7 @@ public void testSnapshotAndRestore() throws Exception { RestStatus status = createSnapshotResponse.getSnapshotInfo().status(); assertEquals(RestStatus.OK, status); - assertEquals(Collections.singletonList(DS_BACKING_INDEX_NAME), getSnapshot(REPO, SNAPSHOT).indices()); + assertEquals(Collections.singletonList(dsBackingIndexName), getSnapshot(REPO, SNAPSHOT).indices()); assertTrue( client.execute(DeleteDataStreamAction.INSTANCE, new DeleteDataStreamAction.Request(new String[] { "ds" })) @@ -146,7 +154,7 @@ public void testSnapshotAndRestore() throws Exception { assertEquals(1, restoreSnapshotResponse.getRestoreInfo().successfulShards()); - assertEquals(DOCUMENT_SOURCE, client.prepareGet(DS_BACKING_INDEX_NAME, id).get().getSourceAsMap()); + assertEquals(DOCUMENT_SOURCE, client.prepareGet(dsBackingIndexName, id).get().getSourceAsMap()); SearchHit[] hits = client.prepareSearch("ds").get().getHits().getHits(); assertEquals(1, hits.length); assertEquals(DOCUMENT_SOURCE, hits[0].getSourceAsMap()); @@ -157,7 +165,7 @@ public void testSnapshotAndRestore() throws Exception { ).get(); assertEquals(1, ds.getDataStreams().size()); assertEquals(1, ds.getDataStreams().get(0).getDataStream().getIndices().size()); - assertEquals(DS_BACKING_INDEX_NAME, ds.getDataStreams().get(0).getDataStream().getIndices().get(0).getName()); + assertEquals(dsBackingIndexName, ds.getDataStreams().get(0).getDataStream().getIndices().get(0).getName()); GetAliasesResponse getAliasesResponse = client.admin().indices().getAliases(new GetAliasesRequest("my-alias")).actionGet(); assertThat(getAliasesResponse.getDataStreamAliases().keySet(), containsInAnyOrder("ds", "other-ds")); @@ -179,7 +187,7 @@ public void testSnapshotAndRestoreAllDataStreamsInPlace() throws Exception { RestStatus status = createSnapshotResponse.getSnapshotInfo().status(); assertEquals(RestStatus.OK, status); - assertEquals(Collections.singletonList(DS_BACKING_INDEX_NAME), getSnapshot(REPO, SNAPSHOT).indices()); + assertEquals(Collections.singletonList(dsBackingIndexName), getSnapshot(REPO, SNAPSHOT).indices()); // Close all indices: CloseIndexRequest closeIndexRequest = new CloseIndexRequest("*"); @@ -194,7 +202,7 @@ public void testSnapshotAndRestoreAllDataStreamsInPlace() throws Exception { .get(); assertEquals(1, restoreSnapshotResponse.getRestoreInfo().successfulShards()); - assertEquals(DOCUMENT_SOURCE, client.prepareGet(DS_BACKING_INDEX_NAME, id).get().getSourceAsMap()); + assertEquals(DOCUMENT_SOURCE, client.prepareGet(dsBackingIndexName, id).get().getSourceAsMap()); SearchHit[] hits = client.prepareSearch("ds").get().getHits().getHits(); assertEquals(1, hits.length); assertEquals(DOCUMENT_SOURCE, hits[0].getSourceAsMap()); @@ -206,7 +214,7 @@ public void testSnapshotAndRestoreAllDataStreamsInPlace() throws Exception { contains(equalTo("ds"), equalTo("other-ds")) ); List backingIndices = ds.getDataStreams().get(0).getDataStream().getIndices(); - assertThat(backingIndices.stream().map(Index::getName).collect(Collectors.toList()), contains(DS_BACKING_INDEX_NAME)); + assertThat(backingIndices.stream().map(Index::getName).collect(Collectors.toList()), contains(dsBackingIndexName)); backingIndices = ds.getDataStreams().get(1).getDataStream().getIndices(); String expectedBackingIndexName = DataStream.getDefaultBackingIndexName("other-ds", 1); assertThat(backingIndices.stream().map(Index::getName).collect(Collectors.toList()), contains(expectedBackingIndexName)); @@ -224,7 +232,7 @@ public void testSnapshotAndRestoreInPlace() throws Exception { RestStatus status = createSnapshotResponse.getSnapshotInfo().status(); assertEquals(RestStatus.OK, status); - assertEquals(Collections.singletonList(DS_BACKING_INDEX_NAME), getSnapshot(REPO, SNAPSHOT).indices()); + assertEquals(Collections.singletonList(dsBackingIndexName), getSnapshot(REPO, SNAPSHOT).indices()); // A rollover after taking snapshot. The new backing index should be a standalone index after restoring // and not part of the data stream: @@ -246,7 +254,7 @@ public void testSnapshotAndRestoreInPlace() throws Exception { .get(); assertEquals(1, restoreSnapshotResponse.getRestoreInfo().successfulShards()); - assertEquals(DOCUMENT_SOURCE, client.prepareGet(DS_BACKING_INDEX_NAME, id).get().getSourceAsMap()); + assertEquals(DOCUMENT_SOURCE, client.prepareGet(dsBackingIndexName, id).get().getSourceAsMap()); SearchHit[] hits = client.prepareSearch("ds").get().getHits().getHits(); assertEquals(1, hits.length); assertEquals(DOCUMENT_SOURCE, hits[0].getSourceAsMap()); @@ -259,7 +267,7 @@ public void testSnapshotAndRestoreInPlace() throws Exception { ); List backingIndices = ds.getDataStreams().get(0).getDataStream().getIndices(); assertThat(ds.getDataStreams().get(0).getDataStream().getIndices(), hasSize(1)); - assertThat(backingIndices.stream().map(Index::getName).collect(Collectors.toList()), contains(equalTo(DS_BACKING_INDEX_NAME))); + assertThat(backingIndices.stream().map(Index::getName).collect(Collectors.toList()), contains(equalTo(dsBackingIndexName))); // The backing index created as part of rollover should still exist (but just not part of the data stream) assertThat(indexExists(DataStream.getDefaultBackingIndexName("ds", 2)), is(true)); @@ -282,7 +290,7 @@ public void testSnapshotAndRestoreAll() throws Exception { RestStatus status = createSnapshotResponse.getSnapshotInfo().status(); assertEquals(RestStatus.OK, status); - assertEquals(Collections.singletonList(DS_BACKING_INDEX_NAME), getSnapshot(REPO, SNAPSHOT).indices()); + assertEquals(Collections.singletonList(dsBackingIndexName), getSnapshot(REPO, SNAPSHOT).indices()); assertAcked(client.execute(DeleteDataStreamAction.INSTANCE, new DeleteDataStreamAction.Request(new String[] { "*" })).get()); assertAcked(client.admin().indices().prepareDelete("*").setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN)); @@ -296,7 +304,7 @@ public void testSnapshotAndRestoreAll() throws Exception { assertEquals(1, restoreSnapshotResponse.getRestoreInfo().successfulShards()); - assertEquals(DOCUMENT_SOURCE, client.prepareGet(DS_BACKING_INDEX_NAME, id).get().getSourceAsMap()); + assertEquals(DOCUMENT_SOURCE, client.prepareGet(dsBackingIndexName, id).get().getSourceAsMap()); SearchHit[] hits = client.prepareSearch("ds").get().getHits().getHits(); assertEquals(1, hits.length); assertEquals(DOCUMENT_SOURCE, hits[0].getSourceAsMap()); @@ -307,7 +315,7 @@ public void testSnapshotAndRestoreAll() throws Exception { ).get(); assertEquals(1, ds.getDataStreams().size()); assertEquals(1, ds.getDataStreams().get(0).getDataStream().getIndices().size()); - assertEquals(DS_BACKING_INDEX_NAME, ds.getDataStreams().get(0).getDataStream().getIndices().get(0).getName()); + assertEquals(dsBackingIndexName, ds.getDataStreams().get(0).getDataStream().getIndices().get(0).getName()); GetAliasesResponse getAliasesResponse = client.admin().indices().getAliases(new GetAliasesRequest("my-alias")).actionGet(); assertThat(getAliasesResponse.getDataStreamAliases().keySet(), containsInAnyOrder("ds")); @@ -348,9 +356,9 @@ public void testRename() throws Exception { ).get(); assertEquals(1, ds.getDataStreams().size()); assertEquals(1, ds.getDataStreams().get(0).getDataStream().getIndices().size()); - assertEquals(DS2_BACKING_INDEX_NAME, ds.getDataStreams().get(0).getDataStream().getIndices().get(0).getName()); + assertEquals(ds2BackingIndexName, ds.getDataStreams().get(0).getDataStream().getIndices().get(0).getName()); assertEquals(DOCUMENT_SOURCE, client.prepareSearch("ds2").get().getHits().getHits()[0].getSourceAsMap()); - assertEquals(DOCUMENT_SOURCE, client.prepareGet(DS2_BACKING_INDEX_NAME, id).get().getSourceAsMap()); + assertEquals(DOCUMENT_SOURCE, client.prepareGet(ds2BackingIndexName, id).get().getSourceAsMap()); GetAliasesResponse getAliasesResponse = client.admin().indices().getAliases(new GetAliasesRequest("my-alias")).actionGet(); assertThat(getAliasesResponse.getDataStreamAliases().keySet(), containsInAnyOrder("ds", "ds2", "other-ds")); @@ -388,7 +396,7 @@ public void testBackingIndexIsNotRenamedWhenRestoringDataStream() { .prepareRestoreSnapshot(REPO, SNAPSHOT) .setWaitForCompletion(true) .setIndices("ds") - .setRenamePattern(DS_BACKING_INDEX_NAME) + .setRenamePattern(dsBackingIndexName) .setRenameReplacement("new_index_name") .get(); @@ -396,7 +404,7 @@ public void testBackingIndexIsNotRenamedWhenRestoringDataStream() { GetDataStreamAction.Request getDSRequest = new GetDataStreamAction.Request(new String[] { "ds" }); GetDataStreamAction.Response response = client.execute(GetDataStreamAction.INSTANCE, getDSRequest).actionGet(); - assertThat(response.getDataStreams().get(0).getDataStream().getIndices().get(0).getName(), is(DS_BACKING_INDEX_NAME)); + assertThat(response.getDataStreams().get(0).getDataStream().getIndices().get(0).getName(), is(dsBackingIndexName)); } public void testDataStreamAndBackingIndicesAreRenamedUsingRegex() { @@ -439,7 +447,7 @@ public void testDataStreamAndBackingIndicesAreRenamedUsingRegex() { // data stream "ds" should still exist in the system GetDataStreamAction.Request getDSRequest = new GetDataStreamAction.Request(new String[] { "ds" }); response = client.execute(GetDataStreamAction.INSTANCE, getDSRequest).actionGet(); - assertThat(response.getDataStreams().get(0).getDataStream().getIndices().get(0).getName(), is(DS_BACKING_INDEX_NAME)); + assertThat(response.getDataStreams().get(0).getDataStream().getIndices().get(0).getName(), is(dsBackingIndexName)); } public void testWildcards() throws Exception { @@ -471,7 +479,7 @@ public void testWildcards() throws Exception { ).get(); assertEquals(1, ds.getDataStreams().size()); assertEquals(1, ds.getDataStreams().get(0).getDataStream().getIndices().size()); - assertEquals(DS2_BACKING_INDEX_NAME, ds.getDataStreams().get(0).getDataStream().getIndices().get(0).getName()); + assertEquals(ds2BackingIndexName, ds.getDataStreams().get(0).getDataStream().getIndices().get(0).getName()); assertThat( "we renamed the restored data stream to one that doesn't match any existing composable template", ds.getDataStreams().get(0).getIndexTemplate(), @@ -484,7 +492,7 @@ public void testDataStreamNotStoredWhenIndexRequested() { .cluster() .prepareCreateSnapshot(REPO, "snap2") .setWaitForCompletion(true) - .setIndices(DS_BACKING_INDEX_NAME) + .setIndices(dsBackingIndexName) .setIncludeGlobalState(false) .get();