Search in sources :

Example 1 with CreateSnapshotResponse

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

the class SysSnapshotsTest method createSnapshot.

private void createSnapshot(String snapshotName, String... tables) {
    CreateSnapshotResponse createSnapshotResponse = client().admin().cluster().prepareCreateSnapshot(REPOSITORY_NAME, snapshotName).setWaitForCompletion(true).setIndices(tables).get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    snapshots.add(snapshotName);
}
Also used : CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse)

Example 2 with CreateSnapshotResponse

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

the class SnapshotRestoreDDLDispatcher method dispatch.

public CompletableFuture<Long> dispatch(final CreateSnapshotAnalyzedStatement statement) {
    final CompletableFuture<Long> resultFuture = new CompletableFuture<>();
    boolean waitForCompletion = statement.snapshotSettings().getAsBoolean(WAIT_FOR_COMPLETION.settingName(), WAIT_FOR_COMPLETION.defaultValue());
    boolean ignoreUnavailable = statement.snapshotSettings().getAsBoolean(IGNORE_UNAVAILABLE.settingName(), IGNORE_UNAVAILABLE.defaultValue());
    // ignore_unavailable as set by statement
    IndicesOptions indicesOptions = IndicesOptions.fromOptions(ignoreUnavailable, true, true, false, IndicesOptions.lenientExpandOpen());
    CreateSnapshotRequest request = new CreateSnapshotRequest(statement.snapshotId().getRepository(), statement.snapshotId().getSnapshot()).includeGlobalState(statement.includeMetadata()).waitForCompletion(waitForCompletion).indices(statement.indices()).indicesOptions(indicesOptions).settings(statement.snapshotSettings());
    //noinspection ThrowableResultOfMethodCallIgnored
    assert request.validate() == null : "invalid CREATE SNAPSHOT statement";
    transportActionProvider.transportCreateSnapshotAction().execute(request, new ActionListener<CreateSnapshotResponse>() {

        @Override
        public void onResponse(CreateSnapshotResponse createSnapshotResponse) {
            SnapshotInfo snapshotInfo = createSnapshotResponse.getSnapshotInfo();
            if (snapshotInfo == null) {
                // if wait_for_completion is false the snapshotInfo is null
                resultFuture.complete(1L);
            } else if (snapshotInfo.state() == SnapshotState.FAILED) {
                // fail request if snapshot creation failed
                String reason = createSnapshotResponse.getSnapshotInfo().reason().replaceAll("Index", "Table").replaceAll("Indices", "Tables");
                resultFuture.completeExceptionally(new CreateSnapshotException(statement.snapshotId(), reason));
            } else {
                resultFuture.complete(1L);
            }
        }

        @Override
        public void onFailure(Throwable e) {
            resultFuture.completeExceptionally(e);
        }
    });
    return resultFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) CreateSnapshotException(io.crate.exceptions.CreateSnapshotException) CreateSnapshotRequest(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest) IndicesOptions(org.elasticsearch.action.support.IndicesOptions)

Example 3 with CreateSnapshotResponse

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

the class SharedClusterSnapshotRestoreIT method testSnapshotMoreThanOnce.

public void testSnapshotMoreThanOnce() throws ExecutionException, InterruptedException {
    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)));
    // only one shard
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1)));
    ensureGreen();
    logger.info("-->  indexing");
    final int numdocs = randomIntBetween(10, 100);
    IndexRequestBuilder[] builders = new IndexRequestBuilder[numdocs];
    for (int i = 0; i < builders.length; i++) {
        builders[i] = client().prepareIndex("test", "doc", Integer.toString(i)).setSource("foo", "bar" + i);
    }
    indexRandom(true, builders);
    flushAndRefresh();
    assertNoFailures(client().admin().indices().prepareForceMerge("test").setFlush(true).setMaxNumSegments(1).get());
    CreateSnapshotResponse createSnapshotResponseFirst = client.admin().cluster().prepareCreateSnapshot("test-repo", "test").setWaitForCompletion(true).setIndices("test").get();
    assertThat(createSnapshotResponseFirst.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponseFirst.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponseFirst.getSnapshotInfo().totalShards()));
    assertThat(client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test").get().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
    {
        SnapshotStatus snapshotStatus = client.admin().cluster().prepareSnapshotStatus("test-repo").setSnapshots("test").get().getSnapshots().get(0);
        List<SnapshotIndexShardStatus> shards = snapshotStatus.getShards();
        for (SnapshotIndexShardStatus status : shards) {
            assertThat(status.getStats().getProcessedFiles(), greaterThan(1));
        }
    }
    CreateSnapshotResponse createSnapshotResponseSecond = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-1").setWaitForCompletion(true).setIndices("test").get();
    assertThat(createSnapshotResponseSecond.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponseSecond.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponseSecond.getSnapshotInfo().totalShards()));
    assertThat(client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-1").get().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
    {
        SnapshotStatus snapshotStatus = client.admin().cluster().prepareSnapshotStatus("test-repo").setSnapshots("test-1").get().getSnapshots().get(0);
        List<SnapshotIndexShardStatus> shards = snapshotStatus.getShards();
        for (SnapshotIndexShardStatus status : shards) {
            assertThat(status.getStats().getProcessedFiles(), equalTo(0));
        }
    }
    client().prepareDelete("test", "doc", "1").get();
    CreateSnapshotResponse createSnapshotResponseThird = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-2").setWaitForCompletion(true).setIndices("test").get();
    assertThat(createSnapshotResponseThird.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponseThird.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponseThird.getSnapshotInfo().totalShards()));
    assertThat(client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-2").get().getSnapshots().get(0).state(), equalTo(SnapshotState.SUCCESS));
    {
        SnapshotStatus snapshotStatus = client.admin().cluster().prepareSnapshotStatus("test-repo").setSnapshots("test-2").get().getSnapshots().get(0);
        List<SnapshotIndexShardStatus> shards = snapshotStatus.getShards();
        for (SnapshotIndexShardStatus status : shards) {
            // we flush before the snapshot such that we have to process the segments_N files plus the .del file
            assertThat(status.getStats().getProcessedFiles(), equalTo(2));
        }
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ShardSnapshotStatus(org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) SnapshotStatus(org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotStatus) CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) ArrayList(java.util.ArrayList) List(java.util.List) SnapshotIndexShardStatus(org.elasticsearch.action.admin.cluster.snapshots.status.SnapshotIndexShardStatus) Client(org.elasticsearch.client.Client)

Example 4 with CreateSnapshotResponse

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

the class SharedClusterSnapshotRestoreIT method testRestoreWithDifferentMappingsAndSettings.

public void testRestoreWithDifferentMappingsAndSettings() 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 index with foo type");
    assertAcked(prepareCreate("test-idx", 2, Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_REPLICAS, between(0, 1)).put("refresh_interval", 10, TimeUnit.SECONDS)));
    NumShards numShards = getNumShards("test-idx");
    assertAcked(client().admin().indices().preparePutMapping("test-idx").setType("foo").setSource("baz", "type=text"));
    ensureGreen();
    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 with bar type");
    cluster().wipeIndices("test-idx");
    assertAcked(prepareCreate("test-idx", 2, Settings.builder().put(SETTING_NUMBER_OF_SHARDS, numShards.numPrimaries).put(SETTING_NUMBER_OF_REPLICAS, between(0, 1)).put("refresh_interval", 5, TimeUnit.SECONDS)));
    assertAcked(client().admin().indices().preparePutMapping("test-idx").setType("bar").setSource("baz", "type=text"));
    ensureGreen();
    logger.info("--> close index");
    client.admin().indices().prepareClose("test-idx").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));
    logger.info("--> assert that old mapping is restored");
    ImmutableOpenMap<String, MappingMetaData> mappings = client().admin().cluster().prepareState().get().getState().getMetaData().getIndices().get("test-idx").getMappings();
    assertThat(mappings.get("foo"), notNullValue());
    assertThat(mappings.get("bar"), nullValue());
    logger.info("--> assert that old settings are restored");
    GetSettingsResponse getSettingsResponse = client.admin().indices().prepareGetSettings("test-idx").execute().actionGet();
    assertThat(getSettingsResponse.getSetting("test-idx", "index.refresh_interval"), equalTo("10000ms"));
}
Also used : CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) GetSettingsResponse(org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) Client(org.elasticsearch.client.Client) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Example 5 with CreateSnapshotResponse

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

the class SharedClusterSnapshotRestoreIT method testDeleteSnapshotWithMissingMetadata.

public void testDeleteSnapshotWithMissingMetadata() 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("--> delete index metadata and shard metadata");
    Path metadata = repo.resolve("meta-" + createSnapshotResponse.getSnapshotInfo().snapshotId().getUUID() + ".dat");
    Files.delete(metadata);
    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);
}
Also used : Path(java.nio.file.Path) 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