You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2018/01/02 10:42:31 UTC

[incubator-servicecomb-java-chassis] 02/05: SCB-126 change event trigger from client to InvokerUtils

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

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

commit 8f2d6cce7a4a7df17e35eedc17b5f3bbdd312abf
Author: zhengyangyong <ya...@huawei.com>
AuthorDate: Wed Dec 27 13:05:23 2017 +0800

    SCB-126 change event trigger from client to InvokerUtils
    
    Signed-off-by: zhengyangyong <ya...@huawei.com>
---
 .../core/provider/consumer/InvokerUtils.java       | 16 +++++++
 .../transport/highway/HighwayClient.java           | 49 +++++++++-------------
 .../rest/client/http/VertxHttpMethod.java          | 10 -----
 3 files changed, 36 insertions(+), 39 deletions(-)

diff --git a/core/src/main/java/io/servicecomb/core/provider/consumer/InvokerUtils.java b/core/src/main/java/io/servicecomb/core/provider/consumer/InvokerUtils.java
index c142493..3005ce1 100644
--- a/core/src/main/java/io/servicecomb/core/provider/consumer/InvokerUtils.java
+++ b/core/src/main/java/io/servicecomb/core/provider/consumer/InvokerUtils.java
@@ -23,7 +23,10 @@ import org.slf4j.LoggerFactory;
 import io.servicecomb.core.Invocation;
 import io.servicecomb.core.definition.SchemaMeta;
 import io.servicecomb.core.invocation.InvocationFactory;
+import io.servicecomb.core.metrics.InvocationStartedEvent;
+import io.servicecomb.foundation.common.utils.EventUtils;
 import io.servicecomb.swagger.invocation.AsyncResponse;
+import io.servicecomb.swagger.invocation.InvocationType;
 import io.servicecomb.swagger.invocation.Response;
 import io.servicecomb.swagger.invocation.exception.ExceptionFactory;
 import io.servicecomb.swagger.invocation.exception.InvocationException;
@@ -61,6 +64,7 @@ public final class InvokerUtils {
 
   public static Response innerSyncInvoke(Invocation invocation) {
     try {
+      triggerStartedEvent(invocation);
       SyncResponseExecutor respExecutor = new SyncResponseExecutor();
       invocation.setResponseExecutor(respExecutor);
 
@@ -72,11 +76,14 @@ public final class InvokerUtils {
           String.format("invoke failed, %s", invocation.getOperationMeta().getMicroserviceQualifiedName());
       LOGGER.debug(msg, e);
       return Response.createConsumerFail(e);
+    } finally {
+      invocation.triggerFinishedEvent();
     }
   }
 
   public static void reactiveInvoke(Invocation invocation, AsyncResponse asyncResp) {
     try {
+      triggerStartedEvent(invocation);
       ReactiveResponseExecutor respExecutor = new ReactiveResponseExecutor();
       invocation.setResponseExecutor(respExecutor);
 
@@ -84,6 +91,8 @@ public final class InvokerUtils {
     } catch (Throwable e) {
       LOGGER.error("invoke failed, {}", invocation.getOperationMeta().getMicroserviceQualifiedName());
       asyncResp.consumerFail(e);
+    } finally {
+      invocation.triggerFinishedEvent();
     }
   }
 
@@ -91,4 +100,11 @@ public final class InvokerUtils {
   public static Object invoke(Invocation invocation) {
     return syncInvoke(invocation);
   }
+
+  private static void triggerStartedEvent(Invocation invocation) {
+    long startTime = System.nanoTime();
+    EventUtils.triggerEvent(new InvocationStartedEvent(invocation.getMicroserviceQualifiedName(),
+        InvocationType.CONSUMER, startTime));
+    invocation.setStartTime(startTime);
+  }
 }
diff --git a/transports/transport-highway/src/main/java/io/servicecomb/transport/highway/HighwayClient.java b/transports/transport-highway/src/main/java/io/servicecomb/transport/highway/HighwayClient.java
index 967fc1f..a2be8f2 100644
--- a/transports/transport-highway/src/main/java/io/servicecomb/transport/highway/HighwayClient.java
+++ b/transports/transport-highway/src/main/java/io/servicecomb/transport/highway/HighwayClient.java
@@ -26,9 +26,7 @@ import io.servicecomb.codec.protobuf.definition.OperationProtobuf;
 import io.servicecomb.codec.protobuf.definition.ProtobufManager;
 import io.servicecomb.core.Invocation;
 import io.servicecomb.core.definition.OperationMeta;
-import io.servicecomb.core.metrics.InvocationStartedEvent;
 import io.servicecomb.core.transport.AbstractTransport;
-import io.servicecomb.foundation.common.utils.EventUtils;
 import io.servicecomb.foundation.ssl.SSLCustom;
 import io.servicecomb.foundation.ssl.SSLOption;
 import io.servicecomb.foundation.ssl.SSLOptionFactory;
@@ -37,7 +35,6 @@ import io.servicecomb.foundation.vertx.VertxUtils;
 import io.servicecomb.foundation.vertx.client.ClientPoolManager;
 import io.servicecomb.foundation.vertx.client.tcp.TcpClientConfig;
 import io.servicecomb.swagger.invocation.AsyncResponse;
-import io.servicecomb.swagger.invocation.InvocationType;
 import io.servicecomb.swagger.invocation.Response;
 import io.vertx.core.DeploymentOptions;
 import io.vertx.core.Vertx;
@@ -69,7 +66,11 @@ public class HighwayClient {
   private TcpClientConfig createTcpClientConfig() {
     TcpClientConfig tcpClientConfig = new TcpClientConfig();
     DynamicLongProperty prop = AbstractTransport.getRequestTimeoutProperty();
-    prop.addCallback(() -> tcpClientConfig.setRequestTimeoutMillis(prop.get()));
+    prop.addCallback(new Runnable() {
+      public void run() {
+        tcpClientConfig.setRequestTimeoutMillis(prop.get());
+      }
+    });
     tcpClientConfig.setRequestTimeoutMillis(prop.get());
 
     if (this.sslEnabled) {
@@ -88,12 +89,6 @@ public class HighwayClient {
   }
 
   public void send(Invocation invocation, AsyncResponse asyncResp) throws Exception {
-
-    long startTime = System.nanoTime();
-    EventUtils.triggerEvent(new InvocationStartedEvent(invocation.getMicroserviceQualifiedName(),
-        InvocationType.CONSUMER, startTime));
-    invocation.setStartTime(startTime);
-
     HighwayClientConnectionPool tcpClientPool = clientMgr.findThreadBindClientPool();
 
     OperationMeta operationMeta = invocation.getOperationMeta();
@@ -105,26 +100,22 @@ public class HighwayClient {
     tcpClientPool.send(tcpClient, clientPackage, ar -> {
       // 此时是在网络线程中,转换线程
       invocation.getResponseExecutor().execute(() -> {
+        if (ar.failed()) {
+          // 只会是本地异常
+          asyncResp.consumerFail(ar.cause());
+          return;
+        }
+
+        // 处理应答
         try {
-          if (ar.failed()) {
-            // 只会是本地异常
-            asyncResp.consumerFail(ar.cause());
-            return;
-          }
-
-          // 处理应答
-          try {
-            Response response =
-                HighwayCodec.decodeResponse(invocation,
-                    operationProtobuf,
-                    ar.result(),
-                    tcpClient.getProtobufFeature());
-            asyncResp.complete(response);
-          } catch (Throwable e) {
-            asyncResp.consumerFail(e);
-          }
-        } finally {
-          invocation.triggerFinishedEvent();
+          Response response =
+              HighwayCodec.decodeResponse(invocation,
+                  operationProtobuf,
+                  ar.result(),
+                  tcpClient.getProtobufFeature());
+          asyncResp.complete(response);
+        } catch (Throwable e) {
+          asyncResp.consumerFail(e);
         }
       });
     });
diff --git a/transports/transport-rest/transport-rest-client/src/main/java/io/servicecomb/transport/rest/client/http/VertxHttpMethod.java b/transports/transport-rest/transport-rest-client/src/main/java/io/servicecomb/transport/rest/client/http/VertxHttpMethod.java
index c15866e..b432cc8 100644
--- a/transports/transport-rest/transport-rest-client/src/main/java/io/servicecomb/transport/rest/client/http/VertxHttpMethod.java
+++ b/transports/transport-rest/transport-rest-client/src/main/java/io/servicecomb/transport/rest/client/http/VertxHttpMethod.java
@@ -30,11 +30,9 @@ import io.servicecomb.common.rest.definition.RestOperationMeta;
 import io.servicecomb.common.rest.filter.HttpClientFilter;
 import io.servicecomb.core.Invocation;
 import io.servicecomb.core.definition.OperationMeta;
-import io.servicecomb.core.metrics.InvocationStartedEvent;
 import io.servicecomb.core.transport.AbstractTransport;
 import io.servicecomb.foundation.common.net.IpPort;
 import io.servicecomb.foundation.common.net.URIEndpointObject;
-import io.servicecomb.foundation.common.utils.EventUtils;
 import io.servicecomb.foundation.common.utils.JsonUtils;
 import io.servicecomb.foundation.common.utils.SPIServiceUtils;
 import io.servicecomb.foundation.vertx.client.http.HttpClientWithContext;
@@ -44,7 +42,6 @@ import io.servicecomb.foundation.vertx.http.VertxClientRequestToHttpServletReque
 import io.servicecomb.foundation.vertx.http.VertxClientResponseToHttpServletResponse;
 import io.servicecomb.serviceregistry.api.Const;
 import io.servicecomb.swagger.invocation.AsyncResponse;
-import io.servicecomb.swagger.invocation.InvocationType;
 import io.servicecomb.swagger.invocation.Response;
 import io.vertx.core.buffer.Buffer;
 import io.vertx.core.http.HttpClient;
@@ -70,11 +67,6 @@ public class VertxHttpMethod {
     OperationMeta operationMeta = invocation.getOperationMeta();
     RestOperationMeta swaggerRestOperation = operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
 
-    long startTime = System.nanoTime();
-    EventUtils.triggerEvent(new InvocationStartedEvent(invocation.getMicroserviceQualifiedName(),
-        InvocationType.CONSUMER, startTime));
-    invocation.setStartTime(startTime);
-
     String path = this.createRequestPath(invocation, swaggerRestOperation);
     IpPort ipPort = (IpPort) invocation.getEndpoint().getAddress();
 
@@ -155,8 +147,6 @@ public class VertxHttpMethod {
           }
         } catch (Throwable e) {
           asyncResp.fail(invocation.getInvocationType(), e);
-        } finally {
-          invocation.triggerFinishedEvent();
         }
       });
     });

-- 
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.