Search in sources :

Example 16 with ShardSnapshotStatus

use of org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus in project crate by crate.

the class SnapshotsService method endSnapshot.

/**
 * Finalizes the shard in repository and then removes it from cluster state
 * <p>
 * This is non-blocking method that runs on a thread from SNAPSHOT thread pool
 *
 * @param entry SnapshotsInProgress.Entry   current snapshot in progress
 * @param metadata Metadata                 cluster metadata
 */
private void endSnapshot(SnapshotsInProgress.Entry entry, Metadata metadata) {
    if (endingSnapshots.add(entry.snapshot()) == false) {
        return;
    }
    threadPool.executor(ThreadPool.Names.SNAPSHOT).execute(new AbstractRunnable() {

        @Override
        protected void doRun() {
            final Snapshot snapshot = entry.snapshot();
            final Repository repository = repositoriesService.repository(snapshot.getRepository());
            final String failure = entry.failure();
            LOGGER.trace("[{}] finalizing snapshot in repository, state: [{}], failure[{}]", snapshot, entry.state(), failure);
            ArrayList<SnapshotShardFailure> shardFailures = new ArrayList<>();
            for (ObjectObjectCursor<ShardId, ShardSnapshotStatus> shardStatus : entry.shards()) {
                ShardId shardId = shardStatus.key;
                ShardSnapshotStatus status = shardStatus.value;
                if (status.state().failed()) {
                    shardFailures.add(new SnapshotShardFailure(status.nodeId(), shardId, status.reason()));
                }
            }
            final ShardGenerations shardGenerations = buildGenerations(entry, metadata);
            repository.finalizeSnapshot(snapshot.getSnapshotId(), shardGenerations, entry.startTime(), failure, entry.partial() ? shardGenerations.totalShards() : entry.shards().size(), unmodifiableList(shardFailures), entry.repositoryStateId(), entry.includeGlobalState(), metadataForSnapshot(entry, metadata), entry.useShardGenerations(), ActionListener.wrap(snapshotInfo -> {
                removeSnapshotFromClusterState(snapshot, snapshotInfo, null);
                LOGGER.info("snapshot [{}] completed with state [{}]", snapshot, snapshotInfo.state());
            }, this::onFailure));
        }

        @Override
        public void onFailure(final Exception e) {
            Snapshot snapshot = entry.snapshot();
            if (ExceptionsHelper.unwrap(e, NotMasterException.class, FailedToCommitClusterStateException.class) != null) {
                // Failure due to not being master any more, don't try to remove snapshot from cluster state the next master
                // will try ending this snapshot again
                LOGGER.debug(() -> new ParameterizedMessage("[{}] failed to update cluster state during snapshot finalization", snapshot), e);
                endingSnapshots.remove(snapshot);
            } else {
                LOGGER.warn(() -> new ParameterizedMessage("[{}] failed to finalize snapshot", snapshot), e);
                removeSnapshotFromClusterState(snapshot, null, e);
            }
        }
    });
}
Also used : AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) ShardGenerations(org.elasticsearch.repositories.ShardGenerations) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) FailedToCommitClusterStateException(org.elasticsearch.cluster.coordination.FailedToCommitClusterStateException) RepositoryException(org.elasticsearch.repositories.RepositoryException) RepositoryMissingException(org.elasticsearch.repositories.RepositoryMissingException) NotMasterException(org.elasticsearch.cluster.NotMasterException) ShardId(org.elasticsearch.index.shard.ShardId) Repository(org.elasticsearch.repositories.Repository) ShardSnapshotStatus(org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor)

Aggregations

ShardSnapshotStatus (org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus)16 ShardId (org.elasticsearch.index.shard.ShardId)13 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)12 SnapshotsInProgress (org.elasticsearch.cluster.SnapshotsInProgress)11 IndexShardSnapshotStatus (org.elasticsearch.index.snapshots.IndexShardSnapshotStatus)9 ObjectObjectCursor (com.carrotsearch.hppc.cursors.ObjectObjectCursor)8 ArrayList (java.util.ArrayList)7 ClusterState (org.elasticsearch.cluster.ClusterState)7 RepositoryMissingException (org.elasticsearch.repositories.RepositoryMissingException)7 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)6 ClusterStateUpdateTask (org.elasticsearch.cluster.ClusterStateUpdateTask)6 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)5 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)5 IndexId (org.elasticsearch.repositories.IndexId)5 RepositoryException (org.elasticsearch.repositories.RepositoryException)5 Collections.emptyMap (java.util.Collections.emptyMap)4 List (java.util.List)4