From 234f1c64abb34cfc49bf233cbf37a4f6e2953857 Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Sun, 12 Mar 2023 10:26:18 +0800 Subject: [PATCH] Fix lock acquire blocked --- .../org/apache/dubbo/rpc/model/ApplicationModel.java | 6 ++++++ .../org/apache/dubbo/rpc/model/FrameworkModel.java | 12 +++++++++++- .../java/org/apache/dubbo/rpc/model/ModuleModel.java | 6 ++++++ .../java/org/apache/dubbo/rpc/model/ScopeModel.java | 9 ++++++++- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java index 31c11f6acab..50b94b956e2 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ApplicationModel.java @@ -36,6 +36,7 @@ import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.Lock; /** * {@link ExtensionLoader}, {@code DubboBootstrap} and this class are at present designed to be @@ -345,6 +346,11 @@ public void setDeployer(ApplicationDeployer deployer) { this.deployer = deployer; } + @Override + protected Lock acquireDestroyLock() { + return frameworkModel.acquireDestroyLock(); + } + // =============================== Deprecated Methods Start ======================================= /** diff --git a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/FrameworkModel.java b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/FrameworkModel.java index 8c2787eadc7..2bdfa9f577c 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/FrameworkModel.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/FrameworkModel.java @@ -33,6 +33,8 @@ import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; /** @@ -48,7 +50,7 @@ public class FrameworkModel extends ScopeModel { private static final AtomicLong index = new AtomicLong(1); private static final Object globalLock = new Object(); - + private volatile static FrameworkModel defaultInstance; private static final List allInstances = new CopyOnWriteArrayList<>(); @@ -68,6 +70,8 @@ public class FrameworkModel extends ScopeModel { private final ApplicationModel internalApplicationModel; + private final ReentrantLock destroyLock = new ReentrantLock(); + /** * Use {@link FrameworkModel#newModel()} to create a new model */ @@ -356,6 +360,12 @@ public FrameworkServiceRepository getServiceRepository() { return serviceRepository; } + + @Override + protected Lock acquireDestroyLock() { + return destroyLock; + } + @Override public Environment getModelEnvironment() { throw new UnsupportedOperationException("Environment is inaccessible for FrameworkModel"); diff --git a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ModuleModel.java b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ModuleModel.java index c06e3d075de..2178d0b3a91 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ModuleModel.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ModuleModel.java @@ -32,6 +32,7 @@ import java.util.HashMap; import java.util.Set; +import java.util.concurrent.locks.Lock; /** * Model of a service module @@ -172,6 +173,11 @@ public void setDeployer(ModuleDeployer deployer) { this.deployer = deployer; } + @Override + protected Lock acquireDestroyLock() { + return getApplicationModel().getFrameworkModel().acquireDestroyLock(); + } + /** * for ut only */ diff --git a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ScopeModel.java b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ScopeModel.java index e0669525281..bbf1bcc9875 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ScopeModel.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ScopeModel.java @@ -34,6 +34,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.Lock; import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_UNABLE_DESTROY_MODEL; @@ -109,8 +110,12 @@ protected void initialize() { } } + protected abstract Lock acquireDestroyLock(); + public void destroy() { - synchronized (instLock) { + Lock lock = acquireDestroyLock(); + try { + lock.lock(); if (destroyed.compareAndSet(false, true)) { try { onDestroy(); @@ -128,6 +133,8 @@ public void destroy() { LOGGER.error(CONFIG_UNABLE_DESTROY_MODEL, "", "", "Error happened when destroying ScopeModel.", t); } } + } finally { + lock.unlock(); } }