Search in sources :

Example 11 with Snapshot

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

the class SnapshotRestoreDDLDispatcherTest method testResolveMultiTablesIndexNamesFromSnapshot.

@Test
public void testResolveMultiTablesIndexNamesFromSnapshot() throws Exception {
    List<RestoreSnapshotAnalyzedStatement.RestoreTableInfo> tables = Arrays.asList(new RestoreSnapshotAnalyzedStatement.RestoreTableInfo(new TableIdent(null, "my_table"), null), new RestoreSnapshotAnalyzedStatement.RestoreTableInfo(new TableIdent(null, "my_partitioned_table"), null));
    List<SnapshotInfo> snapshots = Arrays.asList(new SnapshotInfo(new Snapshot("snapshot01", Collections.singletonList(".partitioned.my_partitioned_table.046jcchm6krj4e1g60o30c0"), 0)), new SnapshotInfo(new Snapshot("snapshot03", Collections.singletonList("my_table"), 0)));
    CompletableFuture<List<String>> future = new CompletableFuture<>();
    SnapshotRestoreDDLDispatcher.ResolveFromSnapshotActionListener actionListener = new SnapshotRestoreDDLDispatcher.ResolveFromSnapshotActionListener(future, tables, new HashSet<>());
    // need to mock here as constructor is not accessible
    GetSnapshotsResponse response = mock(GetSnapshotsResponse.class);
    when(response.getSnapshots()).thenReturn(snapshots);
    actionListener.onResponse(response);
    assertThat(future.get(), containsInAnyOrder("my_table", PartitionName.templateName(null, "my_partitioned_table") + "*"));
}
Also used : TableIdent(io.crate.metadata.TableIdent) RestoreSnapshotAnalyzedStatement(io.crate.analyze.RestoreSnapshotAnalyzedStatement) Snapshot(org.elasticsearch.snapshots.Snapshot) SnapshotInfo(org.elasticsearch.snapshots.SnapshotInfo) CompletableFuture(java.util.concurrent.CompletableFuture) GetSnapshotsResponse(org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse) List(java.util.List) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 12 with Snapshot

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

the class TransportRestoreSnapshotAction method masterOperation.

@Override
protected void masterOperation(final RestoreSnapshotRequest request, final ClusterState state, final ActionListener<RestoreSnapshotResponse> listener) {
    RestoreService.RestoreRequest restoreRequest = new RestoreService.RestoreRequest(request.repository(), request.snapshot(), request.indices(), request.indicesOptions(), request.renamePattern(), request.renameReplacement(), request.settings(), request.masterNodeTimeout(), request.includeGlobalState(), request.partial(), request.includeAliases(), request.indexSettings(), request.ignoreIndexSettings(), "restore_snapshot[" + request.snapshot() + "]");
    restoreService.restoreSnapshot(restoreRequest, new ActionListener<RestoreCompletionResponse>() {

        @Override
        public void onResponse(RestoreCompletionResponse restoreCompletionResponse) {
            if (restoreCompletionResponse.getRestoreInfo() == null && request.waitForCompletion()) {
                final Snapshot snapshot = restoreCompletionResponse.getSnapshot();
                ClusterStateListener clusterStateListener = new ClusterStateListener() {

                    @Override
                    public void clusterChanged(ClusterChangedEvent changedEvent) {
                        final RestoreInProgress.Entry prevEntry = restoreInProgress(changedEvent.previousState(), snapshot);
                        final RestoreInProgress.Entry newEntry = restoreInProgress(changedEvent.state(), snapshot);
                        if (prevEntry == null) {
                            // When there is a master failure after a restore has been started, this listener might not be registered
                            // on the current master and as such it might miss some intermediary cluster states due to batching.
                            // Clean up listener in that case and acknowledge completion of restore operation to client.
                            clusterService.removeListener(this);
                            listener.onResponse(new RestoreSnapshotResponse(null));
                        } else if (newEntry == null) {
                            clusterService.removeListener(this);
                            ImmutableOpenMap<ShardId, RestoreInProgress.ShardRestoreStatus> shards = prevEntry.shards();
                            assert prevEntry.state().completed() : "expected completed snapshot state but was " + prevEntry.state();
                            assert RestoreService.completed(shards) : "expected all restore entries to be completed";
                            RestoreInfo ri = new RestoreInfo(prevEntry.snapshot().getSnapshotId().getName(), prevEntry.indices(), shards.size(), shards.size() - RestoreService.failedShards(shards));
                            RestoreSnapshotResponse response = new RestoreSnapshotResponse(ri);
                            logger.debug("restore of [{}] completed", snapshot);
                            listener.onResponse(response);
                        } else {
                        // restore not completed yet, wait for next cluster state update
                        }
                    }
                };
                clusterService.addListener(clusterStateListener);
            } else {
                listener.onResponse(new RestoreSnapshotResponse(restoreCompletionResponse.getRestoreInfo()));
            }
        }

        @Override
        public void onFailure(Exception t) {
            listener.onFailure(t);
        }
    });
}
Also used : RestoreService(org.elasticsearch.snapshots.RestoreService) RestoreCompletionResponse(org.elasticsearch.snapshots.RestoreService.RestoreCompletionResponse) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) ClusterStateListener(org.elasticsearch.cluster.ClusterStateListener) ShardId(org.elasticsearch.index.shard.ShardId) Snapshot(org.elasticsearch.snapshots.Snapshot) RestoreInfo(org.elasticsearch.snapshots.RestoreInfo)

Example 13 with Snapshot

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

the class TransportNodesSnapshotsStatus method nodeOperation.

@Override
protected NodeSnapshotStatus nodeOperation(NodeRequest request) {
    Map<Snapshot, Map<ShardId, SnapshotIndexShardStatus>> snapshotMapBuilder = new HashMap<>();
    try {
        String nodeId = clusterService.localNode().getId();
        for (Snapshot snapshot : request.snapshots) {
            Map<ShardId, IndexShardSnapshotStatus> shardsStatus = snapshotShardsService.currentSnapshotShards(snapshot);
            if (shardsStatus == null) {
                continue;
            }
            Map<ShardId, SnapshotIndexShardStatus> shardMapBuilder = new HashMap<>();
            for (Map.Entry<ShardId, IndexShardSnapshotStatus> shardEntry : shardsStatus.entrySet()) {
                SnapshotIndexShardStatus shardStatus;
                IndexShardSnapshotStatus.Stage stage = shardEntry.getValue().stage();
                if (stage != IndexShardSnapshotStatus.Stage.DONE && stage != IndexShardSnapshotStatus.Stage.FAILURE) {
                    // Store node id for the snapshots that are currently running.
                    shardStatus = new SnapshotIndexShardStatus(shardEntry.getKey(), shardEntry.getValue(), nodeId);
                } else {
                    shardStatus = new SnapshotIndexShardStatus(shardEntry.getKey(), shardEntry.getValue());
                }
                shardMapBuilder.put(shardEntry.getKey(), shardStatus);
            }
            snapshotMapBuilder.put(snapshot, unmodifiableMap(shardMapBuilder));
        }
        return new NodeSnapshotStatus(clusterService.localNode(), unmodifiableMap(snapshotMapBuilder));
    } catch (Exception e) {
        throw new ElasticsearchException("failed to load metadata", e);
    }
}
Also used : IndexShardSnapshotStatus(org.elasticsearch.index.snapshots.IndexShardSnapshotStatus) HashMap(java.util.HashMap) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) FailedNodeException(org.elasticsearch.action.FailedNodeException) IOException(java.io.IOException) ShardId(org.elasticsearch.index.shard.ShardId) Snapshot(org.elasticsearch.snapshots.Snapshot) HashMap(java.util.HashMap) Map(java.util.Map) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap)

Example 14 with Snapshot

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

the class PrimaryShardAllocatorTests method getRecoverOnAnyNodeRoutingAllocation.

private RoutingAllocation getRecoverOnAnyNodeRoutingAllocation(AllocationDeciders allocationDeciders, String... allocIds) {
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder(shardId.getIndexName()).settings(settings(Version.CURRENT).put(IndexMetaData.SETTING_SHARED_FILESYSTEM, true).put(IndexMetaData.SETTING_SHARED_FS_ALLOW_RECOVERY_ON_ANY_NODE, true)).numberOfShards(1).numberOfReplicas(0).putInSyncAllocationIds(0, Sets.newHashSet(allocIds))).build();
    RoutingTable routingTable = RoutingTable.builder().addAsRestore(metaData.index(shardId.getIndex()), new SnapshotRecoverySource(new Snapshot("test", new SnapshotId("test", UUIDs.randomBase64UUID())), Version.CURRENT, shardId.getIndexName())).build();
    ClusterState state = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).nodes(DiscoveryNodes.builder().add(node1).add(node2).add(node3)).build();
    return new RoutingAllocation(allocationDeciders, new RoutingNodes(state, false), state, null, System.nanoTime(), false);
}
Also used : Snapshot(org.elasticsearch.snapshots.Snapshot) SnapshotId(org.elasticsearch.snapshots.SnapshotId) ClusterState(org.elasticsearch.cluster.ClusterState) SnapshotRecoverySource(org.elasticsearch.cluster.routing.RecoverySource.SnapshotRecoverySource) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RoutingAllocation(org.elasticsearch.cluster.routing.allocation.RoutingAllocation)

Example 15 with Snapshot

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

the class PrimaryShardAllocatorTests method getRestoreRoutingAllocation.

private RoutingAllocation getRestoreRoutingAllocation(AllocationDeciders allocationDeciders, String... allocIds) {
    MetaData metaData = MetaData.builder().put(IndexMetaData.builder(shardId.getIndexName()).settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0).putInSyncAllocationIds(0, Sets.newHashSet(allocIds))).build();
    final Snapshot snapshot = new Snapshot("test", new SnapshotId("test", UUIDs.randomBase64UUID()));
    RoutingTable routingTable = RoutingTable.builder().addAsRestore(metaData.index(shardId.getIndex()), new SnapshotRecoverySource(snapshot, Version.CURRENT, shardId.getIndexName())).build();
    ClusterState state = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).nodes(DiscoveryNodes.builder().add(node1).add(node2).add(node3)).build();
    return new RoutingAllocation(allocationDeciders, new RoutingNodes(state, false), state, null, System.nanoTime(), false);
}
Also used : Snapshot(org.elasticsearch.snapshots.Snapshot) SnapshotId(org.elasticsearch.snapshots.SnapshotId) ClusterState(org.elasticsearch.cluster.ClusterState) SnapshotRecoverySource(org.elasticsearch.cluster.routing.RecoverySource.SnapshotRecoverySource) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RoutingNodes(org.elasticsearch.cluster.routing.RoutingNodes) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RoutingAllocation(org.elasticsearch.cluster.routing.allocation.RoutingAllocation)

Aggregations

Snapshot (org.elasticsearch.snapshots.Snapshot)20 SnapshotId (org.elasticsearch.snapshots.SnapshotId)13 ClusterState (org.elasticsearch.cluster.ClusterState)9 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)7 MetaData (org.elasticsearch.cluster.metadata.MetaData)7 SnapshotRecoverySource (org.elasticsearch.cluster.routing.RecoverySource.SnapshotRecoverySource)7 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)4 ShardId (org.elasticsearch.index.shard.ShardId)4 TableIdent (io.crate.metadata.TableIdent)3 CrateUnitTest (io.crate.test.integration.CrateUnitTest)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)3 SnapshotInfo (org.elasticsearch.snapshots.SnapshotInfo)3 IntHashSet (com.carrotsearch.hppc.IntHashSet)2 HashMap (java.util.HashMap)2 SnapshotsInProgress (org.elasticsearch.cluster.SnapshotsInProgress)2 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)2