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/01/31 02:31:49 UTC

[incubator-servicecomb-java-chassis] branch master updated: SCB-307 avoid to create unnecessary vertx context during send http response.

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


The following commit(s) were added to refs/heads/master by this push:
     new 27009c4  SCB-307 avoid to create unnecessary vertx context during send http response.
27009c4 is described below

commit 27009c4dc5c4f83be4cf65d680b037aad898817f
Author: wujimin <wu...@huawei.com>
AuthorDate: Fri Jan 26 10:41:57 2018 +0800

    SCB-307 avoid to create unnecessary vertx context during send http response.
---
 .../servicecomb/edge/core/TestEdgeInvocation.java  | 16 ++++-
 .../VertxServerResponseToHttpServletResponse.java  | 19 ++++++
 ...stVertxServerResponseToHttpServletResponse.java | 71 +++++++++++++++++++++-
 .../rest/vertx/TestVertxRestDispatcher.java        | 16 ++++-
 4 files changed, 114 insertions(+), 8 deletions(-)

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 505e8d6..81160b4 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
@@ -47,6 +47,8 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 
+import io.vertx.core.Context;
+import io.vertx.core.impl.VertxImpl;
 import io.vertx.ext.web.RoutingContext;
 import io.vertx.ext.web.impl.HttpServerRequestWrapperForTest;
 import mockit.Deencapsulation;
@@ -57,7 +59,10 @@ public class TestEdgeInvocation {
   String microserviceName = "ms";
 
   @Mocked
-  RoutingContext context;
+  RoutingContext routingContext;
+
+  @Mocked
+  Context context;
 
   @Mocked
   HttpServerRequestWrapperForTest request;
@@ -79,11 +84,18 @@ public class TestEdgeInvocation {
 
   @Before
   public void setup() {
+    new Expectations(VertxImpl.class) {
+      {
+        VertxImpl.context();
+        result = context;
+      }
+    };
+
     Deencapsulation.setField(referenceConfig, "microserviceMeta", microserviceMeta);
     referenceConfig.setMicroserviceVersionRule("latest");
     referenceConfig.setTransport("rest");
 
-    edgeInvocation.init(microserviceName, context, "/base", httpServerFilters);
+    edgeInvocation.init(microserviceName, routingContext, "/base", httpServerFilters);
 
     requestEx = Deencapsulation.getField(edgeInvocation, "requestEx");
     responseEx = Deencapsulation.getField(edgeInvocation, "responseEx");
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxServerResponseToHttpServletResponse.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxServerResponseToHttpServletResponse.java
index 89daeb7..ca5510c 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxServerResponseToHttpServletResponse.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/http/VertxServerResponseToHttpServletResponse.java
@@ -19,21 +19,29 @@ package org.apache.servicecomb.foundation.vertx.http;
 
 import java.io.IOException;
 import java.util.Collection;
+import java.util.Objects;
 
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.Response.StatusType;
 
 import org.apache.servicecomb.foundation.common.http.HttpStatus;
 
+import io.vertx.core.Context;
+import io.vertx.core.Vertx;
 import io.vertx.core.http.HttpServerResponse;
 
 public class VertxServerResponseToHttpServletResponse extends AbstractHttpServletResponse {
+  private Context context;
+
   private HttpServerResponse serverResponse;
 
   private StatusType statusType;
 
   public VertxServerResponseToHttpServletResponse(HttpServerResponse serverResponse) {
+    this.context = Vertx.currentContext();
     this.serverResponse = serverResponse;
+
+    Objects.requireNonNull(context, "must run in vertx context.");
   }
 
   @Override
@@ -92,6 +100,17 @@ public class VertxServerResponseToHttpServletResponse extends AbstractHttpServle
 
   @Override
   public void flushBuffer() throws IOException {
+    if (context == Vertx.currentContext()) {
+      internalFlushBuffer();
+      return;
+    }
+
+    context.runOnContext(V -> {
+      internalFlushBuffer();
+    });
+  }
+
+  public void internalFlushBuffer() {
     if (bodyBuffer == null) {
       serverResponse.end();
       return;
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerResponseToHttpServletResponse.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerResponseToHttpServletResponse.java
index d519c2c..d821d14 100644
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerResponseToHttpServletResponse.java
+++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxServerResponseToHttpServletResponse.java
@@ -26,14 +26,21 @@ import org.apache.servicecomb.foundation.common.http.HttpStatus;
 import org.hamcrest.Matchers;
 import org.junit.Assert;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 
+import io.vertx.core.Context;
+import io.vertx.core.Handler;
 import io.vertx.core.MultiMap;
 import io.vertx.core.buffer.Buffer;
 import io.vertx.core.http.HttpServerResponse;
+import io.vertx.core.impl.VertxImpl;
 import mockit.Deencapsulation;
+import mockit.Expectations;
 import mockit.Mock;
 import mockit.MockUp;
+import mockit.Mocked;
 
 public class TestVertxServerResponseToHttpServletResponse {
   MultiMap headers = MultiMap.caseInsensitiveMultiMap();
@@ -46,6 +53,14 @@ public class TestVertxServerResponseToHttpServletResponse {
 
   boolean flushWithBody;
 
+  boolean runOnContextInvoked;
+
+  @Mocked
+  Context context;
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
   @Before
   public void setup() {
     serverResponse = new MockUp<HttpServerResponse>() {
@@ -87,10 +102,40 @@ public class TestVertxServerResponseToHttpServletResponse {
       }
     }.getMockInstance();
 
+    new Expectations(VertxImpl.class) {
+      {
+        VertxImpl.context();
+        result = context;
+      }
+    };
+
+    new MockUp<Context>(context) {
+      @Mock
+      void runOnContext(Handler<Void> action) {
+        runOnContextInvoked = true;
+        action.handle(null);
+      }
+    };
+
     response = new VertxServerResponseToHttpServletResponse(serverResponse);
   }
 
   @Test
+  public void construct_invalid() throws IOException {
+    new Expectations(VertxImpl.class) {
+      {
+        VertxImpl.context();
+        result = null;
+      }
+    };
+
+    expectedException.expect(NullPointerException.class);
+    expectedException.expectMessage(Matchers.is("must run in vertx context."));
+
+    new VertxServerResponseToHttpServletResponse(serverResponse);
+  }
+
+  @Test
   public void setContentType() {
     response.setContentType("json");
     Assert.assertEquals("json", headers.get(HttpHeaders.CONTENT_TYPE));
@@ -168,16 +213,36 @@ public class TestVertxServerResponseToHttpServletResponse {
   }
 
   @Test
-  public void flushBufferNoBody() throws IOException {
+  public void flushBuffer_sameContext() throws IOException {
+    response.flushBuffer();
+
+    Assert.assertFalse(runOnContextInvoked);
+  }
+
+  @Test
+  public void flushBuffer_diffContext() throws IOException {
+    new Expectations(VertxImpl.class) {
+      {
+        VertxImpl.context();
+        result = null;
+      }
+    };
     response.flushBuffer();
 
+    Assert.assertTrue(runOnContextInvoked);
+  }
+
+  @Test
+  public void internalFlushBufferNoBody() throws IOException {
+    response.internalFlushBuffer();
+
     Assert.assertFalse(flushWithBody);
   }
 
   @Test
-  public void flushBufferWithBody() throws IOException {
+  public void internalFlushBufferWithBody() throws IOException {
     response.setBodyBuffer(Buffer.buffer());
-    response.flushBuffer();
+    response.internalFlushBuffer();
 
     Assert.assertTrue(flushWithBody);
   }
diff --git a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestVertxRestDispatcher.java b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestVertxRestDispatcher.java
index 6daafab..b567195 100644
--- a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestVertxRestDispatcher.java
+++ b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestVertxRestDispatcher.java
@@ -36,7 +36,9 @@ import org.junit.Before;
 import org.junit.Test;
 
 import io.netty.handler.codec.http.multipart.HttpPostRequestDecoder.ErrorDataDecoderException;
+import io.vertx.core.Context;
 import io.vertx.core.http.HttpServerRequest;
+import io.vertx.core.impl.VertxImpl;
 import io.vertx.core.net.SocketAddress;
 import io.vertx.ext.web.Router;
 import io.vertx.ext.web.RoutingContext;
@@ -149,9 +151,10 @@ public class TestVertxRestDispatcher {
   }
 
   @Test
-  public void onRequest(@Mocked HttpServerRequest request, @Mocked SocketAddress socketAdrress) {
+  public void onRequest(@Mocked Context context, @Mocked HttpServerRequest request,
+      @Mocked SocketAddress socketAdrress) {
     Map<String, Object> map = new HashMap<>();
-    RoutingContext context = new MockUp<RoutingContext>() {
+    RoutingContext routingContext = new MockUp<RoutingContext>() {
       @Mock
       RoutingContext put(String key, Object obj) {
         map.put(key, obj);
@@ -163,7 +166,14 @@ public class TestVertxRestDispatcher {
         return request;
       }
     }.getMockInstance();
-    Deencapsulation.invoke(dispatcher, "onRequest", context);
+
+    new Expectations(VertxImpl.class) {
+      {
+        VertxImpl.context();
+        result = context;
+      }
+    };
+    Deencapsulation.invoke(dispatcher, "onRequest", routingContext);
 
     Assert.assertEquals(RestProducerInvocation.class, map.get(RestConst.REST_PRODUCER_INVOCATION).getClass());
     Assert.assertTrue(invoked);

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