最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Java ES多節(jié)點任務的高效分發(fā)與收集實現(xiàn)

 更新時間:2021年06月30日 14:37:18   作者:等你歸去來  
ElasticSearch 是一個高可用開源全文檢索和分析組件。提供存儲服務,搜索服務,大數(shù)據(jù)準實時分析等。一般用于提供一些提供復雜搜索的應用

一、概述

我們知道,當我們對es發(fā)起search請求或其他操作時,往往都是隨機選擇一個coordinator發(fā)起請求。而這請求,可能是該節(jié)點能處理,也可能是該節(jié)點不能處理的,也可能是需要多節(jié)點共同處理的,可以說是情況比較復雜。

所以,coordinator的重要工作是,做請求分發(fā)與結(jié)果收集。那么,如何高性能和安全準確地實現(xiàn)這一功能則至關(guān)重要。

二、請求分發(fā)的簡單思路

我們這里所說的請求分發(fā),一般是針對多個網(wǎng)絡節(jié)點而言的。那么,如何將請求發(fā)往多節(jié)點,并在最終將結(jié)果合并起來呢?

同步請求各節(jié)點,當?shù)谝粋€節(jié)點響應后,再向第二個節(jié)點發(fā)起請求,以此類推,直到所有節(jié)點請求完成,然后再將結(jié)果聚合起來。就完成了需求了,不費吹灰之力。簡單不?

無腦處理自有無腦處理的缺點。依次請求各節(jié)點,無法很好利用系統(tǒng)的分布式特點,變并行為串行了,好不厲害。另外,對于當前請求,當其未處理完成這所有節(jié)點的分發(fā)收集工作時,當前線程將會一直被占用。從而,下游請求將無法再接入,從而將你了并發(fā)能力,使其與線程池大小同日而語了。這可不好。

我們依次想辦法優(yōu)化下。

首先,我們可以將串行分發(fā)請求變成并行分發(fā),即可以使用多線程,向多節(jié)點發(fā)起請求,當某線程處理完成時,就返回結(jié)果。使用類似于CountDownLatch的同步工具,保證所有節(jié)點都處理完成后,再由外單主線程進行結(jié)果合并操作。

以上優(yōu)化,看起來不錯,避免了同步的性能問題。但是,當有某個節(jié)點響應非常慢時,它將阻塞后續(xù)節(jié)點的工作,從而使整個請求變慢,從而同樣變成線程池的大小即是并發(fā)能力的瓶頸??梢哉f,治標不治本。

再來,繼續(xù)優(yōu)化。我們可以釋放掉主線程的持有,讓每個分發(fā)線程處理完成當前任務時,都去檢查任務隊列,是否已完成。如果未完成則忽略,如果已完成,則啟動合并任務。

看起來不錯,已經(jīng)有完全并發(fā)樣子了。但還能不能再優(yōu)化?各節(jié)點的分發(fā),同樣是同步請求,雖然處理簡單,但在這server響應期間,該線程仍是無法被使用的,如果類似請求過多,則必然是不小的消耗。如果能將單節(jié)點的請求,能夠做到異步處理,那樣豈不完美?但這恐怕不好做吧!不過,終歸是一個不錯的想法了。

三、es中search的多節(jié)點分發(fā)收集

我們以search的分發(fā)收集為出發(fā)點,觀看es如何辦成這件。原因是search在es中最為普遍與經(jīng)典,雖說不得每個地方實現(xiàn)都一樣,但至少參考意義還是有的。故以search為切入點。search的框架工作流程,我們之前已經(jīng)研究過,本節(jié)就直接以核心開始講解,它是在 TransportSearchAction.executeRequest() 中的。

// org.elasticsearch.action.search.TransportSearchAction#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 {
                // 多節(jié)點數(shù)據(jù)請求
                if (shouldMinimizeRoundtrips(searchRequest)) {
                    // 通過 parentTaskId 關(guān)聯(lián)所有子任務
                    final TaskId parentTaskId = task.taskInfo(clusterService.localNode().getId(), false).getTaskId();
                    ccsRemoteReduce(parentTaskId, 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);
                    // 直接分發(fā)多shard請求到各節(jié)點
                    collectSearchShards(searchRequest.indicesOptions(), searchRequest.preference(), searchRequest.routing(),
                        skippedClusters, remoteClusterIndices, remoteClusterService, threadPool,
                        ActionListener.wrap(
                            searchShardsResponses -> {
                                // 當所有節(jié)點都響應后,再做后續(xù)邏輯處理,即此處的后置監(jiān)聽
                                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;
                                // 至于后續(xù)搜索實現(xiàn)如何,不在此間
                                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);
        }
    }

可以看到,es的search功能,會被劃分為幾種類型,有點會走集群分發(fā),而有的則不需要。我們自然是希望走集群分發(fā)的,所以,只需看 collectSearchShards() 即可。這里面其實就是對多個集群節(jié)點的依次請求,當然還有結(jié)果收集。

// org.elasticsearch.action.search.TransportSearchAction#collectSearchShards
static void collectSearchShards(IndicesOptions indicesOptions, String preference, String routing, AtomicInteger skippedClusters,
                                Map<String, OriginalIndices> remoteIndicesByCluster, RemoteClusterService remoteClusterService,
                                ThreadPool threadPool, ActionListener<Map<String, ClusterSearchShardsResponse>> listener) {
    // 使用該計數(shù)器進行結(jié)果控制
    final CountDown responsesCountDown = new CountDown(remoteIndicesByCluster.size());
    final Map<String, ClusterSearchShardsResponse> searchShardsResponses = new ConcurrentHashMap<>();
    final AtomicReference<Exception> exceptions = new AtomicReference<>();
    // 迭代各節(jié)點,依次發(fā)送請求
    for (Map.Entry<String, OriginalIndices> entry : remoteIndicesByCluster.entrySet()) {
        final String clusterAlias = entry.getKey();
        boolean skipUnavailable = remoteClusterService.isSkipUnavailable(clusterAlias);
        Client clusterClient = remoteClusterService.getRemoteClusterClient(threadPool, clusterAlias);
        final String[] indices = entry.getValue().indices();
        ClusterSearchShardsRequest searchShardsRequest = new ClusterSearchShardsRequest(indices)
            .indicesOptions(indicesOptions).local(true).preference(preference).routing(routing);
        // 向集群中 clusterAlias 異步發(fā)起請求處理 search
        clusterClient.admin().cluster().searchShards(searchShardsRequest,
            new CCSActionListener<ClusterSearchShardsResponse, Map<String, ClusterSearchShardsResponse>>(
                clusterAlias, skipUnavailable, responsesCountDown, skippedClusters, exceptions, listener) {
                @Override
                void innerOnResponse(ClusterSearchShardsResponse clusterSearchShardsResponse) {
                    // 每次單節(jié)點響應時,將結(jié)果存放到 searchShardsResponses 中
                    searchShardsResponses.put(clusterAlias, clusterSearchShardsResponse);
                }

                @Override
                Map<String, ClusterSearchShardsResponse> createFinalResponse() {
                    // 所有節(jié)點都返回時,將結(jié)果集返回
                    return searchShardsResponses;
                }
            }
        );
    }
}
// org.elasticsearch.client.support.AbstractClient.ClusterAdmin#searchShards
@Override
public void searchShards(final ClusterSearchShardsRequest request, final ActionListener<ClusterSearchShardsResponse> listener) {
    // 發(fā)起請求 indices:admin/shards/search_shards, 其對應處理器為 TransportClusterSearchShardsAction
    execute(ClusterSearchShardsAction.INSTANCE, request, listener);
}

以上是es向集群中多節(jié)點發(fā)起請求的過程,其重點在于所有的請求都是異步請求,即向各節(jié)點發(fā)送完成請求后,當前線程即為斷開狀態(tài)。這就體現(xiàn)了無阻塞的能力了,以listner形式進行處理后續(xù)業(yè)務。這對于發(fā)送自然沒有問題,但如何進行結(jié)果收集呢?實際上就是通過listner來處理的。在遠程節(jié)點響應后,listener.onResponse()將被調(diào)用。

3.1、多節(jié)點響應結(jié)果處理

這是我們本文討論的重點。前面我們看到es已經(jīng)異步發(fā)送請求出去了(且不論其如何發(fā)送),所以如何收集結(jié)果也很關(guān)鍵。而es中的做法則很簡單,使用一個 ConcurrentHashMap 收集每個結(jié)果,一個CountDown標識是否已處理完成。

// org.elasticsearch.action.search.TransportSearchAction.CCSActionListener#CCSActionListener
CCSActionListener(String clusterAlias, boolean skipUnavailable, CountDown countDown, AtomicInteger skippedClusters,
                    AtomicReference<Exception> exceptions, ActionListener<FinalResponse> originalListener) {
    this.clusterAlias = clusterAlias;
    this.skipUnavailable = skipUnavailable;
    this.countDown = countDown;
    this.skippedClusters = skippedClusters;
    this.exceptions = exceptions;
    this.originalListener = originalListener;
}

// 成功時的響應
@Override
public final void onResponse(Response response) {
    // inner響應為將結(jié)果放入 searchShardsResponses 中
    innerOnResponse(response);
    // maybeFinish 則進行結(jié)果是否完成判定,如果完成,則調(diào)用回調(diào)方法,構(gòu)造結(jié)果
    maybeFinish();
}

private void maybeFinish() {
    // 使用一個 AtomicInteger 進行控制
    if (countDown.countDown()) {
        Exception exception = exceptions.get();
        if (exception == null) {
            FinalResponse response;
            try {
                // 創(chuàng)建響應結(jié)果,此處 search 即為 searchShardsResponses
                response = createFinalResponse();
            } catch(Exception e) {
                originalListener.onFailure(e);
                return;
            }
            // 成功響應回調(diào),實現(xiàn)結(jié)果收集后的其他業(yè)務處理
            originalListener.onResponse(response);
        } else {
            originalListener.onFailure(exceptions.get());
        }
    }
}
// CountDown 實現(xiàn)比較簡單,只有最后一個返回true, 其他皆為false, 即實現(xiàn)了 At Most Once 語義
/**
* Decrements the count-down and returns <code>true</code> iff this call
* reached zero otherwise <code>false</code>
*/
public boolean countDown() {
    assert originalCount > 0;
    for (;;) {
        final int current = countDown.get();
        assert current >= 0;
        if (current == 0) {
            return false;
        }
        if (countDown.compareAndSet(current, current - 1)) {
            return current == 1;
        }
    }
}

可見,ES中的結(jié)果收集,是以一個 AtomicInteger 實現(xiàn)的CountDown來處理的,當所有節(jié)點都響應時,就處理最終結(jié)果,否則將每個節(jié)點的數(shù)據(jù)放入ConcurrentHashMap中暫存起來。

而通過一個Client通用的異步調(diào)用框架,實現(xiàn)多節(jié)點的異步提交。整個節(jié)點響應以 CCSActionListener 作為接收者。可以說是比較簡潔的了,好像也沒有我們前面討論的復雜性。因為:大道至簡。

3.2、異步提交請求實現(xiàn)

我們知道,如果本地想實現(xiàn)異步提交請求,只需使用另一個線程或者線程池技術(shù),即可實現(xiàn)。而對于遠程Client的異步提交,則還需要借助于外部工具了。此處借助于Netty的channel.write()實現(xiàn),節(jié)點響應時再回調(diào)回來,從而恢復上下文。整個過程,沒有一點阻塞同步,從而達到了高效的處理能力,當然還有其他的一些異常處理,自不必說。

具體樣例大致如下:因最終的處理器是以 TransportClusterSearchShardsAction 進行處理的,所以直接轉(zhuǎn)到 TransportClusterSearchShardsAction。

// org.elasticsearch.action.admin.cluster.shards.TransportClusterSearchShardsAction
public class TransportClusterSearchShardsAction extends
    TransportMasterNodeReadAction<ClusterSearchShardsRequest, ClusterSearchShardsResponse> {

    private final IndicesService indicesService;

    @Inject
    public TransportClusterSearchShardsAction(TransportService transportService, ClusterService clusterService,
                                              IndicesService indicesService, ThreadPool threadPool, ActionFilters actionFilters,
                                              IndexNameExpressionResolver indexNameExpressionResolver) {
        super(ClusterSearchShardsAction.NAME, transportService, clusterService, threadPool, actionFilters,
            ClusterSearchShardsRequest::new, indexNameExpressionResolver, ClusterSearchShardsResponse::new, ThreadPool.Names.SAME);
        this.indicesService = indicesService;
    }

    @Override
    protected ClusterBlockException checkBlock(ClusterSearchShardsRequest request, ClusterState state) {
        return state.blocks().indicesBlockedException(ClusterBlockLevel.METADATA_READ,
                indexNameExpressionResolver.concreteIndexNames(state, request));
    }

    @Override
    protected void masterOperation(final ClusterSearchShardsRequest request, final ClusterState state,
                                   final ActionListener<ClusterSearchShardsResponse> listener) {
        ClusterState clusterState = clusterService.state();
        String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(clusterState, request);
        Map<String, Set<String>> routingMap = indexNameExpressionResolver.resolveSearchRouting(state, request.routing(), request.indices());
        Map<String, AliasFilter> indicesAndFilters = new HashMap<>();
        Set<String> indicesAndAliases = indexNameExpressionResolver.resolveExpressions(clusterState, request.indices());
        for (String index : concreteIndices) {
            final AliasFilter aliasFilter = indicesService.buildAliasFilter(clusterState, index, indicesAndAliases);
            final String[] aliases = indexNameExpressionResolver.indexAliases(clusterState, index, aliasMetadata -> true, true,
                indicesAndAliases);
            indicesAndFilters.put(index, new AliasFilter(aliasFilter.getQueryBuilder(), aliases));
        }

        Set<String> nodeIds = new HashSet<>();
        GroupShardsIterator<ShardIterator> groupShardsIterator = clusterService.operationRouting()
            .searchShards(clusterState, concreteIndices, routingMap, request.preference());
        ShardRouting shard;
        ClusterSearchShardsGroup[] groupResponses = new ClusterSearchShardsGroup[groupShardsIterator.size()];
        int currentGroup = 0;
        for (ShardIterator shardIt : groupShardsIterator) {
            ShardId shardId = shardIt.shardId();
            ShardRouting[] shardRoutings = new ShardRouting[shardIt.size()];
            int currentShard = 0;
            shardIt.reset();
            while ((shard = shardIt.nextOrNull()) != null) {
                shardRoutings[currentShard++] = shard;
                nodeIds.add(shard.currentNodeId());
            }
            groupResponses[currentGroup++] = new ClusterSearchShardsGroup(shardId, shardRoutings);
        }
        DiscoveryNode[] nodes = new DiscoveryNode[nodeIds.size()];
        int currentNode = 0;
        for (String nodeId : nodeIds) {
            nodes[currentNode++] = clusterState.getNodes().get(nodeId);
        }
        listener.onResponse(new ClusterSearchShardsResponse(groupResponses, nodes, indicesAndFilters));
    }
}
// doExecute 在父類中完成
// org.elasticsearch.action.support.master.TransportMasterNodeAction#doExecute
@Override
protected void doExecute(Task task, final Request request, ActionListener<Response> listener) {
    ClusterState state = clusterService.state();
    logger.trace("starting processing request [{}] with cluster state version [{}]", request, state.version());
    if (task != null) {
        request.setParentTask(clusterService.localNode().getId(), task.getId());
    }
    new AsyncSingleAction(task, request, listener).doStart(state);
}

// org.elasticsearch.action.support.master.TransportMasterNodeAction.AsyncSingleAction#doStart
AsyncSingleAction(Task task, Request request, ActionListener<Response> listener) {
    this.task = task;
    this.request = request;
    this.listener = listener;
    this.startTime = threadPool.relativeTimeInMillis();
}

protected void doStart(ClusterState clusterState) {
    try {
        final DiscoveryNodes nodes = clusterState.nodes();
        if (nodes.isLocalNodeElectedMaster() || localExecute(request)) {
            // check for block, if blocked, retry, else, execute locally
            final ClusterBlockException blockException = checkBlock(request, clusterState);
            if (blockException != null) {
                if (!blockException.retryable()) {
                    listener.onFailure(blockException);
                } else {
                    logger.debug("can't execute due to a cluster block, retrying", blockException);
                    // 重試處理
                    retry(clusterState, blockException, newState -> {
                        try {
                            ClusterBlockException newException = checkBlock(request, newState);
                            return (newException == null || !newException.retryable());
                        } catch (Exception e) {
                            // accept state as block will be rechecked by doStart() and listener.onFailure() then called
                            logger.trace("exception occurred during cluster block checking, accepting state", e);
                            return true;
                        }
                    });
                }
            } else {
                ActionListener<Response> delegate = ActionListener.delegateResponse(listener, (delegatedListener, t) -> {
                    if (t instanceof FailedToCommitClusterStateException || t instanceof NotMasterException) {
                        logger.debug(() -> new ParameterizedMessage("master could not publish cluster state or " +
                            "stepped down before publishing action [{}], scheduling a retry", actionName), t);
                        retryOnMasterChange(clusterState, t);
                    } else {
                        delegatedListener.onFailure(t);
                    }
                });
                // 本地節(jié)點執(zhí)行結(jié)果,直接以異步線程處理即可
                threadPool.executor(executor)
                    .execute(ActionRunnable.wrap(delegate, l -> masterOperation(task, request, clusterState, l)));
            }
        } else {
            if (nodes.getMasterNode() == null) {
                logger.debug("no known master node, scheduling a retry");
                retryOnMasterChange(clusterState, null);
            } else {
                DiscoveryNode masterNode = nodes.getMasterNode();
                final String actionName = getMasterActionName(masterNode);
                // 發(fā)送到master節(jié)點,以netty作為通訊工具,完成后回調(diào) 當前l(fā)istner
                transportService.sendRequest(masterNode, actionName, request,
                    new ActionListenerResponseHandler<Response>(listener, responseReader) {
                        @Override
                        public void handleException(final TransportException exp) {
                            Throwable cause = exp.unwrapCause();
                            if (cause instanceof ConnectTransportException ||
                                (exp instanceof RemoteTransportException && cause instanceof NodeClosedException)) {
                                // we want to retry here a bit to see if a new master is elected
                                logger.debug("connection exception while trying to forward request with action name [{}] to " +
                                        "master node [{}], scheduling a retry. Error: [{}]",
                                    actionName, nodes.getMasterNode(), exp.getDetailedMessage());
                                retryOnMasterChange(clusterState, cause);
                            } else {
                                listener.onFailure(exp);
                            }
                        }
                });
            }
        }
    } catch (Exception e) {
        listener.onFailure(e);
    }
}

可見,es中確實有兩種異步的提交方式,一種是當前節(jié)點就是執(zhí)行節(jié)點,直接使用線程池提交;另一種是遠程節(jié)點則起網(wǎng)絡調(diào)用,最終如何實現(xiàn)異步且往下看。

// org.elasticsearch.transport.TransportService#sendRequest
public final <T extends TransportResponse> void sendRequest(final DiscoveryNode node, final String action,
                                                            final TransportRequest request,
                                                            final TransportRequestOptions options,
                                                            TransportResponseHandler<T> handler) {
    final Transport.Connection connection;
    try {
        // 假設不是本節(jié)點,則獲取遠程的一個 connection, channel
        connection = getConnection(node);
    } catch (final NodeNotConnectedException ex) {
        // the caller might not handle this so we invoke the handler
        handler.handleException(ex);
        return;
    }
    sendRequest(connection, action, request, options, handler);
}
// org.elasticsearch.transport.TransportService#getConnection
/**
    * Returns either a real transport connection or a local node connection if we are using the local node optimization.
    * @throws NodeNotConnectedException if the given node is not connected
    */
public Transport.Connection getConnection(DiscoveryNode node) {
    if (isLocalNode(node)) {
        return localNodeConnection;
    } else {
        return connectionManager.getConnection(node);
    }
}

// org.elasticsearch.transport.TransportService#sendRequest
/**
    * Sends a request on the specified connection. If there is a failure sending the request, the specified handler is invoked.
    *
    * @param connection the connection to send the request on
    * @param action     the name of the action
    * @param request    the request
    * @param options    the options for this request
    * @param handler    the response handler
    * @param <T>        the type of the transport response
    */
public final <T extends TransportResponse> void sendRequest(final Transport.Connection connection, final String action,
                                                            final TransportRequest request,
                                                            final TransportRequestOptions options,
                                                            final TransportResponseHandler<T> handler) {
    try {
        final TransportResponseHandler<T> delegate;
        if (request.getParentTask().isSet()) {
            // If the connection is a proxy connection, then we will create a cancellable proxy task on the proxy node and an actual
            // child task on the target node of the remote cluster.
            //  ----> a parent task on the local cluster
            //        |
            //         ----> a proxy task on the proxy node on the remote cluster
            //               |
            //                ----> an actual child task on the target node on the remote cluster
            // To cancel the child task on the remote cluster, we must send a cancel request to the proxy node instead of the target
            // node as the parent task of the child task is the proxy task not the parent task on the local cluster. Hence, here we
            // unwrap the connection and keep track of the connection to the proxy node instead of the proxy connection.
            final Transport.Connection unwrappedConn = unwrapConnection(connection);
            final Releasable unregisterChildNode = taskManager.registerChildConnection(request.getParentTask().getId(), unwrappedConn);
            delegate = new TransportResponseHandler<T>() {
                @Override
                public void handleResponse(T response) {
                    unregisterChildNode.close();
                    handler.handleResponse(response);
                }

                @Override
                public void handleException(TransportException exp) {
                    unregisterChildNode.close();
                    handler.handleException(exp);
                }

                @Override
                public String executor() {
                    return handler.executor();
                }

                @Override
                public T read(StreamInput in) throws IOException {
                    return handler.read(in);
                }

                @Override
                public String toString() {
                    return getClass().getName() + "/[" + action + "]:" + handler.toString();
                }
            };
        } else {
            delegate = handler;
        }
        asyncSender.sendRequest(connection, action, request, options, delegate);
    } catch (final Exception ex) {
        // the caller might not handle this so we invoke the handler
        final TransportException te;
        if (ex instanceof TransportException) {
            te = (TransportException) ex;
        } else {
            te = new TransportException("failure to send", ex);
        }
        handler.handleException(te);
    }
}

// org.elasticsearch.transport.TransportService#sendRequestInternal
private <T extends TransportResponse> void sendRequestInternal(final Transport.Connection connection, final String action,
                                                                final TransportRequest request,
                                                                final TransportRequestOptions options,
                                                                TransportResponseHandler<T> handler) {
    if (connection == null) {
        throw new IllegalStateException("can't send request to a null connection");
    }
    DiscoveryNode node = connection.getNode();

    Supplier<ThreadContext.StoredContext> storedContextSupplier = threadPool.getThreadContext().newRestorableContext(true);
    ContextRestoreResponseHandler<T> responseHandler = new ContextRestoreResponseHandler<>(storedContextSupplier, handler);
    // TODO we can probably fold this entire request ID dance into connection.sendReqeust but it will be a bigger refactoring
    final long requestId = responseHandlers.add(new Transport.ResponseContext<>(responseHandler, connection, action));
    final TimeoutHandler timeoutHandler;
    if (options.timeout() != null) {
        timeoutHandler = new TimeoutHandler(requestId, connection.getNode(), action);
        responseHandler.setTimeoutHandler(timeoutHandler);
    } else {
        timeoutHandler = null;
    }
    try {
        if (lifecycle.stoppedOrClosed()) {
            /*
                * If we are not started the exception handling will remove the request holder again and calls the handler to notify the
                * caller. It will only notify if toStop hasn't done the work yet.
                */
            throw new NodeClosedException(localNode);
        }
        if (timeoutHandler != null) {
            assert options.timeout() != null;
            timeoutHandler.scheduleTimeout(options.timeout());
        }
        connection.sendRequest(requestId, action, request, options); // local node optimization happens upstream
    } catch (final Exception e) {
        // usually happen either because we failed to connect to the node
        // or because we failed serializing the message
        final Transport.ResponseContext<? extends TransportResponse> contextToNotify = responseHandlers.remove(requestId);
        // If holderToNotify == null then handler has already been taken care of.
        if (contextToNotify != null) {
            if (timeoutHandler != null) {
                timeoutHandler.cancel();
            }
            // callback that an exception happened, but on a different thread since we don't
            // want handlers to worry about stack overflows. In the special case of running into a closing node we run on the current
            // thread on a best effort basis though.
            final SendRequestTransportException sendRequestException = new SendRequestTransportException(node, action, e);
            final String executor = lifecycle.stoppedOrClosed() ? ThreadPool.Names.SAME : ThreadPool.Names.GENERIC;
            threadPool.executor(executor).execute(new AbstractRunnable() {
                @Override
                public void onRejection(Exception e) {
                    // if we get rejected during node shutdown we don't wanna bubble it up
                    logger.debug(
                        () -> new ParameterizedMessage(
                            "failed to notify response handler on rejection, action: {}",
                            contextToNotify.action()),
                        e);
                }
                @Override
                public void onFailure(Exception e) {
                    logger.warn(
                        () -> new ParameterizedMessage(
                            "failed to notify response handler on exception, action: {}",
                            contextToNotify.action()),
                        e);
                }
                @Override
                protected void doRun() throws Exception {
                    contextToNotify.handler().handleException(sendRequestException);
                }
            });
        } else {
            logger.debug("Exception while sending request, handler likely already notified due to timeout", e);
        }
    }
}
// org.elasticsearch.transport.RemoteConnectionManager.ProxyConnection#sendRequest
@Override
public void sendRequest(long requestId, String action, TransportRequest request, TransportRequestOptions options)
    throws IOException, TransportException {
    connection.sendRequest(requestId, TransportActionProxy.getProxyAction(action),
        TransportActionProxy.wrapRequest(targetNode, request), options);
}
// org.elasticsearch.transport.TcpTransport.NodeChannels#sendRequest
@Override
public void sendRequest(long requestId, String action, TransportRequest request, TransportRequestOptions options)
    throws IOException, TransportException {
    if (isClosing.get()) {
        throw new NodeNotConnectedException(node, "connection already closed");
    }
    TcpChannel channel = channel(options.type());
    outboundHandler.sendRequest(node, channel, requestId, action, request, options, getVersion(), compress, false);
}
// org.elasticsearch.transport.OutboundHandler#sendRequest
/**
    * Sends the request to the given channel. This method should be used to send {@link TransportRequest}
    * objects back to the caller.
    */
void sendRequest(final DiscoveryNode node, final TcpChannel channel, final long requestId, final String action,
                    final TransportRequest request, final TransportRequestOptions options, final Version channelVersion,
                    final boolean compressRequest, final boolean isHandshake) throws IOException, TransportException {
    Version version = Version.min(this.version, channelVersion);
    OutboundMessage.Request message = new OutboundMessage.Request(threadPool.getThreadContext(), features, request, version, action,
        requestId, isHandshake, compressRequest);
    ActionListener<Void> listener = ActionListener.wrap(() ->
        messageListener.onRequestSent(node, requestId, action, request, options));
    sendMessage(channel, message, listener);
}
// org.elasticsearch.transport.OutboundHandler#sendMessage
private void sendMessage(TcpChannel channel, OutboundMessage networkMessage, ActionListener<Void> listener) throws IOException {
    MessageSerializer serializer = new MessageSerializer(networkMessage, bigArrays);
    SendContext sendContext = new SendContext(channel, serializer, listener, serializer);
    internalSend(channel, sendContext);
}
private void internalSend(TcpChannel channel, SendContext sendContext) throws IOException {
    channel.getChannelStats().markAccessed(threadPool.relativeTimeInMillis());
    BytesReference reference = sendContext.get();
    // stash thread context so that channel event loop is not polluted by thread context
    try (ThreadContext.StoredContext existing = threadPool.getThreadContext().stashContext()) {
        channel.sendMessage(reference, sendContext);
    } catch (RuntimeException ex) {
        sendContext.onFailure(ex);
        CloseableChannel.closeChannel(channel);
        throw ex;
    }
}
// org.elasticsearch.transport.netty4.Netty4TcpChannel#sendMessage
@Override
public void sendMessage(BytesReference reference, ActionListener<Void> listener) {
    // netty 發(fā)送數(shù)據(jù),異步回調(diào),完成異步請求
    channel.writeAndFlush(Netty4Utils.toByteBuf(reference), addPromise(listener, channel));

    if (channel.eventLoop().isShutdown()) {
        listener.onFailure(new TransportException("Cannot send message, event loop is shutting down."));
    }
}

簡單說,就是依托于netty的pipeline機制以及eventLoop實現(xiàn)遠程異步請求,至于具體實現(xiàn)如何,請參考之前文章或各網(wǎng)文。

以上就是詳解Java ES多節(jié)點任務的高效分發(fā)與收集實現(xiàn)的詳細內(nèi)容,更多關(guān)于Java ES多節(jié)點任務的高效分發(fā) 收集實現(xiàn)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 關(guān)于JVM翻越內(nèi)存管理的墻

    關(guān)于JVM翻越內(nèi)存管理的墻

    這篇文章主要介紹了JVM翻越內(nèi)存管理的墻,由虛擬機管理內(nèi)存看起來一切都很美好,但也正是因為把控制內(nèi)存的權(quán)力交給了Java虛擬機,一旦出現(xiàn)內(nèi)存泄漏和溢出方面的問題,就不得不從Java虛擬機角度上去排查問題,本文給大家介紹的非常詳細,需要的朋友可以參考下
    2022-05-05
  • 深入了解Spring中的@Autowired和@Resource注解

    深入了解Spring中的@Autowired和@Resource注解

    Spring中的@Autowired和@Resource注解都可以實現(xiàn)依賴注入,但使用方式、注入策略和適用場景略有不同。本文將深入探討這兩種注解的原理、使用方法及優(yōu)缺點,幫助讀者更好地理解和運用Spring依賴注入機制
    2023-04-04
  • dubbo服務注冊到nacos的過程剖析

    dubbo服務注冊到nacos的過程剖析

    這篇文章主要為大家介紹了dubbo服務注冊到nacos的過程剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職極限
    2022-02-02
  • spring cloud consul注冊的服務報錯critical的解決

    spring cloud consul注冊的服務報錯critical的解決

    這篇文章主要介紹了spring cloud consul注冊的服務報錯critical的解決,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • Java中ArrayList和Vector的區(qū)別

    Java中ArrayList和Vector的區(qū)別

    本文主要介紹了Java中ArrayList和Vector的區(qū)別,包括線程安全性、性能、同步機制、擴容機制、遍歷方式等,具有一定的參考價值,感興趣的可以了解一下
    2024-11-11
  • 全面理解java中的異常處理機制

    全面理解java中的異常處理機制

    下面小編就為大家?guī)硪黄胬斫鈐ava中的異常處理機制。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • Java實現(xiàn)辦公文檔在線預覽功能

    Java實現(xiàn)辦公文檔在線預覽功能

    java實現(xiàn)辦公文件在線預覽功能是一個大家在工作中也許會遇到的需求,這篇文章就教大家如何實現(xiàn)這一功能,感興趣的小伙伴可以了解一下
    2021-12-12
  • Spring MVC實現(xiàn)文件上傳及優(yōu)化案例解析

    Spring MVC實現(xiàn)文件上傳及優(yōu)化案例解析

    本文介紹如何在SpringMVC框架中實現(xiàn)文件上傳和異步登錄功能,通過添加必要的依賴和配置,創(chuàng)建文件上傳頁面和控制器,實現(xiàn)文件上傳到指定文件夾,同時,展示了如何使用AJAX實現(xiàn)局部刷新的異步登錄,優(yōu)化用戶體驗,詳細步驟包括配置springmvc.xml、編寫前端頁面和控制器等
    2024-10-10
  • java 將byte中的有效長度轉(zhuǎn)換為String的實例代碼

    java 將byte中的有效長度轉(zhuǎn)換為String的實例代碼

    下面小編就為大家?guī)硪黄猨ava 將byte中的有效長度轉(zhuǎn)換為String的實例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • Java單例模式的6種實現(xiàn)方式詳解

    Java單例模式的6種實現(xiàn)方式詳解

    這篇文章主要介紹了Java單例模式的6種實現(xiàn)方式的相關(guān)資料,需要的朋友可以參考下,希望能夠給你帶來幫助
    2021-09-09

最新評論

会昌县| 丹巴县| 滨海县| 柏乡县| 剑川县| 溧水县| 和林格尔县| 保德县| 上高县| 米易县| 昌黎县| 左云县| 卢湾区| 五原县| 福海县| 怀来县| 金门县| 遂昌县| 余干县| 峨山| 弥勒县| 安化县| 肥城市| 肥西县| 两当县| 丹江口市| 磐石市| 平南县| 林州市| 长宁区| 青州市| 龙南县| 澄城县| 运城市| 砀山县| 宁国市| 孝义市| 石门县| 江川县| 吐鲁番市| 文化|