Skip to content

Commit

Permalink
Merge branch '3.3' into feat/support_springboot3_native
Browse files Browse the repository at this point in the history
# Conflicts:
#	dubbo-test/dubbo-test-modules/src/test/java/org/apache/dubbo/dependency/FileTest.java
  • Loading branch information
CrazyHZM committed Apr 17, 2023
2 parents e4a6f75 + 13558a8 commit 0d842f0
Show file tree
Hide file tree
Showing 38 changed files with 648 additions and 674 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.metrics.event.MetricsEventBus;
import org.apache.dubbo.metrics.model.key.MetricsKey;
import org.apache.dubbo.metrics.registry.event.RegistryEvent;
import org.apache.dubbo.metrics.registry.event.type.ServiceType;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcContext;
Expand Down Expand Up @@ -502,13 +502,13 @@ protected String joinValidInvokerAddresses() {
.collect(Collectors.joining(","));
}

private Map<ServiceType, Map<String, Integer>> getSummary() {
Map<ServiceType, Map<String, Integer>> summaryMap = new HashMap<>();
private Map<MetricsKey, Map<String, Integer>> getSummary() {
Map<MetricsKey, Map<String, Integer>> summaryMap = new HashMap<>();

summaryMap.put(ServiceType.D_VALID, groupByServiceKey(getValidInvokers()));
summaryMap.put(ServiceType.D_DISABLE, groupByServiceKey(getDisabledInvokers()));
summaryMap.put(ServiceType.D_TO_RECONNECT, groupByServiceKey(getInvokersToReconnect()));
summaryMap.put(ServiceType.D_ALL, groupByServiceKey(getInvokers()));
summaryMap.put(MetricsKey.DIRECTORY_METRIC_NUM_VALID, groupByServiceKey(getValidInvokers()));
summaryMap.put(MetricsKey.DIRECTORY_METRIC_NUM_DISABLE, groupByServiceKey(getDisabledInvokers()));
summaryMap.put(MetricsKey.DIRECTORY_METRIC_NUM_TO_RECONNECT, groupByServiceKey(getInvokersToReconnect()));
summaryMap.put(MetricsKey.DIRECTORY_METRIC_NUM_ALL, groupByServiceKey(getInvokers()));
return summaryMap;
}

Expand Down
2 changes: 1 addition & 1 deletion dubbo-dependencies-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
<nacos_version>2.1.2</nacos_version>
<grpc.version>1.54.0</grpc.version>
<grpc_contrib_verdion>0.8.1</grpc_contrib_verdion>
<jprotoc_version>1.2.1</jprotoc_version>
<jprotoc_version>1.2.2</jprotoc_version>
<!-- Log libs -->
<slf4j_version>1.7.36</slf4j_version>
<jcl_version>1.2</jcl_version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public interface MetricsConstants {
String ATTACHMENT_KEY_LAST_NUM_MAP = "lastNumMap";
String ATTACHMENT_DIRECTORY_MAP = "dirNum";

int SELF_INCREMENT_SIZE = 1;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@

package org.apache.dubbo.metrics.collector;

import org.apache.dubbo.metrics.event.MetricsEvent;
import org.apache.dubbo.metrics.event.TimeCounterEvent;
import org.apache.dubbo.metrics.model.key.MetricsKey;

/**
* Application-level collector.
* registration center, configuration center and other scenarios
*
* @Params <T> metrics type
*/
public interface ApplicationMetricsCollector<T, E extends MetricsEvent> extends MetricsCollector<E> {
public interface ApplicationMetricsCollector<E extends TimeCounterEvent> extends MetricsCollector<E> {

void increment(String applicationName, T type);
void increment(String applicationName, MetricsKey metricsKey);

void addRt(String applicationName, String registryOpType, Long responseTime);

void addApplicationRT(String applicationName, String registryOpType, Long responseTime);
}

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

import org.apache.dubbo.metrics.data.BaseStatComposite;
import org.apache.dubbo.metrics.event.MetricsEventMulticaster;
import org.apache.dubbo.metrics.event.TimeCounterEvent;
import org.apache.dubbo.metrics.model.MetricsCategory;
import org.apache.dubbo.metrics.model.key.MetricsKey;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;

import java.util.List;

import static org.apache.dubbo.metrics.MetricsConstants.SELF_INCREMENT_SIZE;

public abstract class CombMetricsCollector<E extends TimeCounterEvent> implements ApplicationMetricsCollector<E>, ServiceMetricsCollector<E> {

private final BaseStatComposite stats;
private MetricsEventMulticaster eventMulticaster;


public CombMetricsCollector(BaseStatComposite stats) {
this.stats = stats;
}

protected void setEventMulticaster(MetricsEventMulticaster eventMulticaster) {
this.eventMulticaster = eventMulticaster;
}

@Override
public void setNum(MetricsKey metricsKey, String applicationName, String serviceKey, int num) {
this.stats.setServiceKey(metricsKey, applicationName, serviceKey, num);
}

@Override
public void increment(String applicationName, MetricsKey metricsKey) {
this.stats.incrementApp(metricsKey, applicationName, SELF_INCREMENT_SIZE);
}

public void increment(String applicationName, String serviceKey, MetricsKey metricsKey, int size) {
this.stats.incrementServiceKey(metricsKey, applicationName, serviceKey, size);
}

@Override
public void addRt(String applicationName, String registryOpType, Long responseTime) {
stats.calcApplicationRt(applicationName, registryOpType, responseTime);
}

public void addRt(String applicationName, String serviceKey, String registryOpType, Long responseTime) {
stats.calcServiceKeyRt(applicationName, serviceKey, registryOpType, responseTime);
}

@SuppressWarnings({"rawtypes"})
protected List<GaugeMetricSample> export(MetricsCategory category) {
return stats.export(category);
}

@Override
public void onEvent(TimeCounterEvent event) {
eventMulticaster.publishEvent(event);
}


@Override
public void onEventFinish(TimeCounterEvent event) {
eventMulticaster.publishFinishEvent(event);
}

@Override
public void onEventError(TimeCounterEvent event) {
eventMulticaster.publishErrorEvent(event);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.dubbo.metrics.collector;

import org.apache.dubbo.common.extension.SPI;
import org.apache.dubbo.metrics.event.MetricsEvent;
import org.apache.dubbo.metrics.event.TimeCounterEvent;
import org.apache.dubbo.metrics.listener.MetricsLifeListener;
import org.apache.dubbo.metrics.model.sample.MetricSample;

Expand All @@ -29,7 +29,7 @@
* An interface of collector to collect framework internal metrics.
*/
@SPI
public interface MetricsCollector<E extends MetricsEvent> extends MetricsLifeListener<E> {
public interface MetricsCollector<E extends TimeCounterEvent> extends MetricsLifeListener<E> {

default boolean isCollectEnabled() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,23 @@
* limitations under the License.
*/

package org.apache.dubbo.metrics.registry.event.type;
package org.apache.dubbo.metrics.collector;

import org.apache.dubbo.metrics.event.TimeCounterEvent;
import org.apache.dubbo.metrics.model.key.MetricsKey;

public enum ApplicationType {
R_TOTAL(MetricsKey.REGISTER_METRIC_REQUESTS),
R_SUCCEED(MetricsKey.REGISTER_METRIC_REQUESTS_SUCCEED),
R_FAILED(MetricsKey.REGISTER_METRIC_REQUESTS_FAILED),

S_TOTAL(MetricsKey.SUBSCRIBE_METRIC_NUM),
S_SUCCEED(MetricsKey.SUBSCRIBE_METRIC_NUM_SUCCEED),
S_FAILED(MetricsKey.SUBSCRIBE_METRIC_NUM_FAILED),

N_TOTAL(MetricsKey.NOTIFY_METRIC_REQUESTS),
;

private final MetricsKey metricsKey;

/**
* Application-level collector.
* registration center, configuration center and other scenarios
*
* @Params <T> metrics type
*/
public interface ServiceMetricsCollector<E extends TimeCounterEvent> extends MetricsCollector<E> {

ApplicationType(MetricsKey metricsKey) {
this.metricsKey = metricsKey;
}
void increment(String applicationName, String serviceKey, MetricsKey metricsKey, int size);

public MetricsKey getMetricsKey() {
return metricsKey;
}
void setNum(MetricsKey metricsKey, String applicationName, String serviceKey, int num);

void addRt(String applicationName, String serviceKey, String registryOpType, Long responseTime);
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@

package org.apache.dubbo.metrics.event;

import org.apache.dubbo.metrics.exception.MetricsNeverHappenException;
import org.apache.dubbo.metrics.model.MethodMetric;
import org.apache.dubbo.metrics.model.key.TypeWrapper;
import org.apache.dubbo.rpc.model.ApplicationModel;

import java.util.HashMap;
import java.util.Map;

/**
* BaseMetricsEvent.
*/
Expand All @@ -33,7 +37,8 @@ public abstract class MetricsEvent {
private boolean available = true;
protected TypeWrapper typeWrapper;

@SuppressWarnings({"unchecked"})
private final Map<String, Object> attachment = new HashMap<>(8);

public MetricsEvent(ApplicationModel source) {
if (source == null) {
this.source = ApplicationModel.defaultModel();
Expand All @@ -44,6 +49,18 @@ public MetricsEvent(ApplicationModel source) {
}
}

@SuppressWarnings("unchecked")
public <T> T getAttachmentValue(String key) {
if (!attachment.containsKey(key)) {
throw new MetricsNeverHappenException("Attachment key [" + key + "] not found");
}
return (T) attachment.get(key);
}

public void putAttachment(String key, Object value) {
attachment.put(key, value);
}

public void setAvailable(boolean available) {
this.available = available;
}
Expand All @@ -61,6 +78,10 @@ public ApplicationModel getSource() {
return source;
}

public String appName() {
return getSource().getApplicationName();
}

public boolean isAssignableFrom(Object type) {
return typeWrapper.isAssignableFrom(type);
}
Expand All @@ -84,7 +105,7 @@ public enum Type {
CODEC_EXCEPTION("CODEC_EXCEPTION_%s"),
;

private String name;
private final String name;

public final String getName() {
return this.name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,28 @@ public static <T> T post(MetricsEvent event, Supplier<T> targetSupplier, Functio
return targetSupplier.get();
}
dispatcher.publishEvent(event);
if (!(event instanceof TimeCounterEvent)) {
return targetSupplier.get();
}
TimeCounterEvent timeCounterEvent = (TimeCounterEvent) event;
T result;
if (trFunction == null) {
try {
result = targetSupplier.get();
} catch (Throwable e) {
dispatcher.publishErrorEvent(event);
dispatcher.publishErrorEvent(timeCounterEvent);
throw e;
}
event.customAfterPost(result);
dispatcher.publishFinishEvent(event);
dispatcher.publishFinishEvent(timeCounterEvent);
} else {
// Custom failure status
result = targetSupplier.get();
if (trFunction.apply(result)) {
event.customAfterPost(result);
dispatcher.publishFinishEvent(event);
dispatcher.publishFinishEvent(timeCounterEvent);
} else {
dispatcher.publishErrorEvent(event);
dispatcher.publishErrorEvent(timeCounterEvent);
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface MetricsEventMulticaster {

void publishEvent(MetricsEvent event);

void publishFinishEvent(MetricsEvent event);
void publishFinishEvent(TimeCounterEvent event);

void publishErrorEvent(MetricsEvent event);
void publishErrorEvent(TimeCounterEvent event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ private boolean validateIfApplicationConfigExist(MetricsEvent event) {

@Override
@SuppressWarnings({"unchecked"})
public void publishFinishEvent(MetricsEvent event) {
public void publishFinishEvent(TimeCounterEvent event) {
publishTimeEvent(event, metricsLifeListener -> metricsLifeListener.onEventFinish(event));
}

@Override
@SuppressWarnings({"unchecked"})
public void publishErrorEvent(MetricsEvent event) {
public void publishErrorEvent(TimeCounterEvent event) {
publishTimeEvent(event, metricsLifeListener -> metricsLifeListener.onEventError(event));
}

Expand Down
Loading

0 comments on commit 0d842f0

Please sign in to comment.