Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
FIX_graceful_shutdown
  • Loading branch information
taige committed Aug 8, 2017
1 parent 529f26b commit ed08ffa
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 89 deletions.
Expand Up @@ -35,6 +35,7 @@
* ExchangeReceiver
*
* @author william.liangf
* modify by wuhongqiang for graceful close work well
*/
final class HeaderExchangeChannel implements ExchangeChannel {

Expand Down Expand Up @@ -137,7 +138,10 @@ public void close(int timeout) {
closed = true;
if (timeout > 0) {
long start = System.currentTimeMillis();
while (DefaultFuture.hasFuture(HeaderExchangeChannel.this)
//while (DefaultFuture.hasFuture(HeaderExchangeChannel.this)
//modify by wuhongqiang 2014.4.16
//DefaultFuture 实例化时传入的是channel,而非HeaderExchangeChannel.this (参见111行)
while (DefaultFuture.hasFuture(channel)
&& System.currentTimeMillis() - start < timeout) {
try {
Thread.sleep(10);
Expand Down
@@ -1,46 +1,47 @@
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.remoting.transport;

import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.common.utils.ExecutorUtil;
import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.dubbo.remoting.Channel;
import com.alibaba.dubbo.remoting.ChannelHandler;
import com.alibaba.dubbo.remoting.RemotingException;
import com.alibaba.dubbo.remoting.Server;
import com.alibaba.dubbo.remoting.transport.dispatcher.WrappedChannelHandler;
import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.common.store.DataStore;
import com.alibaba.dubbo.common.utils.ExecutorUtil;
import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.dubbo.remoting.Channel;
import com.alibaba.dubbo.remoting.ChannelHandler;
import com.alibaba.dubbo.remoting.RemotingException;
import com.alibaba.dubbo.remoting.Server;

/**
* AbstractServer
*
* @author qian.lei
* @author ding.lid
*/
public abstract class AbstractServer extends AbstractEndpoint implements Server {

private static final Logger logger = LoggerFactory.getLogger(AbstractServer.class);
public abstract class AbstractServer extends AbstractEndpoint implements Server {

private static final Logger logger = LoggerFactory.getLogger(AbstractServer.class);

private InetSocketAddress localAddress;

Expand All @@ -56,25 +57,26 @@ public abstract class AbstractServer extends AbstractEndpoint implements Server

public AbstractServer(URL url, ChannelHandler handler) throws RemotingException {
super(url, handler);
localAddress = getUrl().toInetSocketAddress();
String host = url.getParameter(Constants.ANYHOST_KEY, false)
localAddress = getUrl().toInetSocketAddress();
String host = url.getParameter(Constants.ANYHOST_KEY, false)
|| NetUtils.isInvalidLocalHost(getUrl().getHost())
? NetUtils.ANYHOST : getUrl().getHost();
bindAddress = new InetSocketAddress(host, getUrl().getPort());
this.accepts = url.getParameter(Constants.ACCEPTS_KEY, Constants.DEFAULT_ACCEPTS);
this.idleTimeout = url.getParameter(Constants.IDLE_TIMEOUT_KEY, Constants.DEFAULT_IDLE_TIMEOUT);
this.accepts = url.getParameter(Constants.ACCEPTS_KEY, Constants.DEFAULT_ACCEPTS);
this.idleTimeout = url.getParameter(Constants.IDLE_TIMEOUT_KEY, Constants.DEFAULT_IDLE_TIMEOUT);
try {
doOpen();
if (logger.isInfoEnabled()) {
logger.info("Start " + getClass().getSimpleName() + " bind " + getBindAddress() + ", export " + getLocalAddress());
}
} catch (Throwable t) {
throw new RemotingException(url.toInetSocketAddress(), null, "Failed to bind " + getClass().getSimpleName()
throw new RemotingException(url.toInetSocketAddress(), null, "Failed to bind " + getClass().getSimpleName()
+ " on " + getLocalAddress() + ", cause: " + t.getMessage(), t);
}
if (handler instanceof WrappedChannelHandler ){
executor = ((WrappedChannelHandler)handler).getExecutor();
}
// fix 服务销毁时,线程池资源没有释放 且 graceful shutdown NOT work by (wuhongqiang copy from) wuwen5 <wuwen.55@gmail.com>
executor = (ExecutorService) ExtensionLoader.getExtensionLoader(DataStore.class)
.getDefaultExtension().get(Constants.EXECUTOR_SERVICE_COMPONENT_KEY, Integer.toString(url.getPort()));

}

protected abstract void doOpen() throws Throwable;
Expand All @@ -87,7 +89,7 @@ public void reset(URL url) {
}
try {
if (url.hasParameter(Constants.ACCEPTS_KEY)) {
int a = url.getParameter(Constants.ACCEPTS_KEY, 0);
int a = url.getParameter(Constants.ACCEPTS_KEY, 0);
if (a > 0) {
this.accepts = a;
}
Expand All @@ -97,7 +99,7 @@ public void reset(URL url) {
}
try {
if (url.hasParameter(Constants.IDLE_TIMEOUT_KEY)) {
int t = url.getParameter(Constants.IDLE_TIMEOUT_KEY, 0);
int t = url.getParameter(Constants.IDLE_TIMEOUT_KEY, 0);
if (t > 0) {
this.idleTimeout = t;
}
Expand All @@ -109,7 +111,7 @@ public void reset(URL url) {
if (url.hasParameter(Constants.THREADS_KEY)
&& executor instanceof ThreadPoolExecutor && !executor.isShutdown()) {
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
int threads = url.getParameter(Constants.THREADS_KEY, 0);
int threads = url.getParameter(Constants.THREADS_KEY, 0);
int max = threadPoolExecutor.getMaximumPoolSize();
int core = threadPoolExecutor.getCorePoolSize();
if (threads > 0 && (threads != max || threads != core)) {
Expand All @@ -135,16 +137,16 @@ public void reset(URL url) {
public void send(Object message, boolean sent) throws RemotingException {
Collection<Channel> channels = getChannels();
for (Channel channel : channels) {
if (channel.isConnected()) {
channel.send(message, sent);
if (channel.isConnected()) {
channel.send(message, sent);
}
}
}

public void close() {
if (logger.isInfoEnabled()) {
logger.info("Close " + getClass().getSimpleName() + " bind " + getBindAddress() + ", export " + getLocalAddress());
}
}

public void close() {
if (logger.isInfoEnabled()) {
logger.info("Close " + getClass().getSimpleName() + " bind " + getBindAddress() + ", export " + getLocalAddress());
}
ExecutorUtil.shutdownNow(executor ,100);
try {
super.close();
Expand Down
@@ -1,18 +1,18 @@
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.remoting.transport.netty;

import java.net.InetSocketAddress;
Expand All @@ -35,6 +35,7 @@
*
* @author qian.lei
* @author william.liangf
* modify by wuhongqiang
*/
final class NettyChannel extends AbstractChannel {

Expand All @@ -46,6 +47,9 @@ final class NettyChannel extends AbstractChannel {

private final Map<String, Object> attributes = new ConcurrentHashMap<String, Object>();

//add by wuhongqiang 2014.4.16
private volatile ChannelFuture lastChannelFuture;

private NettyChannel(org.jboss.netty.channel.Channel channel, URL url, ChannelHandler handler){
super(url, handler);
if (channel == null) {
Expand Down Expand Up @@ -96,19 +100,22 @@ public void send(Object message, boolean sent) throws RemotingException {
int timeout = 0;
try {
ChannelFuture future = channel.write(message);
//add by wuhongqiang 2014.4.16
//记录最后一个write的future,在close时判断write是否done,否则有可能丢失该message
lastChannelFuture = future;
if (sent) {
timeout = getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
success = future.await(timeout);
}
Throwable cause = future.getCause();
if (cause != null) {
throw cause;
success = future.await(timeout);
}
Throwable cause = future.getCause();
if (cause != null) {
throw cause;
}
} catch (Throwable e) {
throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
}

if(! success) {
if(! success) {
throw new RemotingException(this, "Failed to send message " + message + " to " + getRemoteAddress()
+ "in timeout(" + timeout + "ms) limit");
}
Expand All @@ -134,6 +141,11 @@ public void close() {
if (logger.isInfoEnabled()) {
logger.info("Close netty channel " + channel);
}
//add by wuhongqiang 2014.4.16
//等待最后一个write事件完成才close channel,否则有可能丢失该write的message
if (lastChannelFuture != null) {
lastChannelFuture.await(getUrl().getPositiveParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
}
channel.close();
} catch (Exception e) {
logger.warn(e.getMessage(), e);
Expand Down
Expand Up @@ -83,7 +83,7 @@ public void destroy() {
}
}
@SuppressWarnings("deprecation")
protected static int getServerShutdownTimeout() {
public static int getServerShutdownTimeout() { // protected to public, by wuhonqiang
int timeout = Constants.DEFAULT_SERVER_SHUTDOWN_TIMEOUT;
String value = ConfigUtils.getProperty(Constants.SHUTDOWN_WAIT_KEY);
if (value != null && value.length() > 0) {
Expand Down
Expand Up @@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.rpc.protocol.dubbo;

package com.alibaba.dubbo.rpc.protocol.dubbo;

import java.net.InetSocketAddress;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -27,15 +27,17 @@
import com.alibaba.dubbo.remoting.exchange.ExchangeClient;
import com.alibaba.dubbo.remoting.exchange.ExchangeHandler;
import com.alibaba.dubbo.remoting.exchange.ResponseFuture;

/**
* dubbo protocol support class.
*
* @author chao.liuc
*/
import com.alibaba.dubbo.rpc.protocol.AbstractProtocol;

/**
* dubbo protocol support class.
*
* @author chao.liuc
* modify by wuhongqiang for graceful shutdown work well
*/
@SuppressWarnings("deprecation")
final class ReferenceCountExchangeClient implements ExchangeClient {

final class ReferenceCountExchangeClient implements ExchangeClient {

private ExchangeClient client;

private final URL url;
Expand All @@ -55,8 +57,8 @@ public ReferenceCountExchangeClient(ExchangeClient client, ConcurrentMap<String,
throw new IllegalStateException("ghostClientMap can not be null, url: " + url);
}
this.ghostClientMap = ghostClientMap;
}

}

public void reset(URL url) {
client.reset(url);
}
Expand Down Expand Up @@ -128,7 +130,9 @@ public void removeAttribute(String key) {
* close方法将不再幂等,调用需要注意.
*/
public void close() {
close(0);
//modify by wuhongqiang 2014.4.16
//默认设置close超时时间,否则没法真正实现graceful shutdown
close(AbstractProtocol.getServerShutdownTimeout());
}

public void close(int timeout) {
Expand Down

0 comments on commit ed08ffa

Please sign in to comment.