Search in sources :

Example 11 with Releasable

use of org.opensearch.common.lease.Releasable in project OpenSearch by opensearch-project.

the class ReopenWhileClosingIT method testReopenDuringCloseOnMultipleIndices.

public void testReopenDuringCloseOnMultipleIndices() throws Exception {
    List<String> dataOnlyNodes = internalCluster().startDataOnlyNodes(randomIntBetween(2, 3));
    final List<String> indices = new ArrayList<>();
    for (int i = 0; i < randomIntBetween(2, 10); i++) {
        indices.add("index-" + i);
        createIndexWithDocs(indices.get(i), dataOnlyNodes);
    }
    ensureYellowAndNoInitializingShards(indices.toArray(Strings.EMPTY_ARRAY));
    final CountDownLatch block = new CountDownLatch(1);
    final Releasable releaseBlock = interceptVerifyShardBeforeCloseActions(randomFrom(indices), block::countDown);
    ActionFuture<CloseIndexResponse> closeIndexResponse = client().admin().indices().prepareClose("index-*").execute();
    assertTrue("Waiting for index to have a closing blocked", block.await(60, TimeUnit.SECONDS));
    assertFalse(closeIndexResponse.isDone());
    indices.forEach(ReopenWhileClosingIT::assertIndexIsBlocked);
    final List<String> reopenedIndices = randomSubsetOf(randomIntBetween(1, indices.size()), indices);
    assertAcked(client().admin().indices().prepareOpen(reopenedIndices.toArray(Strings.EMPTY_ARRAY)));
    releaseBlock.close();
    assertFalse(closeIndexResponse.get().isAcknowledged());
    indices.forEach(index -> {
        if (reopenedIndices.contains(index)) {
            assertIndexIsOpened(index);
        } else {
            assertIndexIsClosed(index);
        }
    });
}
Also used : CloseIndexResponse(org.opensearch.action.admin.indices.close.CloseIndexResponse) ArrayList(java.util.ArrayList) Releasable(org.opensearch.common.lease.Releasable) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 12 with Releasable

use of org.opensearch.common.lease.Releasable in project OpenSearch by opensearch-project.

the class TransportAction method execute.

/**
 * Use this method when the transport action call should result in creation of a new task associated with the call.
 *
 * This is a typical behavior.
 */
public final Task execute(Request request, ActionListener<Response> listener) {
    /*
         * While this version of execute could delegate to the TaskListener
         * version of execute that'd add yet another layer of wrapping on the
         * listener and prevent us from using the listener bare if there isn't a
         * task. That just seems like too many objects. Thus the two versions of
         * this method.
         */
    final Releasable unregisterChildNode = registerChildNode(request.getParentTask());
    final Task task;
    try {
        task = taskManager.register("transport", actionName, request);
    } catch (TaskCancelledException e) {
        unregisterChildNode.close();
        throw e;
    }
    execute(task, request, new ActionListener<Response>() {

        @Override
        public void onResponse(Response response) {
            try {
                Releasables.close(unregisterChildNode, () -> taskManager.unregister(task));
            } finally {
                listener.onResponse(response);
            }
        }

        @Override
        public void onFailure(Exception e) {
            try {
                Releasables.close(unregisterChildNode, () -> taskManager.unregister(task));
            } finally {
                listener.onFailure(e);
            }
        }
    });
    return task;
}
Also used : ActionResponse(org.opensearch.action.ActionResponse) Task(org.opensearch.tasks.Task) Releasable(org.opensearch.common.lease.Releasable) TaskCancelledException(org.opensearch.tasks.TaskCancelledException) ActionRequestValidationException(org.opensearch.action.ActionRequestValidationException) TaskCancelledException(org.opensearch.tasks.TaskCancelledException)

Example 13 with Releasable

use of org.opensearch.common.lease.Releasable in project OpenSearch by opensearch-project.

the class Engine method acquireSearcherSupplier.

/**
 * Acquires a point-in-time reader that can be used to create {@link Engine.Searcher}s on demand.
 */
public SearcherSupplier acquireSearcherSupplier(Function<Searcher, Searcher> wrapper, SearcherScope scope) throws EngineException {
    /* Acquire order here is store -> manager since we need
         * to make sure that the store is not closed before
         * the searcher is acquired. */
    if (store.tryIncRef() == false) {
        throw new AlreadyClosedException(shardId + " store is closed", failedEngine.get());
    }
    Releasable releasable = store::decRef;
    try {
        ReferenceManager<OpenSearchDirectoryReader> referenceManager = getReferenceManager(scope);
        OpenSearchDirectoryReader acquire = referenceManager.acquire();
        SearcherSupplier reader = new SearcherSupplier(wrapper) {

            @Override
            public Searcher acquireSearcherInternal(String source) {
                assert assertSearcherIsWarmedUp(source, scope);
                return new Searcher(source, acquire, engineConfig.getSimilarity(), engineConfig.getQueryCache(), engineConfig.getQueryCachingPolicy(), () -> {
                });
            }

            @Override
            protected void doClose() {
                try {
                    referenceManager.release(acquire);
                } catch (IOException e) {
                    throw new UncheckedIOException("failed to close", e);
                } catch (AlreadyClosedException e) {
                    // This means there's a bug somewhere: don't suppress it
                    throw new AssertionError(e);
                } finally {
                    store.decRef();
                }
            }
        };
        // success - hand over the reference to the engine reader
        releasable = null;
        return reader;
    } catch (AlreadyClosedException ex) {
        throw ex;
    } catch (Exception ex) {
        maybeFailEngine("acquire_reader", ex);
        // throw EngineCloseException here if we are already closed
        ensureOpen(ex);
        logger.error(() -> new ParameterizedMessage("failed to acquire reader"), ex);
        throw new EngineException(shardId, "failed to acquire reader", ex);
    } finally {
        Releasables.close(releasable);
    }
}
Also used : OpenSearchDirectoryReader(org.opensearch.common.lucene.index.OpenSearchDirectoryReader) IndexSearcher(org.apache.lucene.search.IndexSearcher) UncheckedIOException(java.io.UncheckedIOException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) NoSuchFileException(java.nio.file.NoSuchFileException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) FileNotFoundException(java.io.FileNotFoundException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) Releasable(org.opensearch.common.lease.Releasable) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 14 with Releasable

use of org.opensearch.common.lease.Releasable in project OpenSearch by opensearch-project.

the class IndexShardOperationPermits method asyncBlockOperations.

/**
 * Immediately delays operations and on another thread waits for in-flight operations to finish and then acquires all permits. When all
 * permits are acquired, the provided {@link ActionListener} is called under the guarantee that no new operations are started. Delayed
 * operations are run once the {@link Releasable} is released or if a failure occurs while acquiring all permits; in this case the
 * {@code onFailure} handler will be invoked after delayed operations are released.
 *
 * @param onAcquired {@link ActionListener} that is invoked once acquisition is successful or failed
 * @param timeout    the maximum time to wait for the in-flight operations block
 * @param timeUnit   the time unit of the {@code timeout} argument
 */
public void asyncBlockOperations(final ActionListener<Releasable> onAcquired, final long timeout, final TimeUnit timeUnit) {
    delayOperations();
    threadPool.executor(ThreadPool.Names.GENERIC).execute(new AbstractRunnable() {

        final RunOnce released = new RunOnce(() -> releaseDelayedOperations());

        @Override
        public void onFailure(final Exception e) {
            try {
                // resume delayed operations as soon as possible
                released.run();
            } finally {
                onAcquired.onFailure(e);
            }
        }

        @Override
        protected void doRun() throws Exception {
            final Releasable releasable = acquireAll(timeout, timeUnit);
            onAcquired.onResponse(() -> {
                try {
                    releasable.close();
                } finally {
                    released.run();
                }
            });
        }
    });
}
Also used : AbstractRunnable(org.opensearch.common.util.concurrent.AbstractRunnable) RunOnce(org.opensearch.common.util.concurrent.RunOnce) Releasable(org.opensearch.common.lease.Releasable) TimeoutException(java.util.concurrent.TimeoutException)

Example 15 with Releasable

use of org.opensearch.common.lease.Releasable 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

Releasable (org.opensearch.common.lease.Releasable)168 ShardId (org.opensearch.index.shard.ShardId)50 IOException (java.io.IOException)45 CountDownLatch (java.util.concurrent.CountDownLatch)40 Settings (org.opensearch.common.settings.Settings)37 ExecutionException (java.util.concurrent.ExecutionException)34 IndexingPressurePerShardStats (org.opensearch.index.stats.IndexingPressurePerShardStats)32 ArrayList (java.util.ArrayList)30 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)29 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)27 ThreadPool (org.opensearch.threadpool.ThreadPool)27 ActionListener (org.opensearch.action.ActionListener)26 OpenSearchException (org.opensearch.OpenSearchException)25 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)25 OpenSearchRejectedExecutionException (org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException)25 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)24 IndexRequest (org.opensearch.action.index.IndexRequest)22 ShardRouting (org.opensearch.cluster.routing.ShardRouting)22 IndexingPressureStats (org.opensearch.index.stats.IndexingPressureStats)22 List (java.util.List)20