Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The interval at which the heartbeat is sent in BeatReactor is not controlled by the server return value. #768

Closed
darkness463 opened this issue Feb 15, 2019 · 4 comments
Assignees
Labels
kind/bug Category issues or prs related to bug.
Milestone

Comments

@darkness463
Copy link
Contributor

最近在看Nacos源码,截止目前的master分支,在com.alibaba.nacos.client.naming.beat.BeatReactor类的实现中,默认启动时会以5秒一次的频率发送心跳,BeatProcessor会对dom2Beat中的每个值创建BeatTask发送心跳,在BeatTask中会接收服务端返回的clientBeatInterval字段并设置给BeatReactor类中的clientBeatInterval。关键代码如下:

    class BeatProcessor implements Runnable {

        @Override
        public void run() {
            try {
                for (Map.Entry<String, BeatInfo> entry : dom2Beat.entrySet()) {
                    BeatInfo beatInfo = entry.getValue();
                    if (beatInfo.isScheduled()) {
                        continue;
                    }
                    beatInfo.setScheduled(true);
                    executorService.schedule(new BeatTask(beatInfo), 0, TimeUnit.MILLISECONDS);
                }
            } catch (Exception e) {
                LogUtils.LOG.error("CLIENT-BEAT", "Exception while scheduling beat.", e);
            }
        }
    }

    class BeatTask implements Runnable {

        BeatInfo beatInfo;

        public BeatTask(BeatInfo beatInfo) {
            this.beatInfo = beatInfo;
        }

        @Override
        public void run() {
            long result = serverProxy.sendBeat(beatInfo);
            beatInfo.setScheduled(false);
            if (result > 0) {
                clientBeatInterval = result;
            }
        }
    }

我理解设计目的是根据服务端返回的心跳间隔来调整客户端发送心跳的频率,但在BeatReactor的构造函数中,BeatProcessor是用以下代码启动的:

executorService.scheduleAtFixedRate(new BeatProcessor(), 0, clientBeatInterval, TimeUnit.MILLISECONDS);

所以导致的结果是,不管服务端返回的clientBeatInterval值是多少,客户端都只会以5秒的间隔发送心跳,返回值对心跳任务没有任何影响。是bug还是我理解有问题?

@nacos-bot
Copy link
Collaborator

Hi @darkness463, we detect non-English characters in the issue. This comment is an auto translation from @nacos-bot to help other users to understand this issue.
We encourage you to describe your issue in English which is more friendly to other users.

The interval at which the heartbeat is sent in BeatReactor is not controlled by the server return value.

Recently, I am looking at the Nacos source. As of the current master branch, in the implementation of the com.alibaba.nacos.client.naming.beat.BeatReactor class, the heartbeat will be sent at a frequency of 5 seconds by default. BeatProcessor will Create a BeatTask for each value in dom2Beat to send a heartbeat. In BeatTask, it will receive the clientBeatInterval field returned by the server and set it to clientBeatInterval in the BeatReactor class. The key code is as follows:

    class BeatProcessor implements Runnable {

        @Override
        public void run() {
            try {
                for (Map.Entry<String, BeatInfo> entry : dom2Beat.entrySet()) {
                    BeatInfo beatInfo = entry.getValue();
                    if (beatInfo.isScheduled()) {
                        continue;
                    }
                    beatInfo.setScheduled(true);
                    executorService.schedule(new BeatTask(beatInfo), 0, TimeUnit.MILLISECONDS);
                }
            } catch (Exception e) {
                LogUtils.LOG.error("CLIENT-BEAT", "Exception while scheduling beat.", e);
            }
        }
    }

    class BeatTask implements Runnable {

        BeatInfo beatInfo;

        public BeatTask(BeatInfo beatInfo) {
            this.beatInfo = beatInfo;
        }

        @Override
        public void run() {
            long result = serverProxy.sendBeat(beatInfo);
            beatInfo.setScheduled(false);
            if (result > 0) {
                clientBeatInterval = result;
            }
        }
    }

I understand that the design goal is to adjust the frequency at which the client sends heartbeats based on the heartbeat interval returned by the server, but in the constructor of BeatReactor, BeatProcessor is started with the following code:

executorService.scheduleAtFixedRate(new BeatProcessor(), 0, clientBeatInterval, TimeUnit.MILLISECONDS);

So the result is that regardless of the value of the clientBeatInterval returned by the server, the client will only send heartbeats at 5 second intervals, and the return value has no effect on the heartbeat task. Is it a bug or do I understand that there is a problem?

@nacos-bot nacos-bot changed the title BeatReactor中发送心跳的间隔不受服务端返回值控制 The interval at which the heartbeat is sent in BeatReactor is not controlled by the server return value. Feb 15, 2019
@nkorange nkorange added the kind/bug Category issues or prs related to bug. label Feb 15, 2019
@nkorange nkorange added this to the 0.9.0 milestone Feb 15, 2019
@nkorange
Copy link
Collaborator

@darkness463
Can you create a PR to fix this?

@darkness463
Copy link
Contributor Author

@i will solve it@

nkorange pushed a commit that referenced this issue Feb 17, 2019
[ISSUE #768] Fix: client beat interval is not controlled by server.
@nkorange
Copy link
Collaborator

@darkness463 Thank you very much, your commit has been accepted.

xuechaos added a commit that referenced this issue Feb 28, 2019
* [#840] Nacos server adds startup mode to distinguish between config and naming.
* [#762] Register instance returns failed when the health check mode is 'server' in standalone mode.
* [#473] Nacos Cluster Mode kubernate Startup nacos.log error Log.
* [#240] Log strong dependence problem.
* [#824] getServicesOfServer throws exception if service list is empty.
* [#802] Nacos server multi-boot mode support.
* [#800] Nacos's client-to-server addressing mode document introduction.
* [#768] The interval at which the heartbeat is sent in BeatReactor is not controlled by the server return value.
* [#759] why instance can't auto-delete.
* [#756] Format of instance and service should be validated.
* [#720] Memory leak in PushService.
* [#653] IoUtils under nacos-common-0.2.1-RC1.jar lacks "" judgment on encoding.
* [#588] Client compatible to jdk1.6. On branch develop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Category issues or prs related to bug.
Projects
None yet
Development

No branches or pull requests

3 participants