You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by wu...@apache.org on 2019/02/26 08:15:30 UTC

[servicecomb-java-chassis] branch master updated: [SCB-1149] remove VerxImplEx, remove the eventloop counter function

This is an automated email from the ASF dual-hosted git repository.

wujimin 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 e8f1af7  [SCB-1149] remove VerxImplEx, remove the eventloop counter function
e8f1af7 is described below

commit e8f1af7de2b74d0dbe206361e655b6c5fa69492b
Author: weixing <je...@163.com>
AuthorDate: Mon Feb 25 19:31:45 2019 +0800

    [SCB-1149] remove VerxImplEx, remove the eventloop counter function
---
 .../main/java/io/vertx/core/impl/VertxImplEx.java  | 61 ----------------------
 .../servicecomb/foundation/vertx/VertxUtils.java   | 32 ++++++++++--
 .../java/io/vertx/core/impl/TestVertxImplEx.java   | 47 -----------------
 .../metrics/core/publish/DefaultLogPublisher.java  |  7 +--
 .../metrics/core/TestVertxMetersInitializer.java   | 11 +++-
 .../core/publish/TestDefaultLogPublisher.java      | 13 ++---
 6 files changed, 47 insertions(+), 124 deletions(-)

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
deleted file mode 100644
index 2ea0182..0000000
--- a/foundations/foundation-vertx/src/main/java/io/vertx/core/impl/VertxImplEx.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * 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 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;
-import io.vertx.core.net.impl.transport.Transport;
-
-public class VertxImplEx extends VertxImpl {
-  private AtomicLong eventLoopContextCreated = new AtomicLong();
-
-  public VertxImplEx(String name, VertxOptions vertxOptions) {
-    super(vertxOptions, Transport.transport(vertxOptions.getPreferNativeTransport()));
-    init();
-    if (StringUtils.isEmpty(name)) {
-      return;
-    }
-
-    Field field = ReflectionUtils.findField(VertxImpl.class, "eventLoopThreadFactory");
-    field.setAccessible(true);
-    VertxThreadFactory eventLoopThreadFactory = (VertxThreadFactory) ReflectionUtils.getField(field, this);
-
-    field = ReflectionUtils.findField(eventLoopThreadFactory.getClass(), "prefix");
-    field.setAccessible(true);
-
-    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 8861a79..bb6ff4a 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
@@ -20,6 +20,8 @@ package org.apache.servicecomb.foundation.vertx;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.management.ManagementFactory;
+import java.lang.reflect.Field;
+import java.lang.reflect.Proxy;
 import java.util.Map;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CountDownLatch;
@@ -33,6 +35,8 @@ import org.apache.servicecomb.foundation.vertx.client.ClientVerticle;
 import org.apache.servicecomb.foundation.vertx.stream.BufferInputStream;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.util.ReflectionUtils;
+import org.springframework.util.StringUtils;
 
 import io.netty.buffer.ByteBuf;
 import io.vertx.core.AbstractVerticle;
@@ -42,7 +46,8 @@ import io.vertx.core.Vertx;
 import io.vertx.core.VertxOptions;
 import io.vertx.core.buffer.Buffer;
 import io.vertx.core.file.impl.FileResolver;
-import io.vertx.core.impl.VertxImplEx;
+import io.vertx.core.impl.VertxImpl;
+import io.vertx.core.impl.VertxThreadFactory;
 import io.vertx.core.logging.SLF4JLogDelegateFactory;
 
 /**
@@ -62,12 +67,12 @@ public final class VertxUtils {
   private static final long BLOCKED_THREAD_CHECK_INTERVAL = Long.MAX_VALUE / 2;
 
   // key为vertx实例名称,以支撑vertx功能分组
-  private static Map<String, VertxImplEx> vertxMap = new ConcurrentHashMapEx<>();
+  private static Map<String, Vertx> vertxMap = new ConcurrentHashMapEx<>();
 
   private VertxUtils() {
   }
 
-  public static Map<String, VertxImplEx> getVertxMap() {
+  public static Map<String, Vertx> getVertxMap() {
     return vertxMap;
   }
 
@@ -111,7 +116,7 @@ public final class VertxUtils {
   }
 
   public static Vertx getOrCreateVertxByName(String name, VertxOptions vertxOptions) {
-    return vertxMap.computeIfAbsent(name, vertxName -> (VertxImplEx) init(vertxName, vertxOptions));
+    return vertxMap.computeIfAbsent(name, vertxName -> init(vertxName, vertxOptions));
   }
 
   public static Vertx init(VertxOptions vertxOptions) {
@@ -130,7 +135,24 @@ public final class VertxUtils {
     }
 
     configureVertxFileCaching();
-    return new VertxImplEx(name, vertxOptions);
+    Vertx vertx = Vertx.vertx(vertxOptions);
+    enhanceVertx(name, vertx);
+    return vertx;
+  }
+
+  private static void enhanceVertx(String name, Vertx vertx) {
+    if (StringUtils.isEmpty(name)) {
+      return;
+    }
+    Field field = ReflectionUtils.findField(VertxImpl.class, "eventLoopThreadFactory");
+    field.setAccessible(true);
+    VertxThreadFactory eventLoopThreadFactory = (VertxThreadFactory) ReflectionUtils.getField(field, vertx);
+
+    field = ReflectionUtils.findField(eventLoopThreadFactory.getClass(), "prefix");
+    field.setAccessible(true);
+
+    String prefix = (String) ReflectionUtils.getField(field, eventLoopThreadFactory);
+    ReflectionUtils.setField(field, eventLoopThreadFactory, name + "-" + prefix);
   }
 
   /**
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
deleted file mode 100644
index 733d150..0000000
--- a/foundations/foundation-vertx/src/test/java/io/vertx/core/impl/TestVertxImplEx.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * 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());
-  }
-}
diff --git a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/publish/DefaultLogPublisher.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/publish/DefaultLogPublisher.java
index f7ed8d2..fbae1fd 100644
--- a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/publish/DefaultLogPublisher.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/publish/DefaultLogPublisher.java
@@ -48,7 +48,7 @@ import com.google.common.eventbus.Subscribe;
 import com.netflix.config.DynamicPropertyFactory;
 import com.netflix.spectator.api.Meter;
 
-import io.vertx.core.impl.VertxImplEx;
+import io.vertx.core.Vertx;
 
 public class DefaultLogPublisher implements MetricsInitializer {
   private static final Logger LOGGER = LoggerFactory.getLogger(DefaultLogPublisher.class);
@@ -432,10 +432,11 @@ public class DefaultLogPublisher implements MetricsInitializer {
 
     appendLine(sb, "  instances:");
     appendLine(sb, "    name       eventLoopContext-created");
-    for (Entry<String, VertxImplEx> entry : VertxUtils.getVertxMap().entrySet()) {
+    for (Entry<String, Vertx> entry : VertxUtils.getVertxMap().entrySet()) {
       appendLine(sb, "    %-10s %d",
           entry.getKey(),
-          entry.getValue().getEventLoopContextCreatedCount());
+          // TODO will be fixed by next vertx update.entry.getValue().getEventLoopContextCreatedCount()
+          0);
     }
 
     ClientEndpointsLogPublisher client = new ClientEndpointsLogPublisher(tree, sb,
diff --git a/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestVertxMetersInitializer.java b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestVertxMetersInitializer.java
index 6f5b9d5..055e2d5 100644
--- a/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestVertxMetersInitializer.java
+++ b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestVertxMetersInitializer.java
@@ -119,6 +119,14 @@ public class TestVertxMetersInitializer {
         result = transportVertxFactory;
       }
     };
+    // TODO will be fixed by next vertx update.
+//    new Expectations(VertxUtils.class) {
+//      {
+
+//        VertxUtils.getEventLoopContextCreatedCount(anyString);
+//        result = 4;
+//      }
+//    };
 
     globalRegistry.add(registry);
     vertxMetersInitializer.init(globalRegistry, eventBus, null);
@@ -158,7 +166,7 @@ public class TestVertxMetersInitializer {
     String expect = "vertx:\n"
         + "  instances:\n"
         + "    name       eventLoopContext-created\n"
-        + "    transport  4\n"
+        + "    transport  0\n"
         + "  transport:\n"
         + "    client.endpoints:\n"
         + "      remote                connectCount    disconnectCount connections     send(Bps)    receive(Bps)\n";
@@ -172,7 +180,6 @@ public class TestVertxMetersInitializer {
         + "      listen                connectCount    disconnectCount rejectByLimit   connections  send(Bps)    receive(Bps)\n"
         + "      0.0.0.0:0             1               0               0               1            21           4           \n"
         + "      (summary)             1               0               0               1            21           4           \n\n";
-
     Assert.assertEquals(expect, actual);
   }
 }
diff --git a/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/publish/TestDefaultLogPublisher.java b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/publish/TestDefaultLogPublisher.java
index 622ca46..10528ef 100644
--- a/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/publish/TestDefaultLogPublisher.java
+++ b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/publish/TestDefaultLogPublisher.java
@@ -50,7 +50,7 @@ import org.junit.Test;
 import com.google.common.eventbus.EventBus;
 import com.netflix.spectator.api.Measurement;
 
-import io.vertx.core.impl.VertxImplEx;
+import io.vertx.core.impl.VertxImpl;
 import mockit.Expectations;
 import mockit.Mock;
 import mockit.MockUp;
@@ -132,13 +132,14 @@ public class TestDefaultLogPublisher {
   }
 
   @Test
-  public void onPolledEvent(@Mocked VertxImplEx vertxImplEx, @Mocked MeasurementTree tree) {
+  public void onPolledEvent(@Mocked VertxImpl vertxImpl, @Mocked MeasurementTree tree) {
     new Expectations(VertxUtils.class) {
       {
         VertxUtils.getVertxMap();
-        result = Collections.singletonMap("v", vertxImplEx);
-        vertxImplEx.getEventLoopContextCreatedCount();
-        result = 1;
+        result = Collections.singletonMap("v", vertxImpl);
+        // TODO will be fixed by next vertx update.
+//        vertxImpl.getEventLoopContextCreatedCount();;
+//        result = 1;
       }
     };
 
@@ -245,7 +246,7 @@ public class TestDefaultLogPublisher {
             + "vertx:\n"
             + "  instances:\n"
             + "    name       eventLoopContext-created\n"
-            + "    v          1\n"
+            + "    v          0\n"
             + "threadPool:\n"
             + "  corePoolSize maxThreads poolSize currentThreadsBusy queueSize taskCount completedTaskCount name\n"
             + "  0            0          0        0                  0         0.0       0.0                test\n"