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 5e651a3b2214b3a6f14e4e967426762eb3cc2487 Author: liubao <[email protected]> AuthorDate: Tue Sep 4 19:38:02 2018 +0800 [SCB-887]using invocation nano time & add highway --- .../servicecomb/common/rest/AbstractRestInvocation.java | 9 ++------- .../java/org/apache/servicecomb/common/rest/RestConst.java | 2 -- .../servicecomb/common/rest/RestProducerInvocation.java | 2 +- .../servicecomb/common/rest/TestAbstractRestInvocation.java | 1 - .../servicecomb/common/rest/TestCommonRestConfig.java | 2 +- .../org/apache/servicecomb/edge/core/EdgeInvocation.java | 1 - transports/transport-highway/pom.xml | 4 ++++ .../apache/servicecomb/transport/highway/HighwayConfig.java | 13 +++++++++++++ .../servicecomb/transport/highway/HighwayServerInvoke.java | 12 ++++++++++++ .../servicecomb/transport/highway/TestHighwayConfig.java | 13 +++++++++++++ 10 files changed, 46 insertions(+), 13 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 53daf7b..b3ec3d1 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 @@ -146,13 +146,8 @@ public abstract class AbstractRestInvocation { } private boolean isInQueueTimeout() { - Object timeout = requestEx.getAttribute(RestConst.REST_REQUEST_IN_QUEUE_TIME); - if (timeout != null) { - if (System.currentTimeMillis() - (Long) timeout - > CommonRestConfig - .getRequestWaitInPoolTimeout()) { - return true; - } + if (System.nanoTime() - invocation.getStartTime() > CommonRestConfig.getRequestWaitInPoolTimeout() * 1_000_000) { + return true; } return false; } diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestConst.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestConst.java index 5e917d0..57b4029 100644 --- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestConst.java +++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/RestConst.java @@ -55,8 +55,6 @@ public final class RestConst { public static final String REST_REQUEST = "servicecomb-rest-request"; - public static final String REST_REQUEST_IN_QUEUE_TIME = "servicecomb-rest-request-in-queue-time"; - public static final String CONSUMER_HEADER = "servicecomb-rest-consumer-header"; public static final String READ_STREAM_PART = "servicecomb-readStreamPart"; 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 9c71bae..965cebf 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 @@ -31,6 +31,7 @@ import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; public class RestProducerInvocation extends AbstractRestInvocation { + protected Transport transport; public void invoke(Transport transport, HttpServletRequestEx requestEx, HttpServletResponseEx responseEx, @@ -40,7 +41,6 @@ public class RestProducerInvocation extends AbstractRestInvocation { this.responseEx = responseEx; this.httpServerFilters = httpServerFilters; requestEx.setAttribute(RestConst.REST_REQUEST, requestEx); - requestEx.setAttribute(RestConst.REST_REQUEST_IN_QUEUE_TIME, System.currentTimeMillis()); try { findRestOperation(); 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 2114d5f..dcc6110 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 @@ -739,7 +739,6 @@ public class TestAbstractRestInvocation { Executor executor = new ReactiveExecutor(); requestEx = new AbstractHttpServletRequestForTest(); requestEx.setAttribute(RestConst.REST_REQUEST, requestEx); - requestEx.setAttribute(RestConst.REST_REQUEST_IN_QUEUE_TIME, System.currentTimeMillis()); new Expectations() { { restOperation.getOperationMeta(); diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestCommonRestConfig.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestCommonRestConfig.java index 608a94d..d3bb2a2 100644 --- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestCommonRestConfig.java +++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestCommonRestConfig.java @@ -24,7 +24,7 @@ import org.junit.Test; public class TestCommonRestConfig { @After - public void teanDown() { + public void tearDown() { ArchaiusUtils.resetConfig(); } 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 ef5e69c..742762a 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 @@ -62,7 +62,6 @@ public class EdgeInvocation extends AbstractRestInvocation { this.routingContext = context; this.httpServerFilters = httpServerFilters; requestEx.setAttribute(RestConst.REST_REQUEST, requestEx); - requestEx.setAttribute(RestConst.REST_REQUEST_IN_QUEUE_TIME, System.currentTimeMillis()); } public void edgeInvoke() { diff --git a/transports/transport-highway/pom.xml b/transports/transport-highway/pom.xml index e338582..2824517 100644 --- a/transports/transport-highway/pom.xml +++ b/transports/transport-highway/pom.xml @@ -52,5 +52,9 @@ <groupId>org.apache.servicecomb</groupId> <artifactId>foundation-metrics</artifactId> </dependency> + <dependency> + <groupId>org.apache.servicecomb</groupId> + <artifactId>foundation-test-scaffolding</artifactId> + </dependency> </dependencies> </project> diff --git a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayConfig.java b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayConfig.java index b058390..35ea257 100644 --- a/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayConfig.java +++ b/transports/transport-highway/src/main/java/org/apache/servicecomb/transport/highway/HighwayConfig.java @@ -18,6 +18,7 @@ package org.apache.servicecomb.transport.highway; import com.netflix.config.DynamicIntProperty; +import com.netflix.config.DynamicLongProperty; import com.netflix.config.DynamicPropertyFactory; import com.netflix.config.DynamicStringProperty; @@ -25,6 +26,18 @@ public final class HighwayConfig { private HighwayConfig() { } + public static final String KEY_SERVICECOMB_REQUEST_WAIT_IN_POOL_TIMEOUT = "servicecomb.highway.server.requestWaitInPoolTimeout"; + + public static final long DEFAULT_REQUEST_WAIT_IN_POOL_TIMEOUT = 30000; + + private static final DynamicLongProperty requestWaitInPoolTimeoutProperty = + DynamicPropertyFactory.getInstance().getLongProperty(KEY_SERVICECOMB_REQUEST_WAIT_IN_POOL_TIMEOUT, + DEFAULT_REQUEST_WAIT_IN_POOL_TIMEOUT); + + public static long getRequestWaitInPoolTimeout() { + return requestWaitInPoolTimeoutProperty.get(); + } + public static String getAddress() { DynamicStringProperty address = DynamicPropertyFactory.getInstance().getStringProperty("servicecomb.highway.address", null); 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 4878c41..44606f1 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 @@ -19,6 +19,8 @@ package org.apache.servicecomb.transport.highway; import java.util.Map; +import javax.ws.rs.core.Response.Status; + import org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf; import org.apache.servicecomb.codec.protobuf.definition.ProtobufManager; import org.apache.servicecomb.codec.protobuf.utils.WrapSchema; @@ -106,6 +108,9 @@ public class HighwayServerInvoke { private void runInExecutor() { try { + if (isInQueueTimeout()) { + throw new InvocationException(Status.INTERNAL_SERVER_ERROR, "Timeout when processing the request."); + } doRunInExecutor(); } catch (Throwable e) { String msg = String.format("handle request error, %s, msgId=%d", @@ -117,6 +122,13 @@ public class HighwayServerInvoke { } } + private boolean isInQueueTimeout() { + if (System.nanoTime() - invocation.getStartTime() > HighwayConfig.getRequestWaitInPoolTimeout() * 1_000_000) { + return true; + } + return false; + } + private void doRunInExecutor() throws Exception { invocation.onStartExecute(); diff --git a/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/TestHighwayConfig.java b/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/TestHighwayConfig.java index 8c490da..31889e5 100644 --- a/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/TestHighwayConfig.java +++ b/transports/transport-highway/src/test/java/org/apache/servicecomb/transport/highway/TestHighwayConfig.java @@ -17,10 +17,16 @@ package org.apache.servicecomb.transport.highway; +import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils; +import org.junit.After; import org.junit.Assert; import org.junit.Test; public class TestHighwayConfig { + @After + public void tearDown() { + ArchaiusUtils.resetConfig(); + } @Test public void getServerThreadCount() { Assert.assertEquals(HighwayConfig.getServerThreadCount(), 1); @@ -35,4 +41,11 @@ public class TestHighwayConfig { public void getAddress() { Assert.assertEquals(HighwayConfig.getAddress(), null); } + + @Test + public void testTimeoutConfig() { + Assert.assertEquals(HighwayConfig.getRequestWaitInPoolTimeout(), 30000); + ArchaiusUtils.setProperty(HighwayConfig.KEY_SERVICECOMB_REQUEST_WAIT_IN_POOL_TIMEOUT, 50000); + Assert.assertEquals(HighwayConfig.getRequestWaitInPoolTimeout(), 50000); + } }
