Search in sources :

Example 1 with IndexShardRelocatedException

use of org.elasticsearch.index.shard.IndexShardRelocatedException in project elasticsearch by elastic.

the class RecoverySourceHandler method recoverToTarget.

/**
     * performs the recovery from the local engine to the target
     */
public RecoveryResponse recoverToTarget() throws IOException {
    try (Translog.View translogView = shard.acquireTranslogView()) {
        logger.trace("captured translog id [{}] for recovery", translogView.minTranslogGeneration());
        boolean isSequenceNumberBasedRecoveryPossible = request.startingSeqNo() != SequenceNumbersService.UNASSIGNED_SEQ_NO && isTranslogReadyForSequenceNumberBasedRecovery(translogView);
        if (isSequenceNumberBasedRecoveryPossible) {
            logger.trace("performing sequence numbers based recovery. starting at [{}]", request.startingSeqNo());
        } else {
            final IndexCommit phase1Snapshot;
            try {
                phase1Snapshot = shard.acquireIndexCommit(false);
            } catch (final Exception e) {
                IOUtils.closeWhileHandlingException(translogView);
                throw new RecoveryEngineException(shard.shardId(), 1, "snapshot failed", e);
            }
            try {
                phase1(phase1Snapshot, translogView);
            } catch (final Exception e) {
                throw new RecoveryEngineException(shard.shardId(), 1, "phase1 failed", e);
            } finally {
                try {
                    shard.releaseIndexCommit(phase1Snapshot);
                } catch (final IOException ex) {
                    logger.warn("releasing snapshot caused exception", ex);
                }
            }
        }
        try {
            prepareTargetForTranslog(translogView.totalOperations(), shard.segmentStats(false).getMaxUnsafeAutoIdTimestamp());
        } catch (final Exception e) {
            throw new RecoveryEngineException(shard.shardId(), 1, "prepare target for translog failed", e);
        }
        // engine was just started at the end of phase1
        if (shard.state() == IndexShardState.RELOCATED) {
            assert request.isPrimaryRelocation() == false : "recovery target should not retry primary relocation if previous attempt made it past finalization step";
            /*
                 * The primary shard has been relocated while we copied files. This means that we can't guarantee any more that all
                 * operations that were replicated during the file copy (when the target engine was not yet opened) will be present in the
                 * local translog and thus will be resent on phase2. The reason is that an operation replicated by the target primary is
                 * sent to the recovery target and the local shard (old primary) concurrently, meaning it may have arrived at the recovery
                 * target before we opened the engine and is still in-flight on the local shard.
                 *
                 * Checking the relocated status here, after we opened the engine on the target, is safe because primary relocation waits
                 * for all ongoing operations to complete and be fully replicated. Therefore all future operation by the new primary are
                 * guaranteed to reach the target shard when its engine is open.
                 */
            throw new IndexShardRelocatedException(request.shardId());
        }
        logger.trace("snapshot translog for recovery; current size is [{}]", translogView.totalOperations());
        try {
            phase2(isSequenceNumberBasedRecoveryPossible ? request.startingSeqNo() : SequenceNumbersService.UNASSIGNED_SEQ_NO, translogView.snapshot());
        } catch (Exception e) {
            throw new RecoveryEngineException(shard.shardId(), 2, "phase2 failed", e);
        }
        finalizeRecovery();
    }
    return response;
}
Also used : IndexShardRelocatedException(org.elasticsearch.index.shard.IndexShardRelocatedException) RecoveryEngineException(org.elasticsearch.index.engine.RecoveryEngineException) IOException(java.io.IOException) IndexCommit(org.apache.lucene.index.IndexCommit) IndexShardClosedException(org.elasticsearch.index.shard.IndexShardClosedException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) IndexShardRelocatedException(org.elasticsearch.index.shard.IndexShardRelocatedException) RecoveryEngineException(org.elasticsearch.index.engine.RecoveryEngineException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) IOException(java.io.IOException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) Translog(org.elasticsearch.index.translog.Translog)

Example 2 with IndexShardRelocatedException

use of org.elasticsearch.index.shard.IndexShardRelocatedException in project crate by crate.

the class RecoverySourceHandler method runUnderPrimaryPermit.

static void runUnderPrimaryPermit(CancellableThreads.Interruptable runnable, String reason, IndexShard primary, CancellableThreads cancellableThreads, Logger logger) {
    cancellableThreads.execute(() -> {
        CompletableFuture<Releasable> permit = new CompletableFuture<>();
        final ActionListener<Releasable> onAcquired = new ActionListener<>() {

            @Override
            public void onResponse(Releasable releasable) {
                if (permit.complete(releasable) == false) {
                    releasable.close();
                }
            }

            @Override
            public void onFailure(Exception e) {
                permit.completeExceptionally(e);
            }
        };
        primary.acquirePrimaryOperationPermit(onAcquired, ThreadPool.Names.SAME, reason);
        try (Releasable ignored = FutureUtils.get(permit)) {
            // races, as IndexShard will switch its authority only when it holds all operation permits, see IndexShard.relocated()
            if (primary.isRelocatedPrimary()) {
                throw new IndexShardRelocatedException(primary.shardId());
            }
            runnable.run();
        } finally {
            // just in case we got an exception (likely interrupted) while waiting for the get
            permit.whenComplete((r, e) -> {
                if (r != null) {
                    r.close();
                }
                if (e != null) {
                    logger.trace("suppressing exception on completion (it was already bubbled up or the operation was aborted)", e);
                }
            });
        }
    });
}
Also used : IndexShardRelocatedException(org.elasticsearch.index.shard.IndexShardRelocatedException) CompletableFuture(java.util.concurrent.CompletableFuture) ThreadedActionListener(org.elasticsearch.action.support.ThreadedActionListener) ActionListener(org.elasticsearch.action.ActionListener) Releasable(org.elasticsearch.common.lease.Releasable) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) RecoveryEngineException(org.elasticsearch.index.engine.RecoveryEngineException) RetentionLeaseNotFoundException(org.elasticsearch.index.seqno.RetentionLeaseNotFoundException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) RemoteTransportException(org.elasticsearch.transport.RemoteTransportException) IndexShardClosedException(org.elasticsearch.index.shard.IndexShardClosedException) IndexShardRelocatedException(org.elasticsearch.index.shard.IndexShardRelocatedException) IOException(java.io.IOException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException)

Aggregations

IOException (java.io.IOException)2 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)2 IndexFormatTooNewException (org.apache.lucene.index.IndexFormatTooNewException)2 IndexFormatTooOldException (org.apache.lucene.index.IndexFormatTooOldException)2 RecoveryEngineException (org.elasticsearch.index.engine.RecoveryEngineException)2 IndexShardClosedException (org.elasticsearch.index.shard.IndexShardClosedException)2 IndexShardRelocatedException (org.elasticsearch.index.shard.IndexShardRelocatedException)2 RemoteTransportException (org.elasticsearch.transport.RemoteTransportException)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 IndexCommit (org.apache.lucene.index.IndexCommit)1 ActionListener (org.elasticsearch.action.ActionListener)1 ThreadedActionListener (org.elasticsearch.action.support.ThreadedActionListener)1 Releasable (org.elasticsearch.common.lease.Releasable)1 RetentionLeaseNotFoundException (org.elasticsearch.index.seqno.RetentionLeaseNotFoundException)1 Translog (org.elasticsearch.index.translog.Translog)1