This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.3
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.3 by this push:
     new 6a44987f5f chore: Use ReentrantLock for inti ReferenceConfig. (#14136)
6a44987f5f is described below

commit 6a44987f5f9d4dafda7e3ac82e40892a01b64397
Author: He-Pin(kerr) <hepin1...@gmail.com>
AuthorDate: Mon May 13 10:52:48 2024 +0800

    chore: Use ReentrantLock for inti ReferenceConfig. (#14136)
---
 .../org/apache/dubbo/config/ReferenceConfig.java   | 161 +++++++++++----------
 .../client/migration/MigrationRuleHandler.java     |  48 +++---
 2 files changed, 117 insertions(+), 92 deletions(-)

diff --git 
a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java
 
b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java
index c14d669767..c0aad1a209 100644
--- 
a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java
+++ 
b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java
@@ -64,6 +64,7 @@ import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
 import java.util.TreeSet;
+import java.util.concurrent.locks.ReentrantLock;
 
 import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE;
 import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_DOMAIN;
@@ -162,6 +163,8 @@ public class ReferenceConfig<T> extends 
ReferenceConfigBase<T> {
      */
     private String services;
 
+    protected final transient ReentrantLock lock = new ReentrantLock();
+
     public ReferenceConfig() {
         super();
     }
@@ -291,103 +294,113 @@ public class ReferenceConfig<T> extends 
ReferenceConfigBase<T> {
     }
 
     @Override
-    public synchronized void destroy() {
-        super.destroy();
-        if (destroyed) {
-            return;
-        }
-        destroyed = true;
+    public void destroy() {
+        lock.lock();
         try {
-            if (invoker != null) {
-                invoker.destroy();
+            super.destroy();
+            if (destroyed) {
+                return;
             }
-        } catch (Throwable t) {
-            logger.warn(
-                    CONFIG_FAILED_DESTROY_INVOKER,
-                    "",
-                    "",
-                    "Unexpected error occurred when destroy invoker of 
ReferenceConfig(" + url + ").",
-                    t);
-        }
-        invoker = null;
-        ref = null;
-        if (consumerModel != null) {
-            ModuleServiceRepository repository = 
getScopeModel().getServiceRepository();
-            repository.unregisterConsumer(consumerModel);
+            destroyed = true;
+            try {
+                if (invoker != null) {
+                    invoker.destroy();
+                }
+            } catch (Throwable t) {
+                logger.warn(
+                        CONFIG_FAILED_DESTROY_INVOKER,
+                        "",
+                        "",
+                        "Unexpected error occurred when destroy invoker of 
ReferenceConfig(" + url + ").",
+                        t);
+            }
+            invoker = null;
+            ref = null;
+            if (consumerModel != null) {
+                ModuleServiceRepository repository = 
getScopeModel().getServiceRepository();
+                repository.unregisterConsumer(consumerModel);
+            }
+        } finally {
+            lock.unlock();
         }
     }
 
-    protected synchronized void init() {
+    protected void init() {
         init(true);
     }
 
-    protected synchronized void init(boolean check) {
-        if (initialized && ref != null) {
-            return;
-        }
+    protected void init(boolean check) {
+        lock.lock();
         try {
-            if (!this.isRefreshed()) {
-                this.refresh();
-            }
-            // auto detect proxy type
-            String proxyType = getProxy();
-            if (StringUtils.isBlank(proxyType) && 
DubboStub.class.isAssignableFrom(interfaceClass)) {
-                setProxy(CommonConstants.NATIVE_STUB);
+            if (initialized && ref != null) {
+                return;
             }
+            try {
+                if (!this.isRefreshed()) {
+                    this.refresh();
+                }
+                // auto detect proxy type
+                String proxyType = getProxy();
+                if (StringUtils.isBlank(proxyType) && 
DubboStub.class.isAssignableFrom(interfaceClass)) {
+                    setProxy(CommonConstants.NATIVE_STUB);
+                }
 
-            // init serviceMetadata
-            initServiceMetadata(consumer);
+                // init serviceMetadata
+                initServiceMetadata(consumer);
 
-            serviceMetadata.setServiceType(getServiceInterfaceClass());
-            // TODO, uncomment this line once service key is unified
-            serviceMetadata.generateServiceKey();
+                serviceMetadata.setServiceType(getServiceInterfaceClass());
+                // TODO, uncomment this line once service key is unified
+                serviceMetadata.generateServiceKey();
 
-            Map<String, String> referenceParameters = appendConfig();
+                Map<String, String> referenceParameters = appendConfig();
 
-            ModuleServiceRepository repository = 
getScopeModel().getServiceRepository();
-            ServiceDescriptor serviceDescriptor;
-            if (CommonConstants.NATIVE_STUB.equals(getProxy())) {
-                serviceDescriptor = 
StubSuppliers.getServiceDescriptor(interfaceName);
-                repository.registerService(serviceDescriptor);
-                setInterface(serviceDescriptor.getInterfaceName());
-            } else {
-                serviceDescriptor = repository.registerService(interfaceClass);
-            }
-            consumerModel = new ConsumerModel(
-                    serviceMetadata.getServiceKey(),
-                    proxy,
-                    serviceDescriptor,
-                    getScopeModel(),
-                    serviceMetadata,
-                    createAsyncMethodInfo(),
-                    interfaceClassLoader);
+                ModuleServiceRepository repository = 
getScopeModel().getServiceRepository();
+                ServiceDescriptor serviceDescriptor;
+                if (CommonConstants.NATIVE_STUB.equals(getProxy())) {
+                    serviceDescriptor = 
StubSuppliers.getServiceDescriptor(interfaceName);
+                    repository.registerService(serviceDescriptor);
+                    setInterface(serviceDescriptor.getInterfaceName());
+                } else {
+                    serviceDescriptor = 
repository.registerService(interfaceClass);
+                }
+                consumerModel = new ConsumerModel(
+                        serviceMetadata.getServiceKey(),
+                        proxy,
+                        serviceDescriptor,
+                        getScopeModel(),
+                        serviceMetadata,
+                        createAsyncMethodInfo(),
+                        interfaceClassLoader);
 
-            // Compatible with dependencies on 
ServiceModel#getReferenceConfig() , and will be removed in a future
-            // version.
-            consumerModel.setConfig(this);
+                // Compatible with dependencies on 
ServiceModel#getReferenceConfig() , and will be removed in a future
+                // version.
+                consumerModel.setConfig(this);
 
-            repository.registerConsumer(consumerModel);
+                repository.registerConsumer(consumerModel);
 
-            serviceMetadata.getAttachments().putAll(referenceParameters);
+                serviceMetadata.getAttachments().putAll(referenceParameters);
 
-            ref = createProxy(referenceParameters);
+                ref = createProxy(referenceParameters);
 
-            serviceMetadata.setTarget(ref);
-            serviceMetadata.addAttribute(PROXY_CLASS_REF, ref);
+                serviceMetadata.setTarget(ref);
+                serviceMetadata.addAttribute(PROXY_CLASS_REF, ref);
 
-            consumerModel.setDestroyRunner(getDestroyRunner());
-            consumerModel.setProxyObject(ref);
-            consumerModel.initMethodModels();
+                consumerModel.setDestroyRunner(getDestroyRunner());
+                consumerModel.setProxyObject(ref);
+                consumerModel.initMethodModels();
 
-            if (check) {
-                checkInvokerAvailable(0);
-            }
-        } catch (Throwable t) {
-            logAndCleanup(t);
+                if (check) {
+                    checkInvokerAvailable(0);
+                }
+            } catch (Throwable t) {
+                logAndCleanup(t);
 
-            throw t;
+                throw t;
+            }
+            initialized = true;
+        } finally {
+            lock.unlock();
         }
-        initialized = true;
     }
 
     /**
diff --git 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/migration/MigrationRuleHandler.java
 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/migration/MigrationRuleHandler.java
index ccf2dd8cd6..e83ace2c3c 100644
--- 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/migration/MigrationRuleHandler.java
+++ 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/migration/MigrationRuleHandler.java
@@ -23,6 +23,8 @@ import 
org.apache.dubbo.common.status.reporter.FrameworkStatusReportService;
 import org.apache.dubbo.registry.client.migration.model.MigrationRule;
 import org.apache.dubbo.registry.client.migration.model.MigrationStep;
 
+import java.util.concurrent.locks.ReentrantLock;
+
 import static 
org.apache.dubbo.common.constants.LoggerCodeConstants.INTERNAL_ERROR;
 import static 
org.apache.dubbo.common.constants.LoggerCodeConstants.REGISTRY_NO_PARAMETERS_URL;
 
@@ -35,33 +37,43 @@ public class MigrationRuleHandler<T> {
     private volatile MigrationStep currentStep;
     private volatile Float currentThreshold = 0f;
     private final URL consumerURL;
+    private final ReentrantLock lock = new ReentrantLock();
 
     public MigrationRuleHandler(MigrationClusterInvoker<T> invoker, URL url) {
         this.migrationInvoker = invoker;
         this.consumerURL = url;
     }
 
-    public synchronized void doMigrate(MigrationRule rule) {
-        if (migrationInvoker instanceof ServiceDiscoveryMigrationInvoker) {
-            refreshInvoker(MigrationStep.FORCE_APPLICATION, 1.0f, rule);
-            return;
-        }
+    public void doMigrate(MigrationRule rule) {
+        lock.lock();
+        try {
+            if (migrationInvoker instanceof ServiceDiscoveryMigrationInvoker) {
+                refreshInvoker(MigrationStep.FORCE_APPLICATION, 1.0f, rule);
+                return;
+            }
 
-        // initial step : APPLICATION_FIRST
-        MigrationStep step = MigrationStep.APPLICATION_FIRST;
-        float threshold = -1f;
+            // initial step : APPLICATION_FIRST
+            MigrationStep step = MigrationStep.APPLICATION_FIRST;
+            float threshold = -1f;
 
-        try {
-            step = rule.getStep(consumerURL);
-            threshold = rule.getThreshold(consumerURL);
-        } catch (Exception e) {
-            logger.error(
-                    REGISTRY_NO_PARAMETERS_URL, "", "", "Failed to get step 
and threshold info from rule: " + rule, e);
-        }
+            try {
+                step = rule.getStep(consumerURL);
+                threshold = rule.getThreshold(consumerURL);
+            } catch (Exception e) {
+                logger.error(
+                        REGISTRY_NO_PARAMETERS_URL,
+                        "",
+                        "",
+                        "Failed to get step and threshold info from rule: " + 
rule,
+                        e);
+            }
 
-        if (refreshInvoker(step, threshold, rule)) {
-            // refresh success, update rule
-            setMigrationRule(rule);
+            if (refreshInvoker(step, threshold, rule)) {
+                // refresh success, update rule
+                setMigrationRule(rule);
+            }
+        } finally {
+            lock.unlock();
         }
     }
 

Reply via email to