Search in sources :

Example 6 with RemoteClusterService

use of org.opensearch.transport.RemoteClusterService in project OpenSearch by opensearch-project.

the class TransportSearchAction method executeRequest.

private void executeRequest(Task task, SearchRequest searchRequest, SearchAsyncActionProvider searchAsyncActionProvider, ActionListener<SearchResponse> listener) {
    final long relativeStartNanos = System.nanoTime();
    final SearchTimeProvider timeProvider = new SearchTimeProvider(searchRequest.getOrCreateAbsoluteStartMillis(), relativeStartNanos, System::nanoTime);
    ActionListener<SearchSourceBuilder> rewriteListener = ActionListener.wrap(source -> {
        if (source != searchRequest.source()) {
            // only set it if it changed - we don't allow null values to be set but it might be already null. this way we catch
            // situations when source is rewritten to null due to a bug
            searchRequest.source(source);
        }
        final ClusterState clusterState = clusterService.state();
        final SearchContextId searchContext;
        final Map<String, OriginalIndices> remoteClusterIndices;
        if (searchRequest.pointInTimeBuilder() != null) {
            searchContext = SearchContextId.decode(namedWriteableRegistry, searchRequest.pointInTimeBuilder().getId());
            remoteClusterIndices = getIndicesFromSearchContexts(searchContext, searchRequest.indicesOptions());
        } else {
            searchContext = null;
            remoteClusterIndices = remoteClusterService.groupIndices(searchRequest.indicesOptions(), searchRequest.indices(), idx -> indexNameExpressionResolver.hasIndexAbstraction(idx, clusterState));
        }
        OriginalIndices localIndices = remoteClusterIndices.remove(RemoteClusterAware.LOCAL_CLUSTER_GROUP_KEY);
        if (remoteClusterIndices.isEmpty()) {
            executeLocalSearch(task, timeProvider, searchRequest, localIndices, clusterState, listener, searchContext, searchAsyncActionProvider);
        } else {
            if (shouldMinimizeRoundtrips(searchRequest)) {
                ccsRemoteReduce(searchRequest, localIndices, remoteClusterIndices, timeProvider, searchService.aggReduceContextBuilder(searchRequest), remoteClusterService, threadPool, listener, (r, l) -> executeLocalSearch(task, timeProvider, r, localIndices, clusterState, l, searchContext, searchAsyncActionProvider));
            } else {
                AtomicInteger skippedClusters = new AtomicInteger(0);
                collectSearchShards(searchRequest.indicesOptions(), searchRequest.preference(), searchRequest.routing(), skippedClusters, remoteClusterIndices, remoteClusterService, threadPool, ActionListener.wrap(searchShardsResponses -> {
                    final BiFunction<String, String, DiscoveryNode> clusterNodeLookup = getRemoteClusterNodeLookup(searchShardsResponses);
                    final Map<String, AliasFilter> remoteAliasFilters;
                    final List<SearchShardIterator> remoteShardIterators;
                    if (searchContext != null) {
                        remoteAliasFilters = searchContext.aliasFilter();
                        remoteShardIterators = getRemoteShardsIteratorFromPointInTime(searchShardsResponses, searchContext, searchRequest.pointInTimeBuilder().getKeepAlive(), remoteClusterIndices);
                    } else {
                        remoteAliasFilters = getRemoteAliasFilters(searchShardsResponses);
                        remoteShardIterators = getRemoteShardsIterator(searchShardsResponses, remoteClusterIndices, remoteAliasFilters);
                    }
                    int localClusters = localIndices == null ? 0 : 1;
                    int totalClusters = remoteClusterIndices.size() + localClusters;
                    int successfulClusters = searchShardsResponses.size() + localClusters;
                    executeSearch((SearchTask) task, timeProvider, searchRequest, localIndices, remoteShardIterators, clusterNodeLookup, clusterState, remoteAliasFilters, listener, new SearchResponse.Clusters(totalClusters, successfulClusters, skippedClusters.get()), searchContext, searchAsyncActionProvider);
                }, listener::onFailure));
            }
        }
    }, listener::onFailure);
    if (searchRequest.source() == null) {
        rewriteListener.onResponse(searchRequest.source());
    } else {
        Rewriteable.rewriteAndFetch(searchRequest.source(), searchService.getRewriteContext(timeProvider::getAbsoluteStartMillis), rewriteListener);
    }
}
Also used : RemoteClusterAware(org.opensearch.transport.RemoteClusterAware) Arrays(java.util.Arrays) LongSupplier(java.util.function.LongSupplier) CountDown(org.opensearch.common.util.concurrent.CountDown) ProfileShardResult(org.opensearch.search.profile.ProfileShardResult) SearchContext(org.opensearch.search.internal.SearchContext) BiFunction(java.util.function.BiFunction) ClusterSearchShardsResponse(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsResponse) CircuitBreaker(org.opensearch.common.breaker.CircuitBreaker) DFS_QUERY_THEN_FETCH(org.opensearch.action.search.SearchType.DFS_QUERY_THEN_FETCH) Strings(org.opensearch.common.Strings) Transport(org.opensearch.transport.Transport) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QUERY_THEN_FETCH(org.opensearch.action.search.SearchType.QUERY_THEN_FETCH) Property(org.opensearch.common.settings.Setting.Property) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) AliasFilter(org.opensearch.search.internal.AliasFilter) Map(java.util.Map) Inject(org.opensearch.common.inject.Inject) ActionListener(org.opensearch.action.ActionListener) CancellableTask(org.opensearch.tasks.CancellableTask) Client(org.opensearch.client.Client) TimeValue(org.opensearch.common.unit.TimeValue) NodeClient(org.opensearch.client.node.NodeClient) Index(org.opensearch.index.Index) RemoteTransportException(org.opensearch.transport.RemoteTransportException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) Set(java.util.Set) Task(org.opensearch.tasks.Task) TransportService(org.opensearch.transport.TransportService) Collectors(java.util.stream.Collectors) OriginalIndices(org.opensearch.action.OriginalIndices) Nullable(org.opensearch.common.Nullable) ActionFilters(org.opensearch.action.support.ActionFilters) OperationRouting(org.opensearch.cluster.routing.OperationRouting) List(java.util.List) SearchPhaseResult(org.opensearch.search.SearchPhaseResult) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) SearchProfileShardResults(org.opensearch.search.profile.SearchProfileShardResults) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) HandledTransportAction(org.opensearch.action.support.HandledTransportAction) FieldSortBuilder.hasPrimaryFieldSort(org.opensearch.search.sort.FieldSortBuilder.hasPrimaryFieldSort) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ShardIterator(org.opensearch.cluster.routing.ShardIterator) ThreadPool(org.opensearch.threadpool.ThreadPool) HashMap(java.util.HashMap) IndicesOptions(org.opensearch.action.support.IndicesOptions) Writeable(org.opensearch.common.io.stream.Writeable) TimeoutTaskCancellationUtility(org.opensearch.action.support.TimeoutTaskCancellationUtility) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) BiConsumer(java.util.function.BiConsumer) StreamSupport(java.util.stream.StreamSupport) SearchService(org.opensearch.search.SearchService) TASKS_ORIGIN(org.opensearch.action.admin.cluster.node.tasks.get.GetTaskAction.TASKS_ORIGIN) Setting(org.opensearch.common.settings.Setting) RemoteClusterService(org.opensearch.transport.RemoteClusterService) Executor(java.util.concurrent.Executor) ClusterBlockLevel(org.opensearch.cluster.block.ClusterBlockLevel) Rewriteable(org.opensearch.index.query.Rewriteable) TaskId(org.opensearch.tasks.TaskId) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) ClusterSearchShardsGroup(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsGroup) ClusterSearchShardsRequest(org.opensearch.action.admin.cluster.shards.ClusterSearchShardsRequest) ShardId(org.opensearch.index.shard.ShardId) TimeUnit(java.util.concurrent.TimeUnit) GroupShardsIterator(org.opensearch.cluster.routing.GroupShardsIterator) SearchShardTarget(org.opensearch.search.SearchShardTarget) CancelTasksRequest(org.opensearch.action.admin.cluster.node.tasks.cancel.CancelTasksRequest) CircuitBreakerService(org.opensearch.indices.breaker.CircuitBreakerService) ClusterService(org.opensearch.cluster.service.ClusterService) Collections(java.util.Collections) OriginSettingClient(org.opensearch.client.OriginSettingClient) ClusterState(org.opensearch.cluster.ClusterState) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiFunction(java.util.function.BiFunction) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) OriginalIndices(org.opensearch.action.OriginalIndices)

Aggregations

DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)6 SearchShardTarget (org.opensearch.search.SearchShardTarget)6 RemoteClusterService (org.opensearch.transport.RemoteClusterService)6 ArrayList (java.util.ArrayList)5 Arrays (java.util.Arrays)5 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 List (java.util.List)5 Set (java.util.Set)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 BiFunction (java.util.function.BiFunction)5 ActionListener (org.opensearch.action.ActionListener)5 OriginalIndices (org.opensearch.action.OriginalIndices)5 ClusterSearchShardsGroup (org.opensearch.action.admin.cluster.shards.ClusterSearchShardsGroup)5 ClusterSearchShardsResponse (org.opensearch.action.admin.cluster.shards.ClusterSearchShardsResponse)5 ShardId (org.opensearch.index.shard.ShardId)5 AliasFilter (org.opensearch.search.internal.AliasFilter)5 Collections (java.util.Collections)4 Map (java.util.Map)4 TimeUnit (java.util.concurrent.TimeUnit)4