Search in sources :

Example 66 with Releasable

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

the class SearchService method createAndPutReaderContext.

final ReaderContext createAndPutReaderContext(ShardSearchRequest request, IndexService indexService, IndexShard shard, Engine.SearcherSupplier reader, boolean keepStatesInContext) {
    assert request.readerId() == null;
    assert request.keepAlive() == null;
    ReaderContext readerContext = null;
    Releasable decreaseScrollContexts = null;
    try {
        if (request.scroll() != null) {
            decreaseScrollContexts = openScrollContexts::decrementAndGet;
            if (openScrollContexts.incrementAndGet() > maxOpenScrollContext) {
                throw new OpenSearchRejectedExecutionException("Trying to create too many scroll contexts. Must be less than or equal to: [" + maxOpenScrollContext + "]. " + "This limit can be set by changing the [" + MAX_OPEN_SCROLL_CONTEXT.getKey() + "] setting.");
            }
        }
        final long keepAlive = getKeepAlive(request);
        final ShardSearchContextId id = new ShardSearchContextId(sessionId, idGenerator.incrementAndGet());
        if (keepStatesInContext || request.scroll() != null) {
            readerContext = new LegacyReaderContext(id, indexService, shard, reader, request, keepAlive);
            if (request.scroll() != null) {
                readerContext.addOnClose(decreaseScrollContexts);
                decreaseScrollContexts = null;
            }
        } else {
            readerContext = new ReaderContext(id, indexService, shard, reader, keepAlive, request.keepAlive() == null);
        }
        reader = null;
        final ReaderContext finalReaderContext = readerContext;
        final SearchOperationListener searchOperationListener = shard.getSearchOperationListener();
        searchOperationListener.onNewReaderContext(finalReaderContext);
        if (finalReaderContext.scrollContext() != null) {
            searchOperationListener.onNewScrollContext(finalReaderContext);
        }
        readerContext.addOnClose(() -> {
            try {
                if (finalReaderContext.scrollContext() != null) {
                    searchOperationListener.onFreeScrollContext(finalReaderContext);
                }
            } finally {
                searchOperationListener.onFreeReaderContext(finalReaderContext);
            }
        });
        putReaderContext(finalReaderContext);
        readerContext = null;
        return finalReaderContext;
    } finally {
        Releasables.close(reader, readerContext, decreaseScrollContexts);
    }
}
Also used : ShardSearchContextId(org.opensearch.search.internal.ShardSearchContextId) LegacyReaderContext(org.opensearch.search.internal.LegacyReaderContext) ReaderContext(org.opensearch.search.internal.ReaderContext) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) LegacyReaderContext(org.opensearch.search.internal.LegacyReaderContext) Releasable(org.opensearch.common.lease.Releasable) SearchOperationListener(org.opensearch.index.shard.SearchOperationListener)

Example 67 with Releasable

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

the class TransportReplicationActionTests method mockIndexShard.

@SuppressWarnings("unchecked")
private IndexShard mockIndexShard(ShardId shardId, ClusterService clusterService) {
    final IndexShard indexShard = mock(IndexShard.class);
    when(indexShard.shardId()).thenReturn(shardId);
    when(indexShard.state()).thenReturn(IndexShardState.STARTED);
    doAnswer(invocation -> {
        ActionListener<Releasable> callback = (ActionListener<Releasable>) invocation.getArguments()[0];
        if (isPrimaryMode.get()) {
            count.incrementAndGet();
            callback.onResponse(count::decrementAndGet);
        } else {
            callback.onFailure(new ShardNotInPrimaryModeException(shardId, IndexShardState.STARTED));
        }
        return null;
    }).when(indexShard).acquirePrimaryOperationPermit(any(ActionListener.class), anyString(), any(), eq(forceExecute));
    doAnswer(invocation -> {
        long term = (Long) invocation.getArguments()[0];
        ActionListener<Releasable> callback = (ActionListener<Releasable>) invocation.getArguments()[3];
        final long primaryTerm = indexShard.getPendingPrimaryTerm();
        if (term < primaryTerm) {
            throw new IllegalArgumentException(String.format(Locale.ROOT, "%s operation term [%d] is too old (current [%d])", shardId, term, primaryTerm));
        }
        count.incrementAndGet();
        callback.onResponse(count::decrementAndGet);
        return null;
    }).when(indexShard).acquireReplicaOperationPermit(anyLong(), anyLong(), anyLong(), any(ActionListener.class), anyString(), any());
    when(indexShard.getActiveOperationsCount()).thenAnswer(i -> count.get());
    when(indexShard.routingEntry()).thenAnswer(invocationOnMock -> {
        final ClusterState state = clusterService.state();
        final RoutingNode node = state.getRoutingNodes().node(state.nodes().getLocalNodeId());
        final ShardRouting routing = node.getByShardId(shardId);
        if (routing == null) {
            throw new ShardNotFoundException(shardId, "shard is no longer assigned to current node");
        }
        return routing;
    });
    when(indexShard.isRelocatedPrimary()).thenAnswer(invocationOnMock -> isRelocated.get());
    doThrow(new AssertionError("failed shard is not supported")).when(indexShard).failShard(anyString(), any(Exception.class));
    when(indexShard.getPendingPrimaryTerm()).thenAnswer(i -> clusterService.state().metadata().getIndexSafe(shardId.getIndex()).primaryTerm(shardId.id()));
    ReplicationGroup replicationGroup = mock(ReplicationGroup.class);
    when(indexShard.getReplicationGroup()).thenReturn(replicationGroup);
    return indexShard;
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ShardNotInPrimaryModeException(org.opensearch.index.shard.ShardNotInPrimaryModeException) IndexShard(org.opensearch.index.shard.IndexShard) ReplicationGroup(org.opensearch.index.shard.ReplicationGroup) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) NoNodeAvailableException(org.opensearch.client.transport.NoNodeAvailableException) TransportException(org.opensearch.transport.TransportException) IOException(java.io.IOException) ShardNotFoundException(org.opensearch.index.shard.ShardNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) OpenSearchException(org.opensearch.OpenSearchException) UnavailableShardsException(org.opensearch.action.UnavailableShardsException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IndexShardClosedException(org.opensearch.index.shard.IndexShardClosedException) ShardNotInPrimaryModeException(org.opensearch.index.shard.ShardNotInPrimaryModeException) IndexClosedException(org.opensearch.indices.IndexClosedException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ActionListener(org.opensearch.action.ActionListener) RoutingNode(org.opensearch.cluster.routing.RoutingNode) ShardNotFoundException(org.opensearch.index.shard.ShardNotFoundException) Mockito.anyLong(org.mockito.Mockito.anyLong) Releasable(org.opensearch.common.lease.Releasable) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting)

Example 68 with Releasable

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

the class TransportWriteActionForIndexingPressureTests method mockIndexShard.

@SuppressWarnings("unchecked")
private IndexShard mockIndexShard(ShardId shardId, ClusterService clusterService) {
    final IndexShard indexShard = mock(IndexShard.class);
    when(indexShard.shardId()).thenReturn(shardId);
    when(indexShard.state()).thenReturn(IndexShardState.STARTED);
    doAnswer(invocation -> {
        ActionListener<Releasable> callback = (ActionListener<Releasable>) invocation.getArguments()[0];
        if (isPrimaryMode.get()) {
            count.incrementAndGet();
            callback.onResponse(count::decrementAndGet);
        } else {
            callback.onFailure(new ShardNotInPrimaryModeException(shardId, IndexShardState.STARTED));
        }
        return null;
    }).when(indexShard).acquirePrimaryOperationPermit(any(ActionListener.class), anyString(), any());
    doAnswer(invocation -> {
        long term = (Long) invocation.getArguments()[0];
        ActionListener<Releasable> callback = (ActionListener<Releasable>) invocation.getArguments()[3];
        final long primaryTerm = indexShard.getPendingPrimaryTerm();
        if (term < primaryTerm) {
            throw new IllegalArgumentException(String.format(Locale.ROOT, "%s operation term [%d] is too old (current [%d])", shardId, term, primaryTerm));
        }
        count.incrementAndGet();
        callback.onResponse(count::decrementAndGet);
        return null;
    }).when(indexShard).acquireReplicaOperationPermit(anyLong(), anyLong(), anyLong(), any(ActionListener.class), anyString(), any());
    when(indexShard.getActiveOperationsCount()).thenAnswer(i -> count.get());
    when(indexShard.routingEntry()).thenAnswer(invocationOnMock -> {
        final ClusterState state = clusterService.state();
        final RoutingNode node = state.getRoutingNodes().node(state.nodes().getLocalNodeId());
        final ShardRouting routing = node.getByShardId(shardId);
        if (routing == null) {
            throw new ShardNotFoundException(shardId, "shard is no longer assigned to current node");
        }
        return routing;
    });
    when(indexShard.isRelocatedPrimary()).thenAnswer(invocationOnMock -> isRelocated.get());
    doThrow(new AssertionError("failed shard is not supported")).when(indexShard).failShard(anyString(), any(Exception.class));
    when(indexShard.getPendingPrimaryTerm()).thenAnswer(i -> clusterService.state().metadata().getIndexSafe(shardId.getIndex()).primaryTerm(shardId.id()));
    ReplicationGroup replicationGroup = mock(ReplicationGroup.class);
    when(indexShard.getReplicationGroup()).thenReturn(replicationGroup);
    return indexShard;
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ShardNotInPrimaryModeException(org.opensearch.index.shard.ShardNotInPrimaryModeException) IndexShard(org.opensearch.index.shard.IndexShard) ReplicationGroup(org.opensearch.index.shard.ReplicationGroup) ShardNotInPrimaryModeException(org.opensearch.index.shard.ShardNotInPrimaryModeException) IOException(java.io.IOException) ShardNotFoundException(org.opensearch.index.shard.ShardNotFoundException) ActionListener(org.opensearch.action.ActionListener) RoutingNode(org.opensearch.cluster.routing.RoutingNode) ShardNotFoundException(org.opensearch.index.shard.ShardNotFoundException) Mockito.anyLong(org.mockito.Mockito.anyLong) Releasable(org.opensearch.common.lease.Releasable) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 69 with Releasable

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

the class KeyedLockTests method testTryAcquire.

public void testTryAcquire() throws InterruptedException {
    KeyedLock<String> lock = new KeyedLock<>();
    Releasable foo = lock.tryAcquire("foo");
    Releasable second = lock.tryAcquire("foo");
    assertTrue(lock.hasLockedKeys());
    foo.close();
    assertTrue(lock.hasLockedKeys());
    second.close();
    assertFalse(lock.hasLockedKeys());
    // lock again
    Releasable acquire = lock.tryAcquire("foo");
    assertNotNull(acquire);
    final AtomicBoolean check = new AtomicBoolean(false);
    CountDownLatch latch = new CountDownLatch(1);
    Thread thread = new Thread(() -> {
        latch.countDown();
        try (Releasable ignore = lock.acquire("foo")) {
            assertTrue(check.get());
        }
    });
    thread.start();
    latch.await();
    check.set(true);
    acquire.close();
    foo.close();
    thread.join();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Releasable(org.opensearch.common.lease.Releasable) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 70 with Releasable

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

the class ReleasableLockTests method acquire.

private void acquire(final ReleasableLock lockToAcquire, final ReleasableLock otherLock) {
    try (@SuppressWarnings("unused") Releasable outer = randomAcquireMethod(lockToAcquire)) {
        assertTrue(lockToAcquire.isHeldByCurrentThread());
        assertFalse(otherLock.isHeldByCurrentThread());
        try (@SuppressWarnings("unused") Releasable inner = randomAcquireMethod(lockToAcquire)) {
            assertTrue(lockToAcquire.isHeldByCurrentThread());
            assertFalse(otherLock.isHeldByCurrentThread());
        }
        // previously there was a bug here and this would return false
        assertTrue(lockToAcquire.isHeldByCurrentThread());
        assertFalse(otherLock.isHeldByCurrentThread());
    }
    assertFalse(lockToAcquire.isHeldByCurrentThread());
    assertFalse(otherLock.isHeldByCurrentThread());
}
Also used : Releasable(org.opensearch.common.lease.Releasable)

Aggregations

Releasable (org.opensearch.common.lease.Releasable)161 ShardId (org.opensearch.index.shard.ShardId)50 IOException (java.io.IOException)45 CountDownLatch (java.util.concurrent.CountDownLatch)36 Settings (org.opensearch.common.settings.Settings)35 IndexingPressurePerShardStats (org.opensearch.index.stats.IndexingPressurePerShardStats)32 ExecutionException (java.util.concurrent.ExecutionException)30 ArrayList (java.util.ArrayList)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)28 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)27 OpenSearchException (org.opensearch.OpenSearchException)25 ThreadPool (org.opensearch.threadpool.ThreadPool)25 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)24 ActionListener (org.opensearch.action.ActionListener)23 IndexRequest (org.opensearch.action.index.IndexRequest)22 ShardRouting (org.opensearch.cluster.routing.ShardRouting)22 IndexingPressureStats (org.opensearch.index.stats.IndexingPressureStats)22 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)21 List (java.util.List)20 Translog (org.opensearch.index.translog.Translog)19