Search in sources :

Example 1 with ContextPreservingActionListener

use of org.elasticsearch.action.support.ContextPreservingActionListener in project elasticsearch by elastic.

the class IndexShardOperationsLock method acquire.

/**
     * Acquires a lock whenever lock acquisition is not blocked. If the lock is directly available, the provided
     * ActionListener will be called on the calling thread. During calls of {@link #blockOperations(long, TimeUnit, Runnable)}, lock
     * acquisition can be delayed. The provided ActionListener will then be called using the provided executor once blockOperations
     * terminates.
     *
     * @param onAcquired ActionListener that is invoked once acquisition is successful or failed
     * @param executorOnDelay executor to use for delayed call
     * @param forceExecution whether the runnable should force its execution in case it gets rejected
     */
public void acquire(ActionListener<Releasable> onAcquired, String executorOnDelay, boolean forceExecution) {
    if (closed) {
        onAcquired.onFailure(new IndexShardClosedException(shardId));
        return;
    }
    Releasable releasable;
    try {
        synchronized (this) {
            releasable = tryAcquire();
            if (releasable == null) {
                // blockOperations is executing, this operation will be retried by blockOperations once it finishes
                if (delayedOperations == null) {
                    delayedOperations = new ArrayList<>();
                }
                final Supplier<StoredContext> contextSupplier = threadPool.getThreadContext().newRestorableContext(false);
                if (executorOnDelay != null) {
                    delayedOperations.add(new ThreadedActionListener<>(logger, threadPool, executorOnDelay, new ContextPreservingActionListener<>(contextSupplier, onAcquired), forceExecution));
                } else {
                    delayedOperations.add(new ContextPreservingActionListener<>(contextSupplier, onAcquired));
                }
                return;
            }
        }
    } catch (InterruptedException e) {
        onAcquired.onFailure(e);
        return;
    }
    onAcquired.onResponse(releasable);
}
Also used : StoredContext(org.elasticsearch.common.util.concurrent.ThreadContext.StoredContext) ContextPreservingActionListener(org.elasticsearch.action.support.ContextPreservingActionListener) Releasable(org.elasticsearch.common.lease.Releasable)

Aggregations

ContextPreservingActionListener (org.elasticsearch.action.support.ContextPreservingActionListener)1 Releasable (org.elasticsearch.common.lease.Releasable)1 StoredContext (org.elasticsearch.common.util.concurrent.ThreadContext.StoredContext)1