Search in sources :

Example 16 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 17 with Releasable

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

the class TaskManagerTests method testTrackingChannelTask.

public void testTrackingChannelTask() throws Exception {
    final TaskManager taskManager = new TaskManager(Settings.EMPTY, threadPool, Collections.emptySet());
    Set<Task> cancelledTasks = ConcurrentCollections.newConcurrentSet();
    taskManager.setTaskCancellationService(new TaskCancellationService(mock(TransportService.class)) {

        @Override
        void cancelTaskAndDescendants(CancellableTask task, String reason, boolean waitForCompletion, ActionListener<Void> listener) {
            assertThat(reason, equalTo("channel was closed"));
            assertFalse(waitForCompletion);
            assertTrue("task [" + task + "] was cancelled already", cancelledTasks.add(task));
        }
    });
    Map<TcpChannel, Set<Task>> pendingTasks = new HashMap<>();
    Set<Task> expectedCancelledTasks = new HashSet<>();
    FakeTcpChannel[] channels = new FakeTcpChannel[randomIntBetween(1, 10)];
    List<Releasable> stopTrackingTasks = new ArrayList<>();
    for (int i = 0; i < channels.length; i++) {
        channels[i] = new SingleThreadedTcpChannel();
    }
    int iterations = randomIntBetween(1, 200);
    for (int i = 0; i < iterations; i++) {
        final List<Releasable> subset = randomSubsetOf(stopTrackingTasks);
        stopTrackingTasks.removeAll(subset);
        Releasables.close(subset);
        final FakeTcpChannel channel = randomFrom(channels);
        final Task task = taskManager.register("transport", "test", new CancellableRequest(Integer.toString(i)));
        if (channel.isOpen() && randomBoolean()) {
            channel.close();
            expectedCancelledTasks.addAll(pendingTasks.getOrDefault(channel, Collections.emptySet()));
        }
        final Releasable stopTracking = taskManager.startTrackingCancellableChannelTask(channel, (CancellableTask) task);
        if (channel.isOpen()) {
            pendingTasks.computeIfAbsent(channel, k -> new HashSet<>()).add(task);
            stopTrackingTasks.add(() -> {
                stopTracking.close();
                assertTrue(pendingTasks.get(channel).remove(task));
                expectedCancelledTasks.remove(task);
            });
        } else {
            expectedCancelledTasks.add(task);
        }
    }
    assertBusy(() -> assertThat(expectedCancelledTasks, everyItem(in(cancelledTasks))), 30, TimeUnit.SECONDS);
    for (FakeTcpChannel channel : channels) {
        channel.close();
    }
    assertThat(taskManager.numberOfChannelPendingTaskTrackers(), equalTo(0));
}
Also used : ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) HashMap(java.util.HashMap) Releasable(org.opensearch.common.lease.Releasable) Releasables(org.opensearch.common.lease.Releasables) ArrayList(java.util.ArrayList) ConcurrentCollections(org.opensearch.common.util.concurrent.ConcurrentCollections) HashSet(java.util.HashSet) Matchers.everyItem(org.hamcrest.Matchers.everyItem) After(org.junit.After) Map(java.util.Map) ActionListener(org.opensearch.action.ActionListener) TransportTasksActionTests(org.opensearch.action.admin.cluster.node.tasks.TransportTasksActionTests) Before(org.junit.Before) TimeValue(org.opensearch.common.unit.TimeValue) TransportRequest(org.opensearch.transport.TransportRequest) Iterator(java.util.Iterator) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) TcpChannel(org.opensearch.transport.TcpChannel) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) FakeTcpChannel(org.opensearch.transport.FakeTcpChannel) TransportService(org.opensearch.transport.TransportService) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Phaser(java.util.concurrent.Phaser) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Collections(java.util.Collections) Matchers.in(org.hamcrest.Matchers.in) Mockito.mock(org.mockito.Mockito.mock) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FakeTcpChannel(org.opensearch.transport.FakeTcpChannel) TcpChannel(org.opensearch.transport.TcpChannel) FakeTcpChannel(org.opensearch.transport.FakeTcpChannel) Releasable(org.opensearch.common.lease.Releasable) HashSet(java.util.HashSet)

Example 18 with Releasable

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

the class InboundPipelineTests method testDecodeExceptionIsPropagated.

public void testDecodeExceptionIsPropagated() throws IOException {
    BiConsumer<TcpChannel, InboundMessage> messageHandler = (c, m) -> {
    };
    final StatsTracker statsTracker = new StatsTracker();
    final LongSupplier millisSupplier = () -> TimeValue.nsecToMSec(System.nanoTime());
    final InboundDecoder decoder = new InboundDecoder(Version.CURRENT, PageCacheRecycler.NON_RECYCLING_INSTANCE);
    final Supplier<CircuitBreaker> breaker = () -> new NoopCircuitBreaker("test");
    final InboundAggregator aggregator = new InboundAggregator(breaker, (Predicate<String>) action -> true);
    final InboundPipeline pipeline = new InboundPipeline(statsTracker, millisSupplier, decoder, aggregator, messageHandler);
    try (BytesStreamOutput streamOutput = new BytesStreamOutput()) {
        String actionName = "actionName";
        final Version invalidVersion = Version.CURRENT.minimumCompatibilityVersion().minimumCompatibilityVersion();
        final String value = randomAlphaOfLength(1000);
        final boolean isRequest = randomBoolean();
        final long requestId = randomNonNegativeLong();
        OutboundMessage message;
        if (isRequest) {
            message = new OutboundMessage.Request(threadContext, new String[0], new TestRequest(value), invalidVersion, actionName, requestId, false, false);
        } else {
            message = new OutboundMessage.Response(threadContext, Collections.emptySet(), new TestResponse(value), invalidVersion, requestId, false, false);
        }
        final BytesReference reference = message.serialize(streamOutput);
        try (ReleasableBytesReference releasable = ReleasableBytesReference.wrap(reference)) {
            expectThrows(IllegalStateException.class, () -> pipeline.handleBytes(new FakeTcpChannel(), releasable));
        }
        // Pipeline cannot be reused after uncaught exception
        final IllegalStateException ise = expectThrows(IllegalStateException.class, () -> pipeline.handleBytes(new FakeTcpChannel(), ReleasableBytesReference.wrap(BytesArray.EMPTY)));
        assertEquals("Pipeline state corrupted by uncaught exception", ise.getMessage());
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) LongSupplier(java.util.function.LongSupplier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Version(org.opensearch.Version) Releasable(org.opensearch.common.lease.Releasable) CircuitBreaker(org.opensearch.common.breaker.CircuitBreaker) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) BiConsumer(java.util.function.BiConsumer) CircuitBreakingException(org.opensearch.common.breaker.CircuitBreakingException) NoopCircuitBreaker(org.opensearch.common.breaker.NoopCircuitBreaker) TimeValue(org.opensearch.common.unit.TimeValue) Streams(org.opensearch.core.internal.io.Streams) Predicate(java.util.function.Predicate) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Tuple(org.opensearch.common.collect.Tuple) Objects(java.util.Objects) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) BytesArray(org.opensearch.common.bytes.BytesArray) Collections(java.util.Collections) PageCacheRecycler(org.opensearch.common.util.PageCacheRecycler) TestCircuitBreaker(org.opensearch.common.breaker.TestCircuitBreaker) CircuitBreaker(org.opensearch.common.breaker.CircuitBreaker) NoopCircuitBreaker(org.opensearch.common.breaker.NoopCircuitBreaker) TestCircuitBreaker(org.opensearch.common.breaker.TestCircuitBreaker) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Version(org.opensearch.Version) NoopCircuitBreaker(org.opensearch.common.breaker.NoopCircuitBreaker) BytesReference(org.opensearch.common.bytes.BytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) LongSupplier(java.util.function.LongSupplier)

Example 19 with Releasable

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

the class InboundPipelineTests method testEnsureBodyIsNotPrematurelyReleased.

public void testEnsureBodyIsNotPrematurelyReleased() throws IOException {
    BiConsumer<TcpChannel, InboundMessage> messageHandler = (c, m) -> {
    };
    final StatsTracker statsTracker = new StatsTracker();
    final LongSupplier millisSupplier = () -> TimeValue.nsecToMSec(System.nanoTime());
    final InboundDecoder decoder = new InboundDecoder(Version.CURRENT, PageCacheRecycler.NON_RECYCLING_INSTANCE);
    final Supplier<CircuitBreaker> breaker = () -> new NoopCircuitBreaker("test");
    final InboundAggregator aggregator = new InboundAggregator(breaker, (Predicate<String>) action -> true);
    final InboundPipeline pipeline = new InboundPipeline(statsTracker, millisSupplier, decoder, aggregator, messageHandler);
    try (BytesStreamOutput streamOutput = new BytesStreamOutput()) {
        String actionName = "actionName";
        final Version version = Version.CURRENT;
        final String value = randomAlphaOfLength(1000);
        final boolean isRequest = randomBoolean();
        final long requestId = randomNonNegativeLong();
        OutboundMessage message;
        if (isRequest) {
            message = new OutboundMessage.Request(threadContext, new String[0], new TestRequest(value), version, actionName, requestId, false, false);
        } else {
            message = new OutboundMessage.Response(threadContext, Collections.emptySet(), new TestResponse(value), version, requestId, false, false);
        }
        final BytesReference reference = message.serialize(streamOutput);
        final int fixedHeaderSize = TcpHeader.headerSize(Version.CURRENT);
        final int variableHeaderSize = reference.getInt(fixedHeaderSize - 4);
        final int totalHeaderSize = fixedHeaderSize + variableHeaderSize;
        final AtomicBoolean bodyReleased = new AtomicBoolean(false);
        for (int i = 0; i < totalHeaderSize - 1; ++i) {
            try (ReleasableBytesReference slice = ReleasableBytesReference.wrap(reference.slice(i, 1))) {
                pipeline.handleBytes(new FakeTcpChannel(), slice);
            }
        }
        final Releasable releasable = () -> bodyReleased.set(true);
        final int from = totalHeaderSize - 1;
        final BytesReference partHeaderPartBody = reference.slice(from, reference.length() - from - 1);
        try (ReleasableBytesReference slice = new ReleasableBytesReference(partHeaderPartBody, releasable)) {
            pipeline.handleBytes(new FakeTcpChannel(), slice);
        }
        assertFalse(bodyReleased.get());
        try (ReleasableBytesReference slice = new ReleasableBytesReference(reference.slice(reference.length() - 1, 1), releasable)) {
            pipeline.handleBytes(new FakeTcpChannel(), slice);
        }
        assertTrue(bodyReleased.get());
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) LongSupplier(java.util.function.LongSupplier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Version(org.opensearch.Version) Releasable(org.opensearch.common.lease.Releasable) CircuitBreaker(org.opensearch.common.breaker.CircuitBreaker) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) BiConsumer(java.util.function.BiConsumer) CircuitBreakingException(org.opensearch.common.breaker.CircuitBreakingException) NoopCircuitBreaker(org.opensearch.common.breaker.NoopCircuitBreaker) TimeValue(org.opensearch.common.unit.TimeValue) Streams(org.opensearch.core.internal.io.Streams) Predicate(java.util.function.Predicate) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) IOException(java.io.IOException) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Tuple(org.opensearch.common.collect.Tuple) Objects(java.util.Objects) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) BytesArray(org.opensearch.common.bytes.BytesArray) Collections(java.util.Collections) PageCacheRecycler(org.opensearch.common.util.PageCacheRecycler) TestCircuitBreaker(org.opensearch.common.breaker.TestCircuitBreaker) CircuitBreaker(org.opensearch.common.breaker.CircuitBreaker) NoopCircuitBreaker(org.opensearch.common.breaker.NoopCircuitBreaker) TestCircuitBreaker(org.opensearch.common.breaker.TestCircuitBreaker) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) Version(org.opensearch.Version) NoopCircuitBreaker(org.opensearch.common.breaker.NoopCircuitBreaker) BytesReference(org.opensearch.common.bytes.BytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Releasable(org.opensearch.common.lease.Releasable) LongSupplier(java.util.function.LongSupplier)

Example 20 with Releasable

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

the class OpenSearchIndexLevelReplicationTestCase method executeResyncOnReplica.

private void executeResyncOnReplica(IndexShard replica, ResyncReplicationRequest request, long operationPrimaryTerm, long globalCheckpointOnPrimary, long maxSeqNoOfUpdatesOrDeletes) throws Exception {
    final Translog.Location location;
    final PlainActionFuture<Releasable> acquirePermitFuture = new PlainActionFuture<>();
    replica.acquireReplicaOperationPermit(operationPrimaryTerm, globalCheckpointOnPrimary, maxSeqNoOfUpdatesOrDeletes, acquirePermitFuture, ThreadPool.Names.SAME, request);
    try (Releasable ignored = acquirePermitFuture.actionGet()) {
        location = TransportResyncReplicationAction.performOnReplica(request, replica);
    }
    TransportWriteActionTestHelper.performPostWriteActions(replica, request, location, logger);
}
Also used : PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Releasable(org.opensearch.common.lease.Releasable) Translog(org.opensearch.index.translog.Translog)

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