From 25cda1b6a436641d51097da0d16b960152b03712 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Thu, 23 May 2019 13:17:02 +0800 Subject: [PATCH 1/6] Performance tuning for TimeoutTask in DefaultFuture, manually merge #4085 and #4087 --- .../exchange/support/DefaultFuture.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java index 1dab931a9c4..307d5c2ff0d 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java @@ -50,20 +50,19 @@ public class DefaultFuture extends CompletableFuture { private static final Map FUTURES = new ConcurrentHashMap<>(); - private static final Map PENDING_TASKS = new ConcurrentHashMap<>(); - public static final Timer TIME_OUT_TIMER = new HashedWheelTimer( new NamedThreadFactory("dubbo-future-timeout", true), 30, TimeUnit.MILLISECONDS); // invoke id. - private final long id; + private final Long id; private final Channel channel; private final Request request; private final int timeout; private final long start = System.currentTimeMillis(); private volatile long sent; + private Timeout timeoutCheckTask; private DefaultFuture(Channel channel, Request request, int timeout) { this.channel = channel; @@ -79,9 +78,8 @@ private DefaultFuture(Channel channel, Request request, int timeout) { * check time out of the future */ private static void timeoutCheck(DefaultFuture future) { - TimeoutCheckTask task = new TimeoutCheckTask(future); - Timeout t = TIME_OUT_TIMER.newTimeout(task, future.getTimeout(), TimeUnit.MILLISECONDS); - PENDING_TASKS.put(future.getId(), t); + TimeoutCheckTask task = new TimeoutCheckTask(future.getId()); + future.timeoutCheckTask = TIME_OUT_TIMER.newTimeout(task, future.getTimeout(), TimeUnit.MILLISECONDS); } /** @@ -140,15 +138,19 @@ public static void closeChannel(Channel channel) { } public static void received(Channel channel, Response response) { + received(channel, response, false); + } + + public static void received(Channel channel, Response response, boolean timeout) { try { DefaultFuture future = FUTURES.remove(response.getId()); if (future != null) { - future.doReceived(response); - Timeout t = PENDING_TASKS.remove(future.getId()); + Timeout t = future.timeoutCheckTask; if (t != null) { // decrease Time t.cancel(); } + future.doReceived(response); } else { logger.warn("The timeout response finally returned at " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date())) @@ -229,15 +231,16 @@ private String getTimeoutMessage(boolean scan) { private static class TimeoutCheckTask implements TimerTask { - private DefaultFuture future; + private final Long requestID; - TimeoutCheckTask(DefaultFuture future) { - this.future = future; + TimeoutCheckTask(Long requestID) { + this.requestID = requestID; } @Override public void run(Timeout timeout) { - if (future == null || future.isDone()) { + DefaultFuture future = FUTURES.remove(requestID); + if (future.isDone()) { return; } // create exception response. @@ -246,7 +249,7 @@ public void run(Timeout timeout) { timeoutResponse.setStatus(future.isSent() ? Response.SERVER_TIMEOUT : Response.CLIENT_TIMEOUT); timeoutResponse.setErrorMessage(future.getTimeoutMessage(true)); // handle response. - DefaultFuture.received(future.getChannel(), timeoutResponse); + DefaultFuture.received(future.getChannel(), timeoutResponse, true); } } From 280d91cf1cec9afee5a8c4ccbde6a42cee75b7b6 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Thu, 23 May 2019 14:27:37 +0800 Subject: [PATCH 2/6] use getFuture to avoid future being removed too early. --- .../apache/dubbo/remoting/exchange/support/DefaultFuture.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java index 307d5c2ff0d..363e13c4163 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java @@ -239,7 +239,7 @@ private static class TimeoutCheckTask implements TimerTask { @Override public void run(Timeout timeout) { - DefaultFuture future = FUTURES.remove(requestID); + DefaultFuture future = DefaultFuture.getFuture(requestID); if (future.isDone()) { return; } From 85e91dc5e8ffb61d1f2af3600c913c7d30f53543 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Thu, 23 May 2019 14:28:58 +0800 Subject: [PATCH 3/6] check timeout --- .../apache/dubbo/remoting/exchange/support/DefaultFuture.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java index 363e13c4163..298422c0416 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java @@ -146,7 +146,7 @@ public static void received(Channel channel, Response response, boolean timeout) DefaultFuture future = FUTURES.remove(response.getId()); if (future != null) { Timeout t = future.timeoutCheckTask; - if (t != null) { + if (!timeout) { // decrease Time t.cancel(); } From 02901f4fba3dd1e710ba9b68e16facb67b00129f Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Thu, 23 May 2019 15:22:41 +0800 Subject: [PATCH 4/6] check future is not null --- .../apache/dubbo/remoting/exchange/support/DefaultFuture.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java index 298422c0416..ba9f7a785c2 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java @@ -240,7 +240,7 @@ private static class TimeoutCheckTask implements TimerTask { @Override public void run(Timeout timeout) { DefaultFuture future = DefaultFuture.getFuture(requestID); - if (future.isDone()) { + if (future != null && future.isDone()) { return; } // create exception response. From 957400b11a4cb6e75c74502447ae7b523c10ac74 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Thu, 23 May 2019 15:26:00 +0800 Subject: [PATCH 5/6] check future is null --- .../apache/dubbo/remoting/exchange/support/DefaultFuture.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java index ba9f7a785c2..ec9bf57e65c 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java @@ -240,7 +240,7 @@ private static class TimeoutCheckTask implements TimerTask { @Override public void run(Timeout timeout) { DefaultFuture future = DefaultFuture.getFuture(requestID); - if (future != null && future.isDone()) { + if (future == null && future.isDone()) { return; } // create exception response. From cc170ae52f1fe77d9a7c9b74ed9f24f4b9f9e8b7 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Thu, 23 May 2019 15:28:27 +0800 Subject: [PATCH 6/6] change if condition from && to || --- .../apache/dubbo/remoting/exchange/support/DefaultFuture.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java index ec9bf57e65c..7f0e3a7227c 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/DefaultFuture.java @@ -240,7 +240,7 @@ private static class TimeoutCheckTask implements TimerTask { @Override public void run(Timeout timeout) { DefaultFuture future = DefaultFuture.getFuture(requestID); - if (future == null && future.isDone()) { + if (future == null || future.isDone()) { return; } // create exception response.