Search in sources :

Example 41 with CreateSnapshotResponse

use of org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse in project elasticsearch by elastic.

the class SharedClusterSnapshotRestoreIT method testDeleteSnapshot.

public void testDeleteSnapshot() throws Exception {
    final int numberOfSnapshots = between(5, 15);
    Client client = client();
    Path repo = randomRepoPath();
    logger.info("-->  creating repository at {}", repo.toAbsolutePath());
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", repo).put("compress", false).put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
    createIndex("test-idx");
    ensureGreen();
    int[] numberOfFiles = new int[numberOfSnapshots];
    logger.info("--> creating {} snapshots ", numberOfSnapshots);
    for (int i = 0; i < numberOfSnapshots; i++) {
        for (int j = 0; j < 10; j++) {
            index("test-idx", "doc", Integer.toString(i * 10 + j), "foo", "bar" + i * 10 + j);
        }
        refresh();
        logger.info("--> snapshot {}", i);
        CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-" + i).setWaitForCompletion(true).setIndices("test-idx").get();
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
        assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
        // Store number of files after each snapshot
        numberOfFiles[i] = numberOfFiles(repo);
    }
    assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits(), equalTo(10L * numberOfSnapshots));
    int numberOfFilesBeforeDeletion = numberOfFiles(repo);
    logger.info("--> delete all snapshots except the first one and last one");
    for (int i = 1; i < numberOfSnapshots - 1; i++) {
        client.admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap-" + i).get();
    }
    int numberOfFilesAfterDeletion = numberOfFiles(repo);
    assertThat(numberOfFilesAfterDeletion, lessThan(numberOfFilesBeforeDeletion));
    logger.info("--> delete index");
    cluster().wipeIndices("test-idx");
    logger.info("--> restore index");
    String lastSnapshot = "test-snap-" + (numberOfSnapshots - 1);
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", lastSnapshot).setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits(), equalTo(10L * numberOfSnapshots));
    logger.info("--> delete the last snapshot");
    client.admin().cluster().prepareDeleteSnapshot("test-repo", lastSnapshot).get();
    logger.info("--> make sure that number of files is back to what it was when the first snapshot was made, " + "plus one because one backup index-N file should remain");
    assertThat(numberOfFiles(repo), equalTo(numberOfFiles[0] + 1));
}
Also used : Path(java.nio.file.Path) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) Client(org.elasticsearch.client.Client) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 42 with CreateSnapshotResponse

use of org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse in project elasticsearch by elastic.

the class SharedClusterSnapshotRestoreIT method testSnapshotClosedIndex.

public void testSnapshotClosedIndex() throws Exception {
    Client client = client();
    logger.info("-->  creating repository");
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", randomRepoPath())));
    createIndex("test-idx", "test-idx-closed");
    ensureGreen();
    logger.info("-->  closing index test-idx-closed");
    assertAcked(client.admin().indices().prepareClose("test-idx-closed"));
    ClusterStateResponse stateResponse = client.admin().cluster().prepareState().get();
    assertThat(stateResponse.getState().metaData().index("test-idx-closed").getState(), equalTo(IndexMetaData.State.CLOSE));
    assertThat(stateResponse.getState().routingTable().index("test-idx-closed"), nullValue());
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx*").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().indices().size(), equalTo(1));
    assertThat(createSnapshotResponse.getSnapshotInfo().shardFailures().size(), equalTo(0));
    logger.info("-->  deleting snapshot");
    client.admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap").get();
    logger.info("--> snapshot with closed index");
    assertBlocked(client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx", "test-idx-closed"), MetaDataIndexStateService.INDEX_CLOSED_BLOCK);
}
Also used : CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) Client(org.elasticsearch.client.Client)

Example 43 with CreateSnapshotResponse

use of org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse in project elasticsearch by elastic.

the class SharedClusterSnapshotRestoreIT method testBasicWorkFlow.

public void testBasicWorkFlow() throws Exception {
    Client client = client();
    logger.info("-->  creating repository");
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", randomRepoPath()).put("compress", randomBoolean()).put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
    createIndex("test-idx-1", "test-idx-2", "test-idx-3");
    ensureGreen();
    logger.info("--> indexing some data");
    for (int i = 0; i < 100; i++) {
        index("test-idx-1", "doc", Integer.toString(i), "foo", "bar" + i);
        index("test-idx-2", "doc", Integer.toString(i), "foo", "baz" + i);
        index("test-idx-3", "doc", Integer.toString(i), "foo", "baz" + i);
    }
    refresh();
    assertHitCount(client.prepareSearch("test-idx-1").setSize(0).get(), 100L);
    assertHitCount(client.prepareSearch("test-idx-2").setSize(0).get(), 100L);
    assertHitCount(client.prepareSearch("test-idx-3").setSize(0).get(), 100L);
    ListenableActionFuture<FlushResponse> flushResponseFuture = null;
    if (randomBoolean()) {
        ArrayList<String> indicesToFlush = new ArrayList<>();
        for (int i = 1; i < 4; i++) {
            if (randomBoolean()) {
                indicesToFlush.add("test-idx-" + i);
            }
        }
        if (!indicesToFlush.isEmpty()) {
            String[] indices = indicesToFlush.toArray(new String[indicesToFlush.size()]);
            logger.info("--> starting asynchronous flush for indices {}", Arrays.toString(indices));
            flushResponseFuture = client.admin().indices().prepareFlush(indices).execute();
        }
    }
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx-*", "-test-idx-3").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    List<SnapshotInfo> snapshotInfos = client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots(randomFrom("test-snap", "_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("--> delete some data");
    for (int i = 0; i < 50; i++) {
        client.prepareDelete("test-idx-1", "doc", Integer.toString(i)).get();
    }
    for (int i = 50; i < 100; i++) {
        client.prepareDelete("test-idx-2", "doc", Integer.toString(i)).get();
    }
    for (int i = 0; i < 100; i += 2) {
        client.prepareDelete("test-idx-3", "doc", Integer.toString(i)).get();
    }
    assertAllSuccessful(refresh());
    assertHitCount(client.prepareSearch("test-idx-1").setSize(0).get(), 50L);
    assertHitCount(client.prepareSearch("test-idx-2").setSize(0).get(), 50L);
    assertHitCount(client.prepareSearch("test-idx-3").setSize(0).get(), 50L);
    logger.info("--> close indices");
    client.admin().indices().prepareClose("test-idx-1", "test-idx-2").get();
    logger.info("--> restore all indices from the snapshot");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    ensureGreen();
    for (int i = 0; i < 5; i++) {
        assertHitCount(client.prepareSearch("test-idx-1").setSize(0).get(), 100L);
        assertHitCount(client.prepareSearch("test-idx-2").setSize(0).get(), 100L);
        assertHitCount(client.prepareSearch("test-idx-3").setSize(0).get(), 50L);
    }
    // Test restore after index deletion
    logger.info("--> delete indices");
    cluster().wipeIndices("test-idx-1", "test-idx-2");
    logger.info("--> restore one index after deletion");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx-*", "-test-idx-2").execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    ensureGreen();
    for (int i = 0; i < 5; i++) {
        assertHitCount(client.prepareSearch("test-idx-1").setSize(0).get(), 100L);
    }
    ClusterState clusterState = client.admin().cluster().prepareState().get().getState();
    assertThat(clusterState.getMetaData().hasIndex("test-idx-1"), equalTo(true));
    assertThat(clusterState.getMetaData().hasIndex("test-idx-2"), equalTo(false));
    if (flushResponseFuture != null) {
        // Finish flush
        flushResponseFuture.actionGet();
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) FlushResponse(org.elasticsearch.action.admin.indices.flush.FlushResponse) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Client(org.elasticsearch.client.Client)

Example 44 with CreateSnapshotResponse

use of org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse in project elasticsearch by elastic.

the class SharedClusterSnapshotRestoreIT method testChangeSettingsOnRestore.

public void testChangeSettingsOnRestore() throws Exception {
    Client client = client();
    logger.info("-->  creating repository");
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", randomRepoPath()).put("compress", randomBoolean()).put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
    logger.info("--> create test index with synonyms 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").putArray("index.analysis.analyzer.my_analyzer.filter", "lowercase", "my_synonym").put("index.analysis.filter.my_synonym.type", "synonym").put("index.analysis.filter.my_synonym.synonyms", "foo => bar");
    assertAcked(prepareCreate("test-idx", 2, indexSettings));
    int numberOfShards = getNumShards("test-idx").numPrimaries;
    assertAcked(client().admin().indices().preparePutMapping("test-idx").setType("type1").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", "type1", Integer.toString(i)).setSource("field1", "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", "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");
    assertThrows(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();
    assertThrows(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"));
    assertThat(getSettingsResponse.getSetting("test-idx", "index.analysis.filter.my_synonym.type"), nullValue());
    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("--> 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(), 0);
    assertHitCount(client.prepareSearch("test-idx").setSize(0).setQuery(matchQuery("field1", "bar")).get(), numdocs);
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) GetSettingsResponse(org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse) Client(org.elasticsearch.client.Client) Settings(org.elasticsearch.common.settings.Settings) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 45 with CreateSnapshotResponse

use of org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse in project elasticsearch by elastic.

the class SharedClusterSnapshotRestoreIT method testDeleteSnapshotWithCorruptedSnapshotFile.

public void testDeleteSnapshotWithCorruptedSnapshotFile() throws Exception {
    Client client = client();
    Path repo = randomRepoPath();
    logger.info("-->  creating repository at {}", repo.toAbsolutePath());
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", repo).put("compress", false).put("chunk_size", randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
    createIndex("test-idx-1", "test-idx-2");
    logger.info("--> indexing some data");
    indexRandom(true, client().prepareIndex("test-idx-1", "doc").setSource("foo", "bar"), client().prepareIndex("test-idx-2", "doc").setSource("foo", "bar"));
    logger.info("--> creating snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-1").setWaitForCompletion(true).setIndices("test-idx-*").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    logger.info("--> truncate snapshot file to make it unreadable");
    Path snapshotPath = repo.resolve("snap-" + createSnapshotResponse.getSnapshotInfo().snapshotId().getUUID() + ".dat");
    try (SeekableByteChannel outChan = Files.newByteChannel(snapshotPath, StandardOpenOption.WRITE)) {
        outChan.truncate(randomInt(10));
    }
    logger.info("--> delete snapshot");
    client.admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap-1").get();
    logger.info("--> make sure snapshot doesn't exist");
    assertThrows(client.admin().cluster().prepareGetSnapshots("test-repo").addSnapshots("test-snap-1"), SnapshotMissingException.class);
    logger.info("--> make sure that we can create the snapshot again");
    createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-1").setWaitForCompletion(true).setIndices("test-idx-*").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) Client(org.elasticsearch.client.Client)

Aggregations

CreateSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse)54 Client (org.elasticsearch.client.Client)42 RestoreSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)28 Path (java.nio.file.Path)20 PutRepositoryResponse (org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse)13 Matchers.containsString (org.hamcrest.Matchers.containsString)10 ClusterState (org.elasticsearch.cluster.ClusterState)9 Settings (org.elasticsearch.common.settings.Settings)8 ArrayList (java.util.ArrayList)7 GetSnapshotsResponse (org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse)7 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)7 List (java.util.List)6 ClusterAdminClient (org.elasticsearch.client.ClusterAdminClient)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 RepositoriesService (org.elasticsearch.repositories.RepositoriesService)5 RepositoryException (org.elasticsearch.repositories.RepositoryException)5 SnapshotStatus (org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotStatus)4 ShardSnapshotStatus (org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus)4 ClusterService (org.elasticsearch.cluster.service.ClusterService)4 InvalidIndexNameException (org.elasticsearch.indices.InvalidIndexNameException)4