Skip to content

Commit ad7497e

Browse files
CrazyHZMkhanimteyaz
authored andcommitted
Qos heart (apache#3170)
* qos heart question fix apache#3165 * modify * judge if it's a IdleStateEvent * add UT * modify
1 parent e07735b commit ad7497e

File tree

7 files changed

+31
-18
lines changed

7 files changed

+31
-18
lines changed

dubbo-common/src/main/java/org/apache/dubbo/common/utils/ExecutorUtil.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.concurrent.LinkedBlockingQueue;
2727
import java.util.concurrent.ThreadPoolExecutor;
2828
import java.util.concurrent.TimeUnit;
29+
import java.util.concurrent.ScheduledFuture;
2930

3031
public class ExecutorUtil {
3132
private static final Logger logger = LoggerFactory.getLogger(ExecutorUtil.class);
@@ -45,9 +46,10 @@ public static boolean isTerminated(Executor executor) {
4546

4647
/**
4748
* Use the shutdown pattern from:
48-
* https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html
49+
* https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html
50+
*
4951
* @param executor the Executor to shutdown
50-
* @param timeout the timeout in milliseconds before termination
52+
* @param timeout the timeout in milliseconds before termination
5153
*/
5254
public static void gracefulShutdown(Executor executor, int timeout) {
5355
if (!(executor instanceof ExecutorService) || isTerminated(executor)) {
@@ -131,4 +133,11 @@ public static URL setThreadName(URL url, String defaultName) {
131133
url = url.addParameter(Constants.THREAD_NAME_KEY, name);
132134
return url;
133135
}
136+
137+
public static void cancelScheduledFuture(ScheduledFuture<?> scheduledFuture) {
138+
ScheduledFuture<?> future = scheduledFuture;
139+
if (future != null && !future.isCancelled()) {
140+
future.cancel(true);
141+
}
142+
}
134143
}

dubbo-monitor/dubbo-monitor-default/src/main/java/org/apache/dubbo/monitor/dubbo/DubboMonitor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.dubbo.common.URL;
2121
import org.apache.dubbo.common.logger.Logger;
2222
import org.apache.dubbo.common.logger.LoggerFactory;
23+
import org.apache.dubbo.common.utils.ExecutorUtil;
2324
import org.apache.dubbo.common.utils.NamedThreadFactory;
2425
import org.apache.dubbo.monitor.Monitor;
2526
import org.apache.dubbo.monitor.MonitorService;
@@ -209,7 +210,7 @@ public boolean isAvailable() {
209210
@Override
210211
public void destroy() {
211212
try {
212-
sendFuture.cancel(true);
213+
ExecutorUtil.cancelScheduledFuture(sendFuture);
213214
} catch (Throwable t) {
214215
logger.error("Unexpected error occur at cancel sender timer, cause: " + t.getMessage(), t);
215216
}

dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/handler/QosProcessHandler.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
import io.netty.handler.codec.http.HttpServerCodec;
2727
import io.netty.handler.codec.string.StringDecoder;
2828
import io.netty.handler.codec.string.StringEncoder;
29+
import io.netty.handler.timeout.IdleStateEvent;
2930
import io.netty.handler.timeout.IdleStateHandler;
3031
import io.netty.util.CharsetUtil;
3132
import io.netty.util.concurrent.ScheduledFuture;
33+
import org.apache.dubbo.common.utils.ExecutorUtil;
3234

3335
import java.util.List;
3436
import java.util.concurrent.TimeUnit;
@@ -93,6 +95,14 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
9395
}
9496
}
9597

98+
@Override
99+
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
100+
if (evt instanceof IdleStateEvent) {
101+
ExecutorUtil.cancelScheduledFuture(welcomeFuture);
102+
ctx.close();
103+
}
104+
}
105+
96106
// G for GET, and P for POST
97107
private static boolean isHttp(int magic) {
98108
return magic == 'G' || magic == 'P';

dubbo-registry/dubbo-registry-default/src/main/java/org/apache/dubbo/registry/dubbo/DubboRegistry.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838

3939
/**
4040
* DubboRegistry
41-
*
4241
*/
4342
public class DubboRegistry extends FailbackRegistry {
4443

@@ -124,9 +123,7 @@ public void destroy() {
124123
super.destroy();
125124
try {
126125
// Cancel the reconnection timer
127-
if (!reconnectFuture.isCancelled()) {
128-
reconnectFuture.cancel(true);
129-
}
126+
ExecutorUtil.cancelScheduledFuture(reconnectFuture);
130127
} catch (Throwable t) {
131128
logger.warn("Failed to cancel reconnect timer", t);
132129
}

dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,7 @@ public boolean isAvailable() {
298298
public void destroy() {
299299
super.destroy();
300300
try {
301-
if (cleanFuture != null) {
302-
cleanFuture.cancel(true);
303-
}
301+
ExecutorUtil.cancelScheduledFuture(cleanFuture);
304302
} catch (Throwable t) {
305303
logger.warn(t.getMessage(), t);
306304
}
@@ -342,8 +340,8 @@ protected void unregistered(URL url) {
342340
if (urls != null) {
343341
urls.remove(url);
344342
}
345-
if (urls == null || urls.isEmpty()){
346-
if (urls == null){
343+
if (urls == null || urls.isEmpty()) {
344+
if (urls == null) {
347345
urls = new ConcurrentHashSet<URL>();
348346
}
349347
URL empty = url.setProtocol(Constants.EMPTY_PROTOCOL);

dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/exchange/support/FileExchangeGroup.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.dubbo.remoting.p2p.exchange.support;
1818

1919
import org.apache.dubbo.common.URL;
20+
import org.apache.dubbo.common.utils.ExecutorUtil;
2021
import org.apache.dubbo.common.utils.IOUtils;
2122
import org.apache.dubbo.common.utils.NamedThreadFactory;
2223
import org.apache.dubbo.common.utils.NetUtils;
@@ -70,9 +71,7 @@ public void run() {
7071
public void close() {
7172
super.close();
7273
try {
73-
if (!checkModifiedFuture.isCancelled()) {
74-
checkModifiedFuture.cancel(true);
75-
}
74+
ExecutorUtil.cancelScheduledFuture(checkModifiedFuture);
7675
} catch (Throwable t) {
7776
logger.error(t.getMessage(), t);
7877
}

dubbo-remoting/dubbo-remoting-p2p/src/main/java/org/apache/dubbo/remoting/p2p/support/FileGroup.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.dubbo.remoting.p2p.support;
1818

1919
import org.apache.dubbo.common.URL;
20+
import org.apache.dubbo.common.utils.ExecutorUtil;
2021
import org.apache.dubbo.common.utils.IOUtils;
2122
import org.apache.dubbo.common.utils.NamedThreadFactory;
2223
import org.apache.dubbo.common.utils.NetUtils;
@@ -67,9 +68,7 @@ public void run() {
6768
public void close() {
6869
super.close();
6970
try {
70-
if (!checkModifiedFuture.isCancelled()) {
71-
checkModifiedFuture.cancel(true);
72-
}
71+
ExecutorUtil.cancelScheduledFuture(checkModifiedFuture);
7372
} catch (Throwable t) {
7473
logger.error(t.getMessage(), t);
7574
}

0 commit comments

Comments
 (0)