Search in sources :

Example 1 with ContextPreservingActionListener

use of org.opensearch.action.support.ContextPreservingActionListener 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)

Example 2 with ContextPreservingActionListener

use of org.opensearch.action.support.ContextPreservingActionListener in project OpenSearch by opensearch-project.

the class RemoteClusterConnection method collectNodes.

/**
 * Collects all nodes on the connected cluster and returns / passes a nodeID to {@link DiscoveryNode} lookup function
 * that returns <code>null</code> if the node ID is not found.
 *
 * The requests to get cluster state on the connected cluster are made in the system context because logically
 * they are equivalent to checking a single detail in the local cluster state and should not require that the
 * user who made the request that is using this method in its implementation is authorized to view the entire
 * cluster state.
 */
void collectNodes(ActionListener<Function<String, DiscoveryNode>> listener) {
    Runnable runnable = () -> {
        final ThreadContext threadContext = threadPool.getThreadContext();
        final ContextPreservingActionListener<Function<String, DiscoveryNode>> contextPreservingActionListener = new ContextPreservingActionListener<>(threadContext.newRestorableContext(false), listener);
        try (ThreadContext.StoredContext ignore = threadContext.stashContext()) {
            // we stash any context here since this is an internal execution and should not leak any existing context information
            threadContext.markAsSystemContext();
            final ClusterStateRequest request = new ClusterStateRequest();
            request.clear();
            request.nodes(true);
            // run this on the node that gets the request it's as good as any other
            request.local(true);
            Transport.Connection connection = remoteConnectionManager.getAnyRemoteConnection();
            transportService.sendRequest(connection, ClusterStateAction.NAME, request, TransportRequestOptions.EMPTY, new TransportResponseHandler<ClusterStateResponse>() {

                @Override
                public ClusterStateResponse read(StreamInput in) throws IOException {
                    return new ClusterStateResponse(in);
                }

                @Override
                public void handleResponse(ClusterStateResponse response) {
                    DiscoveryNodes nodes = response.getState().nodes();
                    contextPreservingActionListener.onResponse(nodes::get);
                }

                @Override
                public void handleException(TransportException exp) {
                    contextPreservingActionListener.onFailure(exp);
                }

                @Override
                public String executor() {
                    return ThreadPool.Names.SAME;
                }
            });
        }
    };
    try {
        // just in case if we are not connected for some reason we try to connect and if we fail we have to notify the listener
        // this will cause some back pressure on the search end and eventually will cause rejections but that's fine
        // we can't proceed with a search on a cluster level.
        // in the future we might want to just skip the remote nodes in such a case but that can already be implemented on the
        // caller end since they provide the listener.
        ensureConnected(ActionListener.wrap((x) -> runnable.run(), listener::onFailure));
    } catch (Exception ex) {
        listener.onFailure(ex);
    }
}
Also used : StreamInput(org.opensearch.common.io.stream.StreamInput) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) TimeValue(org.opensearch.common.unit.TimeValue) ContextPreservingActionListener(org.opensearch.action.support.ContextPreservingActionListener) ClusterStateAction(org.opensearch.action.admin.cluster.state.ClusterStateAction) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) ThreadPool(org.opensearch.threadpool.ThreadPool) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Function(java.util.function.Function) IOUtils(org.opensearch.core.internal.io.IOUtils) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Closeable(java.io.Closeable) ActionListener(org.opensearch.action.ActionListener) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) IOException(java.io.IOException) ContextPreservingActionListener(org.opensearch.action.support.ContextPreservingActionListener) StreamInput(org.opensearch.common.io.stream.StreamInput) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes)

Aggregations

Closeable (java.io.Closeable)2 ActionListener (org.opensearch.action.ActionListener)2 ContextPreservingActionListener (org.opensearch.action.support.ContextPreservingActionListener)2 IOUtils (org.opensearch.core.internal.io.IOUtils)2 ThreadPool (org.opensearch.threadpool.ThreadPool)2 IOException (java.io.IOException)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 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1 Collectors (java.util.stream.Collectors)1 Assertions (org.opensearch.Assertions)1 ExceptionsHelper (org.opensearch.ExceptionsHelper)1