From 2dba52bd4b4cca9b4a8100ad13f85276d0155ee1 Mon Sep 17 00:00:00 2001 From: xiuyuhang <442367943@qq.com> Date: Wed, 5 Sep 2018 16:50:32 +0800 Subject: [PATCH 1/2] Direct return when the server goes down unnormally. When the server goes down un-normally, return the unfinished requests directly. The current way is to wait until timeout. --- .../dubbo/remoting/exchange/Response.java | 5 ++++ .../exchange/support/DefaultFuture.java | 27 +++++++++++++++++-- .../support/header/HeaderExchangeHandler.java | 1 + 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/Response.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/Response.java index 0422a06367b..d01d20d6638 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/Response.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/Response.java @@ -40,6 +40,11 @@ public class Response { */ public static final byte SERVER_TIMEOUT = 31; + /** + * channel inactive, directly return the unfinished requests. + */ + public static final byte CHANNEL_INACTIVE = 35; + /** * request format error. */ diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/DefaultFuture.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/DefaultFuture.java index eccc86d4815..ba350ac8928 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/DefaultFuture.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/DefaultFuture.java @@ -43,9 +43,9 @@ public class DefaultFuture implements ResponseFuture { private static final Logger logger = LoggerFactory.getLogger(DefaultFuture.class); - private static final Map CHANNELS = new ConcurrentHashMap(); + private static final Map CHANNELS = new ConcurrentHashMap<>(); - private static final Map FUTURES = new ConcurrentHashMap(); + private static final Map FUTURES = new ConcurrentHashMap<>(); static { Thread th = new Thread(new RemotingInvocationTimeoutScan(), "DubboResponseTimeoutScanTimer"); @@ -90,6 +90,29 @@ public static void sent(Channel channel, Request request) { } } + /** + * close a channel when a channel is inactive + * directly return the unfinished requests. + * + * @param channel channel to close + */ + public static void closeChannel(Channel channel) { + for (long id : CHANNELS.keySet()) { + if (channel.equals(CHANNELS.get(id))) { + DefaultFuture future = getFuture(id); + if (future != null && !future.isDone()) { + Response disconnectResponse = new Response(future.getId()); + disconnectResponse.setStatus(Response.CHANNEL_INACTIVE); + disconnectResponse.setErrorMessage("Channel " + + channel + + " is inactive. Directly return the unFinished request : " + + future.getRequest()); + DefaultFuture.received(channel, disconnectResponse); + } + } + } + } + public static void received(Channel channel, Response response) { try { DefaultFuture future = FUTURES.remove(response.getId()); diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeaderExchangeHandler.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeaderExchangeHandler.java index 3c378178e1b..f44318f7c4e 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeaderExchangeHandler.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeaderExchangeHandler.java @@ -123,6 +123,7 @@ public void disconnected(Channel channel) throws RemotingException { try { handler.disconnected(exchangeChannel); } finally { + DefaultFuture.closeChannel(channel); HeaderExchangeChannel.removeChannelIfDisconnected(channel); } } From 263e47e86d8981a8327fe9b0b98cb256032fe370 Mon Sep 17 00:00:00 2001 From: xiuyuhang <442367943@qq.com> Date: Wed, 5 Sep 2018 17:30:51 +0800 Subject: [PATCH 2/2] fix diamond --- .../dubbo/remoting/exchange/support/DefaultFuture.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/DefaultFuture.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/DefaultFuture.java index ba350ac8928..a72fadc5d01 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/DefaultFuture.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/DefaultFuture.java @@ -43,9 +43,9 @@ public class DefaultFuture implements ResponseFuture { private static final Logger logger = LoggerFactory.getLogger(DefaultFuture.class); - private static final Map CHANNELS = new ConcurrentHashMap<>(); + private static final Map CHANNELS = new ConcurrentHashMap(); - private static final Map FUTURES = new ConcurrentHashMap<>(); + private static final Map FUTURES = new ConcurrentHashMap(); static { Thread th = new Thread(new RemotingInvocationTimeoutScan(), "DubboResponseTimeoutScanTimer");