Search in sources :

Example 11 with RestoreSnapshotResponse

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

the class RestoreSnapshotIT method testDynamicRestoreThrottling.

public void testDynamicRestoreThrottling() throws Exception {
    Client client = client();
    createRepository("test-repo", "fs", Settings.builder().put("location", randomRepoPath()).put("compress", randomBoolean()).put("chunk_size", 100, ByteSizeUnit.BYTES));
    createIndexWithRandomDocs("test-idx", 100);
    logger.info("--> snapshot");
    client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").get();
    logger.info("--> delete index");
    cluster().wipeIndices("test-idx");
    logger.info("--> restore index");
    client.admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().put(INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey(), "100b").build()).get();
    ActionFuture<RestoreSnapshotResponse> restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute();
    // check if throttling is active
    assertBusy(() -> {
        long restorePause = 0L;
        for (RepositoriesService repositoriesService : internalCluster().getDataNodeInstances(RepositoriesService.class)) {
            restorePause += repositoriesService.repository("test-repo").getRestoreThrottleTimeInNanos();
        }
        assertThat(restorePause, greaterThan(TimeValue.timeValueSeconds(randomIntBetween(1, 5)).nanos()));
        assertFalse(restoreSnapshotResponse.isDone());
    }, 30, TimeUnit.SECONDS);
    // run at full speed again
    client.admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().putNull(INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey()).build()).get();
    // check that restore now completes quickly (i.e. within 20 seconds)
    assertThat(restoreSnapshotResponse.get(20L, TimeUnit.SECONDS).getRestoreInfo().totalShards(), greaterThan(0));
    assertDocCount("test-idx", 100L);
}
Also used : RepositoriesService(org.opensearch.repositories.RepositoriesService) Client(org.opensearch.client.Client) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 12 with RestoreSnapshotResponse

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

the class RestoreSnapshotIT method testRenameOnRestore.

public void testRenameOnRestore() throws Exception {
    Client client = client();
    createRepository("test-repo", "fs");
    createIndex("test-idx-1", "test-idx-2", "test-idx-3");
    ensureGreen();
    assertAcked(client.admin().indices().prepareAliases().addAlias("test-idx-1", "alias-1", false).addAlias("test-idx-2", "alias-2", false).addAlias("test-idx-3", "alias-3", false));
    indexRandomDocs("test-idx-1", 100);
    indexRandomDocs("test-idx-2", 100);
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx-1", "test-idx-2").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    logger.info("--> restore indices with different names");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setRenamePattern("(.+)").setRenameReplacement("$1-copy").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertDocCount("test-idx-1-copy", 100L);
    assertDocCount("test-idx-2-copy", 100L);
    logger.info("--> close just restored indices");
    client.admin().indices().prepareClose("test-idx-1-copy", "test-idx-2-copy").get();
    logger.info("--> and try to restore these indices again");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setRenamePattern("(.+)").setRenameReplacement("$1-copy").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertDocCount("test-idx-1-copy", 100L);
    assertDocCount("test-idx-2-copy", 100L);
    logger.info("--> close indices");
    assertAcked(client.admin().indices().prepareClose("test-idx-1", "test-idx-2-copy"));
    logger.info("--> restore indices with different names");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setRenamePattern("(.+-2)").setRenameReplacement("$1-copy").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    logger.info("--> delete indices");
    cluster().wipeIndices("test-idx-1", "test-idx-1-copy", "test-idx-2", "test-idx-2-copy");
    logger.info("--> try renaming indices using the same name");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setRenamePattern("(.+)").setRenameReplacement("same-name").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (SnapshotRestoreException ex) {
    // Expected
    }
    logger.info("--> try renaming indices using the same name");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setRenamePattern("test-idx-2").setRenameReplacement("test-idx-1").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (SnapshotRestoreException ex) {
    // Expected
    }
    logger.info("--> try renaming indices using invalid index name");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndices("test-idx-1").setRenamePattern(".+").setRenameReplacement("__WRONG__").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (InvalidIndexNameException ex) {
    // Expected
    }
    logger.info("--> try renaming indices into existing alias name");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndices("test-idx-1").setRenamePattern(".+").setRenameReplacement("alias-3").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (InvalidIndexNameException ex) {
    // Expected
    }
    logger.info("--> try renaming indices into existing alias of itself");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndices("test-idx-1").setRenamePattern("test-idx").setRenameReplacement("alias").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (SnapshotRestoreException ex) {
    // Expected
    }
    logger.info("--> try renaming indices into existing alias of another restored index");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndices("test-idx-1", "test-idx-2").setRenamePattern("test-idx-1").setRenameReplacement("alias-2").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (SnapshotRestoreException ex) {
    // Expected
    }
    logger.info("--> try renaming indices into existing alias of itself, but don't restore aliases ");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndices("test-idx-1").setRenamePattern("test-idx").setRenameReplacement("alias").setWaitForCompletion(true).setIncludeAliases(false).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
}
Also used : CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) InvalidIndexNameException(org.opensearch.indices.InvalidIndexNameException) Client(org.opensearch.client.Client) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 13 with RestoreSnapshotResponse

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

the class RestoreSnapshotIT method testParallelRestoreOperationsFromSingleSnapshot.

public void testParallelRestoreOperationsFromSingleSnapshot() throws Exception {
    String indexName1 = "testindex1";
    String indexName2 = "testindex2";
    String repoName = "test-restore-snapshot-repo";
    String snapshotName = "test-restore-snapshot";
    Path absolutePath = randomRepoPath().toAbsolutePath();
    logger.info("Path [{}]", absolutePath);
    String restoredIndexName1 = indexName1 + "-restored";
    String restoredIndexName2 = indexName2 + "-restored";
    String expectedValue = "expected";
    Client client = client();
    // Write a document
    String docId = Integer.toString(randomInt());
    index(indexName1, "_doc", docId, "value", expectedValue);
    String docId2 = Integer.toString(randomInt());
    index(indexName2, "_doc", docId2, "value", expectedValue);
    createRepository(repoName, "fs", absolutePath);
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot(repoName, snapshotName).setWaitForCompletion(true).setIndices(indexName1, indexName2).get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    assertThat(createSnapshotResponse.getSnapshotInfo().state(), equalTo(SnapshotState.SUCCESS));
    ActionFuture<RestoreSnapshotResponse> restoreSnapshotResponse1 = client.admin().cluster().prepareRestoreSnapshot(repoName, snapshotName).setIndices(indexName1).setRenamePattern(indexName1).setRenameReplacement(restoredIndexName1).execute();
    boolean sameSourceIndex = randomBoolean();
    ActionFuture<RestoreSnapshotResponse> restoreSnapshotResponse2 = client.admin().cluster().prepareRestoreSnapshot(repoName, snapshotName).setIndices(sameSourceIndex ? indexName1 : indexName2).setRenamePattern(sameSourceIndex ? indexName1 : indexName2).setRenameReplacement(restoredIndexName2).execute();
    assertThat(restoreSnapshotResponse1.get().status(), equalTo(RestStatus.ACCEPTED));
    assertThat(restoreSnapshotResponse2.get().status(), equalTo(RestStatus.ACCEPTED));
    ensureGreen(restoredIndexName1, restoredIndexName2);
    assertThat(client.prepareGet(restoredIndexName1, docId).get().isExists(), equalTo(true));
    assertThat(client.prepareGet(restoredIndexName2, sameSourceIndex ? docId : docId2).get().isExists(), equalTo(true));
}
Also used : Path(java.nio.file.Path) 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 14 with RestoreSnapshotResponse

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

the class RestoreSnapshotIT method testChangeSettingsOnRestore.

public void testChangeSettingsOnRestore() throws Exception {
    Client client = client();
    createRepository("test-repo", "fs");
    logger.info("--> create test index with case-preserving search analyzer");
    Settings.Builder indexSettings = Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_REPLICAS, between(0, 1)).put(INDEX_REFRESH_INTERVAL_SETTING.getKey(), "10s").put("index.analysis.analyzer.my_analyzer.type", "custom").put("index.analysis.analyzer.my_analyzer.tokenizer", "standard");
    assertAcked(prepareCreate("test-idx", 2, indexSettings));
    int numberOfShards = getNumShards("test-idx").numPrimaries;
    assertAcked(client().admin().indices().preparePutMapping("test-idx").setSource("field1", "type=text,analyzer=standard,search_analyzer=my_analyzer"));
    final int numdocs = randomIntBetween(10, 100);
    IndexRequestBuilder[] builders = new IndexRequestBuilder[numdocs];
    for (int i = 0; i < builders.length; i++) {
        builders[i] = client().prepareIndex("test-idx").setId(Integer.toString(i)).setSource("field1", "Foo bar " + i);
    }
    indexRandom(true, builders);
    flushAndRefresh();
    assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "foo")).get(), numdocs);
    assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "Foo")).get(), 0);
    assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "bar")).get(), numdocs);
    logger.info("--> snapshot it");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    logger.info("--> delete the index and recreate it while changing refresh interval and analyzer");
    cluster().wipeIndices("test-idx");
    Settings newIndexSettings = Settings.builder().put("refresh_interval", "5s").put("index.analysis.analyzer.my_analyzer.type", "standard").build();
    Settings newIncorrectIndexSettings = Settings.builder().put(newIndexSettings).put(SETTING_NUMBER_OF_SHARDS, numberOfShards + 100).build();
    logger.info("--> try restoring while changing the number of shards - should fail");
    assertRequestBuilderThrows(client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIgnoreIndexSettings("index.analysis.*").setIndexSettings(newIncorrectIndexSettings).setWaitForCompletion(true), SnapshotRestoreException.class);
    logger.info("--> try restoring while changing the number of replicas to a negative number - should fail");
    Settings newIncorrectReplicasIndexSettings = Settings.builder().put(newIndexSettings).put(SETTING_NUMBER_OF_REPLICAS.substring(IndexMetadata.INDEX_SETTING_PREFIX.length()), randomIntBetween(-10, -1)).build();
    assertRequestBuilderThrows(client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIgnoreIndexSettings("index.analysis.*").setIndexSettings(newIncorrectReplicasIndexSettings).setWaitForCompletion(true), IllegalArgumentException.class);
    logger.info("--> restore index with correct settings from the snapshot");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIgnoreIndexSettings("index.analysis.*").setIndexSettings(newIndexSettings).setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    logger.info("--> assert that correct settings are restored");
    GetSettingsResponse getSettingsResponse = client.admin().indices().prepareGetSettings("test-idx").execute().actionGet();
    assertThat(getSettingsResponse.getSetting("test-idx", INDEX_REFRESH_INTERVAL_SETTING.getKey()), equalTo("5s"));
    // Make sure that number of shards didn't change
    assertThat(getSettingsResponse.getSetting("test-idx", SETTING_NUMBER_OF_SHARDS), equalTo("" + numberOfShards));
    assertThat(getSettingsResponse.getSetting("test-idx", "index.analysis.analyzer.my_analyzer.type"), equalTo("standard"));
    assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "Foo")).get(), numdocs);
    assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "bar")).get(), numdocs);
    logger.info("--> delete the index and recreate it while deleting all index settings");
    cluster().wipeIndices("test-idx");
    logger.info("--> restore index with correct settings from the snapshot");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIgnoreIndexSettings(// delete everything we can delete
    "*").setIndexSettings(newIndexSettings).setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    logger.info("--> assert that correct settings are restored and index is still functional");
    getSettingsResponse = client.admin().indices().prepareGetSettings("test-idx").execute().actionGet();
    assertThat(getSettingsResponse.getSetting("test-idx", INDEX_REFRESH_INTERVAL_SETTING.getKey()), equalTo("5s"));
    // Make sure that number of shards didn't change
    assertThat(getSettingsResponse.getSetting("test-idx", SETTING_NUMBER_OF_SHARDS), equalTo("" + numberOfShards));
    assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "Foo")).get(), numdocs);
    assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "bar")).get(), numdocs);
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) CreateSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) GetSettingsResponse(org.opensearch.action.admin.indices.settings.get.GetSettingsResponse) Client(org.opensearch.client.Client) Settings(org.opensearch.common.settings.Settings) RestoreSnapshotResponse(org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 15 with RestoreSnapshotResponse

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

the class SharedClusterSnapshotRestoreIT method testMoveShardWhileSnapshotting.

public void testMoveShardWhileSnapshotting() throws Exception {
    Client client = client();
    Path repositoryLocation = randomRepoPath();
    createRepository("test-repo", "mock", Settings.builder().put("location", repositoryLocation).put("random", randomAlphaOfLength(10)).put("wait_after_unblock", 200));
    // Create index on 2 nodes and make sure each node has a primary by setting no replicas
    assertAcked(prepareCreate("test-idx", 2, Settings.builder().put("number_of_replicas", 0)));
    indexRandomDocs("test-idx", 100);
    // Pick one node and block it
    String blockedNode = blockNodeWithIndex("test-repo", "test-idx");
    logger.info("--> snapshot");
    client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(false).setIndices("test-idx").get();
    logger.info("--> waiting for block to kick in");
    waitForBlock(blockedNode, "test-repo", TimeValue.timeValueSeconds(60));
    logger.info("--> execution was blocked on node [{}], moving shards away from this node", blockedNode);
    Settings.Builder excludeSettings = Settings.builder().put("index.routing.allocation.exclude._name", blockedNode);
    client().admin().indices().prepareUpdateSettings("test-idx").setSettings(excludeSettings).get();
    logger.info("--> unblocking blocked node");
    unblockNode("test-repo", blockedNode);
    logger.info("--> waiting for completion");
    logger.info("Number of failed shards [{}]", waitForCompletion("test-repo", "test-snap", TimeValue.timeValueSeconds(600)).shardFailures().size());
    logger.info("--> done");
    final SnapshotInfo snapshotInfo = getSnapshot("test-repo", "test-snap");
    assertThat(snapshotInfo.state(), equalTo(SnapshotState.SUCCESS));
    assertThat(snapshotInfo.shardFailures(), empty());
    logger.info("--> delete index");
    cluster().wipeIndices("test-idx");
    logger.info("--> replace mock repository with real one at the same location");
    createRepository("test-repo", "fs", repositoryLocation);
    logger.info("--> restore index");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertDocCount("test-idx", 100L);
}
Also used : Path(java.nio.file.Path) Matchers.containsString(org.hamcrest.Matchers.containsString) Client(org.opensearch.client.Client) Settings(org.opensearch.common.settings.Settings) 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