From 4f2f257b12c0903c04d1c0f38ec5c5d91814fcd8 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Mon, 29 Jun 2020 10:58:41 +0200 Subject: [PATCH] Fix DataStream Handling on Restore of Global Metadata (#58631) (#58649) When restoring a global metadata snapshot we were overwriting the correctly adjusted data streams in the metadata when looping over all custom values. Closes #58496 --- .../snapshots/DataStreamsSnapshotsIT.java | 43 +++++++++++++++++++ .../snapshots/RestoreService.java | 5 ++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/server/src/internalClusterTest/java/org/elasticsearch/snapshots/DataStreamsSnapshotsIT.java b/server/src/internalClusterTest/java/org/elasticsearch/snapshots/DataStreamsSnapshotsIT.java index e3a35ab5b28..8b219a8e45d 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/snapshots/DataStreamsSnapshotsIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/snapshots/DataStreamsSnapshotsIT.java @@ -29,6 +29,7 @@ import org.elasticsearch.action.admin.indices.datastream.CreateDataStreamAction; import org.elasticsearch.action.admin.indices.datastream.DeleteDataStreamAction; import org.elasticsearch.action.admin.indices.datastream.GetDataStreamAction; import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.DataStream; @@ -42,6 +43,8 @@ import java.nio.file.Path; import java.util.Collections; import java.util.Map; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; + public class DataStreamsSnapshotsIT extends AbstractSnapshotIntegTestCase { private static final String DS_BACKING_INDEX_NAME = DataStream.getDefaultBackingIndexName("ds", 1); @@ -114,6 +117,46 @@ public class DataStreamsSnapshotsIT extends AbstractSnapshotIntegTestCase { assertEquals(DS_BACKING_INDEX_NAME, ds.getDataStreams().get(0).getIndices().get(0).getName()); } + public void testSnapshotAndRestoreAll() throws Exception { + CreateSnapshotResponse createSnapshotResponse = client.admin().cluster() + .prepareCreateSnapshot(REPO, SNAPSHOT) + .setWaitForCompletion(true) + .setIndices("ds") + .setIncludeGlobalState(false) + .get(); + + RestStatus status = createSnapshotResponse.getSnapshotInfo().status(); + assertEquals(RestStatus.OK, status); + + GetSnapshotsResponse snapshot = client.admin().cluster().prepareGetSnapshots(REPO).setSnapshots(SNAPSHOT).get(); + java.util.List snap = snapshot.getSnapshots(); + assertEquals(1, snap.size()); + assertEquals(Collections.singletonList(DS_BACKING_INDEX_NAME), snap.get(0).indices()); + + assertAcked(client.admin().indices().deleteDataStream(new DeleteDataStreamAction.Request("*")).get()); + assertAcked(client.admin().indices().prepareDelete("*").setIndicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN_CLOSED_HIDDEN)); + + RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster() + .prepareRestoreSnapshot(REPO, SNAPSHOT) + .setWaitForCompletion(true) + .setRestoreGlobalState(true) + .get(); + + assertEquals(1, restoreSnapshotResponse.getRestoreInfo().successfulShards()); + + assertEquals(DOCUMENT_SOURCE, client.prepareGet(DS_BACKING_INDEX_NAME, "_doc", id).get().getSourceAsMap()); + SearchHit[] hits = client.prepareSearch("ds").get().getHits().getHits(); + assertEquals(1, hits.length); + assertEquals(DOCUMENT_SOURCE, hits[0].getSourceAsMap()); + + GetDataStreamAction.Response ds = client.admin().indices().getDataStreams(new GetDataStreamAction.Request("ds")).get(); + assertEquals(1, ds.getDataStreams().size()); + assertEquals(1, ds.getDataStreams().get(0).getIndices().size()); + assertEquals(DS_BACKING_INDEX_NAME, ds.getDataStreams().get(0).getIndices().get(0).getName()); + + assertAcked(client().admin().indices().deleteDataStream(new DeleteDataStreamAction.Request("ds")).get()); + } + public void testRename() throws Exception { CreateSnapshotResponse createSnapshotResponse = client.admin().cluster() .prepareCreateSnapshot(REPO, SNAPSHOT) diff --git a/server/src/main/java/org/elasticsearch/snapshots/RestoreService.java b/server/src/main/java/org/elasticsearch/snapshots/RestoreService.java index d2c6a8c6781..cd2002c6d81 100644 --- a/server/src/main/java/org/elasticsearch/snapshots/RestoreService.java +++ b/server/src/main/java/org/elasticsearch/snapshots/RestoreService.java @@ -43,6 +43,7 @@ import org.elasticsearch.cluster.SnapshotDeletionsInProgress; import org.elasticsearch.cluster.block.ClusterBlocks; import org.elasticsearch.cluster.metadata.AliasMetadata; import org.elasticsearch.cluster.metadata.DataStream; +import org.elasticsearch.cluster.metadata.DataStreamMetadata; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.cluster.metadata.IndexTemplateMetadata; import org.elasticsearch.cluster.metadata.Metadata; @@ -434,9 +435,11 @@ public class RestoreService implements ClusterStateApplier { } if (metadata.customs() != null) { for (ObjectObjectCursor cursor : metadata.customs()) { - if (!RepositoriesMetadata.TYPE.equals(cursor.key)) { + if (RepositoriesMetadata.TYPE.equals(cursor.key) == false + && DataStreamMetadata.TYPE.equals(cursor.key) == false) { // Don't restore repositories while we are working with them // TODO: Should we restore them at the end? + // Also, don't restore data streams here, we already added them to the metadata builder above mdBuilder.putCustom(cursor.key, cursor.value); } }