Search in sources :

Example 1 with SnapshotInfo

use of org.elasticsearch.snapshots.SnapshotInfo in project crate by crate.

the class SnapshotRestoreDDLDispatcherTest method testResolveTableIndexFromSnapshot.

@Test
public void testResolveTableIndexFromSnapshot() throws Exception {
    String resolvedIndex = SnapshotRestoreDDLDispatcher.ResolveFromSnapshotActionListener.resolveIndexNameFromSnapshot(new TableIdent("custom", "restoreme"), Collections.singletonList(new SnapshotInfo(new Snapshot("snapshot01", Collections.singletonList("custom.restoreme"), 0L))));
    assertThat(resolvedIndex, is("custom.restoreme"));
}
Also used : Snapshot(org.elasticsearch.snapshots.Snapshot) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) TableIdent(io.crate.metadata.TableIdent) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 2 with SnapshotInfo

use of org.elasticsearch.snapshots.SnapshotInfo in project crate by crate.

the class SnapshotRestoreDDLDispatcherTest method testResolvePartitionedTableIndexFromSnapshot.

@Test
public void testResolvePartitionedTableIndexFromSnapshot() throws Exception {
    String resolvedIndex = SnapshotRestoreDDLDispatcher.ResolveFromSnapshotActionListener.resolveIndexNameFromSnapshot(new TableIdent(null, "restoreme"), Collections.singletonList(new SnapshotInfo(new Snapshot("snapshot01", Collections.singletonList(".partitioned.restoreme.046jcchm6krj4e1g60o30c0"), 0L))));
    String template = PartitionName.templateName(null, "restoreme") + "*";
    assertThat(resolvedIndex, is(template));
}
Also used : Snapshot(org.elasticsearch.snapshots.Snapshot) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) TableIdent(io.crate.metadata.TableIdent) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 3 with SnapshotInfo

use of org.elasticsearch.snapshots.SnapshotInfo 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 4 with SnapshotInfo

use of org.elasticsearch.snapshots.SnapshotInfo in project elasticsearch by elastic.

the class TransportSnapshotsStatusAction method buildResponse.

private SnapshotsStatusResponse buildResponse(SnapshotsStatusRequest request, List<SnapshotsInProgress.Entry> currentSnapshotEntries, TransportNodesSnapshotsStatus.NodesSnapshotStatus nodeSnapshotStatuses) throws IOException {
    // First process snapshot that are currently processed
    List<SnapshotStatus> builder = new ArrayList<>();
    Set<String> currentSnapshotNames = new HashSet<>();
    if (!currentSnapshotEntries.isEmpty()) {
        Map<String, TransportNodesSnapshotsStatus.NodeSnapshotStatus> nodeSnapshotStatusMap;
        if (nodeSnapshotStatuses != null) {
            nodeSnapshotStatusMap = nodeSnapshotStatuses.getNodesMap();
        } else {
            nodeSnapshotStatusMap = new HashMap<>();
        }
        for (SnapshotsInProgress.Entry entry : currentSnapshotEntries) {
            currentSnapshotNames.add(entry.snapshot().getSnapshotId().getName());
            List<SnapshotIndexShardStatus> shardStatusBuilder = new ArrayList<>();
            for (ObjectObjectCursor<ShardId, SnapshotsInProgress.ShardSnapshotStatus> shardEntry : entry.shards()) {
                SnapshotsInProgress.ShardSnapshotStatus status = shardEntry.value;
                if (status.nodeId() != null) {
                    // We should have information about this shard from the shard:
                    TransportNodesSnapshotsStatus.NodeSnapshotStatus nodeStatus = nodeSnapshotStatusMap.get(status.nodeId());
                    if (nodeStatus != null) {
                        Map<ShardId, SnapshotIndexShardStatus> shardStatues = nodeStatus.status().get(entry.snapshot());
                        if (shardStatues != null) {
                            SnapshotIndexShardStatus shardStatus = shardStatues.get(shardEntry.key);
                            if (shardStatus != null) {
                                // We have full information about this shard
                                shardStatusBuilder.add(shardStatus);
                                continue;
                            }
                        }
                    }
                }
                final SnapshotIndexShardStage stage;
                switch(shardEntry.value.state()) {
                    case FAILED:
                    case ABORTED:
                    case MISSING:
                        stage = SnapshotIndexShardStage.FAILURE;
                        break;
                    case INIT:
                    case WAITING:
                    case STARTED:
                        stage = SnapshotIndexShardStage.STARTED;
                        break;
                    case SUCCESS:
                        stage = SnapshotIndexShardStage.DONE;
                        break;
                    default:
                        throw new IllegalArgumentException("Unknown snapshot state " + shardEntry.value.state());
                }
                SnapshotIndexShardStatus shardStatus = new SnapshotIndexShardStatus(shardEntry.key, stage);
                shardStatusBuilder.add(shardStatus);
            }
            builder.add(new SnapshotStatus(entry.snapshot(), entry.state(), Collections.unmodifiableList(shardStatusBuilder)));
        }
    }
    // Now add snapshots on disk that are not currently running
    final String repositoryName = request.repository();
    if (Strings.hasText(repositoryName) && request.snapshots() != null && request.snapshots().length > 0) {
        final Set<String> requestedSnapshotNames = Sets.newHashSet(request.snapshots());
        final RepositoryData repositoryData = snapshotsService.getRepositoryData(repositoryName);
        final Map<String, SnapshotId> matchedSnapshotIds = repositoryData.getAllSnapshotIds().stream().filter(s -> requestedSnapshotNames.contains(s.getName())).collect(Collectors.toMap(SnapshotId::getName, Function.identity()));
        for (final String snapshotName : request.snapshots()) {
            if (currentSnapshotNames.contains(snapshotName)) {
                // we've already found this snapshot in the current snapshot entries, so skip over
                continue;
            }
            SnapshotId snapshotId = matchedSnapshotIds.get(snapshotName);
            if (snapshotId == null) {
                // neither in the current snapshot entries nor found in the repository
                if (request.ignoreUnavailable()) {
                    // ignoring unavailable snapshots, so skip over
                    logger.debug("snapshot status request ignoring snapshot [{}], not found in repository [{}]", snapshotName, repositoryName);
                    continue;
                } else {
                    throw new SnapshotMissingException(repositoryName, snapshotName);
                }
            } else if (repositoryData.getIncompatibleSnapshotIds().contains(snapshotId)) {
                throw new SnapshotException(repositoryName, snapshotName, "cannot get the status for an incompatible snapshot");
            }
            SnapshotInfo snapshotInfo = snapshotsService.snapshot(repositoryName, snapshotId);
            List<SnapshotIndexShardStatus> shardStatusBuilder = new ArrayList<>();
            if (snapshotInfo.state().completed()) {
                Map<ShardId, IndexShardSnapshotStatus> shardStatues = snapshotsService.snapshotShards(request.repository(), snapshotInfo);
                for (Map.Entry<ShardId, IndexShardSnapshotStatus> shardStatus : shardStatues.entrySet()) {
                    shardStatusBuilder.add(new SnapshotIndexShardStatus(shardStatus.getKey(), shardStatus.getValue()));
                }
                final SnapshotsInProgress.State state;
                switch(snapshotInfo.state()) {
                    case FAILED:
                        state = SnapshotsInProgress.State.FAILED;
                        break;
                    case SUCCESS:
                    case PARTIAL:
                        // Translating both PARTIAL and SUCCESS to SUCCESS for now
                        // TODO: add the differentiation on the metadata level in the next major release
                        state = SnapshotsInProgress.State.SUCCESS;
                        break;
                    default:
                        throw new IllegalArgumentException("Unknown snapshot state " + snapshotInfo.state());
                }
                builder.add(new SnapshotStatus(new Snapshot(repositoryName, snapshotId), state, Collections.unmodifiableList(shardStatusBuilder)));
            }
        }
    }
    return new SnapshotsStatusResponse(Collections.unmodifiableList(builder));
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) SnapshotMissingException(org.elasticsearch.snapshots.SnapshotMissingException) SnapshotId(org.elasticsearch.snapshots.SnapshotId) Arrays(java.util.Arrays) ClusterService(org.elasticsearch.cluster.service.ClusterService) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) HashMap(java.util.HashMap) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) Function(java.util.function.Function) Strings(org.elasticsearch.common.Strings) Inject(org.elasticsearch.common.inject.Inject) ArrayList(java.util.ArrayList) TransportMasterNodeAction(org.elasticsearch.action.support.master.TransportMasterNodeAction) HashSet(java.util.HashSet) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) Map(java.util.Map) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TransportService(org.elasticsearch.transport.TransportService) ClusterBlockLevel(org.elasticsearch.cluster.block.ClusterBlockLevel) ActionFilters(org.elasticsearch.action.support.ActionFilters) SnapshotsService(org.elasticsearch.snapshots.SnapshotsService) Set(java.util.Set) IOException(java.io.IOException) ObjectCursor(com.carrotsearch.hppc.cursors.ObjectCursor) Collectors(java.util.stream.Collectors) Sets(org.elasticsearch.common.util.set.Sets) List(java.util.List) SnapshotException(org.elasticsearch.snapshots.SnapshotException) SnapshotsInProgress(org.elasticsearch.cluster.SnapshotsInProgress) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Snapshot(org.elasticsearch.snapshots.Snapshot) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) RepositoryData(org.elasticsearch.repositories.RepositoryData) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) ArrayList(java.util.ArrayList) ShardId(org.elasticsearch.index.shard.ShardId) SnapshotMissingException(org.elasticsearch.snapshots.SnapshotMissingException) HashSet(java.util.HashSet) SnapshotException(org.elasticsearch.snapshots.SnapshotException) RepositoryData(org.elasticsearch.repositories.RepositoryData) SnapshotId(org.elasticsearch.snapshots.SnapshotId) Snapshot(org.elasticsearch.snapshots.Snapshot) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) SnapshotsInProgress(org.elasticsearch.cluster.SnapshotsInProgress) IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with SnapshotInfo

use of org.elasticsearch.snapshots.SnapshotInfo in project elasticsearch by elastic.

the class GetSnapshotsResponse method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
    builder.startObject();
    builder.startArray("snapshots");
    for (SnapshotInfo snapshotInfo : snapshots) {
        snapshotInfo.toXContent(builder, params);
    }
    builder.endArray();
    builder.endObject();
    return builder;
}
Also used : SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo)

Aggregations

SnapshotInfo (org.elasticsearch.snapshots.SnapshotInfo)15 Snapshot (org.elasticsearch.snapshots.Snapshot)4 SnapshotId (org.elasticsearch.snapshots.SnapshotId)4 TableIdent (io.crate.metadata.TableIdent)3 CrateUnitTest (io.crate.test.integration.CrateUnitTest)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 SnapshotMissingException (org.elasticsearch.snapshots.SnapshotMissingException)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 GetSnapshotsResponse (org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse)2 ClusterState (org.elasticsearch.cluster.ClusterState)2 SnapshotsInProgress (org.elasticsearch.cluster.SnapshotsInProgress)2 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)2 IndexId (org.elasticsearch.repositories.IndexId)2 RepositoryData (org.elasticsearch.repositories.RepositoryData)2 RepositoryException (org.elasticsearch.repositories.RepositoryException)2