From 3ae15809ed448f9c4e79cb093804475b3264da47 Mon Sep 17 00:00:00 2001 From: HMYDK Date: Mon, 2 Sep 2024 19:23:34 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[ISSUE#12583]=20ProtocolManager=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/distributed/ProtocolManager.java | 28 ++++++++++++------- .../nacos/sys/utils/ApplicationUtils.java | 7 ++--- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/com/alibaba/nacos/core/distributed/ProtocolManager.java b/core/src/main/java/com/alibaba/nacos/core/distributed/ProtocolManager.java index 3559cb1fdca..1af03fc0f48 100644 --- a/core/src/main/java/com/alibaba/nacos/core/distributed/ProtocolManager.java +++ b/core/src/main/java/com/alibaba/nacos/core/distributed/ProtocolManager.java @@ -52,9 +52,13 @@ public class ProtocolManager extends MemberChangeListener implements DisposableB private final ServerMemberManager memberManager; - private boolean apInit = false; + private volatile boolean apInit = false; - private boolean cpInit = false; + private volatile boolean cpInit = false; + + private final Object cpLock = new Object(); + + private final Object apLock = new Object(); private Set oldMembers; @@ -80,20 +84,24 @@ public static Set toCPMembersInfo(Collection members) { } public CPProtocol getCpProtocol() { - synchronized (this) { - if (!cpInit) { - initCPProtocol(); - cpInit = true; + if (!cpInit){ + synchronized (cpLock) { + if (!cpInit) { + initCPProtocol(); + cpInit = true; + } } } return cpProtocol; } public APProtocol getApProtocol() { - synchronized (this) { - if (!apInit) { - initAPProtocol(); - apInit = true; + if (!apInit) { + synchronized (apLock) { + if (!apInit) { + initAPProtocol(); + apInit = true; + } } } return apProtocol; diff --git a/sys/src/main/java/com/alibaba/nacos/sys/utils/ApplicationUtils.java b/sys/src/main/java/com/alibaba/nacos/sys/utils/ApplicationUtils.java index 86ea24ca7c3..189803a1a73 100644 --- a/sys/src/main/java/com/alibaba/nacos/sys/utils/ApplicationUtils.java +++ b/sys/src/main/java/com/alibaba/nacos/sys/utils/ApplicationUtils.java @@ -151,11 +151,8 @@ public static T getBean(Class requiredType) throws BeansException { } public static void getBeanIfExist(Class requiredType, Consumer consumer) throws BeansException { - try { - T bean = applicationContext.getBean(requiredType); - consumer.accept(bean); - } catch (NoSuchBeanDefinitionException ignore) { - } + T bean = applicationContext.getBean(requiredType); + consumer.accept(bean); } public static T getBean(Class requiredType, Object... args) throws BeansException { From 26c440fdab1b65a7bb93df1f254f95973fefc62d Mon Sep 17 00:00:00 2001 From: HMYDK Date: Tue, 3 Sep 2024 15:44:04 +0800 Subject: [PATCH 2/2] update: revert getBeanIfExist() --- .../java/com/alibaba/nacos/sys/utils/ApplicationUtils.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sys/src/main/java/com/alibaba/nacos/sys/utils/ApplicationUtils.java b/sys/src/main/java/com/alibaba/nacos/sys/utils/ApplicationUtils.java index 189803a1a73..86ea24ca7c3 100644 --- a/sys/src/main/java/com/alibaba/nacos/sys/utils/ApplicationUtils.java +++ b/sys/src/main/java/com/alibaba/nacos/sys/utils/ApplicationUtils.java @@ -151,8 +151,11 @@ public static T getBean(Class requiredType) throws BeansException { } public static void getBeanIfExist(Class requiredType, Consumer consumer) throws BeansException { - T bean = applicationContext.getBean(requiredType); - consumer.accept(bean); + try { + T bean = applicationContext.getBean(requiredType); + consumer.accept(bean); + } catch (NoSuchBeanDefinitionException ignore) { + } } public static T getBean(Class requiredType, Object... args) throws BeansException {