Search in sources :

Example 6 with ReaderContext

use of org.opensearch.search.internal.ReaderContext in project OpenSearch by opensearch-project.

the class SearchServiceTests method testLookUpSearchContext.

public void testLookUpSearchContext() throws Exception {
    createIndex("index");
    SearchService searchService = getInstanceFromNode(SearchService.class);
    IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    IndexService indexService = indicesService.indexServiceSafe(resolveIndex("index"));
    IndexShard indexShard = indexService.getShard(0);
    List<ShardSearchContextId> contextIds = new ArrayList<>();
    int numContexts = randomIntBetween(1, 10);
    CountDownLatch latch = new CountDownLatch(1);
    indexShard.getThreadPool().executor(ThreadPool.Names.SEARCH).execute(() -> {
        try {
            for (int i = 0; i < numContexts; i++) {
                ShardSearchRequest request = new ShardSearchRequest(OriginalIndices.NONE, new SearchRequest().allowPartialSearchResults(true), indexShard.shardId(), 1, new AliasFilter(null, Strings.EMPTY_ARRAY), 1.0f, -1, null, null);
                final ReaderContext context = searchService.createAndPutReaderContext(request, indexService, indexShard, indexShard.acquireSearcherSupplier(), randomBoolean());
                assertThat(context.id().getId(), equalTo((long) (i + 1)));
                contextIds.add(context.id());
            }
            assertThat(searchService.getActiveContexts(), equalTo(contextIds.size()));
            while (contextIds.isEmpty() == false) {
                final ShardSearchContextId contextId = randomFrom(contextIds);
                expectThrows(SearchContextMissingException.class, () -> searchService.freeReaderContext(new ShardSearchContextId(UUIDs.randomBase64UUID(), contextId.getId())));
                assertThat(searchService.getActiveContexts(), equalTo(contextIds.size()));
                if (randomBoolean()) {
                    assertTrue(searchService.freeReaderContext(contextId));
                } else {
                    assertTrue(searchService.freeReaderContext((new ShardSearchContextId(contextId.getSessionId(), contextId.getId()))));
                }
                contextIds.remove(contextId);
                assertThat(searchService.getActiveContexts(), equalTo(contextIds.size()));
                assertFalse(searchService.freeReaderContext(contextId));
                assertThat(searchService.getActiveContexts(), equalTo(contextIds.size()));
            }
        } finally {
            latch.countDown();
        }
    });
    latch.await();
}
Also used : ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) AliasFilter(org.opensearch.search.internal.AliasFilter) IndexService(org.opensearch.index.IndexService) IndexShard(org.opensearch.index.shard.IndexShard) IntArrayList(com.carrotsearch.hppc.IntArrayList) ArrayList(java.util.ArrayList) IndicesService(org.opensearch.indices.IndicesService) CountDownLatch(java.util.concurrent.CountDownLatch) ShardSearchContextId(org.opensearch.search.internal.ShardSearchContextId) ReaderContext(org.opensearch.search.internal.ReaderContext) ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest)

Example 7 with ReaderContext

use of org.opensearch.search.internal.ReaderContext in project OpenSearch by opensearch-project.

the class MockSearchServiceTests method testAssertNoInFlightContext.

public void testAssertNoInFlightContext() {
    ReaderContext reader = mock(ReaderContext.class);
    MockSearchService.addActiveContext(reader);
    try {
        Throwable e = expectThrows(AssertionError.class, () -> MockSearchService.assertNoInFlightContext());
        assertEquals("There are still [1] in-flight contexts. The first one's creation site is listed as the cause of this exception.", e.getMessage());
        e = e.getCause();
        assertEquals(MockSearchService.class.getName(), e.getStackTrace()[0].getClassName());
        assertEquals(MockSearchServiceTests.class.getName(), e.getStackTrace()[1].getClassName());
    } finally {
        MockSearchService.removeActiveContext(reader);
    }
}
Also used : ReaderContext(org.opensearch.search.internal.ReaderContext)

Example 8 with ReaderContext

use of org.opensearch.search.internal.ReaderContext in project OpenSearch by opensearch-project.

the class SearchService method openReaderContext.

/**
 * Opens the reader context for given shardId. The newly opened reader context will be keep
 * until the {@code keepAlive} elapsed unless it is manually released.
 */
public void openReaderContext(ShardId shardId, TimeValue keepAlive, ActionListener<ShardSearchContextId> listener) {
    checkKeepAliveLimit(keepAlive.millis());
    final IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    final IndexShard shard = indexService.getShard(shardId.id());
    final SearchOperationListener searchOperationListener = shard.getSearchOperationListener();
    shard.awaitShardSearchActive(ignored -> {
        Engine.SearcherSupplier searcherSupplier = null;
        ReaderContext readerContext = null;
        try {
            searcherSupplier = shard.acquireSearcherSupplier();
            final ShardSearchContextId id = new ShardSearchContextId(sessionId, idGenerator.incrementAndGet());
            readerContext = new ReaderContext(id, indexService, shard, searcherSupplier, keepAlive.millis(), false);
            final ReaderContext finalReaderContext = readerContext;
            // transfer ownership to reader context
            searcherSupplier = null;
            searchOperationListener.onNewReaderContext(readerContext);
            readerContext.addOnClose(() -> searchOperationListener.onFreeReaderContext(finalReaderContext));
            putReaderContext(readerContext);
            readerContext = null;
            listener.onResponse(finalReaderContext.id());
        } catch (Exception exc) {
            Releasables.closeWhileHandlingException(searcherSupplier, readerContext);
            listener.onFailure(exc);
        }
    });
}
Also used : ShardSearchContextId(org.opensearch.search.internal.ShardSearchContextId) IndexService(org.opensearch.index.IndexService) IndexShard(org.opensearch.index.shard.IndexShard) LegacyReaderContext(org.opensearch.search.internal.LegacyReaderContext) ReaderContext(org.opensearch.search.internal.ReaderContext) SearchOperationListener(org.opensearch.index.shard.SearchOperationListener) Engine(org.opensearch.index.engine.Engine) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) AggregationInitializationException(org.opensearch.search.aggregations.AggregationInitializationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) OpenSearchException(org.opensearch.OpenSearchException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException)

Example 9 with ReaderContext

use of org.opensearch.search.internal.ReaderContext in project OpenSearch by opensearch-project.

the class SearchService method executeFetchPhase.

public void executeFetchPhase(ShardFetchRequest request, SearchShardTask task, ActionListener<FetchSearchResult> listener) {
    final ReaderContext readerContext = findReaderContext(request.contextId(), request);
    final ShardSearchRequest shardSearchRequest = readerContext.getShardSearchRequest(request.getShardSearchRequest());
    final Releasable markAsUsed = readerContext.markAsUsed(getKeepAlive(shardSearchRequest));
    runAsync(getExecutor(readerContext.indexShard()), () -> {
        try (SearchContext searchContext = createContext(readerContext, shardSearchRequest, task, false)) {
            if (request.lastEmittedDoc() != null) {
                searchContext.scrollContext().lastEmittedDoc = request.lastEmittedDoc();
            }
            searchContext.assignRescoreDocIds(readerContext.getRescoreDocIds(request.getRescoreDocIds()));
            searchContext.searcher().setAggregatedDfs(readerContext.getAggregatedDfs(request.getAggregatedDfs()));
            searchContext.docIdsToLoad(request.docIds(), 0, request.docIdsSize());
            try (SearchOperationListenerExecutor executor = new SearchOperationListenerExecutor(searchContext, true, System.nanoTime())) {
                fetchPhase.execute(searchContext);
                if (readerContext.singleSession()) {
                    freeReaderContext(request.contextId());
                }
                executor.success();
            }
            return searchContext.fetchResult();
        } catch (Exception e) {
            assert TransportActions.isShardNotAvailableException(e) == false : new AssertionError(e);
            // we handle the failure in the failure listener below
            throw e;
        }
    }, wrapFailureListener(listener, readerContext, markAsUsed));
}
Also used : LegacyReaderContext(org.opensearch.search.internal.LegacyReaderContext) ReaderContext(org.opensearch.search.internal.ReaderContext) ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) SearchContext(org.opensearch.search.internal.SearchContext) Releasable(org.opensearch.common.lease.Releasable) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) AggregationInitializationException(org.opensearch.search.aggregations.AggregationInitializationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) OpenSearchException(org.opensearch.OpenSearchException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException)

Example 10 with ReaderContext

use of org.opensearch.search.internal.ReaderContext in project OpenSearch by opensearch-project.

the class SearchService method canMatch.

private CanMatchResponse canMatch(ShardSearchRequest request, boolean checkRefreshPending) throws IOException {
    assert request.searchType() == SearchType.QUERY_THEN_FETCH : "unexpected search type: " + request.searchType();
    final ReaderContext readerContext = request.readerId() != null ? findReaderContext(request.readerId(), request) : null;
    final Releasable markAsUsed = readerContext != null ? readerContext.markAsUsed(getKeepAlive(request)) : () -> {
    };
    try (Releasable ignored = markAsUsed) {
        final IndexService indexService;
        final Engine.Searcher canMatchSearcher;
        final boolean hasRefreshPending;
        if (readerContext != null) {
            indexService = readerContext.indexService();
            canMatchSearcher = readerContext.acquireSearcher(Engine.CAN_MATCH_SEARCH_SOURCE);
            hasRefreshPending = false;
        } else {
            indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
            IndexShard indexShard = indexService.getShard(request.shardId().getId());
            hasRefreshPending = indexShard.hasRefreshPending() && checkRefreshPending;
            canMatchSearcher = indexShard.acquireSearcher(Engine.CAN_MATCH_SEARCH_SOURCE);
        }
        try (Releasable ignored2 = canMatchSearcher) {
            QueryShardContext context = indexService.newQueryShardContext(request.shardId().id(), canMatchSearcher, request::nowInMillis, request.getClusterAlias());
            Rewriteable.rewrite(request.getRewriteable(), context, false);
            final boolean aliasFilterCanMatch = request.getAliasFilter().getQueryBuilder() instanceof MatchNoneQueryBuilder == false;
            FieldSortBuilder sortBuilder = FieldSortBuilder.getPrimaryFieldSortOrNull(request.source());
            MinAndMax<?> minMax = sortBuilder != null ? FieldSortBuilder.getMinMaxOrNull(context, sortBuilder) : null;
            final boolean canMatch;
            if (canRewriteToMatchNone(request.source())) {
                QueryBuilder queryBuilder = request.source().query();
                canMatch = aliasFilterCanMatch && queryBuilder instanceof MatchNoneQueryBuilder == false;
            } else {
                // null query means match_all
                canMatch = aliasFilterCanMatch;
            }
            return new CanMatchResponse(canMatch || hasRefreshPending, minMax);
        }
    }
}
Also used : IndexService(org.opensearch.index.IndexService) IndexShard(org.opensearch.index.shard.IndexShard) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) LegacyReaderContext(org.opensearch.search.internal.LegacyReaderContext) ReaderContext(org.opensearch.search.internal.ReaderContext) QueryShardContext(org.opensearch.index.query.QueryShardContext) Releasable(org.opensearch.common.lease.Releasable) Engine(org.opensearch.index.engine.Engine)

Aggregations

ReaderContext (org.opensearch.search.internal.ReaderContext)19 IndexService (org.opensearch.index.IndexService)11 IndexShard (org.opensearch.index.shard.IndexShard)10 LegacyReaderContext (org.opensearch.search.internal.LegacyReaderContext)10 ShardSearchRequest (org.opensearch.search.internal.ShardSearchRequest)10 SearchContext (org.opensearch.search.internal.SearchContext)8 IOException (java.io.IOException)5 SearchRequest (org.opensearch.action.search.SearchRequest)5 Releasable (org.opensearch.common.lease.Releasable)5 OpenSearchRejectedExecutionException (org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException)5 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)5 Engine (org.opensearch.index.engine.Engine)5 IndicesService (org.opensearch.indices.IndicesService)5 AliasFilter (org.opensearch.search.internal.AliasFilter)5 ExecutionException (java.util.concurrent.ExecutionException)4 OpenSearchException (org.opensearch.OpenSearchException)4 AggregationInitializationException (org.opensearch.search.aggregations.AggregationInitializationException)4 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)4 QueryShardContext (org.opensearch.index.query.QueryShardContext)3 SearchOperationListener (org.opensearch.index.shard.SearchOperationListener)3