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

commit 8a7cc9f03a4a5379ca22ca16c757db165f3624aa
Author: wujimin <wuji...@huawei.com>
AuthorDate: Wed Apr 4 09:05:33 2018 +0800

    SCB-422 split create registry and create invocation meters to different 
initializer
---
 .../metrics/core/DefaultRegistryInitializer.java   | 64 ++++++++++++++++++++++
 ...lizer.java => InvocationMetersInitializer.java} | 24 +++-----
 ...rvicecomb.foundation.metrics.MetricsInitializer |  3 +-
 .../core/TestDefaultRegistryInitializer.java       | 57 +++++++++++++++++++
 ...r.java => TestInvocationMetersInitializer.java} | 39 ++++++-------
 5 files changed, 150 insertions(+), 37 deletions(-)

diff --git 
a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultRegistryInitializer.java
 
b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultRegistryInitializer.java
new file mode 100644
index 0000000..64650de
--- /dev/null
+++ 
b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultRegistryInitializer.java
@@ -0,0 +1,64 @@
+/*
+ * 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.metrics.core;
+
+import org.apache.servicecomb.foundation.metrics.MetricsBootstrapConfig;
+import org.apache.servicecomb.foundation.metrics.MetricsInitializer;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.servo.DefaultMonitorRegistry;
+import com.netflix.spectator.api.CompositeRegistry;
+import com.netflix.spectator.api.Registry;
+import com.netflix.spectator.servo.ServoRegistry;
+
+public class DefaultRegistryInitializer implements MetricsInitializer {
+  public static final String METRICS_WINDOW_TIME = 
"servicecomb.metrics.window_time";
+
+  public static final int DEFAULT_METRICS_WINDOW_TIME = 5000;
+
+  public static final String SERVO_POLLERS = "servo.pollers";
+
+  private CompositeRegistry globalRegistry;
+
+  private ServoRegistry registry;
+
+  // create registry before init meters
+  @Override
+  public int getOrder() {
+    return -10;
+  }
+
+  @Override
+  public void init(CompositeRegistry globalRegistry, EventBus eventBus, 
MetricsBootstrapConfig config) {
+    this.globalRegistry = globalRegistry;
+
+    System.getProperties().setProperty(SERVO_POLLERS, 
String.valueOf(config.getMsPollInterval()));
+    registry = new ServoRegistry();
+
+    globalRegistry.add(registry);
+  }
+
+  @Override
+  public void uninit() {
+    DefaultMonitorRegistry.getInstance().unregister(registry);
+    globalRegistry.remove(registry);
+  }
+
+  public Registry getRegistry() {
+    return registry;
+  }
+}
diff --git 
a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultMetricsInitializer.java
 
b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/InvocationMetersInitializer.java
similarity index 78%
rename from 
metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultMetricsInitializer.java
rename to 
metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/InvocationMetersInitializer.java
index 12e090e..e040ec2 100644
--- 
a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultMetricsInitializer.java
+++ 
b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/InvocationMetersInitializer.java
@@ -19,6 +19,7 @@ package org.apache.servicecomb.metrics.core;
 import org.apache.servicecomb.core.Invocation;
 import org.apache.servicecomb.core.event.InvocationFinishEvent;
 import org.apache.servicecomb.core.event.InvocationStartEvent;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
 import org.apache.servicecomb.foundation.metrics.MetricsBootstrapConfig;
 import org.apache.servicecomb.foundation.metrics.MetricsInitializer;
 import org.apache.servicecomb.metrics.core.meter.ConsumerMeters;
@@ -30,35 +31,24 @@ import com.google.common.eventbus.EventBus;
 import com.google.common.eventbus.Subscribe;
 import com.netflix.spectator.api.CompositeRegistry;
 import com.netflix.spectator.api.Registry;
-import com.netflix.spectator.servo.ServoRegistry;
-
-public class DefaultMetricsInitializer implements MetricsInitializer {
-  public static final String METRICS_WINDOW_TIME = 
"servicecomb.metrics.window_time";
-
-  public static final int DEFAULT_METRICS_WINDOW_TIME = 5000;
-
-  private Registry registry;
 
+public class InvocationMetersInitializer implements MetricsInitializer {
   private ConsumerMeters consumerMeters;
 
   private ProducerMeters producerMeters;
 
   @Override
   public void init(CompositeRegistry globalRegistry, EventBus eventBus, 
MetricsBootstrapConfig config) {
-    registry = createRegistry(config);
+    DefaultRegistryInitializer defaultRegistryInitializer =
+        SPIServiceUtils.getTargetService(MetricsInitializer.class, 
DefaultRegistryInitializer.class);
+    Registry registry = defaultRegistryInitializer.getRegistry();
 
-    this.consumerMeters = new ConsumerMeters(registry);
-    this.producerMeters = new ProducerMeters(registry);
+    consumerMeters = new ConsumerMeters(registry);
+    producerMeters = new ProducerMeters(registry);
 
-    globalRegistry.add(registry);
     eventBus.register(this);
   }
 
-  protected Registry createRegistry(MetricsBootstrapConfig config) {
-    System.getProperties().setProperty("servo.pollers", 
String.valueOf(config.getMsPollInterval()));
-    return new ServoRegistry();
-  }
-
   protected AbstractInvocationMeters findInvocationMeters(Invocation 
invocation) {
     if (invocation.isConsumer()) {
       return consumerMeters.getInvocationMeters();
diff --git 
a/metrics/metrics-core/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.metrics.MetricsInitializer
 
b/metrics/metrics-core/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.metrics.MetricsInitializer
index fb6c466..ecd13da 100644
--- 
a/metrics/metrics-core/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.metrics.MetricsInitializer
+++ 
b/metrics/metrics-core/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.metrics.MetricsInitializer
@@ -15,6 +15,7 @@
 # limitations under the License.
 #
 
-org.apache.servicecomb.metrics.core.DefaultMetricsInitializer
+org.apache.servicecomb.metrics.core.DefaultRegistryInitializer
+org.apache.servicecomb.metrics.core.InvocationMetersInitializer
 org.apache.servicecomb.metrics.core.publish.DefaultLogPublisher
 org.apache.servicecomb.metrics.core.publish.MetricsRestPublisher
\ No newline at end of file
diff --git 
a/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestDefaultRegistryInitializer.java
 
b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestDefaultRegistryInitializer.java
new file mode 100644
index 0000000..8692a80
--- /dev/null
+++ 
b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestDefaultRegistryInitializer.java
@@ -0,0 +1,57 @@
+/*
+ * 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.metrics.core;
+
+import java.util.List;
+
+import org.apache.servicecomb.foundation.metrics.MetricsBootstrapConfig;
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.servo.DefaultMonitorRegistry;
+import com.netflix.spectator.api.CompositeRegistry;
+import com.netflix.spectator.api.ManualClock;
+import com.netflix.spectator.api.Registry;
+import com.netflix.spectator.api.SpectatorUtils;
+import com.netflix.spectator.servo.ServoRegistry;
+
+import mockit.Deencapsulation;
+
+public class TestDefaultRegistryInitializer {
+  CompositeRegistry globalRegistry = 
SpectatorUtils.createCompositeRegistry(new ManualClock());
+
+  List<Registry> registries = Deencapsulation.getField(globalRegistry, 
"registries");
+
+  DefaultRegistryInitializer registryInitializer = new 
DefaultRegistryInitializer();
+
+  @Test
+  public void init() {
+    registryInitializer.init(globalRegistry, new EventBus(), new 
MetricsBootstrapConfig());
+
+    Assert.assertEquals(-10, registryInitializer.getOrder());
+    Assert.assertThat(registryInitializer.getRegistry(), 
Matchers.instanceOf(ServoRegistry.class));
+    Assert.assertEquals(1, registries.size());
+    Assert.assertEquals(1, 
DefaultMonitorRegistry.getInstance().getRegisteredMonitors().size());
+
+    registryInitializer.uninit();
+
+    Assert.assertEquals(0, registries.size());
+    Assert.assertEquals(0, 
DefaultMonitorRegistry.getInstance().getRegisteredMonitors().size());
+  }
+}
diff --git 
a/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestDefaultMetricsInitializer.java
 
b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestInvocationMetersInitializer.java
similarity index 82%
rename from 
metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestDefaultMetricsInitializer.java
rename to 
metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestInvocationMetersInitializer.java
index 2c48866..e990e9c 100644
--- 
a/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestDefaultMetricsInitializer.java
+++ 
b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestInvocationMetersInitializer.java
@@ -19,7 +19,8 @@ package org.apache.servicecomb.metrics.core;
 import org.apache.servicecomb.core.Const;
 import org.apache.servicecomb.core.Invocation;
 import org.apache.servicecomb.core.event.InvocationFinishEvent;
-import org.apache.servicecomb.foundation.metrics.MetricsBootstrapConfig;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
+import org.apache.servicecomb.foundation.metrics.MetricsInitializer;
 import 
org.apache.servicecomb.foundation.metrics.publish.spectator.MeasurementGroupConfig;
 import 
org.apache.servicecomb.foundation.metrics.publish.spectator.MeasurementTree;
 import 
org.apache.servicecomb.metrics.core.meter.invocation.MeterInvocationConst;
@@ -30,40 +31,40 @@ import org.junit.Before;
 import org.junit.Test;
 
 import com.google.common.eventbus.EventBus;
-import com.netflix.spectator.api.CompositeRegistry;
 import com.netflix.spectator.api.DefaultRegistry;
 import com.netflix.spectator.api.ManualClock;
 import com.netflix.spectator.api.Registry;
-import com.netflix.spectator.api.SpectatorUtils;
 
 import mockit.Expectations;
 import mockit.Mocked;
 
-public class TestDefaultMetricsInitializer {
+public class TestInvocationMetersInitializer {
   EventBus eventBus = new EventBus();
 
-  ManualClock clock = new ManualClock();
+  Registry registry = new DefaultRegistry(new ManualClock());
 
-  CompositeRegistry globalRegistry = 
SpectatorUtils.createCompositeRegistry(new ManualClock());
-
-  DefaultMetricsInitializer metricsInitializer = new 
DefaultMetricsInitializer() {
-    protected Registry createRegistry(MetricsBootstrapConfig config) {
-      return new DefaultRegistry(new ManualClock());
-    };
-  };
-
-  MetricsBootstrapConfig config = new MetricsBootstrapConfig();
+  InvocationMetersInitializer invocationMetersInitializer = new 
InvocationMetersInitializer();
 
   @Mocked
-  private Invocation invocation;
+  Invocation invocation;
 
   @Mocked
-  private Response response;
+  Response response;
 
+  @Mocked
+  DefaultRegistryInitializer defaultRegistryInitializer;
 
   @Before
   public void setup() {
-    metricsInitializer.init(globalRegistry, eventBus, config);
+    new Expectations(SPIServiceUtils.class) {
+      {
+        SPIServiceUtils.getTargetService(MetricsInitializer.class, 
DefaultRegistryInitializer.class);
+        result = defaultRegistryInitializer;
+        defaultRegistryInitializer.getRegistry();
+        result = registry;
+      }
+    };
+    invocationMetersInitializer.init(null, eventBus, null);
   }
 
   @Test
@@ -91,7 +92,7 @@ public class TestDefaultMetricsInitializer {
     eventBus.post(event);
 
     MeasurementTree tree = new MeasurementTree();
-    tree.from(globalRegistry.iterator(), new 
MeasurementGroupConfig(MeterInvocationConst.INVOCATION_NAME));
+    tree.from(registry.iterator(), new 
MeasurementGroupConfig(MeterInvocationConst.INVOCATION_NAME));
     Assert.assertEquals(
         
"[Measurement(servicecomb.invocation:operation=m.s.o:role=CONSUMER:stage=total:statistic=count:status=0:transport=rest,0,2.0),
 "
             + 
"Measurement(servicecomb.invocation:operation=m.s.o:role=CONSUMER:stage=total:statistic=totalTime:status=0:transport=rest,0,18.0)]",
@@ -125,7 +126,7 @@ public class TestDefaultMetricsInitializer {
     eventBus.post(event);
 
     MeasurementTree tree = new MeasurementTree();
-    tree.from(globalRegistry.iterator(), new 
MeasurementGroupConfig(MeterInvocationConst.INVOCATION_NAME));
+    tree.from(registry.iterator(), new 
MeasurementGroupConfig(MeterInvocationConst.INVOCATION_NAME));
     Assert.assertEquals(
         
"[Measurement(servicecomb.invocation:operation=m.s.o:role=PRODUCER:stage=execution:statistic=count:status=0:transport=rest,0,2.0),
 "
             + 
"Measurement(servicecomb.invocation:operation=m.s.o:role=PRODUCER:stage=execution:statistic=totalTime:status=0:transport=rest,0,14.0),
 "

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

Reply via email to