From bded7a3a6b97d20eb06a7a3334a1772901334e3e Mon Sep 17 00:00:00 2001 From: x-shadow-man <1494445739@qq.com> Date: Sat, 25 Feb 2023 20:43:26 +0800 Subject: [PATCH 1/6] registry&metadata opt --- .../model/container/AtomicLongContainer.java | 18 ++++ .../container/LongAccumulatorContainer.java | 11 +++ .../model/container/LongContainer.java | 94 ++++++++++++++++++ .../collector/stat/MetadataStatComposite.java | 96 +------------------ .../MetadataMetricsCollectorTest.java | 4 +- .../metadata/MetadataStatCompositeTest.java | 67 +++++++++++++ .../collector/stat/RegistryStatComposite.java | 95 +----------------- .../collector/stat/ServiceKeyMetric.java | 4 - .../collector/RegistryStatCompositeTest.java | 67 +++++++++++++ .../metrics/model/MethodMetricTest.java | 79 --------------- .../model/sample/GaugeMetricSampleTest.java | 63 ------------ 11 files changed, 265 insertions(+), 333 deletions(-) create mode 100644 dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/AtomicLongContainer.java create mode 100644 dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/LongAccumulatorContainer.java create mode 100644 dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/LongContainer.java rename dubbo-metrics/dubbo-metrics-metadata/src/test/java/{metrics/metrics/collector => org/apache/dubbo/metrics/metadata}/MetadataMetricsCollectorTest.java (98%) create mode 100644 dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataStatCompositeTest.java create mode 100644 dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java delete mode 100644 dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/model/MethodMetricTest.java delete mode 100644 dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/model/sample/GaugeMetricSampleTest.java diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/AtomicLongContainer.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/AtomicLongContainer.java new file mode 100644 index 00000000000..9ad57180e48 --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/AtomicLongContainer.java @@ -0,0 +1,18 @@ +package org.apache.dubbo.metrics.model.container; + +import org.apache.dubbo.metrics.model.MetricsKeyWrapper; + +import java.util.concurrent.atomic.AtomicLong; +import java.util.function.BiConsumer; + +public class AtomicLongContainer extends LongContainer { + + public AtomicLongContainer(MetricsKeyWrapper metricsKeyWrapper) { + super(metricsKeyWrapper, AtomicLong::new, (responseTime, longAccumulator) -> longAccumulator.set(responseTime)); + } + + public AtomicLongContainer(MetricsKeyWrapper metricsKeyWrapper, BiConsumer consumerFunc) { + super(metricsKeyWrapper, AtomicLong::new, consumerFunc); + } + +} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/LongAccumulatorContainer.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/LongAccumulatorContainer.java new file mode 100644 index 00000000000..1353ea5f01b --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/LongAccumulatorContainer.java @@ -0,0 +1,11 @@ +package org.apache.dubbo.metrics.model.container; + +import org.apache.dubbo.metrics.model.MetricsKeyWrapper; + +import java.util.concurrent.atomic.LongAccumulator; + +public class LongAccumulatorContainer extends LongContainer { + public LongAccumulatorContainer(MetricsKeyWrapper metricsKeyWrapper, LongAccumulator accumulator) { + super(metricsKeyWrapper, () -> accumulator, (responseTime, longAccumulator) -> longAccumulator.accumulate(responseTime)); + } +} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/LongContainer.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/LongContainer.java new file mode 100644 index 00000000000..f0b5568f24a --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/LongContainer.java @@ -0,0 +1,94 @@ +/* + * 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.dubbo.metrics.model.container; + +import org.apache.dubbo.metrics.model.MetricsKey; +import org.apache.dubbo.metrics.model.MetricsKeyWrapper; +import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.BiConsumer; +import java.util.function.Function; +import java.util.function.Supplier; + +/** + * Long type data container + * @param + */ +public class LongContainer extends ConcurrentHashMap { + + /** + * Provide the metric type name + */ + private final MetricsKeyWrapper metricsKeyWrapper; + /** + * The initial value corresponding to the key is generally 0 of different data types + */ + private final Function initFunc; + /** + * Statistical data calculation function, which can be self-increment, self-decrement, or more complex avg function + */ + private final BiConsumer consumerFunc; + /** + * Data output function required by {@link GaugeMetricSample GaugeMetricSample} + */ + private Function valueSupplier; + + + public LongContainer(MetricsKeyWrapper metricsKeyWrapper, Supplier initFunc, BiConsumer consumerFunc) { + this.metricsKeyWrapper = metricsKeyWrapper; + this.initFunc = s -> initFunc.get(); + this.consumerFunc = consumerFunc; + this.valueSupplier = k -> this.get(k).longValue(); + } + + public boolean specifyType(String type) { + return type.equals(getMetricsKeyWrapper().getType()); + } + + public MetricsKeyWrapper getMetricsKeyWrapper() { + return metricsKeyWrapper; + } + + public boolean isKeyWrapper(MetricsKey metricsKey, String registryOpType) { + return metricsKeyWrapper.isKey(metricsKey, registryOpType); + } + + public Function getInitFunc() { + return initFunc; + } + + public BiConsumer getConsumerFunc() { + return consumerFunc; + } + + public Function getValueSupplier() { + return valueSupplier; + } + + public void setValueSupplier(Function valueSupplier) { + this.valueSupplier = valueSupplier; + } + + @Override + public String toString() { + return "LongContainer{" + + "metricsKeyWrapper=" + metricsKeyWrapper + + '}'; + } +} diff --git a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/stat/MetadataStatComposite.java b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/stat/MetadataStatComposite.java index 83cdbb855c8..b15b4578881 100644 --- a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/stat/MetadataStatComposite.java +++ b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/stat/MetadataStatComposite.java @@ -19,12 +19,15 @@ import org.apache.dubbo.common.utils.ConcurrentHashMapUtils; import org.apache.dubbo.metrics.collector.MetricsCollector; +import org.apache.dubbo.metrics.metadata.event.MetadataEvent; import org.apache.dubbo.metrics.model.ApplicationMetric; import org.apache.dubbo.metrics.model.MetricsCategory; import org.apache.dubbo.metrics.model.MetricsKey; import org.apache.dubbo.metrics.model.MetricsKeyWrapper; +import org.apache.dubbo.metrics.model.container.AtomicLongContainer; +import org.apache.dubbo.metrics.model.container.LongAccumulatorContainer; +import org.apache.dubbo.metrics.model.container.LongContainer; import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; -import org.apache.dubbo.metrics.metadata.event.MetadataEvent; import org.apache.dubbo.metrics.report.MetricsExport; import java.util.ArrayList; @@ -33,9 +36,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.LongAccumulator; -import java.util.function.BiConsumer; -import java.util.function.Function; -import java.util.function.Supplier; import java.util.stream.Collectors; /** @@ -121,92 +121,4 @@ public GaugeMetricSample convertToSample(String applicationName, MetadataEvent.T return new GaugeMetricSample(type.getMetricsKey(), ApplicationMetric.getTagsByName(applicationName), category, targetNumber::get); } - - /** - * Collect Number type data - * - * @param - */ - public static class LongContainer extends ConcurrentHashMap { - - /** - * Provide the metric type name - */ - private final MetricsKeyWrapper metricsKeyWrapper; - /** - * The initial value corresponding to the key is generally 0 of different data types - */ - private final Function initFunc; - /** - * Statistical data calculation function, which can be self-increment, self-decrement, or more complex avg function - */ - private final BiConsumer consumerFunc; - /** - * Data output function required by {@link GaugeMetricSample GaugeMetricSample} - */ - private Function valueSupplier; - - - public LongContainer(MetricsKeyWrapper metricsKeyWrapper, Supplier initFunc, BiConsumer consumerFunc) { - this.metricsKeyWrapper = metricsKeyWrapper; - this.initFunc = s -> initFunc.get(); - this.consumerFunc = consumerFunc; - this.valueSupplier = k -> this.get(k).longValue(); - } - - public boolean specifyType(String type) { - return type.equals(getMetricsKeyWrapper().getType()); - } - - public MetricsKeyWrapper getMetricsKeyWrapper() { - return metricsKeyWrapper; - } - - public boolean isKeyWrapper(MetricsKey metricsKey, String registryOpType) { - return metricsKeyWrapper.isKey(metricsKey,registryOpType); - } - - public Function getInitFunc() { - return initFunc; - } - - public BiConsumer getConsumerFunc() { - return consumerFunc; - } - - public Function getValueSupplier() { - return valueSupplier; - } - - public void setValueSupplier(Function valueSupplier) { - this.valueSupplier = valueSupplier; - } - - @Override - public String toString() { - return "LongContainer{" + - "metricsKeyWrapper=" + metricsKeyWrapper + - '}'; - } - } - - public static class AtomicLongContainer extends LongContainer { - - public AtomicLongContainer(MetricsKeyWrapper metricsKeyWrapper) { - super(metricsKeyWrapper, AtomicLong::new, (responseTime, longAccumulator) -> longAccumulator.set(responseTime)); - } - - public AtomicLongContainer(MetricsKeyWrapper metricsKeyWrapper, BiConsumer consumerFunc) { - super(metricsKeyWrapper, AtomicLong::new, consumerFunc); - } - - } - - public static class LongAccumulatorContainer extends LongContainer { - public LongAccumulatorContainer(MetricsKeyWrapper metricsKeyWrapper, LongAccumulator accumulator) { - super(metricsKeyWrapper, () -> accumulator, (responseTime, longAccumulator) -> longAccumulator.accumulate(responseTime)); - } - } - - } diff --git a/dubbo-metrics/dubbo-metrics-metadata/src/test/java/metrics/metrics/collector/MetadataMetricsCollectorTest.java b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataMetricsCollectorTest.java similarity index 98% rename from dubbo-metrics/dubbo-metrics-metadata/src/test/java/metrics/metrics/collector/MetadataMetricsCollectorTest.java rename to dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataMetricsCollectorTest.java index a07712bccd7..9be209fd174 100644 --- a/dubbo-metrics/dubbo-metrics-metadata/src/test/java/metrics/metrics/collector/MetadataMetricsCollectorTest.java +++ b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataMetricsCollectorTest.java @@ -15,10 +15,8 @@ * limitations under the License. */ -package metrics.metrics.collector; +package org.apache.dubbo.metrics.metadata; -import org.apache.dubbo.common.logger.ErrorTypeAwareLogger; -import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.config.ApplicationConfig; import org.apache.dubbo.metrics.event.GlobalMetricsEventMulticaster; import org.apache.dubbo.metrics.metadata.collector.MetadataMetricsCollector; diff --git a/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataStatCompositeTest.java b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataStatCompositeTest.java new file mode 100644 index 00000000000..9218ba431af --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataStatCompositeTest.java @@ -0,0 +1,67 @@ +/* + * 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.dubbo.metrics.metadata; + +import org.apache.dubbo.metrics.metadata.collector.stat.MetadataStatComposite; +import org.apache.dubbo.metrics.metadata.event.MetadataEvent; +import org.apache.dubbo.metrics.model.container.LongContainer; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; + +import static org.apache.dubbo.metrics.metadata.collector.stat.MetadataStatComposite.OP_TYPE_SUBSCRIBE; + +public class MetadataStatCompositeTest { + + private final String applicationName = "app1"; + + @Test + void testInit() { + MetadataStatComposite statComposite = new MetadataStatComposite(); + Assertions.assertEquals(statComposite.numStats.size(), MetadataEvent.Type.values().length); + //(rt)5 * (push,subscribe)2 + Assertions.assertEquals(statComposite.rtStats.size(), 5 * 2); + statComposite.numStats.values().forEach((v -> + Assertions.assertEquals(v, new ConcurrentHashMap<>()))); + statComposite.rtStats.forEach(rtContainer -> + { + for (Map.Entry entry : rtContainer.entrySet()) { + Assertions.assertEquals(rtContainer.getValueSupplier().apply(entry.getKey()), 0L); + } + }); + } + + @Test + void testIncrement() { + MetadataStatComposite statComposite = new MetadataStatComposite(); + statComposite.increment(MetadataEvent.Type.P_TOTAL, applicationName); + Assertions.assertEquals(statComposite.numStats.get(MetadataEvent.Type.P_TOTAL).get(applicationName).get(), 1L); + } + + @Test + void testCalcRt() { + MetadataStatComposite statComposite = new MetadataStatComposite(); + statComposite.calcRt(applicationName, OP_TYPE_SUBSCRIBE, 10L); + Assertions.assertTrue(statComposite.rtStats.stream().anyMatch(longContainer -> longContainer.specifyType(OP_TYPE_SUBSCRIBE))); + Optional> subContainer = statComposite.rtStats.stream().filter(longContainer -> longContainer.specifyType(OP_TYPE_SUBSCRIBE)).findFirst(); + subContainer.ifPresent(v -> Assertions.assertEquals(v.get(applicationName).longValue(), 10L)); + } +} diff --git a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/stat/RegistryStatComposite.java b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/stat/RegistryStatComposite.java index 1d955cd7a7c..651672e9f40 100644 --- a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/stat/RegistryStatComposite.java +++ b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/stat/RegistryStatComposite.java @@ -23,6 +23,9 @@ import org.apache.dubbo.metrics.model.MetricsCategory; import org.apache.dubbo.metrics.model.MetricsKey; import org.apache.dubbo.metrics.model.MetricsKeyWrapper; +import org.apache.dubbo.metrics.model.container.AtomicLongContainer; +import org.apache.dubbo.metrics.model.container.LongAccumulatorContainer; +import org.apache.dubbo.metrics.model.container.LongContainer; import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; import org.apache.dubbo.metrics.registry.event.RegistryEvent; import org.apache.dubbo.metrics.report.MetricsExport; @@ -33,9 +36,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.LongAccumulator; -import java.util.function.BiConsumer; -import java.util.function.Function; -import java.util.function.Supplier; import java.util.stream.Collectors; /** @@ -148,93 +148,4 @@ public List exportSkMetrics() { public GaugeMetricSample convertToSample(String applicationName, RegistryEvent.Type type, MetricsCategory category, AtomicLong targetNumber) { return new GaugeMetricSample(type.getMetricsKey(), ApplicationMetric.getTagsByName(applicationName), category, targetNumber::get); } - - - /** - * Collect Number type data - * - * @param - */ - public static class LongContainer extends ConcurrentHashMap { - - /** - * Provide the metric type name - */ - private final MetricsKeyWrapper metricsKeyWrapper; - /** - * The initial value corresponding to the key is generally 0 of different data types - */ - private final Function initFunc; - /** - * Statistical data calculation function, which can be self-increment, self-decrement, or more complex avg function - */ - private final BiConsumer consumerFunc; - /** - * Data output function required by {@link GaugeMetricSample GaugeMetricSample} - */ - private Function valueSupplier; - - - public LongContainer(MetricsKeyWrapper metricsKeyWrapper, Supplier initFunc, BiConsumer consumerFunc) { - this.metricsKeyWrapper = metricsKeyWrapper; - this.initFunc = s -> initFunc.get(); - this.consumerFunc = consumerFunc; - this.valueSupplier = k -> this.get(k).longValue(); - } - - public boolean specifyType(String type) { - return type.equals(getMetricsKeyWrapper().getType()); - } - - public MetricsKeyWrapper getMetricsKeyWrapper() { - return metricsKeyWrapper; - } - - public boolean isKeyWrapper(MetricsKey metricsKey, String registryOpType) { - return metricsKeyWrapper.isKey(metricsKey,registryOpType); - } - - public Function getInitFunc() { - return initFunc; - } - - public BiConsumer getConsumerFunc() { - return consumerFunc; - } - - public Function getValueSupplier() { - return valueSupplier; - } - - public void setValueSupplier(Function valueSupplier) { - this.valueSupplier = valueSupplier; - } - - @Override - public String toString() { - return "LongContainer{" + - "metricsKeyWrapper=" + metricsKeyWrapper + - '}'; - } - } - - public static class AtomicLongContainer extends LongContainer { - - public AtomicLongContainer(MetricsKeyWrapper metricsKeyWrapper) { - super(metricsKeyWrapper, AtomicLong::new, (responseTime, longAccumulator) -> longAccumulator.set(responseTime)); - } - - public AtomicLongContainer(MetricsKeyWrapper metricsKeyWrapper, BiConsumer consumerFunc) { - super(metricsKeyWrapper, AtomicLong::new, consumerFunc); - } - - } - - public static class LongAccumulatorContainer extends LongContainer { - public LongAccumulatorContainer(MetricsKeyWrapper metricsKeyWrapper, LongAccumulator accumulator) { - super(metricsKeyWrapper, () -> accumulator, (responseTime, longAccumulator) -> longAccumulator.accumulate(responseTime)); - } - } - - } diff --git a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/stat/ServiceKeyMetric.java b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/stat/ServiceKeyMetric.java index 0b39ff8376e..f77fe9e5f1b 100644 --- a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/stat/ServiceKeyMetric.java +++ b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/stat/ServiceKeyMetric.java @@ -41,10 +41,6 @@ public ServiceKeyMetric(String applicationName, String serviceKey) { this.serviceKey = serviceKey; } - public String getServiceKey() { - return serviceKey; - } - public Map getTags() { Map tags = new HashMap<>(); tags.put(TAG_IP, getLocalHost()); diff --git a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java new file mode 100644 index 00000000000..b7bf96daf3f --- /dev/null +++ b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java @@ -0,0 +1,67 @@ +/* + * 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.dubbo.metrics.registry.metrics.collector; + +import org.apache.dubbo.metrics.model.container.LongContainer; +import org.apache.dubbo.metrics.registry.collector.stat.RegistryStatComposite; +import org.apache.dubbo.metrics.registry.event.RegistryEvent; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; + +import static org.apache.dubbo.metrics.registry.collector.stat.RegistryStatComposite.OP_TYPE_NOTIFY; + +public class RegistryStatCompositeTest { + + private final String applicationName = "app1"; + + @Test + void testInit() { + RegistryStatComposite statComposite = new RegistryStatComposite(); + Assertions.assertEquals(statComposite.numStats.size(), RegistryEvent.Type.values().length); + //(rt)5 * (register,subscribe,notify)3 + Assertions.assertEquals(statComposite.rtStats.size(), 5 * 3); + statComposite.numStats.values().forEach((v -> + Assertions.assertEquals(v, new ConcurrentHashMap<>()))); + statComposite.rtStats.forEach(rtContainer -> + { + for (Map.Entry entry : rtContainer.entrySet()) { + Assertions.assertEquals(rtContainer.getValueSupplier().apply(entry.getKey()), 0L); + } + }); + } + + @Test + void testIncrement() { + RegistryStatComposite statComposite = new RegistryStatComposite(); + statComposite.increment(RegistryEvent.Type.R_TOTAL, applicationName); + Assertions.assertEquals(statComposite.numStats.get(RegistryEvent.Type.R_TOTAL).get(applicationName).get(), 1L); + } + + @Test + void testCalcRt() { + RegistryStatComposite statComposite = new RegistryStatComposite(); + statComposite.calcRt(applicationName, OP_TYPE_NOTIFY, 10L); + Assertions.assertTrue(statComposite.rtStats.stream().anyMatch(longContainer -> longContainer.specifyType(OP_TYPE_NOTIFY))); + Optional> subContainer = statComposite.rtStats.stream().filter(longContainer -> longContainer.specifyType(OP_TYPE_NOTIFY)).findFirst(); + subContainer.ifPresent(v -> Assertions.assertEquals(v.get(applicationName).longValue(), 10L)); + } +} diff --git a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/model/MethodMetricTest.java b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/model/MethodMetricTest.java deleted file mode 100644 index 44beb3a6080..00000000000 --- a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/model/MethodMetricTest.java +++ /dev/null @@ -1,79 +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 org.apache.dubbo.metrics.registry.metrics.model; - -import org.apache.dubbo.metrics.model.MethodMetric; -import org.apache.dubbo.rpc.RpcInvocation; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import java.util.Map; - -import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; -import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_NAME; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_GROUP_KEY; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_HOSTNAME; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_INTERFACE_KEY; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_IP; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_METHOD_KEY; -import static org.apache.dubbo.common.constants.MetricsConstants.TAG_VERSION_KEY; -import static org.apache.dubbo.common.utils.NetUtils.getLocalHost; -import static org.apache.dubbo.common.utils.NetUtils.getLocalHostName; - -class MethodMetricTest { - - private static final String applicationName = null; - private static String interfaceName; - private static String methodName; - private static String group; - private static String version; - private static RpcInvocation invocation; - - @BeforeAll - public static void setup() { - interfaceName = "org.apache.dubbo.MockInterface"; - methodName = "mockMethod"; - group = "mockGroup"; - version = "1.0.0"; - invocation = new RpcInvocation(methodName, interfaceName, "serviceKey", null, null); - invocation.setTargetServiceUniqueName(group + "/" + interfaceName + ":" + version); - invocation.setAttachment(GROUP_KEY, group); - invocation.setAttachment(VERSION_KEY, version); - } - - @Test - void test() { - MethodMetric metric = new MethodMetric(applicationName, invocation); - Assertions.assertEquals(metric.getInterfaceName(), interfaceName); - Assertions.assertEquals(metric.getMethodName(), methodName); - Assertions.assertEquals(metric.getGroup(), group); - Assertions.assertEquals(metric.getVersion(), version); - - Map tags = metric.getTags(); - Assertions.assertEquals(tags.get(TAG_IP), getLocalHost()); - Assertions.assertEquals(tags.get(TAG_HOSTNAME), getLocalHostName()); - Assertions.assertEquals(tags.get(TAG_APPLICATION_NAME), applicationName); - - Assertions.assertEquals(tags.get(TAG_INTERFACE_KEY), interfaceName); - Assertions.assertEquals(tags.get(TAG_METHOD_KEY), methodName); - Assertions.assertEquals(tags.get(TAG_GROUP_KEY), group); - Assertions.assertEquals(tags.get(TAG_VERSION_KEY), version); - } -} diff --git a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/model/sample/GaugeMetricSampleTest.java b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/model/sample/GaugeMetricSampleTest.java deleted file mode 100644 index bfa6d2e1765..00000000000 --- a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/model/sample/GaugeMetricSampleTest.java +++ /dev/null @@ -1,63 +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 org.apache.dubbo.metrics.registry.metrics.model.sample; - -import org.apache.dubbo.metrics.model.MetricsCategory; -import org.apache.dubbo.metrics.model.sample.GaugeMetricSample; -import org.apache.dubbo.metrics.model.sample.MetricSample; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import java.util.HashMap; -import java.util.Map; -import java.util.function.Supplier; - -class GaugeMetricSampleTest { - - private static String name; - private static String description; - private static Map tags; - private static MetricsCategory category; - private static String baseUnit; - private static Supplier supplier; - - @BeforeAll - public static void setup() { - name = "test"; - description = "test"; - tags = new HashMap<>(); - category = MetricsCategory.REQUESTS; - baseUnit = "byte"; - supplier = () -> 1; - } - - @Test - void test() { - GaugeMetricSample sample = new GaugeMetricSample(name, description, tags, category, baseUnit, supplier); - Assertions.assertEquals(sample.getName(), name); - Assertions.assertEquals(sample.getDescription(), description); - Assertions.assertEquals(sample.getTags(), tags); - Assertions.assertEquals(sample.getType(), MetricSample.Type.GAUGE); - Assertions.assertEquals(sample.getCategory(), category); - Assertions.assertEquals(sample.getBaseUnit(), baseUnit); - Assertions.assertEquals(sample.getSupplier().get(), 1); - sample.setSupplier(() -> 2); - Assertions.assertEquals(sample.getSupplier().get(), 2); - } -} From ca024f78b1c3e49b1678db902a06d6f5fae1fbcd Mon Sep 17 00:00:00 2001 From: x-shadow-man <1494445739@qq.com> Date: Sat, 25 Feb 2023 20:52:20 +0800 Subject: [PATCH 2/6] add licence --- .../model/container/AtomicLongContainer.java | 17 +++++++++++++++++ .../container/LongAccumulatorContainer.java | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/AtomicLongContainer.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/AtomicLongContainer.java index 9ad57180e48..45a1920bb11 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/AtomicLongContainer.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/AtomicLongContainer.java @@ -1,3 +1,20 @@ +/* + * 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.dubbo.metrics.model.container; import org.apache.dubbo.metrics.model.MetricsKeyWrapper; diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/LongAccumulatorContainer.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/LongAccumulatorContainer.java index 1353ea5f01b..a3f1a21dfb9 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/LongAccumulatorContainer.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/LongAccumulatorContainer.java @@ -1,3 +1,20 @@ +/* + * 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.dubbo.metrics.model.container; import org.apache.dubbo.metrics.model.MetricsKeyWrapper; From d51245f025fee23aabda9f394442ee7dfd737f49 Mon Sep 17 00:00:00 2001 From: x-shadow-man <1494445739@qq.com> Date: Sat, 25 Feb 2023 22:02:58 +0800 Subject: [PATCH 3/6] fix sonar --- .../model/container/LongContainer.java | 36 ++++++++++++++----- .../MetadataMetricsCollectorTest.java | 12 +++---- .../metadata/MetadataStatCompositeTest.java | 8 ++--- .../RegistryMetricsCollectorTest.java | 7 ++-- .../collector/RegistryStatCompositeTest.java | 8 ++--- 5 files changed, 44 insertions(+), 27 deletions(-) diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/LongContainer.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/LongContainer.java index f0b5568f24a..2de2d15ee92 100644 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/LongContainer.java +++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/container/LongContainer.java @@ -28,29 +28,29 @@ /** * Long type data container - * @param + * @param */ -public class LongContainer extends ConcurrentHashMap { +public class LongContainer extends ConcurrentHashMap { /** * Provide the metric type name */ - private final MetricsKeyWrapper metricsKeyWrapper; + private final transient MetricsKeyWrapper metricsKeyWrapper; /** * The initial value corresponding to the key is generally 0 of different data types */ - private final Function initFunc; + private final transient Function initFunc; /** * Statistical data calculation function, which can be self-increment, self-decrement, or more complex avg function */ - private final BiConsumer consumerFunc; + private final transient BiConsumer consumerFunc; /** * Data output function required by {@link GaugeMetricSample GaugeMetricSample} */ - private Function valueSupplier; + private transient Function valueSupplier; - public LongContainer(MetricsKeyWrapper metricsKeyWrapper, Supplier initFunc, BiConsumer consumerFunc) { + public LongContainer(MetricsKeyWrapper metricsKeyWrapper, Supplier initFunc, BiConsumer consumerFunc) { this.metricsKeyWrapper = metricsKeyWrapper; this.initFunc = s -> initFunc.get(); this.consumerFunc = consumerFunc; @@ -69,11 +69,11 @@ public boolean isKeyWrapper(MetricsKey metricsKey, String registryOpType) { return metricsKeyWrapper.isKey(metricsKey, registryOpType); } - public Function getInitFunc() { + public Function getInitFunc() { return initFunc; } - public BiConsumer getConsumerFunc() { + public BiConsumer getConsumerFunc() { return consumerFunc; } @@ -91,4 +91,22 @@ public String toString() { "metricsKeyWrapper=" + metricsKeyWrapper + '}'; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + + LongContainer that = (LongContainer) o; + + return metricsKeyWrapper.equals(that.metricsKeyWrapper); + } + + @Override + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + metricsKeyWrapper.hashCode(); + return result; + } } diff --git a/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataMetricsCollectorTest.java b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataMetricsCollectorTest.java index 9be209fd174..75620b040ae 100644 --- a/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataMetricsCollectorTest.java +++ b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataMetricsCollectorTest.java @@ -74,7 +74,7 @@ void testPushMetrics() throws InterruptedException { List metricSamples = collector.collect(); // push success +1 - Assertions.assertEquals(metricSamples.size(), 1); + Assertions.assertEquals(1, metricSamples.size()); Assertions.assertTrue(metricSamples.get(0) instanceof GaugeMetricSample); Assertions.assertEquals(metricSamples.get(0).getName(), MetricsKey.METADATA_PUSH_METRIC_NUM.getName()); @@ -82,7 +82,7 @@ void testPushMetrics() throws InterruptedException { // push finish rt +1 metricSamples = collector.collect(); //num(total+success) + rt(5) = 7 - Assertions.assertEquals(metricSamples.size(), 7); + Assertions.assertEquals(7, metricSamples.size()); long c1 = timePair.calc(); TimePair lastTimePair = TimePair.start(); eventMulticaster.publishEvent(new MetadataEvent.PushEvent(applicationModel, lastTimePair)); @@ -93,7 +93,7 @@ void testPushMetrics() throws InterruptedException { metricSamples = collector.collect(); // num(total+success+error) + rt(5) - Assertions.assertEquals(metricSamples.size(), 8); + Assertions.assertEquals(8, metricSamples.size()); // calc rt for (MetricSample sample : metricSamples) { @@ -124,7 +124,7 @@ void testSubscribeMetrics() throws InterruptedException { List metricSamples = collector.collect(); // push success +1 - Assertions.assertEquals(metricSamples.size(), 1); + Assertions.assertEquals(1, metricSamples.size()); Assertions.assertTrue(metricSamples.get(0) instanceof GaugeMetricSample); Assertions.assertEquals(metricSamples.get(0).getName(), MetricsKey.METADATA_SUBSCRIBE_METRIC_NUM.getName()); @@ -132,7 +132,7 @@ void testSubscribeMetrics() throws InterruptedException { // push finish rt +1 metricSamples = collector.collect(); //num(total+success) + rt(5) = 7 - Assertions.assertEquals(metricSamples.size(), 7); + Assertions.assertEquals(7, metricSamples.size()); long c1 = timePair.calc(); TimePair lastTimePair = TimePair.start(); eventMulticaster.publishEvent(new MetadataEvent.SubscribeEvent(applicationModel, lastTimePair)); @@ -143,7 +143,7 @@ void testSubscribeMetrics() throws InterruptedException { metricSamples = collector.collect(); // num(total+success+error) + rt(5) - Assertions.assertEquals(metricSamples.size(), 8); + Assertions.assertEquals(8, metricSamples.size()); // calc rt for (MetricSample sample : metricSamples) { diff --git a/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataStatCompositeTest.java b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataStatCompositeTest.java index 9218ba431af..66906decce5 100644 --- a/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataStatCompositeTest.java +++ b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataStatCompositeTest.java @@ -38,13 +38,13 @@ void testInit() { MetadataStatComposite statComposite = new MetadataStatComposite(); Assertions.assertEquals(statComposite.numStats.size(), MetadataEvent.Type.values().length); //(rt)5 * (push,subscribe)2 - Assertions.assertEquals(statComposite.rtStats.size(), 5 * 2); + Assertions.assertEquals(5 * 2, statComposite.rtStats.size()); statComposite.numStats.values().forEach((v -> Assertions.assertEquals(v, new ConcurrentHashMap<>()))); statComposite.rtStats.forEach(rtContainer -> { for (Map.Entry entry : rtContainer.entrySet()) { - Assertions.assertEquals(rtContainer.getValueSupplier().apply(entry.getKey()), 0L); + Assertions.assertEquals(0L, rtContainer.getValueSupplier().apply(entry.getKey())); } }); } @@ -53,7 +53,7 @@ void testInit() { void testIncrement() { MetadataStatComposite statComposite = new MetadataStatComposite(); statComposite.increment(MetadataEvent.Type.P_TOTAL, applicationName); - Assertions.assertEquals(statComposite.numStats.get(MetadataEvent.Type.P_TOTAL).get(applicationName).get(), 1L); + Assertions.assertEquals(1L, statComposite.numStats.get(MetadataEvent.Type.P_TOTAL).get(applicationName).get()); } @Test @@ -62,6 +62,6 @@ void testCalcRt() { statComposite.calcRt(applicationName, OP_TYPE_SUBSCRIBE, 10L); Assertions.assertTrue(statComposite.rtStats.stream().anyMatch(longContainer -> longContainer.specifyType(OP_TYPE_SUBSCRIBE))); Optional> subContainer = statComposite.rtStats.stream().filter(longContainer -> longContainer.specifyType(OP_TYPE_SUBSCRIBE)).findFirst(); - subContainer.ifPresent(v -> Assertions.assertEquals(v.get(applicationName).longValue(), 10L)); + subContainer.ifPresent(v -> Assertions.assertEquals(10L, v.get(applicationName).longValue())); } } diff --git a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsCollectorTest.java b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsCollectorTest.java index d4e8f34cec8..d01d2cd1464 100644 --- a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsCollectorTest.java +++ b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsCollectorTest.java @@ -73,14 +73,14 @@ void testPushMetrics() throws InterruptedException { List metricSamples = collector.collect(); // push success +1 - Assertions.assertEquals(metricSamples.size(), 1); + Assertions.assertEquals(1, metricSamples.size()); Assertions.assertTrue(metricSamples.get(0) instanceof GaugeMetricSample); eventMulticaster.publishFinishEvent(new RegistryEvent.MetricsRegisterEvent(applicationModel, timePair)); // push finish rt +1 metricSamples = collector.collect(); //num(total+success) + rt(5) = 7 - Assertions.assertEquals(metricSamples.size(), 7); + Assertions.assertEquals(7, metricSamples.size()); long c1 = timePair.calc(); TimePair lastTimePair = TimePair.start(); eventMulticaster.publishEvent(new RegistryEvent.MetricsRegisterEvent(applicationModel, lastTimePair)); @@ -91,7 +91,7 @@ void testPushMetrics() throws InterruptedException { metricSamples = collector.collect(); // num(total+success+error) + rt(5) - Assertions.assertEquals(metricSamples.size(), 8); + Assertions.assertEquals(8, metricSamples.size()); // calc rt for (MetricSample sample : metricSamples) { @@ -111,5 +111,4 @@ void testPushMetrics() throws InterruptedException { } - } diff --git a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java index b7bf96daf3f..9829809243b 100644 --- a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java +++ b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java @@ -38,13 +38,13 @@ void testInit() { RegistryStatComposite statComposite = new RegistryStatComposite(); Assertions.assertEquals(statComposite.numStats.size(), RegistryEvent.Type.values().length); //(rt)5 * (register,subscribe,notify)3 - Assertions.assertEquals(statComposite.rtStats.size(), 5 * 3); + Assertions.assertEquals(5 * 3, statComposite.rtStats.size()); statComposite.numStats.values().forEach((v -> Assertions.assertEquals(v, new ConcurrentHashMap<>()))); statComposite.rtStats.forEach(rtContainer -> { for (Map.Entry entry : rtContainer.entrySet()) { - Assertions.assertEquals(rtContainer.getValueSupplier().apply(entry.getKey()), 0L); + Assertions.assertEquals(0L, rtContainer.getValueSupplier().apply(entry.getKey())); } }); } @@ -53,7 +53,7 @@ void testInit() { void testIncrement() { RegistryStatComposite statComposite = new RegistryStatComposite(); statComposite.increment(RegistryEvent.Type.R_TOTAL, applicationName); - Assertions.assertEquals(statComposite.numStats.get(RegistryEvent.Type.R_TOTAL).get(applicationName).get(), 1L); + Assertions.assertEquals(1L, statComposite.numStats.get(RegistryEvent.Type.R_TOTAL).get(applicationName).get()); } @Test @@ -62,6 +62,6 @@ void testCalcRt() { statComposite.calcRt(applicationName, OP_TYPE_NOTIFY, 10L); Assertions.assertTrue(statComposite.rtStats.stream().anyMatch(longContainer -> longContainer.specifyType(OP_TYPE_NOTIFY))); Optional> subContainer = statComposite.rtStats.stream().filter(longContainer -> longContainer.specifyType(OP_TYPE_NOTIFY)).findFirst(); - subContainer.ifPresent(v -> Assertions.assertEquals(v.get(applicationName).longValue(), 10L)); + subContainer.ifPresent(v -> Assertions.assertEquals(10L, v.get(applicationName).longValue())); } } From a9f9d5e3930c1674941dad0f7864bcc7621450ae Mon Sep 17 00:00:00 2001 From: x-shadow-man <1494445739@qq.com> Date: Mon, 27 Feb 2023 09:53:08 +0800 Subject: [PATCH 4/6] fix enable failed --- .../metrics/registry/collector/RegistryMetricsCollector.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/RegistryMetricsCollector.java b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/RegistryMetricsCollector.java index c30b7c0f119..903ce918193 100644 --- a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/RegistryMetricsCollector.java +++ b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/RegistryMetricsCollector.java @@ -90,7 +90,7 @@ public void addRT(String applicationName, String registryOpType, Long responseTi @Override public List collect() { if (!isCollectEnabled()) { - new ArrayList<>(); + return new ArrayList<>(); } List list = new ArrayList<>(); list.addAll(stats.exportNumMetrics()); From 1f11b8c36d9815b872bfaf20da564b40c258060a Mon Sep 17 00:00:00 2001 From: x-shadow-man <1494445739@qq.com> Date: Mon, 27 Feb 2023 09:56:52 +0800 Subject: [PATCH 5/6] fix enable failed --- .../metrics/registry/collector/RegistryMetricsCollector.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/RegistryMetricsCollector.java b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/RegistryMetricsCollector.java index 903ce918193..37ee2375007 100644 --- a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/RegistryMetricsCollector.java +++ b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/RegistryMetricsCollector.java @@ -89,10 +89,10 @@ public void addRT(String applicationName, String registryOpType, Long responseTi @Override public List collect() { + List list = new ArrayList<>(); if (!isCollectEnabled()) { - return new ArrayList<>(); + return list; } - List list = new ArrayList<>(); list.addAll(stats.exportNumMetrics()); list.addAll(stats.exportRtMetrics()); list.addAll(stats.exportSkMetrics()); From 0a2a6deddea88e8e8c41c8ccfbb7e1ea712998d5 Mon Sep 17 00:00:00 2001 From: x-shadow-man <1494445739@qq.com> Date: Mon, 27 Feb 2023 21:36:22 +0800 Subject: [PATCH 6/6] add registry&metadata in spring/xsd --- .../src/main/resources/META-INF/dubbo.xsd | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd index 457ebfe6d38..5cdd32d4d01 100644 --- a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd +++ b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd @@ -1022,6 +1022,18 @@ + + + + + + + + + + + +