You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/03/15 11:12:20 UTC

[incubator-servicecomb-java-chassis] 04/05: SCB-374 [WIP] invocation from rest transport publish event

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

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit aa6886df65704d260a72a9bc25878ff9fb9f29a9
Author: wujimin <wu...@huawei.com>
AuthorDate: Sat Mar 10 11:12:32 2018 +0800

    SCB-374 [WIP] invocation from rest transport publish event
---
 .../common/rest/AbstractRestInvocation.java        | 25 +++----
 .../common/rest/RestProducerInvocation.java        |  5 +-
 .../common/rest/TestAbstractRestInvocation.java    | 76 ++++++++++++++++++----
 .../servicecomb/edge/core/EdgeInvocation.java      |  4 +-
 .../servicecomb/edge/core/TestEdgeInvocation.java  |  3 +-
 5 files changed, 79 insertions(+), 34 deletions(-)

diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
index 7d8ab2c..6e6f876 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/AbstractRestInvocation.java
@@ -37,12 +37,9 @@ import org.apache.servicecomb.core.Const;
 import org.apache.servicecomb.core.Invocation;
 import org.apache.servicecomb.core.definition.MicroserviceMeta;
 import org.apache.servicecomb.core.definition.OperationMeta;
-import org.apache.servicecomb.core.metrics.InvocationStartedEvent;
-import org.apache.servicecomb.foundation.common.event.EventBus;
 import org.apache.servicecomb.foundation.common.utils.JsonUtils;
 import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
 import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
-import org.apache.servicecomb.swagger.invocation.InvocationType;
 import org.apache.servicecomb.swagger.invocation.Response;
 import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
 import org.slf4j.Logger;
@@ -109,12 +106,10 @@ public abstract class AbstractRestInvocation {
   }
 
   protected void scheduleInvocation() {
+    createInvocation();
+    invocation.onStart();
     OperationMeta operationMeta = restOperationMeta.getOperationMeta();
 
-    InvocationStartedEvent startedEvent = new InvocationStartedEvent(operationMeta.getMicroserviceQualifiedName(),
-        InvocationType.PRODUCER, System.nanoTime());
-    EventBus.getInstance().triggerEvent(startedEvent);
-
     operationMeta.getExecutor().execute(() -> {
       synchronized (this.requestEx) {
         try {
@@ -127,7 +122,7 @@ public abstract class AbstractRestInvocation {
             return;
           }
 
-          runOnExecutor(startedEvent);
+          runOnExecutor();
         } catch (Throwable e) {
           LOGGER.error("rest server onRequest error", e);
           sendFailResponse(e);
@@ -136,19 +131,16 @@ public abstract class AbstractRestInvocation {
     });
   }
 
-  protected void runOnExecutor(InvocationStartedEvent startedEvent) {
-    createInvocation(null);
-
-    //立刻设置开始时间,否则Finished时无法计算TotalTime
-    invocation.setStartTime(startedEvent.getStartedTime());
-    invocation.triggerStartExecutionEvent();
+  protected void runOnExecutor() {
+    invocation.onStartExecute();
 
     invoke();
   }
 
   protected abstract OperationLocator locateOperation(ServicePathManager servicePathManager);
 
-  protected abstract void createInvocation(Object[] args);
+  // create a invocation without args setted
+  protected abstract void createInvocation();
 
   public void invoke() {
     try {
@@ -184,8 +176,6 @@ public abstract class AbstractRestInvocation {
   protected void doInvoke() throws Throwable {
     invocation.next(resp -> {
       sendResponseQuietly(resp);
-
-      invocation.triggerFinishedEvent(resp.getStatusCode());
     });
   }
 
@@ -207,6 +197,7 @@ public abstract class AbstractRestInvocation {
           e);
     } finally {
       requestEx.getAsyncContext().complete();
+      invocation.onFinish(response);
     }
   }
 
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestProducerInvocation.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestProducerInvocation.java
index cd5e5f3..2e0cbe9 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestProducerInvocation.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestProducerInvocation.java
@@ -50,6 +50,7 @@ public class RestProducerInvocation extends AbstractRestInvocation {
       return;
     }
 
+    createInvocation();
     scheduleInvocation();
   }
 
@@ -70,9 +71,9 @@ public class RestProducerInvocation extends AbstractRestInvocation {
   }
 
   @Override
-  protected void createInvocation(Object[] args) {
+  protected void createInvocation() {
     this.invocation = InvocationFactory.forProvider(transport.getEndpoint(),
         restOperationMeta.getOperationMeta(),
-        args);
+        null);
   }
 }
diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
index aeac2ec..291c731 100644
--- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestAbstractRestInvocation.java
@@ -39,27 +39,33 @@ import org.apache.servicecomb.core.definition.MicroserviceMeta;
 import org.apache.servicecomb.core.definition.MicroserviceMetaManager;
 import org.apache.servicecomb.core.definition.OperationMeta;
 import org.apache.servicecomb.core.definition.SchemaMeta;
+import org.apache.servicecomb.core.event.InvocationFinishEvent;
+import org.apache.servicecomb.core.event.InvocationStartEvent;
 import org.apache.servicecomb.core.executor.ReactiveExecutor;
-import org.apache.servicecomb.core.metrics.InvocationStartedEvent;
 import org.apache.servicecomb.core.provider.consumer.ReferenceConfig;
+import org.apache.servicecomb.foundation.common.event.EventManager;
 import org.apache.servicecomb.foundation.common.utils.JsonUtils;
 import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
 import org.apache.servicecomb.foundation.vertx.http.AbstractHttpServletRequest;
 import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
 import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
 import org.apache.servicecomb.swagger.invocation.AsyncResponse;
-import org.apache.servicecomb.swagger.invocation.InvocationType;
 import org.apache.servicecomb.swagger.invocation.Response;
 import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
 import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
 import org.apache.servicecomb.swagger.invocation.response.Headers;
 import org.hamcrest.Matchers;
+import org.junit.AfterClass;
 import org.junit.Assert;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 
+import com.google.common.eventbus.EventBus;
+import com.google.common.eventbus.Subscribe;
+
 import io.vertx.core.buffer.Buffer;
 import mockit.Deencapsulation;
 import mockit.Expectations;
@@ -104,13 +110,23 @@ public class TestAbstractRestInvocation {
     }
 
     @Override
-    protected void createInvocation(Object[] args) {
+    protected void createInvocation() {
       this.invocation = TestAbstractRestInvocation.this.invocation;
     }
   }
 
   AbstractRestInvocation restInvocation = new AbstractRestInvocationForTest();
 
+  @BeforeClass
+  public static void classSetup() {
+    EventManager.eventBus = new EventBus();
+  }
+
+  @AfterClass
+  public static void classTeardown() {
+    EventManager.eventBus = new EventBus();
+  }
+
   @Before
   public void setup() {
     invocation = new Invocation(endpoint, operationMeta, swaggerArguments);
@@ -369,6 +385,15 @@ public class TestAbstractRestInvocation {
 
   @Test
   public void sendResponseQuietlyNormal(@Mocked Response response) {
+    Holder<InvocationFinishEvent> eventHolder = new Holder<>();
+    Object subscriber = new Object() {
+      @Subscribe
+      public void onFinished(InvocationFinishEvent event) {
+        eventHolder.value = event;
+      }
+    };
+    EventManager.register(subscriber);
+
     Holder<Response> result = new Holder<>();
     restInvocation = new AbstractRestInvocationForTest() {
       @Override
@@ -384,6 +409,9 @@ public class TestAbstractRestInvocation {
 
     restInvocation.sendResponseQuietly(response);
 
+    EventManager.unregister(subscriber);
+
+    Assert.assertSame(invocation, eventHolder.value.getInvocation());
     Assert.assertSame(response, result.value);
   }
 
@@ -636,8 +664,6 @@ public class TestAbstractRestInvocation {
         result = operationMeta;
         operationMeta.getExecutor();
         result = executor;
-        operationMeta.getMicroserviceQualifiedName();
-        result = "sayHi";
       }
     };
 
@@ -645,7 +671,7 @@ public class TestAbstractRestInvocation {
     Error error = new Error("run on executor");
     restInvocation = new AbstractRestInvocationForTest() {
       @Override
-      protected void runOnExecutor(InvocationStartedEvent startedEvent) {
+      protected void runOnExecutor() {
         throw error;
       }
 
@@ -682,7 +708,7 @@ public class TestAbstractRestInvocation {
 
     restInvocation = new AbstractRestInvocationForTest() {
       @Override
-      protected void runOnExecutor(InvocationStartedEvent startedEvent) {
+      protected void runOnExecutor() {
         throw new Error("run on executor");
       }
 
@@ -700,6 +726,22 @@ public class TestAbstractRestInvocation {
 
   @Test
   public void scheduleInvocationNormal(@Mocked OperationMeta operationMeta) {
+    long time = 123;
+    new MockUp<System>() {
+      @Mock
+      long nanoTime() {
+        return time;
+      }
+    };
+    Holder<InvocationStartEvent> eventHolder = new Holder<>();
+    Object subscriber = new Object() {
+      @Subscribe
+      public void onStart(InvocationStartEvent event) {
+        eventHolder.value = event;
+      }
+    };
+    EventManager.register(subscriber);
+
     Executor executor = new ReactiveExecutor();
     requestEx = new AbstractHttpServletRequest() {
     };
@@ -710,15 +752,13 @@ public class TestAbstractRestInvocation {
         result = operationMeta;
         operationMeta.getExecutor();
         result = executor;
-        operationMeta.getMicroserviceQualifiedName();
-        result = "sayHi";
       }
     };
 
     Holder<Boolean> result = new Holder<>();
     restInvocation = new AbstractRestInvocationForTest() {
       @Override
-      protected void runOnExecutor(InvocationStartedEvent startedEvent) {
+      protected void runOnExecutor() {
         result.value = true;
       }
     };
@@ -726,12 +766,23 @@ public class TestAbstractRestInvocation {
     restInvocation.restOperationMeta = restOperation;
 
     restInvocation.scheduleInvocation();
+    EventManager.unregister(subscriber);
 
     Assert.assertTrue(result.value);
+    Assert.assertEquals(time, invocation.getStartTime());
+    Assert.assertSame(invocation, eventHolder.value.getInvocation());
   }
 
   @Test
   public void runOnExecutor() {
+    long time = 123;
+    new MockUp<System>() {
+      @Mock
+      long nanoTime() {
+        return time;
+      }
+    };
+
     Holder<Boolean> result = new Holder<>();
     restInvocation = new AbstractRestInvocationForTest() {
       @Override
@@ -739,12 +790,15 @@ public class TestAbstractRestInvocation {
         result.value = true;
       }
     };
+    restInvocation.createInvocation();
     restInvocation.requestEx = requestEx;
     restInvocation.restOperationMeta = restOperation;
 
-    restInvocation.runOnExecutor(new InvocationStartedEvent("", InvocationType.PRODUCER, System.nanoTime()));
+    restInvocation.runOnExecutor();
+
     Assert.assertTrue(result.value);
     Assert.assertSame(invocation, restInvocation.invocation);
+    Assert.assertEquals(time, invocation.getStartExecutionTime());
   }
 
   @Test
diff --git a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeInvocation.java b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeInvocation.java
index f764d19..e898887 100644
--- a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeInvocation.java
+++ b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeInvocation.java
@@ -112,7 +112,7 @@ public class EdgeInvocation extends AbstractRestInvocation {
   }
 
   @Override
-  protected void createInvocation(Object[] args) {
+  protected void createInvocation() {
     ReferenceConfig referenceConfig = new ReferenceConfig();
     referenceConfig.setMicroserviceMeta(latestMicroserviceVersionMeta.getMicroserviceMeta());
     referenceConfig.setMicroserviceVersionRule(microserviceVersionRule.getVersionRule().getVersionRule());
@@ -120,7 +120,7 @@ public class EdgeInvocation extends AbstractRestInvocation {
 
     this.invocation = InvocationFactory.forConsumer(referenceConfig,
         restOperationMeta.getOperationMeta(),
-        args);
+        null);
     this.invocation.setResponseExecutor(new ReactiveResponseExecutor());
   }
 }
diff --git a/edge/edge-core/src/test/java/org/apache/servicecomb/edge/core/TestEdgeInvocation.java b/edge/edge-core/src/test/java/org/apache/servicecomb/edge/core/TestEdgeInvocation.java
index 81160b4..5cbadbf 100644
--- a/edge/edge-core/src/test/java/org/apache/servicecomb/edge/core/TestEdgeInvocation.java
+++ b/edge/edge-core/src/test/java/org/apache/servicecomb/edge/core/TestEdgeInvocation.java
@@ -217,7 +217,6 @@ public class TestEdgeInvocation {
     edgeInvocation.microserviceVersionRule = microserviceVersionRule;
     Deencapsulation.setField(edgeInvocation, "restOperationMeta", restOperationMeta);
 
-    Object[] args = new Object[] {};
     new Expectations(RegistryUtils.class) {
       {
         RegistryUtils.getMicroservice();
@@ -225,7 +224,7 @@ public class TestEdgeInvocation {
       }
     };
 
-    edgeInvocation.createInvocation(args);
+    edgeInvocation.createInvocation();
     Invocation invocation = Deencapsulation.getField(edgeInvocation, "invocation");
     Assert.assertThat(invocation.getResponseExecutor(), Matchers.instanceOf(ReactiveResponseExecutor.class));
   }

-- 
To stop receiving notification emails like this one, please contact
liubao@apache.org.