Skip to content
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

Fix sofaruntime manager destroy whitout shutdown #1324

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.alipay.sofa.runtime.spi.component.SofaRuntimeManager;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.Assert;
Expand All @@ -34,7 +33,7 @@
* @author huzijie
* @since 2.5.0
*/
public class SofaRuntimeContainer implements ApplicationContextAware, DisposableBean {
public class SofaRuntimeContainer implements ApplicationContextAware {

private static final Map<ClassLoader, ApplicationContext> APPLICATION_CONTEXT_MAP = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -104,10 +103,14 @@ public static void clear() {
JVM_INVOKE_SERIALIZE_MAP.clear();
}

@Override
public void destroy() {
public static void destroy(ClassLoader contextClassLoader) {
APPLICATION_CONTEXT_MAP.remove(contextClassLoader);
SOFA_RUNTIME_MANAGER_MAP.remove(contextClassLoader);

SofaRuntimeManager sofaRuntimeManager = SOFA_RUNTIME_MANAGER_MAP.remove(contextClassLoader);
if (sofaRuntimeManager != null) {
sofaRuntimeManager.shutDownExternally();
}
Comment on lines +109 to +112
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Properly shutting down SofaRuntimeManager before removal is a good practice for clean resource management.

Consider adding logging before and after the shutdown process to enhance traceability and debugging.

+ logger.info("Shutting down SofaRuntimeManager for class loader: " + contextClassLoader);
  sofaRuntimeManager.shutDownExternally();
+ logger.info("Shutdown complete for SofaRuntimeManager.");

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
SofaRuntimeManager sofaRuntimeManager = SOFA_RUNTIME_MANAGER_MAP.remove(contextClassLoader);
if (sofaRuntimeManager != null) {
sofaRuntimeManager.shutDownExternally();
}
SofaRuntimeManager sofaRuntimeManager = SOFA_RUNTIME_MANAGER_MAP.remove(contextClassLoader);
if (sofaRuntimeManager != null) {
logger.info("Shutting down SofaRuntimeManager for class loader: " + contextClassLoader);
sofaRuntimeManager.shutDownExternally();
logger.info("Shutdown complete for SofaRuntimeManager.");
}


JVM_SERVICE_CACHE_MAP.remove(contextClassLoader);
JVM_INVOKE_SERIALIZE_MAP.remove(contextClassLoader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,8 @@ private void doUninstallBiz(Biz biz) {
// Remove dynamic JVM service cache
DynamicJvmServiceProxyFinder.getInstance().afterBizUninstall(biz);

SofaRuntimeManager sofaRuntimeManager = SofaRuntimeContainer.getSofaRuntimeManager(biz
.getBizClassLoader());
SofaRuntimeContainer.destroy(biz.getBizClassLoader());

if (sofaRuntimeManager == null) {
throw new IllegalStateException("No SofaRuntimeManager match classLoader");
}
sofaRuntimeManager.shutDownExternally();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void destroy() throws Exception {

assertThat(SofaRuntimeContainer.getApplicationContext(classLoaderA)).isEqualTo(
genericApplicationContext);
sofaRuntimeContainer.destroy();
sofaRuntimeContainer.destroy(classLoaderA);
assertThat(SofaRuntimeContainer.getApplicationContext(classLoaderA)).isNull();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.support.GenericApplicationContext;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

Expand All @@ -50,14 +49,6 @@ public void clear() {
SofaRuntimeContainer.clear();
}

@Test
public void noSofaRuntimeManager() {
MockBiz mockBiz = new MockBiz();
assertThatThrownBy(() -> sofaBizUninstallEventHandler.handleEvent(new BeforeBizStopEvent(mockBiz)))
.isInstanceOf(IllegalStateException.class)
.hasMessage("No SofaRuntimeManager match classLoader");
}

@Test
public void shutDown() {
SofaRuntimeContainer sofaRuntimeContainer = new SofaRuntimeContainer(sofaRuntimeManager);
Expand Down
Loading