Search in sources :

Example 1 with StoredContext

use of org.opensearch.common.util.concurrent.ThreadContext.StoredContext in project OpenSearch by opensearch-project.

the class IndexShardOperationPermits method acquire.

private void acquire(final ActionListener<Releasable> onAcquired, final String executorOnDelay, final boolean forceExecution, final Object debugInfo, final StackTraceElement[] stackTrace) {
    if (closed) {
        onAcquired.onFailure(new IndexShardClosedException(shardId));
        return;
    }
    final Releasable releasable;
    try {
        synchronized (this) {
            if (queuedBlockOperations > 0) {
                final Supplier<StoredContext> contextSupplier = threadPool.getThreadContext().newRestorableContext(false);
                final ActionListener<Releasable> wrappedListener;
                if (executorOnDelay != null) {
                    wrappedListener = ActionListener.delegateFailure(new ContextPreservingActionListener<>(contextSupplier, onAcquired), (l, r) -> threadPool.executor(executorOnDelay).execute(new ActionRunnable<Releasable>(l) {

                        @Override
                        public boolean isForceExecution() {
                            return forceExecution;
                        }

                        @Override
                        protected void doRun() {
                            listener.onResponse(r);
                        }

                        @Override
                        public void onRejection(Exception e) {
                            IOUtils.closeWhileHandlingException(r);
                            super.onRejection(e);
                        }
                    }));
                } else {
                    wrappedListener = new ContextPreservingActionListener<>(contextSupplier, onAcquired);
                }
                delayedOperations.add(new DelayedOperation(wrappedListener, debugInfo, stackTrace));
                return;
            } else {
                releasable = acquire(debugInfo, stackTrace);
            }
        }
    } catch (final InterruptedException e) {
        onAcquired.onFailure(e);
        return;
    }
    // execute this outside the synchronized block!
    onAcquired.onResponse(releasable);
}
Also used : StoredContext(org.opensearch.common.util.concurrent.ThreadContext.StoredContext) ContextPreservingActionListener(org.opensearch.action.support.ContextPreservingActionListener) ActionRunnable(org.opensearch.action.ActionRunnable) AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) ThreadPool(org.opensearch.threadpool.ThreadPool) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Releasable(org.opensearch.common.lease.Releasable) Supplier(java.util.function.Supplier) RunOnce(org.opensearch.common.util.concurrent.RunOnce) ArrayList(java.util.ArrayList) Assertions(org.opensearch.Assertions) Map(java.util.Map) ActionListener(org.opensearch.action.ActionListener) Semaphore(java.util.concurrent.Semaphore) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ExceptionsHelper(org.opensearch.ExceptionsHelper) Collectors(java.util.stream.Collectors) Tuple(org.opensearch.common.collect.Tuple) IOUtils(org.opensearch.core.internal.io.IOUtils) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Closeable(java.io.Closeable) CheckedRunnable(org.opensearch.common.CheckedRunnable) Collections(java.util.Collections) TimeoutException(java.util.concurrent.TimeoutException) StoredContext(org.opensearch.common.util.concurrent.ThreadContext.StoredContext) ContextPreservingActionListener(org.opensearch.action.support.ContextPreservingActionListener) Releasable(org.opensearch.common.lease.Releasable)

Aggregations

Closeable (java.io.Closeable)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Semaphore (java.util.concurrent.Semaphore)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 Assertions (org.opensearch.Assertions)1 ExceptionsHelper (org.opensearch.ExceptionsHelper)1 ActionListener (org.opensearch.action.ActionListener)1 ActionRunnable (org.opensearch.action.ActionRunnable)1 ContextPreservingActionListener (org.opensearch.action.support.ContextPreservingActionListener)1 CheckedRunnable (org.opensearch.common.CheckedRunnable)1 Tuple (org.opensearch.common.collect.Tuple)1 Releasable (org.opensearch.common.lease.Releasable)1