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 2020/05/27 03:38:00 UTC

[servicecomb-java-chassis] branch master updated: [SCB-1938] OperationConfig can get in pool timeout by transport name

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/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new a440527  [SCB-1938] OperationConfig can get in pool timeout by transport name
a440527 is described below

commit a440527beec223b4555b9866f700eb8ffc21d908
Author: wujimin <wu...@huawei.com>
AuthorDate: Wed May 27 09:28:07 2020 +0800

    [SCB-1938] OperationConfig can get in pool timeout by transport name
---
 .../core/definition/OperationConfig.java           | 32 +++++++++++
 .../core/definition/OperationConfigTest.java       | 62 ++++++++++++++++++++++
 2 files changed, 94 insertions(+)

diff --git a/core/src/main/java/org/apache/servicecomb/core/definition/OperationConfig.java b/core/src/main/java/org/apache/servicecomb/core/definition/OperationConfig.java
index 7a5b7e7..ccbbb68 100644
--- a/core/src/main/java/org/apache/servicecomb/core/definition/OperationConfig.java
+++ b/core/src/main/java/org/apache/servicecomb/core/definition/OperationConfig.java
@@ -17,7 +17,9 @@
 package org.apache.servicecomb.core.definition;
 
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.servicecomb.config.inject.InjectProperties;
@@ -71,6 +73,13 @@ public class OperationConfig {
   /**
    * producer wait in thread pool timeout
    */
+  private Map<String, Long> nanoRequestWaitInPoolTimeoutByTransport = new HashMap<>();
+
+  @InjectProperty(keys = "Provider.requestWaitInPoolTimeout${op-priority}", defaultValue = "30000")
+  private long msDefaultRequestWaitInPoolTimeout;
+
+  private long nanoDefaultRequestWaitInPoolTimeout;
+
   @InjectProperty(keys = {
       "Provider.requestWaitInPoolTimeout${op-priority}",
       "highway.server.requestWaitInPoolTimeout"}, defaultValue = "30000")
@@ -120,6 +129,27 @@ public class OperationConfig {
     this.msRequestTimeout = msRequestTimeout;
   }
 
+  public long getNanoRequestWaitInPoolTimeout(String transport) {
+    return nanoRequestWaitInPoolTimeoutByTransport.getOrDefault(transport, nanoDefaultRequestWaitInPoolTimeout);
+  }
+
+  public void registerRequestWaitInPoolTimeout(String transport, long msTimeout) {
+    nanoRequestWaitInPoolTimeoutByTransport.put(transport, TimeUnit.MILLISECONDS.toNanos(msTimeout));
+  }
+
+  public long getMsDefaultRequestWaitInPoolTimeout() {
+    return msDefaultRequestWaitInPoolTimeout;
+  }
+
+  public void setMsDefaultRequestWaitInPoolTimeout(long msDefaultRequestWaitInPoolTimeout) {
+    this.msDefaultRequestWaitInPoolTimeout = msDefaultRequestWaitInPoolTimeout;
+    this.nanoDefaultRequestWaitInPoolTimeout = TimeUnit.MILLISECONDS.toNanos(msDefaultRequestWaitInPoolTimeout);
+  }
+
+  public long getNanoDefaultRequestWaitInPoolTimeout() {
+    return nanoDefaultRequestWaitInPoolTimeout;
+  }
+
   public long getMsHighwayRequestWaitInPoolTimeout() {
     return msHighwayRequestWaitInPoolTimeout;
   }
@@ -127,6 +157,7 @@ public class OperationConfig {
   public void setMsHighwayRequestWaitInPoolTimeout(long msHighwayRequestWaitInPoolTimeout) {
     this.msHighwayRequestWaitInPoolTimeout = msHighwayRequestWaitInPoolTimeout;
     this.nanoHighwayRequestWaitInPoolTimeout = TimeUnit.MILLISECONDS.toNanos(msHighwayRequestWaitInPoolTimeout);
+    registerRequestWaitInPoolTimeout(Const.HIGHWAY, msHighwayRequestWaitInPoolTimeout);
   }
 
   public long getNanoHighwayRequestWaitInPoolTimeout() {
@@ -140,6 +171,7 @@ public class OperationConfig {
   public void setMsRestRequestWaitInPoolTimeout(long msRestRequestWaitInPoolTimeout) {
     this.msRestRequestWaitInPoolTimeout = msRestRequestWaitInPoolTimeout;
     this.nanoRestRequestWaitInPoolTimeout = TimeUnit.MILLISECONDS.toNanos(msRestRequestWaitInPoolTimeout);
+    registerRequestWaitInPoolTimeout(Const.RESTFUL, msRestRequestWaitInPoolTimeout);
   }
 
   public long getNanoRestRequestWaitInPoolTimeout() {
diff --git a/core/src/test/java/org/apache/servicecomb/core/definition/OperationConfigTest.java b/core/src/test/java/org/apache/servicecomb/core/definition/OperationConfigTest.java
new file mode 100644
index 0000000..1745dfc
--- /dev/null
+++ b/core/src/test/java/org/apache/servicecomb/core/definition/OperationConfigTest.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.servicecomb.core.definition;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.servicecomb.core.Const;
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+
+class OperationConfigTest {
+  OperationConfig config = new OperationConfig();
+
+  @Nested
+  class InPoolTimeout {
+    @Test
+    void should_get_rest_value() {
+      config.setMsDefaultRequestWaitInPoolTimeout(1);
+      config.setMsRestRequestWaitInPoolTimeout(2);
+
+      long nano = TimeUnit.MILLISECONDS.toNanos(2);
+      assertThat(config.getNanoRestRequestWaitInPoolTimeout()).isEqualTo(nano);
+      assertThat(config.getNanoRequestWaitInPoolTimeout(Const.RESTFUL)).isEqualTo(nano);
+    }
+
+    @Test
+    void should_get_highway_value() {
+      config.setMsDefaultRequestWaitInPoolTimeout(1);
+      config.setMsHighwayRequestWaitInPoolTimeout(2);
+
+      long nano = TimeUnit.MILLISECONDS.toNanos(2);
+      assertThat(config.getNanoHighwayRequestWaitInPoolTimeout()).isEqualTo(nano);
+      assertThat(config.getNanoRequestWaitInPoolTimeout(Const.HIGHWAY)).isEqualTo(nano);
+    }
+
+    @Test
+    void should_support_register_customize_transport() {
+      config.setMsDefaultRequestWaitInPoolTimeout(1);
+      config.registerRequestWaitInPoolTimeout("abc", 2);
+
+      long nano = TimeUnit.MILLISECONDS.toNanos(2);
+      assertThat(config.getNanoRequestWaitInPoolTimeout("abc")).isEqualTo(nano);
+    }
+  }
+}
\ No newline at end of file