-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…58371) * Add support for snapshot and restore to data streams (#57675) This change adds support for including data streams in snapshots. Names are provided in indices field (the same way as in other APIs), wildcards are supported. If rename pattern is specified it renames both data streams and backing indices. It also adds test to make sure SLM works correctly. Closes #57127 Relates to #53100 * version fix * compilation fix * compilation fix * remove unused changes * compilation fix * test fix
- Loading branch information
1 parent
bf8641a
commit a44dad9
Showing
24 changed files
with
813 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
218 changes: 218 additions & 0 deletions
218
server/src/internalClusterTest/java/org/elasticsearch/snapshots/DataStreamsSnapshotsIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.snapshots; | ||
|
||
import org.elasticsearch.ResourceNotFoundException; | ||
import org.elasticsearch.action.DocWriteRequest; | ||
import org.elasticsearch.action.DocWriteResponse; | ||
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; | ||
import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse; | ||
import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse; | ||
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.master.AcknowledgedResponse; | ||
import org.elasticsearch.client.Client; | ||
import org.elasticsearch.cluster.metadata.DataStream; | ||
import org.elasticsearch.indices.DataStreamIT; | ||
import org.elasticsearch.rest.RestStatus; | ||
import org.elasticsearch.search.SearchHit; | ||
import org.junit.Before; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
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<String, Integer> DOCUMENT_SOURCE = Collections.singletonMap("@timestamp", 123); | ||
public static final String REPO = "repo"; | ||
public static final String SNAPSHOT = "snap"; | ||
private Client client; | ||
|
||
private String id; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
client = client(); | ||
Path location = randomRepoPath(); | ||
createRepository(REPO, "fs", location); | ||
|
||
DataStreamIT.createIndexTemplate("t1", "@timestamp", "ds", "other-ds"); | ||
|
||
CreateDataStreamAction.Request request = new CreateDataStreamAction.Request("ds"); | ||
AcknowledgedResponse response = client.admin().indices().createDataStream(request).get(); | ||
assertTrue(response.isAcknowledged()); | ||
|
||
request = new CreateDataStreamAction.Request("other-ds"); | ||
response = client.admin().indices().createDataStream(request).get(); | ||
assertTrue(response.isAcknowledged()); | ||
|
||
IndexResponse indexResponse = client.prepareIndex("ds", "_doc") | ||
.setOpType(DocWriteRequest.OpType.CREATE) | ||
.setSource(DOCUMENT_SOURCE) | ||
.get(); | ||
assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult()); | ||
id = indexResponse.getId(); | ||
} | ||
|
||
public void testSnapshotAndRestore() 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(); | ||
List<SnapshotInfo> snap = snapshot.getSnapshots(); | ||
assertEquals(1, snap.size()); | ||
assertEquals(Collections.singletonList(DS_BACKING_INDEX_NAME), snap.get(0).indices()); | ||
|
||
assertTrue(client.admin().indices().deleteDataStream(new DeleteDataStreamAction.Request("ds")).get().isAcknowledged()); | ||
|
||
RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster() | ||
.prepareRestoreSnapshot(REPO, SNAPSHOT) | ||
.setWaitForCompletion(true) | ||
.setIndices("ds") | ||
.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()); | ||
} | ||
|
||
public void testRename() 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); | ||
|
||
expectThrows(SnapshotRestoreException.class, () -> client.admin().cluster() | ||
.prepareRestoreSnapshot(REPO, SNAPSHOT) | ||
.setWaitForCompletion(true) | ||
.setIndices("ds") | ||
.get()); | ||
|
||
client.admin().cluster() | ||
.prepareRestoreSnapshot(REPO, SNAPSHOT) | ||
.setWaitForCompletion(true) | ||
.setIndices("ds") | ||
.setRenamePattern("ds") | ||
.setRenameReplacement("ds2") | ||
.get(); | ||
|
||
GetDataStreamAction.Response ds = client.admin().indices().getDataStreams(new GetDataStreamAction.Request("ds2")).get(); | ||
assertEquals(1, ds.getDataStreams().size()); | ||
assertEquals(1, ds.getDataStreams().get(0).getIndices().size()); | ||
assertEquals(DS2_BACKING_INDEX_NAME, ds.getDataStreams().get(0).getIndices().get(0).getName()); | ||
assertEquals(DOCUMENT_SOURCE, client.prepareSearch("ds2").get().getHits().getHits()[0].getSourceAsMap()); | ||
assertEquals(DOCUMENT_SOURCE, client.prepareGet(DS2_BACKING_INDEX_NAME, "_doc", id).get().getSourceAsMap()); | ||
} | ||
|
||
public void testWildcards() throws Exception { | ||
CreateSnapshotResponse createSnapshotResponse = client.admin().cluster() | ||
.prepareCreateSnapshot(REPO, "snap2") | ||
.setWaitForCompletion(true) | ||
.setIndices("d*") | ||
.setIncludeGlobalState(false) | ||
.get(); | ||
|
||
RestStatus status = createSnapshotResponse.getSnapshotInfo().status(); | ||
assertEquals(RestStatus.OK, status); | ||
|
||
RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster() | ||
.prepareRestoreSnapshot(REPO, "snap2") | ||
.setWaitForCompletion(true) | ||
.setIndices("d*") | ||
.setRenamePattern("ds") | ||
.setRenameReplacement("ds2") | ||
.get(); | ||
|
||
assertEquals(RestStatus.OK, restoreSnapshotResponse.status()); | ||
|
||
GetDataStreamAction.Response ds = client.admin().indices().getDataStreams(new GetDataStreamAction.Request("ds2")).get(); | ||
assertEquals(1, ds.getDataStreams().size()); | ||
assertEquals(1, ds.getDataStreams().get(0).getIndices().size()); | ||
assertEquals(DS2_BACKING_INDEX_NAME, ds.getDataStreams().get(0).getIndices().get(0).getName()); | ||
} | ||
|
||
public void testDataStreamNotStoredWhenIndexRequested() throws Exception { | ||
CreateSnapshotResponse createSnapshotResponse = client.admin().cluster() | ||
.prepareCreateSnapshot(REPO, "snap2") | ||
.setWaitForCompletion(true) | ||
.setIndices(DS_BACKING_INDEX_NAME) | ||
.setIncludeGlobalState(false) | ||
.get(); | ||
|
||
RestStatus status = createSnapshotResponse.getSnapshotInfo().status(); | ||
assertEquals(RestStatus.OK, status); | ||
expectThrows(Exception.class, () -> client.admin().cluster() | ||
.prepareRestoreSnapshot(REPO, "snap2") | ||
.setWaitForCompletion(true) | ||
.setIndices("ds") | ||
.get()); | ||
} | ||
|
||
public void testDataStreamNotRestoredWhenIndexRequested() throws Exception { | ||
CreateSnapshotResponse createSnapshotResponse = client.admin().cluster() | ||
.prepareCreateSnapshot(REPO, "snap2") | ||
.setWaitForCompletion(true) | ||
.setIndices("ds") | ||
.setIncludeGlobalState(false) | ||
.get(); | ||
|
||
RestStatus status = createSnapshotResponse.getSnapshotInfo().status(); | ||
assertEquals(RestStatus.OK, status); | ||
|
||
assertTrue(client.admin().indices().deleteDataStream(new DeleteDataStreamAction.Request("ds")).get().isAcknowledged()); | ||
|
||
RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster() | ||
.prepareRestoreSnapshot(REPO, "snap2") | ||
.setWaitForCompletion(true) | ||
.setIndices(".ds-ds-*") | ||
.get(); | ||
|
||
assertEquals(RestStatus.OK, restoreSnapshotResponse.status()); | ||
|
||
GetDataStreamAction.Request getRequest = new GetDataStreamAction.Request("ds"); | ||
expectThrows(ResourceNotFoundException.class, () -> client.admin().indices().getDataStreams(getRequest).actionGet()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.