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/24 01:21:39 UTC

[incubator-servicecomb-java-chassis] 02/10: SCB-150 impl success /failed in invocation and event

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 98a141bbc5873f6f38c6be1e535ac683ce30d635
Author: zhengyangyong <ya...@huawei.com>
AuthorDate: Fri Jan 12 10:15:43 2018 +0800

    SCB-150 impl success /failed in invocation and event
    
    Signed-off-by: zhengyangyong <ya...@huawei.com>
---
 .../common/rest/AbstractRestInvocation.java        |  2 +-
 .../org/apache/servicecomb/core/Invocation.java    |  4 +-
 .../core/metrics/InvocationFinishedEvent.java      | 10 +++-
 .../core/provider/consumer/InvokerUtils.java       | 12 ++--
 .../metrics/core/utils/MonitorUtils.java           | 66 ++++++++++++++++++++++
 .../event/InvocationFinishedEventListener.java     | 21 ++++---
 .../core/event/InvocationStartedEventListener.java | 21 +++----
 .../metrics/core/monitor/CallMonitor.java          | 47 +++++++--------
 .../metrics/core/monitor/DefaultSystemMonitor.java |  6 +-
 .../metrics/core/monitor/TimerMonitor.java         | 30 +++-------
 .../transport/highway/HighwayServerInvoke.java     |  2 +-
 11 files changed, 144 insertions(+), 77 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 2efcdd1..0ee7999 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
@@ -189,7 +189,7 @@ public abstract class AbstractRestInvocation {
     invocation.next(resp -> {
       sendResponseQuietly(resp);
 
-      invocation.triggerFinishedEvent();
+      invocation.triggerFinishedEvent(resp.isSuccessed());
       endMetrics();
     });
   }
diff --git a/core/src/main/java/org/apache/servicecomb/core/Invocation.java b/core/src/main/java/org/apache/servicecomb/core/Invocation.java
index ea5e811..19241b8 100644
--- a/core/src/main/java/org/apache/servicecomb/core/Invocation.java
+++ b/core/src/main/java/org/apache/servicecomb/core/Invocation.java
@@ -202,12 +202,12 @@ public class Invocation extends SwaggerInvocation {
         operationMeta.getMicroserviceQualifiedName(), this.invocationType, startProcessingTime - startTime));
   }
 
-  public void triggerFinishedEvent() {
+  public void triggerFinishedEvent(boolean success) {
     long finishedTime = System.nanoTime();
     EventUtils
         .triggerEvent(new InvocationFinishedEvent(operationMeta.getMicroserviceQualifiedName(),
             this.invocationType, finishedTime - startProcessingTime,
-            finishedTime - startTime));
+            finishedTime - startTime, success));
   }
 
   public boolean isSync() {
diff --git a/core/src/main/java/org/apache/servicecomb/core/metrics/InvocationFinishedEvent.java b/core/src/main/java/org/apache/servicecomb/core/metrics/InvocationFinishedEvent.java
index 4ecf2ad..ea54d67 100644
--- a/core/src/main/java/org/apache/servicecomb/core/metrics/InvocationFinishedEvent.java
+++ b/core/src/main/java/org/apache/servicecomb/core/metrics/InvocationFinishedEvent.java
@@ -29,6 +29,8 @@ public class InvocationFinishedEvent implements Event {
 
   private final long totalElapsedNanoTime;
 
+  private final boolean success;
+
   public String getOperationName() {
     return operationName;
   }
@@ -45,12 +47,18 @@ public class InvocationFinishedEvent implements Event {
     return totalElapsedNanoTime;
   }
 
+  public boolean isSuccess() {
+    return success;
+  }
+
   public InvocationFinishedEvent(String operationName, InvocationType invocationType,
       long processElapsedNanoTime,
-      long totalElapsedNanoTime) {
+      long totalElapsedNanoTime,
+      boolean success) {
     this.operationName = operationName;
     this.invocationType = invocationType;
     this.processElapsedNanoTime = processElapsedNanoTime;
     this.totalElapsedNanoTime = totalElapsedNanoTime;
+    this.success = success;
   }
 }
diff --git a/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java b/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java
index cabc7fb..e8b8247 100644
--- a/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java
+++ b/core/src/main/java/org/apache/servicecomb/core/provider/consumer/InvokerUtils.java
@@ -62,6 +62,7 @@ public final class InvokerUtils {
   }
 
   public static Response innerSyncInvoke(Invocation invocation) {
+    boolean success = false;
     try {
       triggerStartedEvent(invocation);
       SyncResponseExecutor respExecutor = new SyncResponseExecutor();
@@ -69,14 +70,16 @@ public final class InvokerUtils {
 
       invocation.next(respExecutor::setResponse);
 
-      return respExecutor.waitResponse();
+      Response response = respExecutor.waitResponse();
+      success = response.isSuccessed();
+      return response;
     } catch (Throwable e) {
       String msg =
           String.format("invoke failed, %s", invocation.getOperationMeta().getMicroserviceQualifiedName());
       LOGGER.debug(msg, e);
       return Response.createConsumerFail(e);
     } finally {
-      invocation.triggerFinishedEvent();
+      invocation.triggerFinishedEvent(success);
     }
   }
 
@@ -89,12 +92,11 @@ public final class InvokerUtils {
       invocation.setResponseExecutor(respExecutor);
 
       invocation.next(ar -> {
-        invocation.triggerFinishedEvent();
+        invocation.triggerFinishedEvent(ar.isSuccessed());
         asyncResp.handle(ar);
       });
-
     } catch (Throwable e) {
-      invocation.triggerFinishedEvent();
+      invocation.triggerFinishedEvent(false);
       LOGGER.error("invoke failed, {}", invocation.getOperationMeta().getMicroserviceQualifiedName());
       asyncResp.consumerFail(e);
     }
diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/utils/MonitorUtils.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/utils/MonitorUtils.java
new file mode 100644
index 0000000..7091724
--- /dev/null
+++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/utils/MonitorUtils.java
@@ -0,0 +1,66 @@
+/*
+ * 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 io.servicecomb.metrics.core.utils;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import com.netflix.servo.monitor.Monitor;
+import com.netflix.servo.tag.Tag;
+import com.netflix.servo.tag.TagList;
+
+public class MonitorUtils {
+
+  //for time-related monitor type, if stop poll value over one window time,
+  //the value may return -1 or NaN because servo can't known precise value of previous step
+  //so must change to return 0
+  public static double adjustValue(double value) {
+    return Double.isNaN(value) || value < 0 ? 0 : value;
+  }
+
+  //for time-related monitor type, if stop poll value over one window time,
+  //the value may return -1 because servo can't known precise value of previous step
+  //so must change to return 0
+  public static long adjustValue(long value) {
+    return value < 0 ? 0 : value;
+  }
+
+  public static boolean containsTagValue(Monitor monitor, String tagKey, String tagValue) {
+    TagList tags = monitor.getConfig().getTags();
+    return tags.containsKey(tagKey) && tagValue.equals(tags.getTag(tagKey).getValue());
+  }
+
+  public static Map<String, String> convertTags(Monitor monitor) {
+    TagList tags = monitor.getConfig().getTags();
+    if (tags.size() != 0) {
+      Map<String, String> tagMap = new HashMap<>();
+      for (Tag tag : tags) {
+        tagMap.put(tag.getKey(), tag.getValue());
+      }
+      return tagMap;
+    }
+    return null;
+  }
+
+  //Counting use System.nano get more precise time
+  //so we need change unit to millisecond when ouput
+  public static long convertNanosecondToMillisecond(long nanoValue) {
+    return TimeUnit.NANOSECONDS.toMillis(nanoValue);
+  }
+}
diff --git a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/event/InvocationFinishedEventListener.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/event/InvocationFinishedEventListener.java
index ff356f9..9e2d687 100644
--- a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/event/InvocationFinishedEventListener.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/event/InvocationFinishedEventListener.java
@@ -15,15 +15,16 @@
  * limitations under the License.
  */
 
-package org.apache.servicecomb.metrics.core.event;
+package io.servicecomb.metrics.core.event;
 
-import org.apache.servicecomb.core.metrics.InvocationFinishedEvent;
-import org.apache.servicecomb.foundation.common.event.Event;
-import org.apache.servicecomb.foundation.common.event.EventListener;
-import org.apache.servicecomb.metrics.core.monitor.ConsumerInvocationMonitor;
-import org.apache.servicecomb.metrics.core.monitor.ProducerInvocationMonitor;
-import org.apache.servicecomb.metrics.core.monitor.RegistryMonitor;
-import org.apache.servicecomb.swagger.invocation.InvocationType;
+import io.servicecomb.core.metrics.InvocationFinishedEvent;
+import io.servicecomb.foundation.common.event.Event;
+import io.servicecomb.foundation.common.event.EventListener;
+import io.servicecomb.metrics.core.MetricsDimension;
+import io.servicecomb.metrics.core.monitor.ConsumerInvocationMonitor;
+import io.servicecomb.metrics.core.monitor.ProducerInvocationMonitor;
+import io.servicecomb.metrics.core.monitor.RegistryMonitor;
+import io.servicecomb.swagger.invocation.InvocationType;
 
 public class InvocationFinishedEventListener implements EventListener {
 
@@ -45,9 +46,13 @@ public class InvocationFinishedEventListener implements EventListener {
       ProducerInvocationMonitor monitor = registryMonitor.getProducerInvocationMonitor(event.getOperationName());
       monitor.getExecutionTime().update(event.getProcessElapsedNanoTime());
       monitor.getProducerLatency().update(event.getTotalElapsedNanoTime());
+      monitor.getProducerCall().increment(MetricsDimension.DIMENSION_STATUS,
+          event.isSuccess() ? MetricsDimension.DIMENSION_STATUS_SUCCESS : MetricsDimension.DIMENSION_STATUS_FAILED);
     } else {
       ConsumerInvocationMonitor monitor = registryMonitor.getConsumerInvocationMonitor(event.getOperationName());
       monitor.getConsumerLatency().update(event.getTotalElapsedNanoTime());
+      monitor.getConsumerCall().increment(MetricsDimension.DIMENSION_STATUS,
+          event.isSuccess() ? MetricsDimension.DIMENSION_STATUS_SUCCESS : MetricsDimension.DIMENSION_STATUS_FAILED);
     }
   }
 }
diff --git a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/event/InvocationStartedEventListener.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/event/InvocationStartedEventListener.java
index 166f5ce..f7516bb 100644
--- a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/event/InvocationStartedEventListener.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/event/InvocationStartedEventListener.java
@@ -15,15 +15,16 @@
  * limitations under the License.
  */
 
-package org.apache.servicecomb.metrics.core.event;
+package io.servicecomb.metrics.core.event;
 
-import org.apache.servicecomb.core.metrics.InvocationStartedEvent;
-import org.apache.servicecomb.foundation.common.event.Event;
-import org.apache.servicecomb.foundation.common.event.EventListener;
-import org.apache.servicecomb.metrics.core.monitor.ConsumerInvocationMonitor;
-import org.apache.servicecomb.metrics.core.monitor.ProducerInvocationMonitor;
-import org.apache.servicecomb.metrics.core.monitor.RegistryMonitor;
-import org.apache.servicecomb.swagger.invocation.InvocationType;
+import io.servicecomb.core.metrics.InvocationStartedEvent;
+import io.servicecomb.foundation.common.event.Event;
+import io.servicecomb.foundation.common.event.EventListener;
+import io.servicecomb.metrics.core.MetricsDimension;
+import io.servicecomb.metrics.core.monitor.ConsumerInvocationMonitor;
+import io.servicecomb.metrics.core.monitor.ProducerInvocationMonitor;
+import io.servicecomb.metrics.core.monitor.RegistryMonitor;
+import io.servicecomb.swagger.invocation.InvocationType;
 
 public class InvocationStartedEventListener implements EventListener {
 
@@ -44,10 +45,10 @@ public class InvocationStartedEventListener implements EventListener {
     if (InvocationType.PRODUCER.equals(event.getInvocationType())) {
       ProducerInvocationMonitor monitor = registryMonitor.getProducerInvocationMonitor(event.getOperationName());
       monitor.getWaitInQueue().increment();
-      monitor.getProducerCall().increment();
+      monitor.getProducerCall().increment(MetricsDimension.DIMENSION_STATUS, MetricsDimension.DIMENSION_STATUS_ALL);
     } else {
       ConsumerInvocationMonitor monitor = registryMonitor.getConsumerInvocationMonitor(event.getOperationName());
-      monitor.getConsumerCall().increment();
+      monitor.getConsumerCall().increment(MetricsDimension.DIMENSION_STATUS, MetricsDimension.DIMENSION_STATUS_ALL);
     }
   }
 }
diff --git a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/monitor/CallMonitor.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/monitor/CallMonitor.java
index 7174e33..fb47b9b 100644
--- a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/monitor/CallMonitor.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/monitor/CallMonitor.java
@@ -20,20 +20,17 @@ package org.apache.servicecomb.metrics.core.monitor;
 import org.apache.servicecomb.metrics.common.CallMetric;
 
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 import com.netflix.servo.monitor.BasicCounter;
 import com.netflix.servo.monitor.MonitorConfig;
 import com.netflix.servo.monitor.StepCounter;
-import com.netflix.servo.tag.Tag;
-import com.netflix.servo.tag.TagList;
 
 import io.servicecomb.metrics.common.CallMetric;
 import io.servicecomb.metrics.common.DoubleMetricValue;
 import io.servicecomb.metrics.common.LongMetricValue;
 import io.servicecomb.metrics.core.MetricsDimension;
+import io.servicecomb.metrics.core.utils.MonitorUtils;
 
 public class CallMonitor {
   private final String prefix;
@@ -68,33 +65,31 @@ public class CallMonitor {
     }
   }
 
+  public void increment(String dimensionKey, String dimensionValue) {
+    for (int i = 0; i < totalCounters.size(); i++) {
+      BasicCounter totalCounter = totalCounters.get(i);
+      if (MonitorUtils.containsTagValue(totalCounter, dimensionKey, dimensionValue)) {
+        totalCounter.increment();
+      }
+      StepCounter tpsCounter = tpsCounters.get(i);
+      if (MonitorUtils.containsTagValue(tpsCounter, dimensionKey, dimensionValue)) {
+        tpsCounter.increment();
+      }
+    }
+  }
+
   public CallMetric toMetric(int windowTimeIndex) {
     List<LongMetricValue> totalValues = new ArrayList<>();
     List<DoubleMetricValue> tpsValues = new ArrayList<>();
     for (int i = 0; i < totalCounters.size(); i++) {
-      totalValues.add(new LongMetricValue(totalCounters.get(i).getValue(windowTimeIndex).longValue(),
-          convertTags(totalCounters.get(i).getConfig().getTags())));
-      tpsValues.add(new DoubleMetricValue(adjustValue(tpsCounters.get(i).getValue(windowTimeIndex).doubleValue()),
-          convertTags(tpsCounters.get(i).getConfig().getTags())));
+      BasicCounter totalCounter = totalCounters.get(i);
+      totalValues.add(new LongMetricValue(totalCounter.getValue(windowTimeIndex).longValue(),
+          MonitorUtils.convertTags(totalCounter)));
+      StepCounter tpsCounter = tpsCounters.get(i);
+      tpsValues.add(
+          new DoubleMetricValue(MonitorUtils.adjustValue(tpsCounter.getValue(windowTimeIndex).doubleValue()),
+              MonitorUtils.convertTags(tpsCounter)));
     }
     return new CallMetric(this.prefix, totalValues, tpsValues);
   }
-
-  //for time-related monitor type, if stop poll value over one window time,
-  //the value may return -1 because servo can't known precise value of previous step
-  //so must change to return 0
-  private double adjustValue(double value) {
-    return value < 0 ? 0 : value;
-  }
-
-  private Map<String, String> convertTags(TagList tags) {
-    if (tags.size() != 0) {
-      Map<String, String> tagMap = new HashMap<>();
-      for (Tag tag : tags) {
-        tagMap.put(tag.getKey(), tag.getValue());
-      }
-      return tagMap;
-    }
-    return null;
-  }
 }
diff --git a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/monitor/DefaultSystemMonitor.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/monitor/DefaultSystemMonitor.java
index 12caaaa..3541d82 100644
--- a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/monitor/DefaultSystemMonitor.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/monitor/DefaultSystemMonitor.java
@@ -98,8 +98,12 @@ public class DefaultSystemMonitor implements SystemMonitor {
 
   @Override
   public SystemMetric toMetric() {
-    return new SystemMetric(getCpuLoad(),
+    return new SystemMetric(adjustValue(getCpuLoad()),
         getCpuRunningThreads(), getHeapInit(), getHeapMax(), getHeapCommit(), getHeapUsed(),
         getNonHeapInit(), getNonHeapMax(), getNonHeapCommit(), getNonHeapUsed());
   }
+
+  private double adjustValue(double value) {
+    return value < 0 ? Double.NaN : value;
+  }
 }
diff --git a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/monitor/TimerMonitor.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/monitor/TimerMonitor.java
index 7d4fffe..33ba8ab 100644
--- a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/monitor/TimerMonitor.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/monitor/TimerMonitor.java
@@ -15,17 +15,16 @@
  * limitations under the License.
  */
 
-package org.apache.servicecomb.metrics.core.monitor;
-
-import java.util.concurrent.TimeUnit;
-
-import org.apache.servicecomb.metrics.common.TimerMetric;
+package io.servicecomb.metrics.core.monitor;
 
 import com.netflix.servo.monitor.MaxGauge;
 import com.netflix.servo.monitor.MinGauge;
 import com.netflix.servo.monitor.MonitorConfig;
 import com.netflix.servo.monitor.StepCounter;
 
+import io.servicecomb.metrics.common.TimerMetric;
+import io.servicecomb.metrics.core.utils.MonitorUtils;
+
 public class TimerMonitor {
   private final String prefix;
 
@@ -59,22 +58,9 @@ public class TimerMonitor {
 
   public TimerMetric toMetric(int windowTimeIndex) {
     return new TimerMetric(this.prefix,
-        this.convertNanosecondToMillisecond(this.adjustValue(total.getCount(windowTimeIndex))),
-        this.adjustValue(count.getCount(windowTimeIndex)),
-        this.convertNanosecondToMillisecond(this.adjustValue(min.getValue(windowTimeIndex))),
-        this.convertNanosecondToMillisecond(this.adjustValue(max.getValue(windowTimeIndex))));
-  }
-
-  //for time-related monitor type, if stop poll value over one window time,
-  //the value may return -1 because servo can't known precise value of previous step
-  //so must change to return 0
-  public long adjustValue(long value) {
-    return value < 0 ? 0 : value;
-  }
-
-  //Counting use System.nano get more precise time
-  //so we need change unit to millisecond when ouput
-  public long convertNanosecondToMillisecond(long nanoValue) {
-    return TimeUnit.NANOSECONDS.toMillis(nanoValue);
+        MonitorUtils.convertNanosecondToMillisecond(MonitorUtils.adjustValue(total.getCount(windowTimeIndex))),
+        MonitorUtils.adjustValue(count.getCount(windowTimeIndex)),
+        MonitorUtils.convertNanosecondToMillisecond(MonitorUtils.adjustValue(min.getValue(windowTimeIndex))),
+        MonitorUtils.convertNanosecondToMillisecond(MonitorUtils.adjustValue(max.getValue(windowTimeIndex))));
   }
 }
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 4cebd32..ba8c126 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
@@ -134,7 +134,7 @@ public class HighwayServerInvoke {
     invocation.next(response -> {
       sendResponse(invocation.getContext(), response);
       endMetrics(invocation);
-      invocation.triggerFinishedEvent();
+      invocation.triggerFinishedEvent(response.isSuccessed());
     });
   }
 

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