-
Notifications
You must be signed in to change notification settings - Fork 26.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve graceful shutdown #1820
Changes from 4 commits
1f0702b
8e822e1
eae97c9
4531f20
0ecca8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.config; | ||
|
||
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.registry.support.AbstractRegistryFactory; | ||
import com.alibaba.dubbo.rpc.Protocol; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* The shutdown hook thread to do the clean up stuff. | ||
* This is a singleton in order to ensure there is only one shutdown hook registered. | ||
* Because {@link ApplicationShutdownHooks} use {@link java.util.IdentityHashMap} | ||
* to store the shutdown hooks. | ||
*/ | ||
public class DubboShutdownHook extends Thread { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(DubboShutdownHook.class); | ||
|
||
private static final DubboShutdownHook dubboShutdownHook = new DubboShutdownHook("DubboShutdownHook"); | ||
|
||
public static DubboShutdownHook getDubboShutdownHook() { | ||
return dubboShutdownHook; | ||
} | ||
|
||
/** | ||
* Has it already been destroyed or not? | ||
*/ | ||
private final AtomicBoolean destroyed; | ||
|
||
private DubboShutdownHook(String name) { | ||
super(name); | ||
this.destroyed = new AtomicBoolean(false); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
if (logger.isInfoEnabled()) { | ||
logger.info("Run shutdown hook now."); | ||
} | ||
destroyAll(); | ||
} | ||
|
||
/** | ||
* Destroy all the resources, including registries and protocols. | ||
*/ | ||
public void destroyAll() { | ||
if (!destroyed.compareAndSet(false, true)) { | ||
return; | ||
} | ||
// destroy all the registries | ||
AbstractRegistryFactory.destroyAll(); | ||
// destroy all the protocols | ||
ExtensionLoader<Protocol> loader = ExtensionLoader.getExtensionLoader(Protocol.class); | ||
for (String protocolName : loader.getLoadedExtensions()) { | ||
try { | ||
Protocol protocol = loader.getLoadedExtension(protocolName); | ||
if (protocol != null) { | ||
protocol.destroy(); | ||
} | ||
} catch (Throwable t) { | ||
logger.warn(t.getMessage(), t); | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.config.spring.initializer; | ||
|
||
import com.alibaba.dubbo.config.DubboShutdownHook; | ||
import org.apache.dubbo.bootstrap.DubboBootstrap; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.context.support.ClassPathXmlApplicationContext; | ||
|
||
public class DubboApplicationListenerTest { | ||
|
||
@Test | ||
public void testTwoShutdownHook() { | ||
DubboShutdownHook spyHook = Mockito.spy(DubboShutdownHook.getDubboShutdownHook()); | ||
ClassPathXmlApplicationContext applicationContext = getApplicationContext(spyHook, true); | ||
applicationContext.refresh(); | ||
applicationContext.close(); | ||
// shutdown hook can't be verified, because it will executed after main thread has finished. | ||
// so we can only verify it by manually run it. | ||
try { | ||
spyHook.start(); | ||
spyHook.join(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
Mockito.verify(spyHook, Mockito.times(2)).destroyAll(); | ||
} | ||
|
||
@Test | ||
public void testOneShutdownHook() { | ||
DubboShutdownHook spyHook = Mockito.spy(DubboShutdownHook.getDubboShutdownHook()); | ||
ClassPathXmlApplicationContext applicationContext = getApplicationContext(spyHook, false); | ||
applicationContext.refresh(); | ||
applicationContext.close(); | ||
Mockito.verify(spyHook, Mockito.times(1)).destroyAll(); | ||
} | ||
|
||
private ClassPathXmlApplicationContext getApplicationContext(DubboShutdownHook hook, boolean registerHook) { | ||
DubboBootstrap bootstrap = new DubboBootstrap(registerHook, hook); | ||
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(); | ||
applicationContext.addApplicationListener(new DubboApplicationListener(bootstrap)); | ||
return applicationContext; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
import com.alibaba.dubbo.common.logger.Logger; | ||
import com.alibaba.dubbo.common.logger.LoggerFactory; | ||
import com.alibaba.dubbo.common.utils.ConfigUtils; | ||
import com.alibaba.dubbo.config.spring.initializer.DubboApplicationListener; | ||
import com.alibaba.dubbo.container.Container; | ||
|
||
import org.springframework.context.support.ClassPathXmlApplicationContext; | ||
|
@@ -44,6 +45,7 @@ public void start() { | |
configPath = DEFAULT_SPRING_CONFIG; | ||
} | ||
context = new ClassPathXmlApplicationContext(configPath.split("[,\\s]+")); | ||
context.addApplicationListener(new DubboApplicationListener()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you are interested in ContextRefreshedEvent or other start events, you will not receive a notification. Unless specified not to refresh, eg:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ralf0131
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we should call refresh(). I will fix it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we should also call |
||
context.start(); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will lead to the coexistence of spring hooks,If the spring hook destroys the context before dubbo, the user code uses the aop feature, or calls the getBean method, dubbo gracefully shutdown and reports that the bean was not found.
Only dubbo hooks are registered in non-spring containers.