fix issue: resolve the conficts between Dubbo shutdown hook and Spring. (#10730)

This commit is contained in:
pandaapo 2022-10-17 10:53:36 +08:00 committed by GitHub
parent 348e3160c5
commit c65754681c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 137 additions and 1 deletions

View File

@ -46,6 +46,7 @@ public class ModuleModel extends ScopeModel {
private ModuleServiceRepository serviceRepository;
private ModuleConfigManager moduleConfigManager;
private ModuleDeployer deployer;
private boolean lifeCycleManagedExternally = false;
public ModuleModel(ApplicationModel applicationModel) {
this(applicationModel, false);
@ -196,4 +197,12 @@ public class ModuleModel extends ScopeModel {
serviceRepository.registerConsumer(consumerModel);
return consumerModel;
}
public boolean isLifeCycleManagedExternally() {
return lifeCycleManagedExternally;
}
public void setLifeCycleManagedExternally(boolean lifeCycleManagedExternally) {
this.lifeCycleManagedExternally = lifeCycleManagedExternally;
}
}

View File

@ -40,6 +40,7 @@ public class ModuleModelTest {
Assertions.assertEquals(moduleModel.getApplicationModel(), applicationModel);
Assertions.assertTrue(applicationModel.getPubModuleModels().contains(moduleModel));
Assertions.assertNotNull(moduleModel.getInternalId());
Assertions.assertFalse(moduleModel.isLifeCycleManagedExternally());
Assertions.assertNotNull(moduleModel.getExtensionDirector());
Assertions.assertNotNull(moduleModel.getBeanFactory());

View File

@ -22,7 +22,9 @@ import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.Assert;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ModuleModel;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_FAILED_SHUTDOWN_HOOK;
@ -77,7 +79,47 @@ public class DubboShutdownHook extends Thread {
}
private void doDestroy() {
applicationModel.destroy();
boolean hasModuleBindSpring = false;
// check if any modules are bound to Spring
for (ModuleModel module: applicationModel.getModuleModels()) {
if (module.isLifeCycleManagedExternally()) {
hasModuleBindSpring = true;
break;
}
}
if (hasModuleBindSpring) {
int timeout = ConfigurationUtils.getServerShutdownTimeout(applicationModel);
if (timeout > 0) {
long start = System.currentTimeMillis();
/**
* To avoid shutdown conflicts between Dubbo and Spring,
* wait for the modules bound to Spring to be handled by Spring util timeout.
*/
logger.info("Waiting for modules managed by Spring to be shut down.");
while (!applicationModel.isDestroyed() && hasModuleBindSpring
&& (System.currentTimeMillis() - start) < timeout) {
try {
TimeUnit.MILLISECONDS.sleep(10);
hasModuleBindSpring = false;
if (!applicationModel.isDestroyed()) {
for (ModuleModel module: applicationModel.getModuleModels()) {
if (module.isLifeCycleManagedExternally()) {
hasModuleBindSpring = true;
break;
}
}
}
} catch (InterruptedException e) {
logger.warn(e.getMessage(), e);
}
}
}
}
if (!applicationModel.isDestroyed()) {
logger.info("Dubbo shuts down application " +
"after Spring fails to do in time or doesn't do it completely.");
applicationModel.destroy();
}
}
/**

View File

@ -0,0 +1,83 @@
/*
* 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 org.apache.dubbo.config;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.model.ModuleModel;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.concurrent.TimeUnit;
import static java.util.Arrays.asList;
public class DubboShutdownHookTest {
private DubboShutdownHook dubboShutdownHook;
private ApplicationModel applicationModel;
@BeforeEach
public void init() {
SysProps.setProperty(CommonConstants.IGNORE_LISTEN_SHUTDOWN_HOOK, "false");
FrameworkModel frameworkModel = new FrameworkModel();
applicationModel = new ApplicationModel(frameworkModel);
ModuleModel moduleModel = applicationModel.newModule();
dubboShutdownHook = new DubboShutdownHook(applicationModel);
}
@AfterEach
public void clear() {
SysProps.clear();
}
@Test
public void testDubboShutdownHook() {
Assertions.assertNotNull(dubboShutdownHook);
Assertions.assertLinesMatch(asList("DubboShutdownHook"), asList(dubboShutdownHook.getName()));
Assertions.assertFalse(dubboShutdownHook.getRegistered());
}
@Test
public void testDestoryNoModuleManagedExternally() {
boolean hasModuleManagedExternally = false;
for (ModuleModel moduleModel : applicationModel.getModuleModels()) {
if (moduleModel.isLifeCycleManagedExternally()) {
hasModuleManagedExternally = true;
break;
}
}
Assertions.assertFalse(hasModuleManagedExternally);
dubboShutdownHook.run();
Assertions.assertTrue(applicationModel.isDestroyed());
}
@Test
public void testDestoryWithModuleManagedExternally() throws InterruptedException {
applicationModel.getModuleModels().get(0).setLifeCycleManagedExternally(true);
new Thread(() -> {
applicationModel.getModuleModels().get(0).destroy();
}).start();
TimeUnit.MILLISECONDS.sleep(10);
dubboShutdownHook.run();
Assertions.assertTrue(applicationModel.isDestroyed());
}
}

View File

@ -138,6 +138,7 @@ public class DubboSpringInitializer {
// mark context as bound
context.markAsBound();
moduleModel.setLifeCycleManagedExternally(true);
// register common beans
DubboBeanUtils.registerCommonBeans(registry);