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:44:30 UTC

[incubator-servicecomb-java-chassis] 12/14: JAV-508 & SCB-11 fix pr comment (change publish interface use windowTime instead of Index)

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 689a2ac7d80b8cf6e8c9010fd7249f0c938af430
Author: zhengyangyong <ya...@huawei.com>
AuthorDate: Tue Jan 2 12:46:05 2018 +0800

    JAV-508 & SCB-11 fix pr comment (change publish interface use windowTime instead of Index)
    
    Signed-off-by: zhengyangyong <ya...@huawei.com>
---
 .../metrics/common/MetricsPublisher.java           | 20 +++++-----
 metrics/metrics-core/pom.xml                       | 10 +----
 .../metrics/core/publish/DataSource.java           | 20 +++++-----
 .../metrics/core/publish/DefaultDataSource.java    | 46 ++++++++++++++++++++--
 .../core/publish/DefaultMetricsPublisher.java      | 10 +++--
 .../metrics/core/TestEventAndRunner.java           |  8 ++--
 .../io/servicecomb/metrics/core/TestPublisher.java | 13 +++---
 7 files changed, 81 insertions(+), 46 deletions(-)

diff --git a/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/MetricsPublisher.java b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/MetricsPublisher.java
index 43b965f..5e66a10 100644
--- a/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/MetricsPublisher.java
+++ b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/MetricsPublisher.java
@@ -34,26 +34,26 @@ public interface MetricsPublisher {
    */
   List<Long> getAppliedWindowTime();
 
-  //same as getRegistryMetric(0)
+  //same as getRegistryMetric({first setting windowTime})
   RegistryMetric metrics();
 
   /**
-   * windowTimeIndex usage example:
+   * windowTime usage example:
    * if there is two window time set in "servicecomb.metrics.window_time" like 1000,2000
-   * then windowTimeIndex = 0 will return result of the setting 1000(1 second)
-   * windowTimeIndex = 1 will return result of the setting 2000(2 second)
+   * then windowTime = 1000 will return result of the setting 1000(1 second)
+   * windowTime = 2000 will return result of the setting 2000(2 second)
    *
    * there are three monitor of max,min,total
    * 0----------1----------2----------3----------  <-time line (second)
    *   100,200    300,400                          <-value record
    *
-   *                 ↑ getRegistryMetric(0) will return max=200 min=100 total=300
-   *                   getRegistryMetric(1) will return max=0 min=0 total=0
-   *                             ↑ getRegistryMetric(0) will return max=300 min=400 total=700
-   *                               getRegistryMetric(1) will return max=400 min=100 total=1000
+   *                 ↑ getRegistryMetric(1000) will return max=200 min=100 total=300
+   *                   getRegistryMetric(2000) will return max=0 min=0 total=0
+   *                             ↑ getRegistryMetric(1000) will return max=300 min=400 total=700
+   *                               getRegistryMetric(2000) will return max=400 min=100 total=1000
    *
-   * @param windowTimeIndex index of getAppliedWindowTime() item
+   * @param windowTime getAppliedWindowTime() item
    * @return RegistryMetric
    */
-  RegistryMetric metricsWithWindowTimeIndex(int windowTimeIndex);
+  RegistryMetric metricsWithWindowTimeIndex(long windowTime);
 }
diff --git a/metrics/metrics-core/pom.xml b/metrics/metrics-core/pom.xml
index 8f8fdeb..2566bbe 100644
--- a/metrics/metrics-core/pom.xml
+++ b/metrics/metrics-core/pom.xml
@@ -39,15 +39,7 @@
     </dependency>
     <dependency>
       <groupId>io.servicecomb</groupId>
-      <artifactId>provider-rest-common</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>org.springframework</groupId>
-      <artifactId>spring-web</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>io.servicecomb</groupId>
-      <artifactId>java-chassis-core</artifactId>
+      <artifactId>provider-springmvc</artifactId>
     </dependency>
 
     <dependency>
diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DataSource.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DataSource.java
index bd19a30..80d4570 100644
--- a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DataSource.java
+++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DataSource.java
@@ -37,26 +37,26 @@ public interface DataSource {
    */
   List<Long> getAppliedWindowTime();
 
-  //same as getRegistryMetric(0)
+  //same as getRegistryMetric({first setting windowTime})
   RegistryMetric getRegistryMetric();
 
   /**
-   * windowTimeIndex usage example:
+   * windowTime usage example:
    * if there is two window time set in "servicecomb.metrics.window_time" like 1000,2000
-   * then windowTimeIndex = 0 will return result of the setting 1000(1 second)
-   * windowTimeIndex = 1 will return result of the setting 2000(2 second)
+   * then windowTime = 1000 will return result of the setting 1000(1 second)
+   * windowTime = 2000 will return result of the setting 2000(2 second)
    *
    * there are three monitor of max,min,total
    * 0----------1----------2----------3----------  <-time line (second)
    *   100,200    300,400                          <-value record
    *
-   *                 ↑ getRegistryMetric(0) will return max=200 min=100 total=300
-   *                   getRegistryMetric(1) will return max=0 min=0 total=0
-   *                             ↑ getRegistryMetric(0) will return max=300 min=400 total=700
-   *                               getRegistryMetric(1) will return max=400 min=100 total=1000
+   *                 ↑ getRegistryMetric(1000) will return max=200 min=100 total=300
+   *                   getRegistryMetric(2000) will return max=0 min=0 total=0
+   *                             ↑ getRegistryMetric(1000) will return max=300 min=400 total=700
+   *                               getRegistryMetric(2000) will return max=400 min=100 total=1000
    *
-   * @param windowTimeIndex index of getAppliedWindowTime() item
+   * @param windowTime getAppliedWindowTime() item
    * @return RegistryMetric
    */
-  RegistryMetric getRegistryMetric(int windowTimeIndex);
+  RegistryMetric getRegistryMetric(long windowTime);
 }
diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultDataSource.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultDataSource.java
index d1ce74b..22fb846 100644
--- a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultDataSource.java
+++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultDataSource.java
@@ -17,16 +17,25 @@
 
 package io.servicecomb.metrics.core.publish;
 
+import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
+
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 import com.netflix.config.DynamicPropertyFactory;
 import com.netflix.servo.monitor.Pollers;
+import com.netflix.servo.util.Strings;
 
+import io.servicecomb.foundation.common.exceptions.ServiceCombException;
 import io.servicecomb.metrics.common.RegistryMetric;
 import io.servicecomb.metrics.core.monitor.RegistryMonitor;
+import io.servicecomb.swagger.invocation.exception.InvocationException;
 
 @Component
 public class DefaultDataSource implements DataSource {
@@ -34,6 +43,8 @@ public class DefaultDataSource implements DataSource {
 
   private final RegistryMonitor registryMonitor;
 
+  private final Map<Long, Integer> appliedWindowTimes = new HashMap<>();
+
   @Autowired
   public DefaultDataSource(RegistryMonitor registryMonitor) {
     this(registryMonitor, DynamicPropertyFactory.getInstance().getStringProperty(METRICS_POLLING_TIME, "5000").get());
@@ -41,17 +52,44 @@ public class DefaultDataSource implements DataSource {
 
   public DefaultDataSource(RegistryMonitor registryMonitor, String pollingSettings) {
     this.registryMonitor = registryMonitor;
-    System.getProperties().setProperty("servo.pollers", pollingSettings);
+
+    String[] singlePollingSettings = pollingSettings.split(",");
+    Set<Long> parsePollingSettings = new HashSet<>();
+    for (String singlePollingSetting : singlePollingSettings) {
+      try {
+        long settingValue = Long.parseLong(singlePollingSetting);
+        if (settingValue > 0) {
+          parsePollingSettings.add(settingValue);
+        } else {
+          throw new ServiceCombException(
+              "bad format servicecomb.metrics.window_time : " + String.valueOf(settingValue));
+        }
+      } catch (NumberFormatException e) {
+        throw new ServiceCombException("bad format servicecomb.metrics.window_time", e);
+      }
+    }
+    String finalPollingSettings = Strings.join(",", parsePollingSettings.iterator());
+    System.getProperties().setProperty("servo.pollers", finalPollingSettings);
+    List<Long> appliedWindowTimes = getAppliedWindowTime();
+    for (int i = 0; i < appliedWindowTimes.size(); i++) {
+      this.appliedWindowTimes.put(appliedWindowTimes.get(i), i);
+    }
   }
 
   @Override
   public RegistryMetric getRegistryMetric() {
-    return getRegistryMetric(0);
+    return getRegistryMetric(getAppliedWindowTime().get(0));
   }
 
   @Override
-  public RegistryMetric getRegistryMetric(int windowTimeIndex) {
-    return registryMonitor.toRegistryMetric(windowTimeIndex);
+  public RegistryMetric getRegistryMetric(long windowTime) {
+    if (appliedWindowTimes.containsKey(windowTime)) {
+      return registryMonitor.toRegistryMetric(appliedWindowTimes.get(windowTime));
+    } else {
+      throw new InvocationException(BAD_REQUEST,
+          "windowTime : " + windowTime + " unset in servicecomb.metrics.window_time,current available are : " +
+              Strings.join(",", getAppliedWindowTime().iterator()));
+    }
   }
 
   @Override
diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultMetricsPublisher.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultMetricsPublisher.java
index 54ed0a6..9c3909b 100644
--- a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultMetricsPublisher.java
+++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultMetricsPublisher.java
@@ -19,6 +19,7 @@ package io.servicecomb.metrics.core.publish;
 
 import java.util.List;
 
+import org.springframework.web.bind.annotation.CrossOrigin;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
@@ -38,20 +39,23 @@ public class DefaultMetricsPublisher implements MetricsPublisher {
   }
 
   @RequestMapping(path = "/appliedWindowTime", method = RequestMethod.GET)
+  @CrossOrigin
   @Override
   public List<Long> getAppliedWindowTime() {
     return dataSource.getAppliedWindowTime();
   }
 
   @RequestMapping(path = "/", method = RequestMethod.GET)
+  @CrossOrigin
   @Override
   public RegistryMetric metrics() {
     return dataSource.getRegistryMetric();
   }
 
-  @RequestMapping(path = "/{windowTimeIndex}", method = RequestMethod.GET)
+  @RequestMapping(path = "/{windowTime}", method = RequestMethod.GET)
+  @CrossOrigin
   @Override
-  public RegistryMetric metricsWithWindowTimeIndex(@PathVariable(name = "windowTimeIndex") int windowTimeIndex) {
-    return dataSource.getRegistryMetric(windowTimeIndex);
+  public RegistryMetric metricsWithWindowTimeIndex(@PathVariable(name = "windowTime") long windowTime) {
+    return dataSource.getRegistryMetric(windowTime);
   }
 }
diff --git a/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestEventAndRunner.java b/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestEventAndRunner.java
index 28f6280..7c6ced3 100644
--- a/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestEventAndRunner.java
+++ b/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestEventAndRunner.java
@@ -17,12 +17,14 @@
 
 package io.servicecomb.metrics.core;
 
+import static org.hamcrest.Matchers.containsInAnyOrder;
 import static org.mockito.Mockito.when;
 
 import java.lang.management.MemoryMXBean;
 import java.lang.management.MemoryUsage;
 import java.lang.management.OperatingSystemMXBean;
 import java.lang.management.ThreadMXBean;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
@@ -70,9 +72,7 @@ public class TestEventAndRunner {
 
     List<Long> intervals = dataSource.getAppliedWindowTime();
     Assert.assertEquals(intervals.size(), 3);
-    Assert.assertEquals(intervals.get(0).intValue(), 1000);
-    Assert.assertEquals(intervals.get(1).intValue(), 2000);
-    Assert.assertEquals(intervals.get(2).intValue(), 3000);
+    Assert.assertThat(intervals, containsInAnyOrder(Arrays.asList(1000L, 2000L, 3000L).toArray()));
 
     new DefaultEventListenerManager(monitor);
 
@@ -114,7 +114,7 @@ public class TestEventAndRunner {
     //sim lease one window time
     Thread.sleep(1000);
 
-    RegistryMetric model = dataSource.getRegistryMetric();
+    RegistryMetric model = dataSource.getRegistryMetric(1000);
 
     //check InstanceMetric
     Assert.assertEquals(model.getInstanceMetric().getProducerMetric().getWaitInQueue(), 1);
diff --git a/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestPublisher.java b/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestPublisher.java
index 6aad12d..4c60167 100644
--- a/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestPublisher.java
+++ b/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestPublisher.java
@@ -17,7 +17,10 @@
 
 package io.servicecomb.metrics.core;
 
+import static org.hamcrest.Matchers.containsInAnyOrder;
+
 import java.io.IOException;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 
@@ -37,21 +40,19 @@ public class TestPublisher {
   public void test() throws IOException {
     SystemMonitor systemMonitor = new DefaultSystemMonitor();
     RegistryMonitor registryMonitor = new RegistryMonitor(systemMonitor);
-    DefaultDataSource dataSource = new DefaultDataSource(registryMonitor,"1000,2000,3000");
+    DefaultDataSource dataSource = new DefaultDataSource(registryMonitor, "1000,2000,3000,3000,2000,1000");
     DefaultMetricsPublisher publisher = new DefaultMetricsPublisher(dataSource);
 
     RegistryMetric registryMetric = publisher.metrics();
     Map<String, Number> metricsMap = registryMetric.toMap();
     Assert.assertEquals(metricsMap.size(), 30);
 
-    registryMetric = publisher.metricsWithWindowTimeIndex(0);
+    registryMetric = publisher.metricsWithWindowTimeIndex(1000);
     metricsMap = registryMetric.toMap();
     Assert.assertEquals(metricsMap.size(), 30);
 
     List<Long> appliedWindowTime = publisher.getAppliedWindowTime();
-    Assert.assertEquals(appliedWindowTime.size(),3);
-    Assert.assertEquals(appliedWindowTime.get(0).longValue(),1000);
-    Assert.assertEquals(appliedWindowTime.get(1).longValue(),2000);
-    Assert.assertEquals(appliedWindowTime.get(2).longValue(),3000);
+    Assert.assertEquals(appliedWindowTime.size(), 3);
+    Assert.assertThat(appliedWindowTime, containsInAnyOrder(Arrays.asList(1000L, 2000L, 3000L).toArray()));
   }
 }

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