Search in sources :

Example 41 with RestoreSnapshotResponse

use of org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse in project OpenSearch by opensearch-project.

the class SharedClusterSnapshotRestoreIT method testHiddenIndicesIncludedInSnapshot.

public void testHiddenIndicesIncludedInSnapshot() throws InterruptedException {
    Client client = client();
    final String normalIndex = "normal-index";
    final String hiddenIndex = "hidden-index";
    final String dottedHiddenIndex = ".index-hidden";
    final String repoName = "test-repo";
    createRepository(repoName, "fs");
    logger.info("--> creating indices");
    createIndex(normalIndex, indexSettingsNoReplicas(randomIntBetween(1, 3)).build());
    createIndex(hiddenIndex, indexSettingsNoReplicas(randomIntBetween(1, 3)).put(IndexMetadata.SETTING_INDEX_HIDDEN, true).build());
    createIndex(dottedHiddenIndex, indexSettingsNoReplicas(randomIntBetween(1, 3)).put(IndexMetadata.SETTING_INDEX_HIDDEN, true).build());
    ensureGreen();
    indexRandomDocs(normalIndex, 100);
    indexRandomDocs(hiddenIndex, 100);
    indexRandomDocs(dottedHiddenIndex, 100);
    logger.info("--> taking a snapshot");
    final String snapName = "test-snap";
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot(repoName, snapName).setWaitForCompletion(true).setIndices(randomFrom("*", "_all")).get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    List<SnapshotInfo> snapshotInfos = client.admin().cluster().prepareGetSnapshots(repoName).setSnapshots(randomFrom(snapName, "_all", "*", "*-snap", "test*")).get().getSnapshots();
    assertThat(snapshotInfos.size(), equalTo(1));
    SnapshotInfo snapshotInfo = snapshotInfos.get(0);
    assertThat(snapshotInfo.state(), equalTo(SnapshotState.SUCCESS));
    assertThat(snapshotInfo.version(), equalTo(Version.CURRENT));
    logger.info("--> deleting indices");
    cluster().wipeIndices(normalIndex, hiddenIndex, dottedHiddenIndex);
    // Verify that hidden indices get restored with a wildcard restore
    {
        RestoreSnapshotResponse restoreSnapshotResponse = clusterAdmin().prepareRestoreSnapshot(repoName, snapName).setWaitForCompletion(true).setIndices("*").execute().actionGet();
        assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
        assertThat(restoreSnapshotResponse.getRestoreInfo().successfulShards(), equalTo(restoreSnapshotResponse.getRestoreInfo().totalShards()));
        assertThat(restoreSnapshotResponse.getRestoreInfo().indices(), containsInAnyOrder(normalIndex, hiddenIndex, dottedHiddenIndex));
        ClusterState clusterState = client.admin().cluster().prepareState().get().getState();
        assertThat(clusterState.getMetadata().hasIndex(normalIndex), equalTo(true));
        assertThat(clusterState.getMetadata().hasIndex(hiddenIndex), equalTo(true));
        assertThat(clusterState.getMetadata().hasIndex(dottedHiddenIndex), equalTo(true));
        cluster().wipeIndices(normalIndex, hiddenIndex, dottedHiddenIndex);
    }
    // Verify that exclusions work on hidden indices
    {
        RestoreSnapshotResponse restoreSnapshotResponse = clusterAdmin().prepareRestoreSnapshot(repoName, snapName).setWaitForCompletion(true).setIndices("*", "-.*").execute().actionGet();
        assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
        assertThat(restoreSnapshotResponse.getRestoreInfo().successfulShards(), equalTo(restoreSnapshotResponse.getRestoreInfo().totalShards()));
        assertThat(restoreSnapshotResponse.getRestoreInfo().indices(), containsInAnyOrder(normalIndex, hiddenIndex));
        ClusterState clusterState = client.admin().cluster().prepareState().get().getState();
        assertThat(clusterState.getMetadata().hasIndex(normalIndex), equalTo(true));
        assertThat(clusterState.getMetadata().hasIndex(hiddenIndex), equalTo(true));
        assertThat(clusterState.getMetadata().hasIndex(dottedHiddenIndex), equalTo(false));
        cluster().wipeIndices(normalIndex, hiddenIndex);
    }
    // Verify that hidden indices can be restored with a non-star pattern
    {
        RestoreSnapshotResponse restoreSnapshotResponse = clusterAdmin().prepareRestoreSnapshot(repoName, snapName).setWaitForCompletion(true).setIndices("hid*").execute().actionGet();
        assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
        assertThat(restoreSnapshotResponse.getRestoreInfo().successfulShards(), equalTo(restoreSnapshotResponse.getRestoreInfo().totalShards()));
        assertThat(restoreSnapshotResponse.getRestoreInfo().indices(), containsInAnyOrder(hiddenIndex));
        ClusterState clusterState = client.admin().cluster().prepareState().get().getState();
        assertThat(clusterState.getMetadata().hasIndex(normalIndex), equalTo(false));
        assertThat(clusterState.getMetadata().hasIndex(hiddenIndex), equalTo(true));
        assertThat(clusterState.getMetadata().hasIndex(dottedHiddenIndex), equalTo(false));
        cluster().wipeIndices(hiddenIndex);
    }
    // Verify that hidden indices can be restored by fully specified name
    {
        RestoreSnapshotResponse restoreSnapshotResponse = clusterAdmin().prepareRestoreSnapshot(repoName, snapName).setWaitForCompletion(true).setIndices(dottedHiddenIndex).get();
        assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
        assertThat(restoreSnapshotResponse.getRestoreInfo().successfulShards(), equalTo(restoreSnapshotResponse.getRestoreInfo().totalShards()));
        assertThat(restoreSnapshotResponse.getRestoreInfo().indices(), containsInAnyOrder(dottedHiddenIndex));
        ClusterState clusterState = client.admin().cluster().prepareState().get().getState();
        assertThat(clusterState.getMetadata().hasIndex(normalIndex), equalTo(false));
        assertThat(clusterState.getMetadata().hasIndex(hiddenIndex), equalTo(false));
        assertThat(clusterState.getMetadata().hasIndex(dottedHiddenIndex), equalTo(true));
    }
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) Client(org.opensearch.client.Client) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 42 with RestoreSnapshotResponse

use of org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse in project OpenSearch by opensearch-project.

the class SharedClusterSnapshotRestoreIT method testDeleteSnapshotWhileRestoringFails.

public void testDeleteSnapshotWhileRestoringFails() throws Exception {
    Client client = client();
    final String repoName = "test-repo";
    createRepository(repoName, "mock");
    logger.info("--> creating index");
    final String indexName = "test-idx";
    assertAcked(prepareCreate(indexName).setWaitForActiveShards(ActiveShardCount.ALL));
    indexRandomDocs(indexName, 100);
    logger.info("--> take snapshots");
    final String snapshotName = "test-snap";
    assertThat(client.admin().cluster().prepareCreateSnapshot(repoName, snapshotName).setIndices(indexName).setWaitForCompletion(true).get().getSnapshotInfo().state(), equalTo(SnapshotState.SUCCESS));
    final String snapshotName2 = "test-snap-2";
    assertThat(client.admin().cluster().prepareCreateSnapshot(repoName, snapshotName2).setIndices(indexName).setWaitForCompletion(true).get().getSnapshotInfo().state(), equalTo(SnapshotState.SUCCESS));
    logger.info("--> delete index before restoring");
    assertAcked(client.admin().indices().prepareDelete(indexName).get());
    logger.info("--> execution will be blocked on all data nodes");
    blockAllDataNodes(repoName);
    final ActionFuture<RestoreSnapshotResponse> restoreFut;
    try {
        logger.info("--> start restore");
        restoreFut = client.admin().cluster().prepareRestoreSnapshot(repoName, snapshotName).setWaitForCompletion(true).execute();
        logger.info("--> waiting for block to kick in");
        waitForBlockOnAnyDataNode(repoName, TimeValue.timeValueMinutes(1));
        logger.info("--> try deleting the snapshot while the restore is in progress (should throw an error)");
        ConcurrentSnapshotExecutionException e = expectThrows(ConcurrentSnapshotExecutionException.class, () -> clusterAdmin().prepareDeleteSnapshot(repoName, snapshotName).get());
        assertEquals(repoName, e.getRepositoryName());
        assertEquals(snapshotName, e.getSnapshotName());
        assertThat(e.getMessage(), containsString("cannot delete snapshot during a restore"));
    } finally {
        // unblock even if the try block fails otherwise we will get bogus failures when we delete all indices in test teardown.
        logger.info("--> unblocking all data nodes");
        unblockAllDataNodes(repoName);
    }
    logger.info("--> wait for restore to finish");
    restoreFut.get();
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) Client(org.opensearch.client.Client) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 43 with RestoreSnapshotResponse

use of org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse in project OpenSearch by opensearch-project.

the class SharedClusterSnapshotRestoreIT method testDeletionOfFailingToRecoverIndexShouldStopRestore.

public void testDeletionOfFailingToRecoverIndexShouldStopRestore() throws Exception {
    Path repositoryLocation = randomRepoPath();
    Client client = client();
    createRepository("test-repo", "fs", repositoryLocation);
    createIndexWithRandomDocs("test-idx", 100);
    createSnapshot("test-repo", "test-snap", Collections.singletonList("test-idx"));
    logger.info("-->  update repository with mock version");
    createRepository("test-repo", "mock", Settings.builder().put("location", repositoryLocation).put("random", randomAlphaOfLength(10)).put("random_data_file_io_exception_rate", // Fail completely
    1.0));
    // Test restore after index deletion
    logger.info("--> delete index");
    cluster().wipeIndices("test-idx");
    logger.info("--> restore index after deletion");
    ActionFuture<RestoreSnapshotResponse> restoreSnapshotResponseFuture = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute();
    logger.info("--> wait for the index to appear");
    // that would mean that recovery process started and failing
    waitForIndex("test-idx", TimeValue.timeValueSeconds(10));
    logger.info("--> delete index");
    cluster().wipeIndices("test-idx");
    logger.info("--> get restore results");
    // Now read restore results and make sure it failed
    RestoreSnapshotResponse restoreSnapshotResponse = restoreSnapshotResponseFuture.actionGet(TimeValue.timeValueSeconds(10));
    assertThat(restoreSnapshotResponse.getRestoreInfo().failedShards(), greaterThan(0));
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), equalTo(restoreSnapshotResponse.getRestoreInfo().failedShards()));
    logger.info("--> restoring working repository");
    createRepository("test-repo", "fs", repositoryLocation);
    logger.info("--> trying to restore index again");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertThat(restoreSnapshotResponse.getRestoreInfo().failedShards(), equalTo(0));
    assertDocCount("test-idx", 100L);
}
Also used : Path(java.nio.file.Path) Client(org.opensearch.client.Client) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 44 with RestoreSnapshotResponse

use of org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse in project OpenSearch by opensearch-project.

the class BlobStoreIncrementalityIT method ensureRestoreSingleShardSuccessfully.

private void ensureRestoreSingleShardSuccessfully(String repo, String indexName, String snapshot, String indexSuffix) {
    logger.info("--> restoring [{}]", snapshot);
    final RestoreSnapshotResponse restoreSnapshotResponse = client().admin().cluster().prepareRestoreSnapshot(repo, snapshot).setIndices(indexName).setRenamePattern("(.+)").setRenameReplacement("$1" + indexSuffix).setWaitForCompletion(true).get();
    final RestoreInfo restoreInfo = restoreSnapshotResponse.getRestoreInfo();
    assertThat(restoreInfo.totalShards(), is(1));
    assertThat(restoreInfo.failedShards(), is(0));
}
Also used : RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 45 with RestoreSnapshotResponse

use of org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse in project OpenSearch by opensearch-project.

the class URLSnapshotRestoreIT method testUrlRepository.

public void testUrlRepository() throws Exception {
    Client client = client();
    logger.info("-->  creating repository");
    Path repositoryLocation = randomRepoPath();
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType(FsRepository.TYPE).setSettings(Settings.builder().put(FsRepository.LOCATION_SETTING.getKey(), repositoryLocation).put(FsRepository.COMPRESS_SETTING.getKey(), randomBoolean()).put(FsRepository.CHUNK_SIZE_SETTING.getKey(), randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
    createIndex("test-idx");
    ensureGreen();
    logger.info("--> indexing some data");
    for (int i = 0; i < 100; i++) {
        index("test-idx", "doc", Integer.toString(i), "foo", "bar" + i);
    }
    refresh();
    assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits().value, equalTo(100L));
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    int actualTotalShards = createSnapshotResponse.getSnapshotInfo().totalShards();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(actualTotalShards));
    SnapshotState state = client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").get().getSnapshots().get(0).state();
    assertThat(state, equalTo(SnapshotState.SUCCESS));
    logger.info("--> delete index");
    cluster().wipeIndices("test-idx");
    logger.info("--> create read-only URL repository");
    assertAcked(client.admin().cluster().preparePutRepository("url-repo").setType(URLRepository.TYPE).setSettings(Settings.builder().put(URLRepository.URL_SETTING.getKey(), repositoryLocation.toUri().toURL().toString()).put("list_directories", randomBoolean())));
    logger.info("--> restore index after deletion");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("url-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits().value, equalTo(100L));
    logger.info("--> list available shapshots");
    GetSnapshotsResponse getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots("url-repo").get();
    assertThat(getSnapshotsResponse.getSnapshots(), notNullValue());
    assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(1));
    logger.info("--> delete snapshot");
    AcknowledgedResponse deleteSnapshotResponse = client.admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap").get();
    assertAcked(deleteSnapshotResponse);
    logger.info("--> list available shapshot again, no snapshots should be returned");
    getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots("url-repo").get();
    assertThat(getSnapshotsResponse.getSnapshots(), notNullValue());
    assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(0));
}
Also used : Path(java.nio.file.Path) SnapshotState(org.opensearch.snapshots.SnapshotState) GetSnapshotsResponse(org.opensearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) Client(org.opensearch.client.Client) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Aggregations

RestoreSnapshotResponse (org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)49 CreateSnapshotResponse (org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse)24 Client (org.opensearch.client.Client)23 Path (java.nio.file.Path)18 Matchers.containsString (org.hamcrest.Matchers.containsString)17 Settings (org.opensearch.common.settings.Settings)11 ClusterState (org.opensearch.cluster.ClusterState)8 RepositoriesService (org.opensearch.repositories.RepositoriesService)8 List (java.util.List)6 Map (java.util.Map)6 SnapshotsStatusResponse (org.opensearch.action.admin.cluster.snapshots.status.SnapshotsStatusResponse)6 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)6 Collections (java.util.Collections)5 Matchers.is (org.hamcrest.Matchers.is)5 RestoreSnapshotRequest (org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest)5 ClusterStateResponse (org.opensearch.action.admin.cluster.state.ClusterStateResponse)5 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 TimeUnit (java.util.concurrent.TimeUnit)4