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

Optimize heartbeat #3299

Merged
merged 4 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ public class Constants {
public static final long LEAST_HEARTBEAT_DURATION = 1000;

/**
* ticks per wheel. Currently only contains two tasks, so 16 locations are enough
* ticks per wheel.
*/
public static final int TICKS_PER_WHEEL = 16;
public static final int TICKS_PER_WHEEL = 128;

public static final String HEARTBEAT_TIMEOUT_KEY = "heartbeat.timeout";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.timer.HashedWheelTimer;
import org.apache.dubbo.common.timer.Timeout;
import org.apache.dubbo.common.utils.Assert;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.NamedThreadFactory;
import org.apache.dubbo.remoting.ChannelHandler;
import org.apache.dubbo.remoting.Client;
Expand All @@ -30,7 +32,9 @@
import org.apache.dubbo.remoting.exchange.ResponseFuture;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -43,9 +47,11 @@ public class HeaderExchangeClient implements ExchangeClient {
private int heartbeat;
private int idleTimeout;

private static HashedWheelTimer idleCheckTimer = new HashedWheelTimer(new NamedThreadFactory("dubbo-client-idleCheck", true), 1,
private static final HashedWheelTimer IDLE_CHECK_TIMER = new HashedWheelTimer(new NamedThreadFactory("dubbo-client-idleCheck", true), 1,
TimeUnit.SECONDS, Constants.TICKS_PER_WHEEL);

private List<Timeout> tasks = new ArrayList<>();
beiwei30 marked this conversation as resolved.
Show resolved Hide resolved

public HeaderExchangeClient(Client client, boolean needHeartbeat) {
Assert.notNull(client, "Client can't be null");
this.client = client;
Expand Down Expand Up @@ -183,11 +189,20 @@ private void startIdleCheckTask() {
ReconnectTimerTask reconnectTimerTask = new ReconnectTimerTask(cp, heartbeatTimeoutTick, idleTimeout);

// init task and start timer.
idleCheckTimer.newTimeout(heartBeatTimerTask, heartbeatTick, TimeUnit.MILLISECONDS);
idleCheckTimer.newTimeout(reconnectTimerTask, heartbeatTimeoutTick, TimeUnit.MILLISECONDS);
Timeout heartBeatTimeout = IDLE_CHECK_TIMER.newTimeout(heartBeatTimerTask, heartbeatTick, TimeUnit.MILLISECONDS);
Timeout reconnectTimeout = IDLE_CHECK_TIMER.newTimeout(reconnectTimerTask, heartbeatTimeoutTick, TimeUnit.MILLISECONDS);

tasks.add(heartBeatTimeout);
tasks.add(reconnectTimeout);
}

private void doClose() {
if (CollectionUtils.isNotEmpty(tasks)) {
for (Timeout timeout : tasks) {
timeout.cancel();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timeout.cancel will never success when the task has expired once,because in this case it's status will be ST_EXPIRED , and in this case, based on org.apache.dubbo.common.timer.HashedWheelTimer.HashedWheelTimeout#cancel method code as following

public boolean cancel() {
            // only update the state it will be removed from HashedWheelBucket on next tick.
            if (!compareAndSetState(ST_INIT, ST_CANCELLED)) {
                return false;
            }
            // If a task should be canceled we put this to another queue which will be processed on each tick.
            // So this means that we will have a GC latency of max. 1 tick duration which is good enough. This way
            // we can make again use of our MpscLinkedQueue and so minimize the locking / overhead as much as possible.
            timer.cancelledTimeouts.add(this);
            return true;
        }

compareAndSetState will always reture false. Am I right?

}
tasks.clear();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.timer.HashedWheelTimer;
import org.apache.dubbo.common.timer.Timeout;
import org.apache.dubbo.common.utils.Assert;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.NamedThreadFactory;
Expand All @@ -36,6 +37,7 @@
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

Expand All @@ -53,9 +55,11 @@ public class HeaderExchangeServer implements ExchangeServer {
private int idleTimeout;
private AtomicBoolean closed = new AtomicBoolean(false);

private static HashedWheelTimer idleCheckTimer = new HashedWheelTimer(new NamedThreadFactory("dubbo-server-idleCheck", true), 1,
private static final HashedWheelTimer IDLE_CHECK_TIMER = new HashedWheelTimer(new NamedThreadFactory("dubbo-server-idleCheck", true), 1,
TimeUnit.SECONDS, Constants.TICKS_PER_WHEEL);

private List<Timeout> tasks = new ArrayList<>();
beiwei30 marked this conversation as resolved.
Show resolved Hide resolved

public HeaderExchangeServer(Server server) {
Assert.notNull(server, "server == null");
this.server = server;
Expand Down Expand Up @@ -148,6 +152,16 @@ private void doClose() {
if (!closed.compareAndSet(false, true)) {
return;
}
cancelCloseTimeout();
}

private void cancelCloseTimeout() {
if (CollectionUtils.isNotEmpty(tasks)) {
for (Timeout timeout : tasks) {
timeout.cancel();
}
tasks.clear();
}
}

@Override
Expand Down Expand Up @@ -214,6 +228,8 @@ public void reset(URL url) {
heartbeat = h;
idleTimeout = t;

// we need cancel the exist closeTimeout first.
cancelCloseTimeout();
startIdleCheckTask();
}
}
Expand Down Expand Up @@ -264,7 +280,8 @@ private void startIdleCheckTask() {
CloseTimerTask closeTimerTask = new CloseTimerTask(cp, idleTimeoutTick, idleTimeout);

// init task and start timer.
idleCheckTimer.newTimeout(closeTimerTask, idleTimeoutTick, TimeUnit.MILLISECONDS);
Timeout closeTimeout = IDLE_CHECK_TIMER.newTimeout(closeTimerTask, idleTimeoutTick, TimeUnit.MILLISECONDS);
tasks.add(closeTimeout);
}

}