elasticsearch源码:ping的发送与处理过程

一个节点通过向其它节点发送ping请求来发现其它节点的存在。

这里只讨论在新节点启动时发送的ping请求。

代码入口

UnicastZenPing类的ping方法入手:

    /**
     * a variant of {@link #ping(Consumer, TimeValue)}, but allows separating the scheduling duration
     * from the duration used for request level time outs. This is useful for testing
     */
    protected void ping(final Consumer<PingCollection> resultsConsumer,
                        final TimeValue scheduleDuration,
                        final TimeValue requestDuration) {
   
        ...
        final PingingRound pingingRound = new PingingRound(pingingRoundIdGenerator.incrementAndGet(), seedAddresses, resultsConsumer,
            nodes.getLocalNode(), connectionProfile);
        activePingingRounds.put(pingingRound.id(), pingingRound);
        final AbstractRunnable pingSender = new AbstractRunnable() {
   
            @Override
            public void onFailure(Exception e) {
   
                if (e instanceof AlreadyClosedException == false) {
   
                    logger.warn("unexpected error while pinging", e);
                }
            }

            @Override
            protected void doRun() throws Exception {
   
                sendPings(requestDuration, pingingRound);
            }
        };
        threadPool.generic().execute(pingSender);
        threadPool.schedule(TimeValue.timeValueMillis(scheduleDuration.millis() / 3), ThreadPool.Names.GENERIC, pingSender);
        threadPool.schedule(TimeValue.timeValueMillis(scheduleDuration.millis() / 3 * 2), ThreadPool.Names.GENERIC, pingSender);
        threadPool.schedule(scheduleDuration, ThreadPool.Names.GENERIC, new AbstractRunnable() {
   
            @Override
            protected void doRun() throws Exception {
   
                finishPingingRound(pingingRound);
            }

            @Override
            public void onFailure(Exception e) {
   
                logger.warn("unexpected error while finishing pinging round", e);
            }
        });
    }

《elasticsearch源码:unicast列表解析》一文中已经讨论了如何从elasticsearch.yml文件中读取unicast节点列表。这里会对列表中的这些节点发送ping请求,一共会进行三轮发送,发送间隔为discovery.zen.ping_timeout配置(默认3秒)的1/3,最后关闭ping请求,这时还没有收到的response就不再等待了。

发送ping的过程

sendPings方法里面看下发送ping请求的逻辑:

    protected void sendPings(final TimeValue timeout, final PingingRound pingingRound) {
   
        final ClusterState lastState = contextProvider.clusterState();
        final UnicastPingRequest pingRequest = new UnicastPingRequest(pingingRound.id(), timeout, createPingResponse(lastState));

        List<TransportAddress> temporalAddresses = temporalResponses.stream().map(pingResponse -> {
   
            assert clusterName.equals(pingResponse.clusterName()) :
                "got a ping request from a different cluster. expected " + clusterName + " got " + pingRespo
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值