You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2021/09/08 05:11:27 UTC

[dubbo] branch 3.0 updated: [3.0] verify multi-instance method callback (#8670)

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

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


The following commit(s) were added to refs/heads/3.0 by this push:
     new 7004728  [3.0] verify multi-instance method callback (#8670)
7004728 is described below

commit 70047280e78812a450811e3e8720dbfda23c23b0
Author: zrlw <zr...@sina.com>
AuthorDate: Wed Sep 8 13:11:16 2021 +0800

    [3.0] verify multi-instance method callback (#8670)
    
    * verify multi-instance method callback
    
    * test method callback with all CPU cores
    
    * Update build-and-test-3.yml
---
 .../dubbo/config/spring/api/MethodCallback.java    |  24 +++-
 .../annotation/MethodConfigCallbackTest.java       |  52 +++++++--
 .../config/spring/impl/MethodCallbackImpl.java     | 123 ++++++++++++++++++---
 3 files changed, 168 insertions(+), 31 deletions(-)

diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/api/MethodCallback.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/api/MethodCallback.java
index 1eafa3b..50e60a6 100644
--- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/api/MethodCallback.java
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/api/MethodCallback.java
@@ -17,15 +17,27 @@
 package org.apache.dubbo.config.spring.api;
 
 public interface MethodCallback {
-    void oninvoke(String request);
+    void oninvoke1(String request);
 
-    void onreturn(String response, String request);
+    void onreturn1(String response, String request);
 
-    void onthrow(Throwable ex, String request);
+    void onthrow1(Throwable ex, String request);
 
-    String getOnInvoke();
+    void oninvoke2(String request);
 
-    String getOnReturn();
+    void onreturn2(String response, String request);
 
-    String getOnThrow();
+    void onthrow2(Throwable ex, String request);
+
+    String getOnInvoke1();
+
+    String getOnReturn1();
+
+    String getOnThrow1();
+
+    String getOnInvoke2();
+
+    String getOnReturn2();
+
+    String getOnThrow2();
 }
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/MethodConfigCallbackTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/MethodConfigCallbackTest.java
index 1f0c6bf..f6895f5 100644
--- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/MethodConfigCallbackTest.java
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/MethodConfigCallbackTest.java
@@ -73,20 +73,58 @@ public class MethodConfigCallbackTest {
     @Autowired
     private ConfigurableApplicationContext context;
 
-    @DubboReference(check = false,
+    @DubboReference(check = false, async = true,
         injvm = false, // Currently local call is not supported method callback cause by Injvm protocol is not supported ClusterFilter
         methods = {@Method(name = "sayHello",
-        oninvoke = "methodCallback.oninvoke",
-        onreturn = "methodCallback.onreturn",
-        onthrow = "methodCallback.onthrow")})
+        oninvoke = "methodCallback.oninvoke1",
+        onreturn = "methodCallback.onreturn1",
+        onthrow = "methodCallback.onthrow1")})
     private HelloService helloServiceMethodCallBack;
 
+    @DubboReference(check = false, async = true,
+            injvm = false, // Currently local call is not supported method callback cause by Injvm protocol is not supported ClusterFilter
+            methods = {@Method(name = "sayHello",
+            oninvoke = "methodCallback.oninvoke2",
+            onreturn = "methodCallback.onreturn2",
+            onthrow = "methodCallback.onthrow2")})
+    private HelloService helloServiceMethodCallBack2;
+
     @Test
     public void testMethodAnnotationCallBack() {
-        helloServiceMethodCallBack.sayHello("dubbo");
+        int threadCnt = Runtime.getRuntime().availableProcessors();
+        int callCnt = 2 * threadCnt;
+        for (int i = 0; i < threadCnt; i++) {
+            new Thread(() -> {
+                for (int j = 0; j < callCnt; j++) {
+                    helloServiceMethodCallBack.sayHello("dubbo");
+                    helloServiceMethodCallBack2.sayHello("dubbo(2)");
+                }
+            }).start();
+        }
+        int i = 0;
+        while (MethodCallbackImpl.cnt.get() < ( 2 * threadCnt * callCnt) && i < 50){
+            // wait for async callback finished
+            try {
+                i++;
+                Thread.sleep(100);
+            } catch (InterruptedException e) {
+            }
+        }
         MethodCallback notify = (MethodCallback) context.getBean("methodCallback");
-        Assertions.assertEquals("dubbo invoke success", notify.getOnInvoke());
-        Assertions.assertEquals("dubbo return success", notify.getOnReturn());
+        StringBuilder invoke1Builder = new StringBuilder();
+        StringBuilder invoke2Builder = new StringBuilder();
+        StringBuilder return1Builder = new StringBuilder();
+        StringBuilder return2Builder = new StringBuilder();
+        for (i = 0; i < threadCnt * callCnt; i++) {
+            invoke1Builder.append("dubbo invoke success!");
+            invoke2Builder.append("dubbo invoke success(2)!");
+            return1Builder.append("dubbo return success!");
+            return2Builder.append("dubbo return success(2)!");
+        }
+        Assertions.assertEquals(invoke1Builder.toString(), notify.getOnInvoke1());
+        Assertions.assertEquals(return1Builder.toString(), notify.getOnReturn1());
+        Assertions.assertEquals(invoke2Builder.toString(), notify.getOnInvoke2());
+        Assertions.assertEquals(return2Builder.toString(), notify.getOnReturn2());
     }
 
     @Configuration
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/impl/MethodCallbackImpl.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/impl/MethodCallbackImpl.java
index e457967..a23923e 100644
--- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/impl/MethodCallbackImpl.java
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/impl/MethodCallbackImpl.java
@@ -23,12 +23,18 @@ import org.springframework.core.env.Environment;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.support.TransactionSynchronizationManager;
 
+import java.util.concurrent.atomic.AtomicInteger;
+
 import javax.annotation.PostConstruct;
 
 public class MethodCallbackImpl implements MethodCallback {
-    private String onInvoke;
-    private String onReturn;
-    private String onThrow;
+    private String onInvoke1 = "";
+    private String onReturn1 = "";
+    private String onThrow1 = "";
+
+    private String onInvoke2 = "";
+    private String onReturn2 = "";
+    private String onThrow2 = "";
 
     @Autowired
     private Environment environment;
@@ -36,6 +42,8 @@ public class MethodCallbackImpl implements MethodCallback {
     @Autowired
     private ApplicationContext context;
 
+    public static AtomicInteger cnt = new AtomicInteger();
+
     @PostConstruct
     protected void init() {
         checkInjection();
@@ -43,53 +51,132 @@ public class MethodCallbackImpl implements MethodCallback {
 
     @Transactional(rollbackFor = Exception.class)
     @Override
-    public void oninvoke(String request) {
+    public void oninvoke1(String request) {
+        try {
+            checkInjection();
+            checkTranscation();
+            synchronized (this.onInvoke1) {
+                this.onInvoke1 += "dubbo invoke success!";
+            }
+        } catch (Exception e) {
+            synchronized (this.onInvoke1) {
+                this.onInvoke1 += e.toString();
+            }
+            throw e;
+        }
+    }
+
+    @Transactional(rollbackFor = Exception.class)
+    @Override
+    public void oninvoke2(String request) {
         try {
             checkInjection();
             checkTranscation();
-            this.onInvoke = "dubbo invoke success";
+            synchronized (this.onInvoke2) {
+                this.onInvoke2 += "dubbo invoke success(2)!";
+            }
         } catch (Exception e) {
-            this.onInvoke = e.toString();
+            synchronized (this.onInvoke2) {
+                this.onInvoke2 += e.toString();
+            }
             throw e;
         }
     }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public void onreturn(String response, String request) {
+    public void onreturn1(String response, String request) {
         try {
             checkInjection();
             checkTranscation();
-            this.onReturn = "dubbo return success";
+            synchronized (this.onReturn1) {
+                this.onReturn1 += "dubbo return success!";
+            }
         } catch (Exception e) {
-            this.onReturn = e.toString();
+            synchronized (this.onReturn1) {
+                this.onReturn1 += e.toString();
+            }
             throw e;
+        } finally {
+            cnt.incrementAndGet();
         }
     }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public void onthrow(Throwable ex, String request) {
+    public void onreturn2(String response, String request) {
         try {
             checkInjection();
             checkTranscation();
-            this.onThrow = "dubbo throw exception";
+            synchronized (this.onReturn2) {
+                this.onReturn2 += "dubbo return success(2)!";
+            }
         } catch (Exception e) {
-            this.onThrow = e.toString();
+            synchronized (this.onReturn2) {
+                this.onReturn2 += e.toString();
+            }
             throw e;
+        } finally {
+            cnt.incrementAndGet();
         }
     }
 
-    public String getOnInvoke() {
-        return this.onInvoke;
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void onthrow1(Throwable ex, String request) {
+        try {
+            checkInjection();
+            checkTranscation();
+            synchronized (this.onThrow1) {
+                this.onThrow1 += "dubbo throw exception!";
+            }
+        } catch (Exception e) {
+            synchronized (this.onThrow1) {
+                this.onThrow1 += e.toString();
+            }
+            throw e;
+        }
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void onthrow2(Throwable ex, String request) {
+        try {
+            checkInjection();
+            checkTranscation();
+            synchronized (this.onThrow2) {
+                this.onThrow2 += "dubbo throw exception(2)!";
+            }
+        } catch (Exception e) {
+            synchronized (this.onThrow2) {
+                this.onThrow2 += e.toString();
+            }
+            throw e;
+        }
+    }
+
+    public String getOnInvoke1() {
+        return this.onInvoke1;
+    }
+
+    public String getOnReturn1() {
+        return this.onReturn1;
+    }
+
+    public String getOnThrow1() {
+        return this.onThrow1;
+    }
+
+    public String getOnInvoke2() {
+        return this.onInvoke2;
     }
 
-    public String getOnReturn() {
-        return this.onReturn;
+    public String getOnReturn2() {
+        return this.onReturn2;
     }
 
-    public String getOnThrow() {
-        return this.onThrow;
+    public String getOnThrow2() {
+        return this.onThrow2;
     }
 
     private void checkInjection() {