From 4bc5b67dce76dbc64be2739a5408018bf241d99c Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Wed, 26 Jun 2024 11:28:56 +0800 Subject: [PATCH 1/3] Sync spring init actions to prevent parallel init --- .../dubbo/config/spring/ReferenceBean.java | 60 ++++++++++--------- .../DubboConfigApplicationListener.java | 2 +- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java index dc474498d6..07dfa1c0df 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java @@ -82,14 +82,14 @@ import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROXY_FAILED * } * } * - * + *

* Or register ReferenceBean in xml: *

  * <dubbo:reference id="helloService" interface="org.apache.dubbo.config.spring.api.HelloService"/>
  * <!-- As GenericService -->
  * <dubbo:reference id="genericHelloService" interface="org.apache.dubbo.config.spring.api.HelloService" generic="true"/>
  * 
- * + *

* Step 2: Inject ReferenceBean by @Autowired *

  * public class FooController {
@@ -101,7 +101,6 @@ import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROXY_FAILED
  * }
  * 
* - * * @see org.apache.dubbo.config.annotation.DubboReference * @see org.apache.dubbo.config.spring.reference.ReferenceBeanBuilder */ @@ -148,7 +147,7 @@ public class ReferenceBean private MutablePropertyValues propertyValues; // actual reference config - private ReferenceConfig referenceConfig; + private volatile ReferenceConfig referenceConfig; // ReferenceBeanManager private ReferenceBeanManager referenceBeanManager; @@ -184,23 +183,20 @@ public class ReferenceBean * *

* Why we need a lazy proxy? - * + *

*

- * When Spring searches beans by type, if Spring cannot determine the type of a factory bean, it may try to initialize it. - * The ReferenceBean is also a FactoryBean. - *
- * (This has already been resolved by decorating the BeanDefinition: {@link DubboBeanDefinitionParser#configReferenceBean}) - * + * When Spring searches beans by type, if Spring cannot determine the type of a factory bean, it may try to + * initialize it. The ReferenceBean is also a FactoryBean.
(This has already been resolved by decorating the + * BeanDefinition: {@link DubboBeanDefinitionParser#configReferenceBean}) + *

*

- * In addition, if some ReferenceBeans are dependent on beans that are initialized very early, - * and dubbo config beans are not ready yet, there will be many unexpected problems if initializing the dubbo reference immediately. - * + * In addition, if some ReferenceBeans are dependent on beans that are initialized very early, and dubbo config + * beans are not ready yet, there will be many unexpected problems if initializing the dubbo reference immediately. + *

*

- * When it is initialized, only a lazy proxy object will be created, - * and dubbo reference-related resources will not be initialized. - *
- * In this way, the influence of Spring is eliminated, and the dubbo configuration initialization is controllable. - * + * When it is initialized, only a lazy proxy object will be created, and dubbo reference-related resources will not + * be initialized.
In this way, the influence of Spring is eliminated, and the dubbo configuration + * initialization is controllable. * * @see DubboConfigBeanInitializer * @see ReferenceBeanManager#initReferenceBean(ReferenceBean) @@ -284,6 +280,7 @@ public class ReferenceBean /** * The interface of this ReferenceBean, for injection purpose + * * @return */ public Class getInterfaceClass() { @@ -414,7 +411,7 @@ public class ReferenceBean PROXY_FAILED, "", "", - "Failed to generate proxy by Javassist failed. Fallback to use JDK proxy is also failed. " + "Failed to generate proxy by Javassist failed. Fmvnallback to use JDK proxy is also failed. " + "Interfaces: " + interfaces + " JDK Error.", fromJdk); throw fromJdk; @@ -423,15 +420,22 @@ public class ReferenceBean private Object getCallProxy() throws Exception { if (referenceConfig == null) { - referenceBeanManager.initReferenceBean(this); - applicationContext - .getBean(DubboConfigApplicationListener.class.getName(), DubboConfigApplicationListener.class) - .init(); - logger.warn( - CONFIG_DUBBO_BEAN_INITIALIZER, - "", - "", - "ReferenceBean is not ready yet, please make sure to call reference interface method after dubbo is started."); + synchronized (LockUtils.getSingletonMutex(applicationContext)) { + if (referenceConfig == null) { + referenceBeanManager.initReferenceBean(this); + applicationContext + .getBean( + DubboConfigApplicationListener.class.getName(), + DubboConfigApplicationListener.class) + .init(); + logger.warn( + CONFIG_DUBBO_BEAN_INITIALIZER, + "", + "", + "ReferenceBean is not ready yet, please make sure to " + + "call reference interface method after dubbo is started."); + } + } } // get reference proxy // Subclasses should synchronize on the given Object if they perform any sort of extended singleton creation diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboConfigApplicationListener.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboConfigApplicationListener.java index fa9d5b2b0b..499e81d679 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboConfigApplicationListener.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboConfigApplicationListener.java @@ -60,7 +60,7 @@ public class DubboConfigApplicationListener } } - public void init() { + public synchronized void init() { // It's expected to be notified at // org.springframework.context.support.AbstractApplicationContext.registerListeners(), // before loading non-lazy singleton beans. At this moment, all BeanFactoryPostProcessor have been processed, From 757677f987557cea40c390ac5a840314294384eb Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Wed, 26 Jun 2024 13:52:45 +0800 Subject: [PATCH 2/3] fix code --- .../dubbo/config/spring/ReferenceBean.java | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java index 07dfa1c0df..cddf7f3a1f 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java @@ -82,14 +82,14 @@ import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROXY_FAILED * } * } * - *

+ * * Or register ReferenceBean in xml: *

  * <dubbo:reference id="helloService" interface="org.apache.dubbo.config.spring.api.HelloService"/>
  * <!-- As GenericService -->
  * <dubbo:reference id="genericHelloService" interface="org.apache.dubbo.config.spring.api.HelloService" generic="true"/>
  * 
- *

+ * * Step 2: Inject ReferenceBean by @Autowired *

  * public class FooController {
@@ -101,6 +101,7 @@ import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROXY_FAILED
  * }
  * 
* + * * @see org.apache.dubbo.config.annotation.DubboReference * @see org.apache.dubbo.config.spring.reference.ReferenceBeanBuilder */ @@ -147,7 +148,7 @@ public class ReferenceBean private MutablePropertyValues propertyValues; // actual reference config - private volatile ReferenceConfig referenceConfig; + private ReferenceConfig referenceConfig; // ReferenceBeanManager private ReferenceBeanManager referenceBeanManager; @@ -183,20 +184,23 @@ public class ReferenceBean * *

* Why we need a lazy proxy? - *

+ * *

- * When Spring searches beans by type, if Spring cannot determine the type of a factory bean, it may try to - * initialize it. The ReferenceBean is also a FactoryBean.
(This has already been resolved by decorating the - * BeanDefinition: {@link DubboBeanDefinitionParser#configReferenceBean}) - *

+ * When Spring searches beans by type, if Spring cannot determine the type of a factory bean, it may try to initialize it. + * The ReferenceBean is also a FactoryBean. + *
+ * (This has already been resolved by decorating the BeanDefinition: {@link DubboBeanDefinitionParser#configReferenceBean}) + * *

- * In addition, if some ReferenceBeans are dependent on beans that are initialized very early, and dubbo config - * beans are not ready yet, there will be many unexpected problems if initializing the dubbo reference immediately. - *

+ * In addition, if some ReferenceBeans are dependent on beans that are initialized very early, + * and dubbo config beans are not ready yet, there will be many unexpected problems if initializing the dubbo reference immediately. + * *

- * When it is initialized, only a lazy proxy object will be created, and dubbo reference-related resources will not - * be initialized.
In this way, the influence of Spring is eliminated, and the dubbo configuration - * initialization is controllable. + * When it is initialized, only a lazy proxy object will be created, + * and dubbo reference-related resources will not be initialized. + *
+ * In this way, the influence of Spring is eliminated, and the dubbo configuration initialization is controllable. + * * * @see DubboConfigBeanInitializer * @see ReferenceBeanManager#initReferenceBean(ReferenceBean) @@ -280,7 +284,6 @@ public class ReferenceBean /** * The interface of this ReferenceBean, for injection purpose - * * @return */ public Class getInterfaceClass() { @@ -411,7 +414,7 @@ public class ReferenceBean PROXY_FAILED, "", "", - "Failed to generate proxy by Javassist failed. Fmvnallback to use JDK proxy is also failed. " + "Failed to generate proxy by Javassist failed. Fallback to use JDK proxy is also failed. " + "Interfaces: " + interfaces + " JDK Error.", fromJdk); throw fromJdk; From 9f81ec20f2264ffcc0f7b68dc2b4e8a4524f4a86 Mon Sep 17 00:00:00 2001 From: Albumen Kevin Date: Wed, 26 Jun 2024 13:53:15 +0800 Subject: [PATCH 3/3] fix code --- .../main/java/org/apache/dubbo/config/spring/ReferenceBean.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java index cddf7f3a1f..3bbd3ce241 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java @@ -148,7 +148,7 @@ public class ReferenceBean private MutablePropertyValues propertyValues; // actual reference config - private ReferenceConfig referenceConfig; + private volatile ReferenceConfig referenceConfig; // ReferenceBeanManager private ReferenceBeanManager referenceBeanManager;