Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize the code of metrics registry&metadata #11659

Merged
merged 8 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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;

import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer;

public class AtomicLongContainer extends LongContainer<AtomicLong> {

public AtomicLongContainer(MetricsKeyWrapper metricsKeyWrapper) {
super(metricsKeyWrapper, AtomicLong::new, (responseTime, longAccumulator) -> longAccumulator.set(responseTime));
}

public AtomicLongContainer(MetricsKeyWrapper metricsKeyWrapper, BiConsumer<Long, AtomicLong> consumerFunc) {
super(metricsKeyWrapper, AtomicLong::new, consumerFunc);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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;

import java.util.concurrent.atomic.LongAccumulator;

public class LongAccumulatorContainer extends LongContainer<LongAccumulator> {
public LongAccumulatorContainer(MetricsKeyWrapper metricsKeyWrapper, LongAccumulator accumulator) {
super(metricsKeyWrapper, () -> accumulator, (responseTime, longAccumulator) -> longAccumulator.accumulate(responseTime));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* 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 <N>
*/
public class LongContainer<N extends Number> extends ConcurrentHashMap<String, N> {

/**
* Provide the metric type name
*/
private final transient MetricsKeyWrapper metricsKeyWrapper;
/**
* The initial value corresponding to the key is generally 0 of different data types
*/
private final transient Function<String, N> initFunc;
/**
* Statistical data calculation function, which can be self-increment, self-decrement, or more complex avg function
*/
private final transient BiConsumer<Long, N> consumerFunc;
/**
* Data output function required by {@link GaugeMetricSample GaugeMetricSample}
*/
private transient Function<String, Long> valueSupplier;


public LongContainer(MetricsKeyWrapper metricsKeyWrapper, Supplier<N> initFunc, BiConsumer<Long, N> 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<String, N> getInitFunc() {
return initFunc;
}

public BiConsumer<Long, N> getConsumerFunc() {
return consumerFunc;
}

public Function<String, Long> getValueSupplier() {
return valueSupplier;
}

public void setValueSupplier(Function<String, Long> valueSupplier) {
this.valueSupplier = valueSupplier;
}

@Override
public String toString() {
return "LongContainer{" +
"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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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 <NUMBER>
*/
public static class LongContainer<NUMBER extends Number> extends ConcurrentHashMap<String, NUMBER> {

/**
* 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<String, NUMBER> initFunc;
/**
* Statistical data calculation function, which can be self-increment, self-decrement, or more complex avg function
*/
private final BiConsumer<Long, NUMBER> consumerFunc;
/**
* Data output function required by {@link GaugeMetricSample GaugeMetricSample}
*/
private Function<String, Long> valueSupplier;


public LongContainer(MetricsKeyWrapper metricsKeyWrapper, Supplier<NUMBER> initFunc, BiConsumer<Long, NUMBER> 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<String, NUMBER> getInitFunc() {
return initFunc;
}

public BiConsumer<Long, NUMBER> getConsumerFunc() {
return consumerFunc;
}

public Function<String, Long> getValueSupplier() {
return valueSupplier;
}

public void setValueSupplier(Function<String, Long> valueSupplier) {
this.valueSupplier = valueSupplier;
}

@Override
public String toString() {
return "LongContainer{" +
"metricsKeyWrapper=" + metricsKeyWrapper +
'}';
}
}

public static class AtomicLongContainer extends LongContainer<AtomicLong> {

public AtomicLongContainer(MetricsKeyWrapper metricsKeyWrapper) {
super(metricsKeyWrapper, AtomicLong::new, (responseTime, longAccumulator) -> longAccumulator.set(responseTime));
}

public AtomicLongContainer(MetricsKeyWrapper metricsKeyWrapper, BiConsumer<Long, AtomicLong> consumerFunc) {
super(metricsKeyWrapper, AtomicLong::new, consumerFunc);
}

}

public static class LongAccumulatorContainer extends LongContainer<LongAccumulator> {
public LongAccumulatorContainer(MetricsKeyWrapper metricsKeyWrapper, LongAccumulator accumulator) {
super(metricsKeyWrapper, () -> accumulator, (responseTime, longAccumulator) -> longAccumulator.accumulate(responseTime));
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,15 +74,15 @@ void testPushMetrics() throws InterruptedException {
List<MetricSample> 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());

eventMulticaster.publishFinishEvent(new MetadataEvent.PushEvent(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 MetadataEvent.PushEvent(applicationModel, lastTimePair));
Expand All @@ -95,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) {
Expand Down Expand Up @@ -126,15 +124,15 @@ void testSubscribeMetrics() throws InterruptedException {
List<MetricSample> 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());

eventMulticaster.publishFinishEvent(new MetadataEvent.SubscribeEvent(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 MetadataEvent.SubscribeEvent(applicationModel, lastTimePair));
Expand All @@ -145,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) {
Expand Down
Loading