This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.2 by this push:
new bbb151b0dc Registry Dirctory Metrics should interface level (#12012)
bbb151b0dc is described below
commit bbb151b0dce824b1cf07c1bb116f4a4632780c30
Author: wxbty <[email protected]>
AuthorDate: Mon Apr 10 07:21:08 2023 +0800
Registry Dirctory Metrics should interface level (#12012)
* dirctory service level
* fix mock npe
* fix ci
* refresh count
* refresh count
* refresh when notify
* remove unuse
* set key for dirctory metrics
* test remove clear
* revert
* fix ci
---------
Co-authored-by: x-shadow-man <[email protected]>
Co-authored-by: Albumen Kevin <[email protected]>
---
.../rpc/cluster/directory/AbstractDirectory.java | 39 +++++++++++++---
.../org/apache/dubbo/metrics/model/MetricsKey.java | 5 +--
.../dubbo/metrics/registry/RegistryConstants.java | 6 +++
.../metrics/registry/event/RegistryEvent.java | 13 ++++--
.../event/RegistryMetricsEventMulticaster.java | 30 ++++++-------
.../registry/event/support/DirectorSupport.java | 52 ----------------------
.../registry/event/type/ApplicationType.java | 6 ---
.../metrics/registry/event/type/ServiceType.java | 5 +++
8 files changed, 70 insertions(+), 86 deletions(-)
diff --git
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java
index 8bc44a1479..ee15fc6382 100644
---
a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java
+++
b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/directory/AbstractDirectory.java
@@ -28,7 +28,8 @@ import org.apache.dubbo.common.utils.ConcurrentHashSet;
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.registry.event.support.DirectorSupport;
+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;
@@ -44,6 +45,7 @@ import org.apache.dubbo.rpc.model.ApplicationModel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@@ -350,6 +352,7 @@ public abstract class AbstractDirectory<T> implements
Directory<T> {
}
}, reconnectTaskPeriod, TimeUnit.MILLISECONDS);
}
+
MetricsEventBus.publish(RegistryEvent.refreshDirectoryEvent(applicationModel,
getSummary()));
}
/**
@@ -363,6 +366,7 @@ public abstract class AbstractDirectory<T> implements
Directory<T> {
if (invokersInitialized) {
refreshInvokerInternal();
}
+
MetricsEventBus.publish(RegistryEvent.refreshDirectoryEvent(applicationModel,
getSummary()));
}
private synchronized void refreshInvokerInternal() {
@@ -386,17 +390,16 @@ public abstract class AbstractDirectory<T> implements
Directory<T> {
@Override
public void addDisabledInvoker(Invoker<T> invoker) {
- MetricsEventBus.publish(DirectorSupport.disable(applicationModel));
if (invokers.contains(invoker)) {
disabledInvokers.add(invoker);
removeValidInvoker(invoker);
logger.info("Disable service address: " + invoker.getUrl() + ".");
}
+
MetricsEventBus.publish(RegistryEvent.refreshDirectoryEvent(applicationModel,
getSummary()));
}
@Override
public void recoverDisabledInvoker(Invoker<T> invoker) {
- MetricsEventBus.publish(DirectorSupport.recover(applicationModel));
if (disabledInvokers.remove(invoker)) {
try {
addValidInvoker(invoker);
@@ -405,6 +408,7 @@ public abstract class AbstractDirectory<T> implements
Directory<T> {
}
}
+
MetricsEventBus.publish(RegistryEvent.refreshDirectoryEvent(applicationModel,
getSummary()));
}
protected final void refreshRouter(BitList<Invoker<T>> newlyInvokers,
Runnable switchAction) {
@@ -461,7 +465,7 @@ public abstract class AbstractDirectory<T> implements
Directory<T> {
refreshInvokerInternal();
this.invokersInitialized = true;
-
MetricsEventBus.publish(DirectorSupport.current(applicationModel,invokers.size()));
+
MetricsEventBus.publish(RegistryEvent.refreshDirectoryEvent(applicationModel,
getSummary()));
}
protected void destroyInvokers() {
@@ -472,14 +476,12 @@ public abstract class AbstractDirectory<T> implements
Directory<T> {
}
private boolean addValidInvoker(Invoker<T> invoker) {
- MetricsEventBus.publish(DirectorSupport.valid(applicationModel));
synchronized (this.validInvokers) {
return this.validInvokers.add(invoker);
}
}
private boolean removeValidInvoker(Invoker<T> invoker) {
- MetricsEventBus.publish(DirectorSupport.unValid(applicationModel));
synchronized (this.validInvokers) {
return this.validInvokers.remove(invoker);
}
@@ -499,4 +501,29 @@ public abstract class AbstractDirectory<T> implements
Directory<T> {
.map(URL::getAddress)
.collect(Collectors.joining(","));
}
+
+ private Map<ServiceType, Map<String, Integer>> getSummary() {
+ Map<ServiceType, 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()));
+ return summaryMap;
+ }
+
+ private Map<String, Integer> groupByServiceKey(Collection<Invoker<T>>
invokers) {
+
+ Map<String, Integer> serviceNumMap = new HashMap<>();
+ for (Invoker<T> invoker : invokers) {
+ if (invoker.getClass().getSimpleName().contains("Mockito")) {
+ return serviceNumMap;
+ }
+ }
+ if (invokers.size() > 0) {
+ serviceNumMap = invokers.stream().filter(invoker ->
invoker.getInterface() != null).collect(Collectors.groupingBy(invoker ->
invoker.getInterface().getName(), Collectors.reducing(0, e -> 1,
Integer::sum)));
+ }
+
+ return serviceNumMap;
+ }
}
diff --git
a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsKey.java
b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsKey.java
index 578b0b89a5..5fb3a9ee82 100644
---
a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsKey.java
+++
b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsKey.java
@@ -74,11 +74,10 @@ public enum MetricsKey {
SUBSCRIBE_METRIC_NUM_FAILED("dubbo.registry.subscribe.num.failed.total",
"Failed Subscribe Num"),
// directory metrics key
- DIRECTORY_METRIC_NUM_CURRENT("dubbo.registry.directory.num.current",
"Current Directory Urls"),
+ DIRECTORY_METRIC_NUM_ALL("dubbo.registry.directory.num.all", "All
Directory Urls"),
DIRECTORY_METRIC_NUM_VALID("dubbo.registry.directory.num.valid.total",
"Valid Directory Urls"),
-
DIRECTORY_METRIC_NUM_UN_VALID("dubbo.registry.directory.num.un_valid.total",
"UnValid Directory Urls"),
+
DIRECTORY_METRIC_NUM_TO_RECONNECT("dubbo.registry.directory.num.to_reconnect.total",
"ToReconnect Directory Urls"),
DIRECTORY_METRIC_NUM_DISABLE("dubbo.registry.directory.num.disable.total",
"Disable Directory Urls"),
-
DIRECTORY_METRIC_NUM_RECOVER_DISABLE("dubbo.registry.directory.num.recover.disable.total",
"Recover Disable Directory Urls"),
NOTIFY_METRIC_REQUESTS("dubbo.registry.notify.requests.total", "Total
Notify Requests"),
NOTIFY_METRIC_NUM_LAST("dubbo.registry.notify.num.last", "Last Notify
Nums"),
diff --git
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/RegistryConstants.java
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/RegistryConstants.java
index c0d7bffd5e..9a246250a0 100644
---
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/RegistryConstants.java
+++
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/RegistryConstants.java
@@ -19,6 +19,12 @@ package org.apache.dubbo.metrics.registry;
public interface RegistryConstants {
+ String ATTACHMENT_KEY_SERVICE = "serviceKey";
+ String ATTACHMENT_KEY_SIZE = "size";
+ String ATTACHMENT_KEY_LAST_NUM_MAP = "lastNumMap";
+
+ String ATTACHMENT_DIRECTORY_MAP = "dirMap";
+
String OP_TYPE_REGISTER = "register";
String OP_TYPE_SUBSCRIBE = "subscribe";
String OP_TYPE_NOTIFY = "notify";
diff --git
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/RegistryEvent.java
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/RegistryEvent.java
index bd1fad1e7a..824c16ccd7 100644
---
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/RegistryEvent.java
+++
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/RegistryEvent.java
@@ -30,9 +30,10 @@ import org.apache.dubbo.rpc.model.ApplicationModel;
import java.util.HashMap;
import java.util.Map;
-import static
org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_LAST_NUM_MAP;
-import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_SERVICE;
-import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_SIZE;
+import static
org.apache.dubbo.metrics.registry.RegistryConstants.ATTACHMENT_DIRECTORY_MAP;
+import static
org.apache.dubbo.metrics.registry.RegistryConstants.ATTACHMENT_KEY_LAST_NUM_MAP;
+import static
org.apache.dubbo.metrics.registry.RegistryConstants.ATTACHMENT_KEY_SERVICE;
+import static
org.apache.dubbo.metrics.registry.RegistryConstants.ATTACHMENT_KEY_SIZE;
/**
* Registry related events
@@ -136,5 +137,11 @@ public class RegistryEvent extends TimeCounterEvent {
return ddEvent;
}
+ public static RegistryEvent refreshDirectoryEvent(ApplicationModel
applicationModel, Map<ServiceType, Map<String, Integer>> summaryMap) {
+ RegistryEvent registryEvent = new RegistryEvent(applicationModel, new
TypeWrapper(MetricsLevel.APP, ServiceType.D_VALID, null, null));
+ registryEvent.putAttachment(ATTACHMENT_DIRECTORY_MAP, summaryMap);
+ return registryEvent;
+ }
+
}
diff --git
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/RegistryMetricsEventMulticaster.java
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/RegistryMetricsEventMulticaster.java
index 38be359ffd..ea8be2207b 100644
---
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/RegistryMetricsEventMulticaster.java
+++
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/RegistryMetricsEventMulticaster.java
@@ -18,12 +18,14 @@
package org.apache.dubbo.metrics.registry.event;
import org.apache.dubbo.metrics.event.SimpleMetricsEventMulticaster;
+import org.apache.dubbo.metrics.registry.RegistryConstants;
import org.apache.dubbo.metrics.registry.event.type.ApplicationType;
import org.apache.dubbo.metrics.registry.event.type.ServiceType;
-import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_DIR_NUM;
-import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_SERVICE;
-import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_SIZE;
+import java.util.Map;
+
+import static
org.apache.dubbo.metrics.registry.RegistryConstants.ATTACHMENT_DIRECTORY_MAP;
+import static
org.apache.dubbo.metrics.registry.RegistryConstants.ATTACHMENT_KEY_SERVICE;
import static
org.apache.dubbo.metrics.registry.RegistryConstants.OP_TYPE_NOTIFY;
import static
org.apache.dubbo.metrics.registry.RegistryConstants.OP_TYPE_REGISTER;
import static
org.apache.dubbo.metrics.registry.RegistryConstants.OP_TYPE_REGISTER_SERVICE;
@@ -55,13 +57,14 @@ public final class RegistryMetricsEventMulticaster extends
SimpleMetricsEventMul
// MetricsDirectoryListener
- addIncrListener(ApplicationType.D_VALID);
- addIncrListener(ApplicationType.D_UN_VALID);
- addIncrListener(ApplicationType.D_DISABLE);
- addIncrListener(ApplicationType.D_RECOVER_DISABLE);
- super.addListener(RegistryListener.onEvent(ApplicationType.D_CURRENT,
- (event, type) -> event.setNum(type, ATTACHMENT_KEY_DIR_NUM))
- );
+ super.addListener(RegistryListener.onEvent(ServiceType.D_VALID,
+ (event, type) ->
+ {
+ Map<ServiceType, Map<String, Integer>> summaryMap =
event.getAttachmentValue(ATTACHMENT_DIRECTORY_MAP);
+ summaryMap.forEach((serviceType, map) ->
+ event.getCollector().setNum(serviceType,
event.getSource().getApplicationName(), map));
+ }
+ ));
// MetricsServiceRegisterListener
super.addListener(RegistryListener.onEvent(ServiceType.R_SERVICE_TOTAL,
@@ -76,11 +79,6 @@ public final class RegistryMetricsEventMulticaster extends
SimpleMetricsEventMul
super.addListener(RegistryListener.onError(ServiceType.S_SERVICE_FAILED,
this::onRtEvent));
}
-
- private void addIncrListener(ApplicationType applicationType) {
- super.addListener(onPostEventBuild(applicationType));
- }
-
private RegistryListener onPostEventBuild(ApplicationType applicationType)
{
return RegistryListener.onEvent(applicationType,
(event, type) ->
event.getCollector().increment(event.getSource().getApplicationName(), type)
@@ -111,7 +109,7 @@ public final class RegistryMetricsEventMulticaster extends
SimpleMetricsEventMul
}
private void incrSkSize(RegistryEvent event, ServiceType type) {
- event.incrementServiceKey(type, ATTACHMENT_KEY_SERVICE,
ATTACHMENT_KEY_SIZE);
+ event.incrementServiceKey(type, ATTACHMENT_KEY_SERVICE,
RegistryConstants.ATTACHMENT_KEY_SIZE);
}
private void onRtEvent(RegistryEvent event, ServiceType type) {
diff --git
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/support/DirectorSupport.java
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/support/DirectorSupport.java
deleted file mode 100644
index c54210a1b4..0000000000
---
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/support/DirectorSupport.java
+++ /dev/null
@@ -1,52 +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.event.support;
-
-import org.apache.dubbo.metrics.model.MetricsLevel;
-import org.apache.dubbo.metrics.model.TypeWrapper;
-import org.apache.dubbo.metrics.registry.event.RegistryEvent;
-import org.apache.dubbo.metrics.registry.event.type.ApplicationType;
-import org.apache.dubbo.rpc.model.ApplicationModel;
-
-import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_DIR_NUM;
-
-
-public class DirectorSupport {
-
- public static RegistryEvent disable(ApplicationModel applicationModel) {
- return new RegistryEvent(applicationModel, new
TypeWrapper(MetricsLevel.APP, ApplicationType.D_DISABLE, null, null));
- }
-
- public static RegistryEvent valid(ApplicationModel applicationModel) {
- return new RegistryEvent(applicationModel, new
TypeWrapper(MetricsLevel.APP, ApplicationType.D_VALID, null, null));
- }
-
- public static RegistryEvent unValid(ApplicationModel applicationModel) {
- return new RegistryEvent(applicationModel, new
TypeWrapper(MetricsLevel.APP, ApplicationType.D_UN_VALID, null, null));
- }
-
- public static RegistryEvent current(ApplicationModel applicationModel, int
num) {
- RegistryEvent ddEvent = new RegistryEvent(applicationModel, new
TypeWrapper(MetricsLevel.APP, ApplicationType.D_CURRENT, null, null));
- ddEvent.putAttachment(ATTACHMENT_KEY_DIR_NUM, num);
- return ddEvent;
- }
-
- public static RegistryEvent recover(ApplicationModel applicationModel) {
- return new RegistryEvent(applicationModel, new
TypeWrapper(MetricsLevel.APP, ApplicationType.D_RECOVER_DISABLE, null, null));
- }
-}
diff --git
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/type/ApplicationType.java
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/type/ApplicationType.java
index 75ed1183f7..d6f0c768e5 100644
---
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/type/ApplicationType.java
+++
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/type/ApplicationType.java
@@ -28,12 +28,6 @@ public enum ApplicationType {
S_SUCCEED(MetricsKey.SUBSCRIBE_METRIC_NUM_SUCCEED),
S_FAILED(MetricsKey.SUBSCRIBE_METRIC_NUM_FAILED),
- D_VALID(MetricsKey.DIRECTORY_METRIC_NUM_VALID),
- D_UN_VALID(MetricsKey.DIRECTORY_METRIC_NUM_UN_VALID),
- D_DISABLE(MetricsKey.DIRECTORY_METRIC_NUM_DISABLE),
- D_CURRENT(MetricsKey.DIRECTORY_METRIC_NUM_CURRENT),
- D_RECOVER_DISABLE(MetricsKey.DIRECTORY_METRIC_NUM_RECOVER_DISABLE),
-
N_TOTAL(MetricsKey.NOTIFY_METRIC_REQUESTS),
;
diff --git
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/type/ServiceType.java
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/type/ServiceType.java
index 612ce497a0..86c9d9bc0f 100644
---
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/type/ServiceType.java
+++
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/type/ServiceType.java
@@ -30,6 +30,11 @@ public enum ServiceType {
S_SERVICE_TOTAL(MetricsKey.SERVICE_SUBSCRIBE_METRIC_NUM),
S_SERVICE_SUCCEED(MetricsKey.SERVICE_SUBSCRIBE_METRIC_NUM_SUCCEED),
S_SERVICE_FAILED(MetricsKey.SERVICE_SUBSCRIBE_METRIC_NUM_FAILED),
+
+ D_VALID(MetricsKey.DIRECTORY_METRIC_NUM_VALID),
+ D_TO_RECONNECT(MetricsKey.DIRECTORY_METRIC_NUM_TO_RECONNECT),
+ D_DISABLE(MetricsKey.DIRECTORY_METRIC_NUM_DISABLE),
+ D_ALL(MetricsKey.DIRECTORY_METRIC_NUM_ALL),
;
private final MetricsKey metricsKey;