Skip to content

Commit b4aa515

Browse files
carryxyhbeiwei30
authored andcommitted
Optimize heartbeat (#3299)
* Optimize heartbeat. We should cancel the timeout when the client or server is close. * change the hashedWheelTimer's ticks * Optimize tasks keeper. * fix timeout cancel to task cancel. keep task directly.
1 parent d27fb1f commit b4aa515

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,9 @@ public class Constants {
300300
public static final long LEAST_HEARTBEAT_DURATION = 1000;
301301

302302
/**
303-
* ticks per wheel. Currently only contains two tasks, so 16 locations are enough
303+
* ticks per wheel.
304304
*/
305-
public static final int TICKS_PER_WHEEL = 16;
305+
public static final int TICKS_PER_WHEEL = 128;
306306

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

dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/AbstractTimerTask.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public abstract class AbstractTimerTask implements TimerTask {
3434

3535
private final Long tick;
3636

37+
protected volatile boolean cancel = false;
38+
3739
AbstractTimerTask(ChannelProvider channelProvider, Long tick) {
3840
if (channelProvider == null || tick == null) {
3941
throw new IllegalArgumentException();
@@ -54,11 +56,19 @@ static Long now() {
5456
return System.currentTimeMillis();
5557
}
5658

59+
public void cancel() {
60+
this.cancel = true;
61+
}
62+
5763
private void reput(Timeout timeout, Long tick) {
5864
if (timeout == null || tick == null) {
5965
throw new IllegalArgumentException();
6066
}
6167

68+
if (cancel) {
69+
return;
70+
}
71+
6272
Timer timer = timeout.timer();
6373
if (timer.isStop() || timeout.isCancelled()) {
6474
return;

dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeClient.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,13 @@ public class HeaderExchangeClient implements ExchangeClient {
4343
private int heartbeat;
4444
private int idleTimeout;
4545

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

49+
private HeartbeatTimerTask heartBeatTimerTask;
50+
51+
private ReconnectTimerTask reconnectTimerTask;
52+
4953
public HeaderExchangeClient(Client client, boolean needHeartbeat) {
5054
Assert.notNull(client, "Client can't be null");
5155
this.client = client;
@@ -182,12 +186,17 @@ private void startIdleCheckTask() {
182186
HeartbeatTimerTask heartBeatTimerTask = new HeartbeatTimerTask(cp, heartbeatTick, heartbeat);
183187
ReconnectTimerTask reconnectTimerTask = new ReconnectTimerTask(cp, heartbeatTimeoutTick, idleTimeout);
184188

189+
this.heartBeatTimerTask = heartBeatTimerTask;
190+
this.reconnectTimerTask = reconnectTimerTask;
191+
185192
// init task and start timer.
186-
idleCheckTimer.newTimeout(heartBeatTimerTask, heartbeatTick, TimeUnit.MILLISECONDS);
187-
idleCheckTimer.newTimeout(reconnectTimerTask, heartbeatTimeoutTick, TimeUnit.MILLISECONDS);
193+
IDLE_CHECK_TIMER.newTimeout(heartBeatTimerTask, heartbeatTick, TimeUnit.MILLISECONDS);
194+
IDLE_CHECK_TIMER.newTimeout(reconnectTimerTask, heartbeatTimeoutTick, TimeUnit.MILLISECONDS);
188195
}
189196

190197
private void doClose() {
198+
heartBeatTimerTask.cancel();
199+
reconnectTimerTask.cancel();
191200
}
192201

193202
/**

dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeServer.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ public class HeaderExchangeServer implements ExchangeServer {
5353
private int idleTimeout;
5454
private AtomicBoolean closed = new AtomicBoolean(false);
5555

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

59+
private CloseTimerTask closeTimerTask;
60+
5961
public HeaderExchangeServer(Server server) {
6062
Assert.notNull(server, "server == null");
6163
this.server = server;
@@ -148,6 +150,11 @@ private void doClose() {
148150
if (!closed.compareAndSet(false, true)) {
149151
return;
150152
}
153+
cancelCloseTask();
154+
}
155+
156+
private void cancelCloseTask() {
157+
closeTimerTask.cancel();
151158
}
152159

153160
@Override
@@ -214,6 +221,8 @@ public void reset(URL url) {
214221
heartbeat = h;
215222
idleTimeout = t;
216223

224+
// we need cancel the exist closeTimeout first.
225+
cancelCloseTask();
217226
startIdleCheckTask();
218227
}
219228
}
@@ -262,9 +271,10 @@ private void startIdleCheckTask() {
262271

263272
long idleTimeoutTick = calculateLeastDuration(idleTimeout);
264273
CloseTimerTask closeTimerTask = new CloseTimerTask(cp, idleTimeoutTick, idleTimeout);
274+
this.closeTimerTask = closeTimerTask;
265275

266276
// init task and start timer.
267-
idleCheckTimer.newTimeout(closeTimerTask, idleTimeoutTick, TimeUnit.MILLISECONDS);
277+
IDLE_CHECK_TIMER.newTimeout(closeTimerTask, idleTimeoutTick, TimeUnit.MILLISECONDS);
268278
}
269279

270280
}

0 commit comments

Comments
 (0)