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/09/12 01:41:21 UTC

[incubator-servicecomb-java-chassis] 05/13: [SCB-881] make the invocation start time more precise

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 fe51f84a76b45cb99056a3aff0a0a693fedee3c6
Author: wujimin <wu...@huawei.com>
AuthorDate: Fri Sep 7 14:12:10 2018 +0800

    [SCB-881] make the invocation start time more precise
---
 .../common/rest/AbstractRestInvocation.java        |  9 ++-
 .../org/apache/servicecomb/core/Invocation.java    |  8 +--
 .../core/provider/consumer/InvokerUtils.java       |  4 +-
 .../apache/servicecomb/core/TestInvocation.java    | 10 ++--
 .../core/provider/consumer/TestInvokerUtils.java   | 68 +++++++++++++++++++---
 demo/perf/pom.xml                                  |  2 +-
 .../transport/highway/HighwayServerInvoke.java     |  5 +-
 .../transport/highway/TestHighwayServerInvoke.java | 10 ++++
 8 files changed, 92 insertions(+), 24 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 8125e94..708ee65 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
@@ -49,11 +49,12 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public abstract class AbstractRestInvocation {
-
   private static final Logger LOGGER = LoggerFactory.getLogger(AbstractRestInvocation.class);
 
   public static final String UNKNOWN_OPERATION_ID = "UNKNOWN_OPERATION";
 
+  protected long start;
+
   protected RestOperationMeta restOperationMeta;
 
   protected Invocation invocation;
@@ -66,6 +67,10 @@ public abstract class AbstractRestInvocation {
 
   protected List<HttpServerFilter> httpServerFilters = Collections.emptyList();
 
+  public AbstractRestInvocation() {
+    this.start = System.nanoTime();
+  }
+
   public void setHttpServerFilters(List<HttpServerFilter> httpServerFilters) {
     this.httpServerFilters = httpServerFilters;
   }
@@ -118,7 +123,7 @@ public abstract class AbstractRestInvocation {
       return;
     }
 
-    invocation.onStart(requestEx);
+    invocation.onStart(requestEx, start);
     OperationMeta operationMeta = restOperationMeta.getOperationMeta();
 
     operationMeta.getExecutor().execute(() -> {
diff --git a/core/src/main/java/org/apache/servicecomb/core/Invocation.java b/core/src/main/java/org/apache/servicecomb/core/Invocation.java
index 3ee326e..f8138f1 100644
--- a/core/src/main/java/org/apache/servicecomb/core/Invocation.java
+++ b/core/src/main/java/org/apache/servicecomb/core/Invocation.java
@@ -254,15 +254,15 @@ public class Invocation extends SwaggerInvocation {
     addContext(traceIdGenerator.getTraceIdKeyName(), traceIdGenerator.generate());
   }
 
-  public void onStart() {
-    invocationStageTrace.start(System.nanoTime());
+  public void onStart(long start) {
+    invocationStageTrace.start(start);
     initTraceId();
     EventManager.post(new InvocationStartEvent(this));
   }
 
-  public void onStart(HttpServletRequestEx requestEx) {
+  public void onStart(HttpServletRequestEx requestEx, long start) {
     this.requestEx = requestEx;
-    onStart();
+    onStart(start);
   }
 
   public void onExecuteStart() {
diff --git a/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java b/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java
index 2fef648..01d6213 100644
--- a/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java
+++ b/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java
@@ -65,7 +65,7 @@ public final class InvokerUtils {
    */
   public static Response innerSyncInvoke(Invocation invocation) {
     try {
-      invocation.onStart();
+      invocation.onStart(null, System.nanoTime());
       SyncResponseExecutor respExecutor = new SyncResponseExecutor();
       invocation.setResponseExecutor(respExecutor);
 
@@ -92,7 +92,7 @@ public final class InvokerUtils {
    */
   public static void reactiveInvoke(Invocation invocation, AsyncResponse asyncResp) {
     try {
-      invocation.onStart();
+      invocation.onStart(null, System.nanoTime());
       invocation.setSync(false);
 
       ReactiveResponseExecutor respExecutor = new ReactiveResponseExecutor();
diff --git a/core/src/test/java/org/apache/servicecomb/core/TestInvocation.java b/core/src/test/java/org/apache/servicecomb/core/TestInvocation.java
index 8fa279a..102f5ae 100644
--- a/core/src/test/java/org/apache/servicecomb/core/TestInvocation.java
+++ b/core/src/test/java/org/apache/servicecomb/core/TestInvocation.java
@@ -91,7 +91,7 @@ public class TestInvocation {
     EventManager.register(subscriber);
 
     Invocation invocation = new Invocation(endpoint, operationMeta, swaggerArguments);
-    invocation.onStart();
+    invocation.onStart(currentNanoTime);
 
     Assert.assertSame(invocation, result.value);
     Assert.assertEquals(currentNanoTime, invocation.getInvocationStageTrace().getStart());
@@ -158,7 +158,7 @@ public class TestInvocation {
     Invocation invocation = new Invocation(referenceConfig, operationMeta, swaggerArguments);
     invocation.addContext(Const.TRACE_ID_NAME, "abc");
 
-    invocation.onStart();
+    invocation.onStart(0);
 
     Assert.assertEquals("abc", invocation.getTraceId());
     Assert.assertEquals("abc", invocation.getTraceId(Const.TRACE_ID_NAME));
@@ -175,7 +175,7 @@ public class TestInvocation {
     };
     Invocation invocation = new Invocation(referenceConfig, operationMeta, swaggerArguments);
 
-    invocation.onStart();
+    invocation.onStart(0);
 
     Assert.assertEquals("abc", invocation.getTraceId());
     Assert.assertEquals("abc", invocation.getTraceId(Const.TRACE_ID_NAME));
@@ -191,7 +191,7 @@ public class TestInvocation {
     };
     Invocation invocation = new Invocation(endpoint, operationMeta, swaggerArguments);
 
-    invocation.onStart(requestEx);
+    invocation.onStart(requestEx, 0);
 
     Assert.assertEquals("abc", invocation.getTraceId());
     Assert.assertEquals("abc", invocation.getTraceId(Const.TRACE_ID_NAME));
@@ -208,7 +208,7 @@ public class TestInvocation {
     };
     Invocation invocation = new Invocation(endpoint, operationMeta, swaggerArguments);
 
-    invocation.onStart(requestEx);
+    invocation.onStart(requestEx, 0);
 
     Assert.assertEquals("abc", invocation.getTraceId());
     Assert.assertEquals("abc", invocation.getTraceId(Const.TRACE_ID_NAME));
diff --git a/core/src/test/java/org/apache/servicecomb/core/provider/consumer/TestInvokerUtils.java b/core/src/test/java/org/apache/servicecomb/core/provider/consumer/TestInvokerUtils.java
index 04e32b2..db96482 100644
--- a/core/src/test/java/org/apache/servicecomb/core/provider/consumer/TestInvokerUtils.java
+++ b/core/src/test/java/org/apache/servicecomb/core/provider/consumer/TestInvokerUtils.java
@@ -17,15 +17,19 @@
 
 package org.apache.servicecomb.core.provider.consumer;
 
+import java.util.Arrays;
+
 import javax.xml.ws.Holder;
 
 import org.apache.servicecomb.core.CseContext;
+import org.apache.servicecomb.core.Handler;
 import org.apache.servicecomb.core.Invocation;
 import org.apache.servicecomb.core.SCBEngine;
 import org.apache.servicecomb.core.SCBStatus;
 import org.apache.servicecomb.core.definition.OperationMeta;
 import org.apache.servicecomb.core.definition.SchemaMeta;
 import org.apache.servicecomb.core.invocation.InvocationFactory;
+import org.apache.servicecomb.core.invocation.InvocationStageTrace;
 import org.apache.servicecomb.swagger.invocation.AsyncResponse;
 import org.apache.servicecomb.swagger.invocation.Response;
 import org.apache.servicecomb.swagger.invocation.context.ContextUtils;
@@ -34,6 +38,7 @@ import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
 import org.hamcrest.Matchers;
 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;
@@ -49,8 +54,33 @@ public class TestInvokerUtils {
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
+  @Mocked
+  ReferenceConfig referenceConfig;
+
+  @Mocked
+  SchemaMeta schemaMeta;
+
+  @Mocked
+  OperationMeta operationMeta;
+
+  Invocation invocation;
+
+  static Object invokeResult;
+
   SCBEngine scbEngine = new SCBEngine();
 
+  static long nanoTime = 1;
+
+  @BeforeClass
+  public static void classSetup() {
+    new MockUp<System>() {
+      @Mock
+      long nanoTime() {
+        return nanoTime;
+      }
+    };
+  }
+
   @Before
   public void setup() {
     new MockUp<SCBEngine>() {
@@ -60,34 +90,53 @@ public class TestInvokerUtils {
       }
     };
     scbEngine.setStatus(SCBStatus.UP);
+
+    new Expectations() {
+      {
+        operationMeta.getSchemaMeta();
+        result = schemaMeta;
+        schemaMeta.getConsumerHandlerChain();
+        result = Arrays.asList((Handler) (i, ar) -> {
+          System.out.println(invokeResult);
+          ar.success(invokeResult);
+        });
+      }
+    };
+    invocation = new Invocation(referenceConfig, operationMeta, new Object[] {});
   }
 
   @Test
   public void testSyncInvokeInvocationWithException() {
     Invocation invocation = Mockito.mock(Invocation.class);
+    InvocationStageTrace stageTrace = new InvocationStageTrace(invocation);
+    Mockito.when(invocation.getInvocationStageTrace()).thenReturn(stageTrace);
 
     Response response = Mockito.mock(Response.class);
     new MockUp<SyncResponseExecutor>() {
       @Mock
       public Response waitResponse() {
-        return Mockito.mock(Response.class);
+        return response;
       }
     };
-    Mockito.when(response.isSuccessed()).thenReturn(true);
+    Mockito.when(response.isSuccessed()).thenReturn(false);
     OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
     Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
     Mockito.when(operationMeta.getMicroserviceQualifiedName()).thenReturn("test");
 
-    try {
-      InvokerUtils.syncInvoke(invocation);
-    } catch (InvocationException e) {
-      Assert.assertEquals(490, e.getStatusCode());
-    }
+    expectedException.expect(InvocationException.class);
+    expectedException.expect(Matchers.hasProperty("statusCode", Matchers.is(490)));
+    InvokerUtils.syncInvoke(invocation);
+  }
+
+  @Test
+  public void testSyncInvokeNormal() {
+    invokeResult = 1;
+    Assert.assertEquals(1, (int) InvokerUtils.syncInvoke(invocation));
+    Assert.assertEquals(1, invocation.getInvocationStageTrace().getStart());
   }
 
   @Test
-  public void testReactiveInvoke(@Mocked Invocation invocation, @Mocked InvocationContext parentContext,
-      @Mocked Response response) {
+  public void testReactiveInvoke(@Mocked InvocationContext parentContext, @Mocked Response response) {
     new MockUp<Invocation>(invocation) {
       @Mock
       InvocationContext getParentContext() {
@@ -105,6 +154,7 @@ public class TestInvokerUtils {
 
     Assert.assertNull(ContextUtils.getInvocationContext());
     Assert.assertSame(parentContext, holder.value);
+    Assert.assertEquals(1, invocation.getInvocationStageTrace().getStart());
   }
 
   @SuppressWarnings("deprecation")
diff --git a/demo/perf/pom.xml b/demo/perf/pom.xml
index faf3d44..92efc6d 100644
--- a/demo/perf/pom.xml
+++ b/demo/perf/pom.xml
@@ -52,7 +52,7 @@
     <dependency>
       <groupId>io.vertx</groupId>
       <artifactId>vertx-redis-client</artifactId>
-      <version>3.5.0</version>
+      <version>${vertx.version}</version>
     </dependency>
     <dependency>
       <groupId>org.apache.servicecomb</groupId>
diff --git a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayServerInvoke.java b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayServerInvoke.java
index e55072d..881a24f 100644
--- a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayServerInvoke.java
+++ b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayServerInvoke.java
@@ -64,11 +64,14 @@ public class HighwayServerInvoke {
 
   Invocation invocation;
 
+  protected long start;
+
   public HighwayServerInvoke() {
     this(null, null);
   }
 
   public HighwayServerInvoke(Endpoint endpoint, ProtobufFeature protobufFeature) {
+    this.start = System.nanoTime();
     this.endpoint = endpoint;
     this.protobufFeature = protobufFeature;
   }
@@ -175,7 +178,7 @@ public class HighwayServerInvoke {
       invocation = InvocationFactory.forProvider(endpoint,
           operationProtobuf.getOperationMeta(),
           null);
-      invocation.onStart();
+      invocation.onStart(null, start);
       operationMeta.getExecutor().execute(() -> runInExecutor());
     } catch (IllegalStateException e) {
       sendResponse(header.getContext(), Response.providerFailResp(e));
diff --git a/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/TestHighwayServerInvoke.java b/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/TestHighwayServerInvoke.java
index a62919e..d27a0e1 100644
--- a/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/TestHighwayServerInvoke.java
+++ b/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/TestHighwayServerInvoke.java
@@ -64,10 +64,19 @@ public class TestHighwayServerInvoke {
 
   private SocketAddress socketAddress;
 
+  static long nanoTime = 1;
+
   @BeforeClass
   public static void classSetup() {
     EventManager.eventBus = new EventBus();
     SCBEngine.getInstance().setStatus(SCBStatus.UP);
+
+    new MockUp<System>() {
+      @Mock
+      long nanoTime() {
+        return nanoTime;
+      }
+    };
   }
 
   @AfterClass
@@ -155,5 +164,6 @@ public class TestHighwayServerInvoke {
     Assert.assertSame(highwayServerInvoke.invocation, startHolder.value.getInvocation());
     Assert.assertSame(highwayServerInvoke.invocation, finishHolder.value.getInvocation());
     Assert.assertTrue(highwayServerInvoke.invocation.getInvocationStageTrace().getStartExecution() != 0);
+    Assert.assertEquals(1, highwayServerInvoke.invocation.getInvocationStageTrace().getStart());
   }
 }