Search in sources :

Example 1 with LegacyReaderContext

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

the class SearchService method executeQueryPhase.

private SearchPhaseResult executeQueryPhase(ShardSearchRequest request, SearchShardTask task, boolean keepStatesInContext) throws Exception {
    final ReaderContext readerContext = createOrGetReaderContext(request, keepStatesInContext);
    try (Releasable ignored = readerContext.markAsUsed(getKeepAlive(request));
        SearchContext context = createContext(readerContext, request, task, true)) {
        final long afterQueryTime;
        try (SearchOperationListenerExecutor executor = new SearchOperationListenerExecutor(context)) {
            loadOrExecuteQueryPhase(request, context);
            if (context.queryResult().hasSearchContext() == false && readerContext.singleSession()) {
                freeReaderContext(readerContext.id());
            }
            afterQueryTime = executor.success();
        }
        if (request.numberOfShards() == 1) {
            return executeFetchPhase(readerContext, context, afterQueryTime);
        } else {
            // Pass the rescoreDocIds to the queryResult to send them the coordinating node and receive them back in the fetch phase.
            // We also pass the rescoreDocIds to the LegacyReaderContext in case the search state needs to stay in the data node.
            final RescoreDocIds rescoreDocIds = context.rescoreDocIds();
            context.queryResult().setRescoreDocIds(rescoreDocIds);
            readerContext.setRescoreDocIds(rescoreDocIds);
            return context.queryResult();
        }
    } catch (Exception e) {
        // execution exception can happen while loading the cache, strip it
        if (e instanceof ExecutionException) {
            e = (e.getCause() == null || e.getCause() instanceof Exception) ? (Exception) e.getCause() : new OpenSearchException(e.getCause());
        }
        logger.trace("Query phase failed", e);
        processFailure(readerContext, e);
        throw e;
    }
}
Also used : LegacyReaderContext(org.opensearch.search.internal.LegacyReaderContext) ReaderContext(org.opensearch.search.internal.ReaderContext) SearchContext(org.opensearch.search.internal.SearchContext) Releasable(org.opensearch.common.lease.Releasable) OpenSearchException(org.opensearch.OpenSearchException) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) 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 2 with LegacyReaderContext

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

the class SearchService method executeQueryPhase.

public void executeQueryPhase(QuerySearchRequest request, SearchShardTask task, ActionListener<QuerySearchResult> listener) {
    final ReaderContext readerContext = findReaderContext(request.contextId(), request.shardSearchRequest());
    final ShardSearchRequest shardSearchRequest = readerContext.getShardSearchRequest(request.shardSearchRequest());
    final Releasable markAsUsed = readerContext.markAsUsed(getKeepAlive(shardSearchRequest));
    runAsync(getExecutor(readerContext.indexShard()), () -> {
        readerContext.setAggregatedDfs(request.dfs());
        try (SearchContext searchContext = createContext(readerContext, shardSearchRequest, task, true);
            SearchOperationListenerExecutor executor = new SearchOperationListenerExecutor(searchContext)) {
            searchContext.searcher().setAggregatedDfs(request.dfs());
            queryPhase.execute(searchContext);
            if (searchContext.queryResult().hasSearchContext() == false && readerContext.singleSession()) {
                // no hits, we can release the context since there will be no fetch phase
                freeReaderContext(readerContext.id());
            }
            executor.success();
            // Pass the rescoreDocIds to the queryResult to send them the coordinating node and receive them back in the fetch phase.
            // We also pass the rescoreDocIds to the LegacyReaderContext in case the search state needs to stay in the data node.
            final RescoreDocIds rescoreDocIds = searchContext.rescoreDocIds();
            searchContext.queryResult().setRescoreDocIds(rescoreDocIds);
            readerContext.setRescoreDocIds(rescoreDocIds);
            return searchContext.queryResult();
        } catch (Exception e) {
            assert TransportActions.isShardNotAvailableException(e) == false : new AssertionError(e);
            logger.trace("Query phase failed", 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 3 with LegacyReaderContext

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

the class SearchService method executeFetchPhase.

public void executeFetchPhase(InternalScrollSearchRequest request, SearchShardTask task, ActionListener<ScrollQueryFetchSearchResult> listener) {
    final LegacyReaderContext readerContext = (LegacyReaderContext) findReaderContext(request.contextId(), request);
    final Releasable markAsUsed;
    try {
        markAsUsed = readerContext.markAsUsed(getScrollKeepAlive(request.scroll()));
    } catch (Exception e) {
        // We need to release the reader context of the scroll when we hit any exception (here the keep_alive can be too large)
        freeReaderContext(readerContext.id());
        throw e;
    }
    runAsync(getExecutor(readerContext.indexShard()), () -> {
        final ShardSearchRequest shardSearchRequest = readerContext.getShardSearchRequest(null);
        try (SearchContext searchContext = createContext(readerContext, shardSearchRequest, task, false);
            SearchOperationListenerExecutor executor = new SearchOperationListenerExecutor(searchContext)) {
            searchContext.assignRescoreDocIds(readerContext.getRescoreDocIds(null));
            searchContext.searcher().setAggregatedDfs(readerContext.getAggregatedDfs(null));
            processScroll(request, readerContext, searchContext);
            queryPhase.execute(searchContext);
            final long afterQueryTime = executor.success();
            QueryFetchSearchResult fetchSearchResult = executeFetchPhase(readerContext, searchContext, afterQueryTime);
            return new ScrollQueryFetchSearchResult(fetchSearchResult, searchContext.shardTarget());
        } catch (Exception e) {
            assert TransportActions.isShardNotAvailableException(e) == false : new AssertionError(e);
            logger.trace("Fetch phase failed", e);
            // we handle the failure in the failure listener below
            throw e;
        }
    }, wrapFailureListener(listener, readerContext, markAsUsed));
}
Also used : ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) ScrollQueryFetchSearchResult(org.opensearch.search.fetch.ScrollQueryFetchSearchResult) QueryFetchSearchResult(org.opensearch.search.fetch.QueryFetchSearchResult) LegacyReaderContext(org.opensearch.search.internal.LegacyReaderContext) 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) ScrollQueryFetchSearchResult(org.opensearch.search.fetch.ScrollQueryFetchSearchResult)

Example 4 with LegacyReaderContext

use of org.opensearch.search.internal.LegacyReaderContext 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 5 with LegacyReaderContext

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

the class SearchService method executeQueryPhase.

public void executeQueryPhase(InternalScrollSearchRequest request, SearchShardTask task, ActionListener<ScrollQuerySearchResult> listener) {
    final LegacyReaderContext readerContext = (LegacyReaderContext) findReaderContext(request.contextId(), request);
    final Releasable markAsUsed;
    try {
        markAsUsed = readerContext.markAsUsed(getScrollKeepAlive(request.scroll()));
    } catch (Exception e) {
        // We need to release the reader context of the scroll when we hit any exception (here the keep_alive can be too large)
        freeReaderContext(readerContext.id());
        throw e;
    }
    runAsync(getExecutor(readerContext.indexShard()), () -> {
        final ShardSearchRequest shardSearchRequest = readerContext.getShardSearchRequest(null);
        try (SearchContext searchContext = createContext(readerContext, shardSearchRequest, task, false);
            SearchOperationListenerExecutor executor = new SearchOperationListenerExecutor(searchContext)) {
            searchContext.searcher().setAggregatedDfs(readerContext.getAggregatedDfs(null));
            processScroll(request, readerContext, searchContext);
            queryPhase.execute(searchContext);
            executor.success();
            readerContext.setRescoreDocIds(searchContext.rescoreDocIds());
            return new ScrollQuerySearchResult(searchContext.queryResult(), searchContext.shardTarget());
        } catch (Exception e) {
            logger.trace("Query phase failed", e);
            // we handle the failure in the failure listener below
            throw e;
        }
    }, wrapFailureListener(listener, readerContext, markAsUsed));
}
Also used : ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) LegacyReaderContext(org.opensearch.search.internal.LegacyReaderContext) SearchContext(org.opensearch.search.internal.SearchContext) Releasable(org.opensearch.common.lease.Releasable) ScrollQuerySearchResult(org.opensearch.search.query.ScrollQuerySearchResult) 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)

Aggregations

LegacyReaderContext (org.opensearch.search.internal.LegacyReaderContext)6 IOException (java.io.IOException)5 Releasable (org.opensearch.common.lease.Releasable)5 OpenSearchRejectedExecutionException (org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException)5 ExecutionException (java.util.concurrent.ExecutionException)4 OpenSearchException (org.opensearch.OpenSearchException)4 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)4 AggregationInitializationException (org.opensearch.search.aggregations.AggregationInitializationException)4 ReaderContext (org.opensearch.search.internal.ReaderContext)4 SearchContext (org.opensearch.search.internal.SearchContext)4 ShardSearchRequest (org.opensearch.search.internal.ShardSearchRequest)4 IndexReader (org.apache.lucene.index.IndexReader)1 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 Query (org.apache.lucene.search.Query)1 QueryCachingPolicy (org.apache.lucene.search.QueryCachingPolicy)1 Sort (org.apache.lucene.search.Sort)1 Directory (org.apache.lucene.store.Directory)1 Mockito.anyString (org.mockito.Mockito.anyString)1