Search in sources :

Example 1 with CreateSnapshotRequest

use of org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest in project OpenSearch by opensearch-project.

the class SnapshotRequestConvertersTests method testCreateSnapshot.

public void testCreateSnapshot() throws IOException {
    Map<String, String> expectedParams = new HashMap<>();
    String repository = RequestConvertersTests.randomIndicesNames(1, 1)[0];
    String snapshot = "snapshot-" + generateRandomStringArray(1, randomInt(10), false, false)[0];
    String endpoint = "/_snapshot/" + repository + "/" + snapshot;
    CreateSnapshotRequest createSnapshotRequest = new CreateSnapshotRequest(repository, snapshot);
    RequestConvertersTests.setRandomMasterTimeout(createSnapshotRequest, expectedParams);
    Boolean waitForCompletion = randomBoolean();
    createSnapshotRequest.waitForCompletion(waitForCompletion);
    expectedParams.put("wait_for_completion", waitForCompletion.toString());
    Request request = SnapshotRequestConverters.createSnapshot(createSnapshotRequest);
    assertThat(request.getEndpoint(), equalTo(endpoint));
    assertThat(request.getMethod(), equalTo(HttpPut.METHOD_NAME));
    assertThat(request.getParameters(), equalTo(expectedParams));
    RequestConvertersTests.assertToXContentBody(createSnapshotRequest, request.getEntity());
}
Also used : HashMap(java.util.HashMap) CreateSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) VerifyRepositoryRequest(org.opensearch.action.admin.cluster.repositories.verify.VerifyRepositoryRequest) RestoreSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest) SnapshotsStatusRequest(org.opensearch.action.admin.cluster.snapshots.status.SnapshotsStatusRequest) DeleteRepositoryRequest(org.opensearch.action.admin.cluster.repositories.delete.DeleteRepositoryRequest) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) DeleteSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.delete.DeleteSnapshotRequest) GetRepositoriesRequest(org.opensearch.action.admin.cluster.repositories.get.GetRepositoriesRequest) PutRepositoryRequest(org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest) CreateSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) GetSnapshotsRequest(org.opensearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest)

Example 2 with CreateSnapshotRequest

use of org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest in project OpenSearch by opensearch-project.

the class SnapshotIT method testCreateSnapshot.

public void testCreateSnapshot() throws Exception {
    String repository = "test_repository";
    assertTrue(createTestRepository(repository, FsRepository.TYPE, "{\"location\": \".\"}").isAcknowledged());
    String snapshot = "test_snapshot";
    CreateSnapshotRequest request = new CreateSnapshotRequest(repository, snapshot);
    boolean waitForCompletion = randomBoolean();
    request.waitForCompletion(waitForCompletion);
    if (randomBoolean()) {
        request.userMetadata(randomUserMetadata());
    }
    request.partial(randomBoolean());
    request.includeGlobalState(randomBoolean());
    CreateSnapshotResponse response = createTestSnapshot(request);
    assertEquals(waitForCompletion ? RestStatus.OK : RestStatus.ACCEPTED, response.status());
    if (waitForCompletion == false) {
        // If we don't wait for the snapshot to complete we have to cancel it to not leak the snapshot task
        AcknowledgedResponse deleteResponse = execute(new DeleteSnapshotRequest(repository, snapshot), highLevelClient().snapshot()::delete, highLevelClient().snapshot()::deleteAsync);
        assertTrue(deleteResponse.isAcknowledged());
    }
}
Also used : CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) CreateSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) DeleteSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.delete.DeleteSnapshotRequest)

Example 3 with CreateSnapshotRequest

use of org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest in project OpenSearch by opensearch-project.

the class DedicatedClusterSnapshotRestoreIT method testCreateSnapshotLegacyPath.

/**
 * Tests for the legacy snapshot path that is normally executed if the cluster contains any nodes older than
 * {@link SnapshotsService#NO_REPO_INITIALIZE_VERSION}.
 * Makes sure that blocking as well as non-blocking snapshot create paths execute cleanly as well as that error handling works out
 * correctly by testing a snapshot name collision.
 */
public void testCreateSnapshotLegacyPath() throws Exception {
    final String masterNode = internalCluster().startMasterOnlyNode();
    internalCluster().startDataOnlyNode();
    final String repoName = "test-repo";
    createRepository(repoName, "fs");
    createIndex("some-index");
    final SnapshotsService snapshotsService = internalCluster().getMasterNodeInstance(SnapshotsService.class);
    final Snapshot snapshot1 = PlainActionFuture.get(f -> snapshotsService.createSnapshotLegacy(new CreateSnapshotRequest(repoName, "snap-1"), f));
    awaitNoMoreRunningOperations(masterNode);
    final InvalidSnapshotNameException sne = expectThrows(InvalidSnapshotNameException.class, () -> PlainActionFuture.<SnapshotInfo, Exception>get(f -> snapshotsService.executeSnapshotLegacy(new CreateSnapshotRequest(repoName, snapshot1.getSnapshotId().getName()), f)));
    assertThat(sne.getMessage(), containsString("snapshot with the same name already exists"));
    final SnapshotInfo snapshot2 = PlainActionFuture.get(f -> snapshotsService.executeSnapshotLegacy(new CreateSnapshotRequest(repoName, "snap-2"), f));
    assertThat(snapshot2.state(), is(SnapshotState.SUCCESS));
    final SnapshotInfo snapshot3 = PlainActionFuture.get(f -> snapshotsService.executeSnapshotLegacy(new CreateSnapshotRequest(repoName, "snap-3").indices("does-not-exist-*"), f));
    assertThat(snapshot3.state(), is(SnapshotState.SUCCESS));
}
Also used : RepositoryMissingException(org.opensearch.repositories.RepositoryMissingException) Arrays(java.util.Arrays) Metadata(org.opensearch.cluster.metadata.Metadata) CheckedFunction(org.opensearch.common.CheckedFunction) Matchers.not(org.hamcrest.Matchers.not) ClusterScope(org.opensearch.test.OpenSearchIntegTestCase.ClusterScope) Version(org.opensearch.Version) SnapshotsStatusResponse(org.opensearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse) Strings(org.opensearch.common.Strings) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) RecoveryState(org.opensearch.indices.recovery.RecoveryState) SnapshotStatus(org.opensearch.action.admin.cluster.snapshots.status.SnapshotStatus) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Path(java.nio.file.Path) EnumSet(java.util.EnumSet) OpenSearchAssertions.assertRequestBuilderThrows(org.opensearch.test.hamcrest.OpenSearchAssertions.assertRequestBuilderThrows) Client(org.opensearch.client.Client) TimeValue(org.opensearch.common.unit.TimeValue) NodeClient(org.opensearch.client.node.NodeClient) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Matchers.allOf(org.hamcrest.Matchers.allOf) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) Settings(org.opensearch.common.settings.Settings) TestCustomMetadata(org.opensearch.test.TestCustomMetadata) Scope(org.opensearch.test.OpenSearchIntegTestCase.Scope) TransportService(org.opensearch.transport.TransportService) RetentionLeaseActions(org.opensearch.index.seqno.RetentionLeaseActions) UncheckedIOException(java.io.UncheckedIOException) FileVisitResult(java.nio.file.FileVisitResult) CountDownLatch(java.util.concurrent.CountDownLatch) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Priority(org.opensearch.common.Priority) Node(org.opensearch.node.Node) ParseField(org.opensearch.common.ParseField) Writeable(org.opensearch.common.io.stream.Writeable) MockTransportService(org.opensearch.test.transport.MockTransportService) ArrayList(java.util.ArrayList) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) ClusterState(org.opensearch.cluster.ClusterState) BusyMasterServiceDisruption(org.opensearch.test.disruption.BusyMasterServiceDisruption) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Matchers.hasSize(org.hamcrest.Matchers.hasSize) AbstractRestChannel(org.opensearch.rest.AbstractRestChannel) Environment(org.opensearch.env.Environment) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) Files(java.nio.file.Files) RETAIN_ALL(org.opensearch.index.seqno.RetentionLeaseActions.RETAIN_ALL) IOException(java.io.IOException) TransportMessageListener(org.opensearch.transport.TransportMessageListener) Plugin(org.opensearch.plugins.Plugin) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) RetentionLeases(org.opensearch.index.seqno.RetentionLeases) ClusterService(org.opensearch.cluster.service.ClusterService) ShardStats(org.opensearch.action.admin.indices.stats.ShardStats) IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) BlobStoreRepository(org.opensearch.repositories.blobstore.BlobStoreRepository) NodeRoles.nonMasterNode(org.opensearch.test.NodeRoles.nonMasterNode) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) XContentParser(org.opensearch.common.xcontent.XContentParser) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Locale(java.util.Locale) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) Collection(java.util.Collection) RestStatus(org.opensearch.rest.RestStatus) ServiceDisruptionScheme(org.opensearch.test.disruption.ServiceDisruptionScheme) MockRepository(org.opensearch.snapshots.mockstore.MockRepository) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) SnapshotStats(org.opensearch.action.admin.cluster.snapshots.status.SnapshotStats) RestGetRepositoriesAction(org.opensearch.rest.action.admin.cluster.RestGetRepositoriesAction) SnapshotsInProgress(org.opensearch.cluster.SnapshotsInProgress) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InternalTestCluster(org.opensearch.test.InternalTestCluster) AtomicReference(java.util.concurrent.atomic.AtomicReference) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) PeerRecoveryTargetService(org.opensearch.indices.recovery.PeerRecoveryTargetService) StreamInput(org.opensearch.common.io.stream.StreamInput) SettingsFilter(org.opensearch.common.settings.SettingsFilter) OpenSearchAssertions.assertAcked(org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Setting(org.opensearch.common.settings.Setting) TransportRequest(org.opensearch.transport.TransportRequest) RestRequest(org.opensearch.rest.RestRequest) IntHashSet(com.carrotsearch.hppc.IntHashSet) IntSet(com.carrotsearch.hppc.IntSet) RestClusterStateAction(org.opensearch.rest.action.admin.cluster.RestClusterStateAction) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) OpenSearchAssertions.assertFutureThrows(org.opensearch.test.hamcrest.OpenSearchAssertions.assertFutureThrows) RestResponse(org.opensearch.rest.RestResponse) ActionFuture(org.opensearch.action.ActionFuture) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) ShardId(org.opensearch.index.shard.ShardId) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Sets(org.opensearch.common.util.set.Sets) CreateSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) GetSnapshotsResponse(org.opensearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) NamedDiff(org.opensearch.cluster.NamedDiff) Collections(java.util.Collections) CreateSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 4 with CreateSnapshotRequest

use of org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest in project OpenSearch by opensearch-project.

the class SnapshotRequestsTests method testCreateSnapshotRequestParsing.

public void testCreateSnapshotRequestParsing() throws IOException {
    CreateSnapshotRequest request = new CreateSnapshotRequest("test-repo", "test-snap");
    XContentBuilder builder = jsonBuilder().startObject();
    if (randomBoolean()) {
        builder.field("indices", "foo,bar,baz");
    } else {
        builder.startArray("indices");
        builder.value("foo");
        builder.value("bar");
        builder.value("baz");
        builder.endArray();
    }
    IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean());
    if (indicesOptions.expandWildcardsClosed()) {
        if (indicesOptions.expandWildcardsOpen()) {
            builder.field("expand_wildcards", "all");
        } else {
            builder.field("expand_wildcards", "closed");
        }
    } else {
        if (indicesOptions.expandWildcardsOpen()) {
            builder.field("expand_wildcards", "open");
        } else {
            builder.field("expand_wildcards", "none");
        }
    }
    builder.field("allow_no_indices", indicesOptions.allowNoIndices());
    boolean partial = randomBoolean();
    builder.field("partial", partial);
    builder.startObject("settings").field("set1", "val1").endObject();
    builder.startObject("index_settings").field("set1", "val2").endObject();
    if (randomBoolean()) {
        builder.field("ignore_index_settings", "set2,set3");
    } else {
        builder.startArray("ignore_index_settings");
        builder.value("set2");
        builder.value("set3");
        builder.endArray();
    }
    boolean includeIgnoreUnavailable = randomBoolean();
    if (includeIgnoreUnavailable) {
        builder.field("ignore_unavailable", indicesOptions.ignoreUnavailable());
    }
    BytesReference bytes = BytesReference.bytes(builder.endObject());
    request.source(XContentHelper.convertToMap(bytes, true, builder.contentType()).v2());
    assertEquals("test-repo", request.repository());
    assertEquals("test-snap", request.snapshot());
    assertArrayEquals(request.indices(), new String[] { "foo", "bar", "baz" });
    assertEquals(partial, request.partial());
    assertEquals("val1", request.settings().get("set1"));
    boolean expectedIgnoreAvailable = includeIgnoreUnavailable ? indicesOptions.ignoreUnavailable() : IndicesOptions.strictExpandOpen().ignoreUnavailable();
    assertEquals(expectedIgnoreAvailable, request.indicesOptions().ignoreUnavailable());
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) CreateSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) IndicesOptions(org.opensearch.action.support.IndicesOptions) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 5 with CreateSnapshotRequest

use of org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest in project OpenSearch by opensearch-project.

the class SnapshotIT method testRestoreSnapshot.

public void testRestoreSnapshot() throws IOException {
    String testRepository = "test";
    String testSnapshot = "snapshot_1";
    String testIndex = "test_index";
    String restoredIndex = testIndex + "_restored";
    AcknowledgedResponse putRepositoryResponse = createTestRepository(testRepository, FsRepository.TYPE, "{\"location\": \".\"}");
    assertTrue(putRepositoryResponse.isAcknowledged());
    createIndex(testIndex, Settings.EMPTY);
    assertTrue("index [" + testIndex + "] should have been created", indexExists(testIndex));
    CreateSnapshotRequest createSnapshotRequest = new CreateSnapshotRequest(testRepository, testSnapshot);
    createSnapshotRequest.indices(testIndex);
    createSnapshotRequest.waitForCompletion(true);
    if (randomBoolean()) {
        createSnapshotRequest.userMetadata(randomUserMetadata());
    }
    CreateSnapshotResponse createSnapshotResponse = createTestSnapshot(createSnapshotRequest);
    assertEquals(RestStatus.OK, createSnapshotResponse.status());
    deleteIndex(testIndex);
    assertFalse("index [" + testIndex + "] should have been deleted", indexExists(testIndex));
    RestoreSnapshotRequest request = new RestoreSnapshotRequest(testRepository, testSnapshot);
    request.waitForCompletion(true);
    request.renamePattern(testIndex);
    request.renameReplacement(restoredIndex);
    RestoreSnapshotResponse response = execute(request, highLevelClient().snapshot()::restore, highLevelClient().snapshot()::restoreAsync);
    RestoreInfo restoreInfo = response.getRestoreInfo();
    assertThat(restoreInfo.name(), equalTo(testSnapshot));
    assertThat(restoreInfo.indices(), equalTo(Collections.singletonList(restoredIndex)));
    assertThat(restoreInfo.successfulShards(), greaterThan(0));
    assertThat(restoreInfo.failedShards(), equalTo(0));
}
Also used : RestoreInfo(org.opensearch.snapshots.RestoreInfo) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) CreateSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) RestoreSnapshotRequest(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Aggregations

CreateSnapshotRequest (org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest)14 CreateSnapshotResponse (org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse)9 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)8 DeleteSnapshotRequest (org.opensearch.action.admin.cluster.snapshots.delete.DeleteSnapshotRequest)4 RestoreSnapshotRequest (org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DeleteRepositoryRequest (org.opensearch.action.admin.cluster.repositories.delete.DeleteRepositoryRequest)2 CloneSnapshotRequest (org.opensearch.action.admin.cluster.snapshots.clone.CloneSnapshotRequest)2 GetSnapshotsRequest (org.opensearch.action.admin.cluster.snapshots.get.GetSnapshotsRequest)2 RestoreSnapshotResponse (org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)2 SnapshotsStatusRequest (org.opensearch.action.admin.cluster.snapshots.status.SnapshotsStatusRequest)2 RestoreInfo (org.opensearch.snapshots.RestoreInfo)2 SnapshotInfo (org.opensearch.snapshots.SnapshotInfo)2 IntHashSet (com.carrotsearch.hppc.IntHashSet)1 IntSet (com.carrotsearch.hppc.IntSet)1 UncheckedIOException (java.io.UncheckedIOException)1 FileVisitResult (java.nio.file.FileVisitResult)1 Files (java.nio.file.Files)1