You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/01/31 02:36:10 UTC

[GitHub] liubao68 closed pull request #533: [SCB-310] Sumary eventloop context created count

liubao68 closed pull request #533: [SCB-310] Sumary eventloop context created count
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/533
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/demo/perf/src/main/java/org/apache/servicecomb/demo/perf/PerfMetricsFilePublisher.java b/demo/perf/src/main/java/org/apache/servicecomb/demo/perf/PerfMetricsFilePublisher.java
index f04fdcdbd..1f00bc248 100644
--- a/demo/perf/src/main/java/org/apache/servicecomb/demo/perf/PerfMetricsFilePublisher.java
+++ b/demo/perf/src/main/java/org/apache/servicecomb/demo/perf/PerfMetricsFilePublisher.java
@@ -18,6 +18,7 @@
 
 import java.util.Map.Entry;
 
+import org.apache.servicecomb.foundation.vertx.VertxUtils;
 import org.apache.servicecomb.metrics.common.ConsumerInvocationMetric;
 import org.apache.servicecomb.metrics.common.MetricsDimension;
 import org.apache.servicecomb.metrics.common.ProducerInvocationMetric;
@@ -26,6 +27,8 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import io.vertx.core.impl.VertxImplEx;
+
 public class PerfMetricsFilePublisher {
   private static final Logger LOGGER = LoggerFactory.getLogger(PerfMetricsFilePublisher.class);
 
@@ -41,6 +44,15 @@ public void onCycle() {
     StringBuilder sb = new StringBuilder();
     sb.append("\n");
 
+    collectSystemMetrics(metric, sb);
+    collectVertxMetrics(metric, sb);
+    collectConsumerMetrics(metric, sb);
+    collectProducerMetrics(metric, sb);
+
+    LOGGER.info(sb.toString());
+  }
+
+  protected void collectSystemMetrics(RegistryMetric metric, StringBuilder sb) {
     double cpu = metric.getInstanceMetric().getSystemMetric().getCpuLoad();
     // can not get cpu usage in windows, so skip this information
     if (cpu >= 0) {
@@ -48,41 +60,67 @@ public void onCycle() {
           .append((long) cpu * Runtime.getRuntime().availableProcessors())
           .append("%\n");
     }
+  }
 
-    sb.append("consumer:\n"
-        + "  total           tps             latency(ms)     name\n");
-    for (Entry<String, ConsumerInvocationMetric> entry : metric.getConsumerMetrics().entrySet()) {
-      String opName = entry.getKey();
-      sb.append(String
-          .format("  %-16d%-16d%-16.3f%s\n",
-              entry.getValue().getConsumerCall()
-                  .getTotalValue(MetricsDimension.DIMENSION_STATUS, MetricsDimension.DIMENSION_STATUS_ALL).getValue(),
-              entry.getValue().getConsumerCall()
-                  .getTpsValue(MetricsDimension.DIMENSION_STATUS, MetricsDimension.DIMENSION_STATUS_ALL).getValue()
-                  .longValue(),
-              entry.getValue().getConsumerLatency().getAverage(),
-              opName));
+  protected void collectVertxMetrics(RegistryMetric metric, StringBuilder sb) {
+    sb.append("vertx:\n")
+        .append("  name       eventLoopContext-created\n");
+    for (Entry<String, VertxImplEx> entry : VertxUtils.getVertxMap().entrySet()) {
+      sb.append(String.format("  %-10s %-19d\n",
+          entry.getKey(),
+          entry.getValue().getEventLoopContextCreatedCount()));
+    }
+  }
+
+  protected void collectProducerMetrics(RegistryMetric metric, StringBuilder sb) {
+    if (metric.getProducerMetrics().isEmpty()) {
+      return;
     }
 
     sb.append("producer:\n"
-        + "  total           tps             latency(ms)     queue(ms)       execute(ms)     name\n");
+        + "  total               tps     latency(ms) queue(ms) execute(ms) name\n");
     for (Entry<String, ProducerInvocationMetric> entry : metric.getProducerMetrics().entrySet()) {
       String opName = entry.getKey();
       sb.append(
-          String.format("  %-16d%-16d%-16.3f%-16.3f%-16.3f%s\n",
-              entry.getValue().getProducerCall()
-                  .getTotalValue(MetricsDimension.DIMENSION_STATUS, MetricsDimension.DIMENSION_STATUS_ALL).getValue(),
-              entry.getValue().getProducerCall()
-                  .getTpsValue(MetricsDimension.DIMENSION_STATUS, MetricsDimension.DIMENSION_STATUS_ALL).getValue()
+          String.format("  %-19d %-7d %-11.3f %-9.3f %-11.3f %s\n",
+              entry.getValue()
+                  .getProducerCall()
+                  .getTotalValue(MetricsDimension.DIMENSION_STATUS, MetricsDimension.DIMENSION_STATUS_ALL)
+                  .getValue(),
+              entry.getValue()
+                  .getProducerCall()
+                  .getTpsValue(MetricsDimension.DIMENSION_STATUS, MetricsDimension.DIMENSION_STATUS_ALL)
+                  .getValue()
                   .longValue(),
               entry.getValue().getProducerLatency().getAverage(),
               entry.getValue().getLifeTimeInQueue().getAverage(),
               entry.getValue().getExecutionTime().getAverage(),
               opName));
     }
+  }
 
-    sb.setLength(sb.length() - 1);
+  protected void collectConsumerMetrics(RegistryMetric metric, StringBuilder sb) {
+    if (metric.getConsumerMetrics().isEmpty()) {
+      return;
+    }
 
-    LOGGER.info(sb.toString());
+    sb.append("consumer:\n"
+        + "  total               tps     latency(ms) name\n");
+    for (Entry<String, ConsumerInvocationMetric> entry : metric.getConsumerMetrics().entrySet()) {
+      String opName = entry.getKey();
+      sb.append(String
+          .format("  %-19d %-7d %-11.3f %s\n",
+              entry.getValue()
+                  .getConsumerCall()
+                  .getTotalValue(MetricsDimension.DIMENSION_STATUS, MetricsDimension.DIMENSION_STATUS_ALL)
+                  .getValue(),
+              entry.getValue()
+                  .getConsumerCall()
+                  .getTpsValue(MetricsDimension.DIMENSION_STATUS, MetricsDimension.DIMENSION_STATUS_ALL)
+                  .getValue()
+                  .longValue(),
+              entry.getValue().getConsumerLatency().getAverage(),
+              opName));
+    }
   }
 }
diff --git a/foundations/foundation-vertx/src/main/java/io/vertx/core/impl/VertxImplEx.java b/foundations/foundation-vertx/src/main/java/io/vertx/core/impl/VertxImplEx.java
index be70aabcd..2020ff3b4 100644
--- a/foundations/foundation-vertx/src/main/java/io/vertx/core/impl/VertxImplEx.java
+++ b/foundations/foundation-vertx/src/main/java/io/vertx/core/impl/VertxImplEx.java
@@ -18,13 +18,17 @@
 package io.vertx.core.impl;
 
 import java.lang.reflect.Field;
+import java.util.concurrent.atomic.AtomicLong;
 
 import org.springframework.util.ReflectionUtils;
 import org.springframework.util.StringUtils;
 
 import io.vertx.core.VertxOptions;
+import io.vertx.core.json.JsonObject;
 
 public class VertxImplEx extends VertxImpl {
+  private AtomicLong eventLoopContextCreated = new AtomicLong();
+
   public VertxImplEx(String name, VertxOptions vertxOptions) {
     super(vertxOptions);
 
@@ -42,4 +46,15 @@ public VertxImplEx(String name, VertxOptions vertxOptions) {
     String prefix = (String) ReflectionUtils.getField(field, eventLoopThreadFactory);
     ReflectionUtils.setField(field, eventLoopThreadFactory, name + "-" + prefix);
   }
+
+  @Override
+  public EventLoopContext createEventLoopContext(String deploymentID, WorkerPool workerPool, JsonObject config,
+      ClassLoader tccl) {
+    eventLoopContextCreated.incrementAndGet();
+    return super.createEventLoopContext(deploymentID, workerPool, config, tccl);
+  }
+
+  public long getEventLoopContextCreatedCount() {
+    return eventLoopContextCreated.get();
+  }
 }
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java
index 14fba7b05..8a31563de 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/VertxUtils.java
@@ -21,12 +21,12 @@
 import java.io.InputStream;
 import java.lang.management.ManagementFactory;
 import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CountDownLatch;
 
 import javax.xml.ws.Holder;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
 import org.apache.servicecomb.foundation.vertx.client.ClientPoolManager;
 import org.apache.servicecomb.foundation.vertx.client.ClientVerticle;
 import org.apache.servicecomb.foundation.vertx.stream.BufferInputStream;
@@ -61,11 +61,15 @@
   private static final long BLOCKED_THREAD_CHECK_INTERVAL = Long.MAX_VALUE / 2;
 
   // key?vertx????????vertx????
-  private static Map<String, Vertx> vertxMap = new ConcurrentHashMap<>();
+  private static Map<String, VertxImplEx> vertxMap = new ConcurrentHashMapEx<>();
 
   private VertxUtils() {
   }
 
+  public static Map<String, VertxImplEx> getVertxMap() {
+    return vertxMap;
+  }
+
   public static <T extends AbstractVerticle> void deployVerticle(Vertx vertx, Class<T> cls, int instanceCount) {
     DeploymentOptions options = new DeploymentOptions().setInstances(instanceCount);
 
@@ -106,18 +110,9 @@ private VertxUtils() {
   }
 
   public static Vertx getOrCreateVertxByName(String name, VertxOptions vertxOptions) {
-    Vertx vertx = getVertxByName(name);
-    if (vertx == null) {
-      synchronized (VertxUtils.class) {
-        vertx = getVertxByName(name);
-        if (vertx == null) {
-          vertx = init(name, vertxOptions);
-          vertxMap.put(name, vertx);
-        }
-      }
-    }
-
-    return vertx;
+    return vertxMap.computeIfAbsent(name, vertxName -> {
+      return (VertxImplEx) init(vertxName, vertxOptions);
+    });
   }
 
   public static Vertx init(VertxOptions vertxOptions) {
diff --git a/foundations/foundation-vertx/src/test/java/io/vertx/core/impl/TestVertxImplEx.java b/foundations/foundation-vertx/src/test/java/io/vertx/core/impl/TestVertxImplEx.java
new file mode 100644
index 000000000..733d15047
--- /dev/null
+++ b/foundations/foundation-vertx/src/test/java/io/vertx/core/impl/TestVertxImplEx.java
@@ -0,0 +1,47 @@
+/*
+ * 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.vertx.core.impl;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import io.vertx.core.VertxOptions;
+import io.vertx.core.json.JsonObject;
+import mockit.Mock;
+import mockit.MockUp;
+import mockit.Mocked;
+
+public class TestVertxImplEx {
+  @Test
+  public void testContextCreatedCount(@Mocked EventLoopContext context) {
+    new MockUp<VertxImpl>() {
+      @Mock
+      EventLoopContext createEventLoopContext(String deploymentID, WorkerPool workerPool, JsonObject config,
+          ClassLoader tccl) {
+        return context;
+      }
+    };
+
+    VertxImplEx vertx = new VertxImplEx("test", new VertxOptions());
+
+    vertx.createEventLoopContext(null, null, null, null);
+    Assert.assertEquals(1, vertx.getEventLoopContextCreatedCount());
+
+    vertx.createEventLoopContext(null, null, null, null);
+    Assert.assertEquals(2, vertx.getEventLoopContextCreatedCount());
+  }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services