This is an automated email from the ASF dual-hosted git repository.

tanxinyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 2bc29bb0d1 [IOTDB-5582] Add region type in ratis metric prefix (#9140)
2bc29bb0d1 is described below

commit 2bc29bb0d17b4573d2f60e685daf2ecc4034b4be
Author: Xiangpeng Hu <[email protected]>
AuthorDate: Mon Feb 27 11:28:21 2023 +0800

    [IOTDB-5582] Add region type in ratis metric prefix (#9140)
---
 .../confignode/manager/consensus/ConsensusManager.java    |  3 +++
 .../apache/iotdb/consensus/config/ConsensusConfig.java    | 15 +++++++++++++++
 .../org/apache/iotdb/consensus/ratis/RatisConsensus.java  | 13 +++++++++++--
 .../consensus/ratis/metrics/IoTDBMetricRegistry.java      |  8 ++++++--
 .../consensus/ratis/metrics/MetricRegistryManager.java    | 13 ++++++++++---
 .../iotdb/consensus/ratis/metrics/RatisMetricSet.java     | 10 ++++++++++
 .../apache/iotdb/consensus/ratis/metrics/TimerProxy.java  |  2 +-
 .../org/apache/iotdb/consensus/iot/ReplicateTest.java     |  2 ++
 .../org/apache/iotdb/consensus/iot/StabilityTest.java     |  2 ++
 .../apache/iotdb/consensus/ratis/RatisConsensusTest.java  |  2 ++
 .../org/apache/iotdb/consensus/simple/RecoveryTest.java   |  2 ++
 .../iotdb/db/consensus/DataRegionConsensusImpl.java       |  2 ++
 .../iotdb/db/consensus/SchemaRegionConsensusImpl.java     |  2 ++
 13 files changed, 68 insertions(+), 8 deletions(-)

diff --git 
a/confignode/src/main/java/org/apache/iotdb/confignode/manager/consensus/ConsensusManager.java
 
b/confignode/src/main/java/org/apache/iotdb/confignode/manager/consensus/ConsensusManager.java
index fddf8326b0..d31e6678a9 100644
--- 
a/confignode/src/main/java/org/apache/iotdb/confignode/manager/consensus/ConsensusManager.java
+++ 
b/confignode/src/main/java/org/apache/iotdb/confignode/manager/consensus/ConsensusManager.java
@@ -20,6 +20,7 @@
 package org.apache.iotdb.confignode.manager.consensus;
 
 import org.apache.iotdb.common.rpc.thrift.TConfigNodeLocation;
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
 import org.apache.iotdb.common.rpc.thrift.TEndPoint;
 import org.apache.iotdb.common.rpc.thrift.TSStatus;
 import org.apache.iotdb.commons.consensus.ConfigNodeRegionId;
@@ -90,6 +91,7 @@ public class ConsensusManager {
                       .setThisNode(
                           new TEndPoint(CONF.getInternalAddress(), 
CONF.getConsensusPort()))
                       .setStorageDir("target" + java.io.File.separator + 
"simple")
+                      
.setConsensusGroupType(TConsensusGroupType.ConfigNodeRegion)
                       .build(),
                   gid -> stateMachine)
               .orElseThrow(
@@ -178,6 +180,7 @@ public class ConsensusManager {
                                       .build())
                               .build())
                       .setStorageDir(CONF.getConsensusDir())
+                      
.setConsensusGroupType(TConsensusGroupType.ConfigNodeRegion)
                       .build(),
                   gid -> stateMachine)
               .orElseThrow(
diff --git 
a/consensus/src/main/java/org/apache/iotdb/consensus/config/ConsensusConfig.java
 
b/consensus/src/main/java/org/apache/iotdb/consensus/config/ConsensusConfig.java
index 92db8f8a84..36aa9fa937 100644
--- 
a/consensus/src/main/java/org/apache/iotdb/consensus/config/ConsensusConfig.java
+++ 
b/consensus/src/main/java/org/apache/iotdb/consensus/config/ConsensusConfig.java
@@ -19,6 +19,7 @@
 
 package org.apache.iotdb.consensus.config;
 
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
 import org.apache.iotdb.common.rpc.thrift.TEndPoint;
 
 import java.util.Optional;
@@ -28,6 +29,7 @@ public class ConsensusConfig {
   private final TEndPoint thisNodeEndPoint;
   private final int thisNodeId;
   private final String storageDir;
+  private final TConsensusGroupType consensusGroupType;
   private final RatisConfig ratisConfig;
   private final IoTConsensusConfig ioTConsensusConfig;
 
@@ -35,11 +37,13 @@ public class ConsensusConfig {
       TEndPoint thisNode,
       int thisNodeId,
       String storageDir,
+      TConsensusGroupType consensusGroupType,
       RatisConfig ratisConfig,
       IoTConsensusConfig ioTConsensusConfig) {
     this.thisNodeEndPoint = thisNode;
     this.thisNodeId = thisNodeId;
     this.storageDir = storageDir;
+    this.consensusGroupType = consensusGroupType;
     this.ratisConfig = ratisConfig;
     this.ioTConsensusConfig = ioTConsensusConfig;
   }
@@ -56,6 +60,10 @@ public class ConsensusConfig {
     return storageDir;
   }
 
+  public TConsensusGroupType getConsensusGroupType() {
+    return consensusGroupType;
+  }
+
   public RatisConfig getRatisConfig() {
     return ratisConfig;
   }
@@ -73,6 +81,7 @@ public class ConsensusConfig {
     private TEndPoint thisNode;
     private int thisNodeId;
     private String storageDir;
+    private TConsensusGroupType consensusGroupType;
     private RatisConfig ratisConfig;
     private IoTConsensusConfig ioTConsensusConfig;
 
@@ -81,6 +90,7 @@ public class ConsensusConfig {
           thisNode,
           thisNodeId,
           storageDir,
+          consensusGroupType,
           Optional.ofNullable(ratisConfig).orElseGet(() -> 
RatisConfig.newBuilder().build()),
           Optional.ofNullable(ioTConsensusConfig)
               .orElseGet(() -> IoTConsensusConfig.newBuilder().build()));
@@ -101,6 +111,11 @@ public class ConsensusConfig {
       return this;
     }
 
+    public Builder setConsensusGroupType(TConsensusGroupType 
consensusGroupType) {
+      this.consensusGroupType = consensusGroupType;
+      return this;
+    }
+
     public Builder setRatisConfig(RatisConfig ratisConfig) {
       this.ratisConfig = ratisConfig;
       return this;
diff --git 
a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java 
b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java
index 1cac98dafb..c80ae31fb1 100644
--- 
a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java
+++ 
b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java
@@ -30,6 +30,7 @@ import 
org.apache.iotdb.commons.concurrent.IoTDBThreadPoolFactory;
 import org.apache.iotdb.commons.concurrent.threadpool.ScheduledExecutorUtil;
 import org.apache.iotdb.commons.conf.CommonDescriptor;
 import org.apache.iotdb.commons.consensus.ConsensusGroupId;
+import org.apache.iotdb.commons.service.metric.MetricService;
 import org.apache.iotdb.commons.utils.TestOnly;
 import org.apache.iotdb.consensus.IConsensus;
 import org.apache.iotdb.consensus.IStateMachine;
@@ -48,6 +49,7 @@ import 
org.apache.iotdb.consensus.exception.NodeReadOnlyException;
 import 
org.apache.iotdb.consensus.exception.PeerAlreadyInConsensusGroupException;
 import org.apache.iotdb.consensus.exception.PeerNotInConsensusGroupException;
 import org.apache.iotdb.consensus.exception.RatisRequestFailedException;
+import org.apache.iotdb.consensus.ratis.metrics.RatisMetricSet;
 
 import org.apache.commons.pool2.KeyedObjectPool;
 import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
@@ -93,8 +95,9 @@ class RatisConsensus implements IConsensus {
 
   private final Logger logger = LoggerFactory.getLogger(RatisConsensus.class);
 
-  // the unique net communication endpoint
+  /** the unique net communication endpoint */
   private final RaftPeer myself;
+
   private final RaftServer server;
 
   private final RaftProperties properties = new RaftProperties();
@@ -110,7 +113,7 @@ class RatisConsensus implements IConsensus {
   private static final int DEFAULT_PRIORITY = 0;
   private static final int LEADER_PRIORITY = 1;
 
-  // TODO make it configurable
+  /** TODO make it configurable */
   private static final int DEFAULT_WAIT_LEADER_READY_TIMEOUT = (int) 
TimeUnit.SECONDS.toMillis(20);
 
   private final ExecutorService addExecutor;
@@ -120,6 +123,8 @@ class RatisConsensus implements IConsensus {
 
   private final ConcurrentHashMap<File, MemorizedFileSizeCalc> calcMap = new 
ConcurrentHashMap<>();
 
+  private final RatisMetricSet ratisMetricSet;
+
   public RatisConsensus(ConsensusConfig config, IStateMachine.Registry 
registry)
       throws IOException {
     myself =
@@ -137,6 +142,8 @@ class RatisConsensus implements IConsensus {
     Utils.initRatisConfig(properties, config.getRatisConfig());
     this.config = config.getRatisConfig();
 
+    this.ratisMetricSet = new RatisMetricSet(config.getConsensusGroupType());
+
     clientManager =
         new IClientManager.Factory<RaftGroup, RatisClient>()
             .createClientManager(new RatisClientPoolFactory());
@@ -157,6 +164,7 @@ class RatisConsensus implements IConsensus {
 
   @Override
   public void start() throws IOException {
+    MetricService.getInstance().addMetricSet(this.ratisMetricSet);
     server.start();
     startSnapshotGuardian();
   }
@@ -175,6 +183,7 @@ class RatisConsensus implements IConsensus {
       clientManager.close();
       server.close();
     }
+    MetricService.getInstance().removeMetricSet(this.ratisMetricSet);
   }
 
   private boolean shouldRetry(RaftClientReply reply) {
diff --git 
a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/IoTDBMetricRegistry.java
 
b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/IoTDBMetricRegistry.java
index 4c54504ed7..8b9af65e6f 100644
--- 
a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/IoTDBMetricRegistry.java
+++ 
b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/IoTDBMetricRegistry.java
@@ -50,12 +50,16 @@ public class IoTDBMetricRegistry implements 
RatisMetricRegistry {
   private final Map<String, TimerProxy> timerCache = new ConcurrentHashMap<>();
   private final Map<String, GaugeProxy> gaugeCache = new ConcurrentHashMap<>();
 
-  IoTDBMetricRegistry(MetricRegistryInfo info, AbstractMetricService service) {
+  IoTDBMetricRegistry(
+      MetricRegistryInfo info, AbstractMetricService service, String 
consensusGroupType) {
     this.info = info;
     this.metricService = service;
     prefix =
         MetricRegistry.name(
-            info.getApplicationName(), info.getMetricsComponentName(), 
info.getPrefix());
+            consensusGroupType,
+            info.getApplicationName(),
+            info.getMetricsComponentName(),
+            info.getPrefix());
   }
 
   private String getMetricName(String name) {
diff --git 
a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/MetricRegistryManager.java
 
b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/MetricRegistryManager.java
index d0d5f0b4da..898ebd4680 100644
--- 
a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/MetricRegistryManager.java
+++ 
b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/MetricRegistryManager.java
@@ -33,11 +33,13 @@ import java.util.Set;
 import java.util.function.Consumer;
 
 public class MetricRegistryManager extends MetricRegistries {
-  // Using RefCountingMap here because of potential duplicate 
MetricRegistryInfos
+  /** Using RefCountingMap here because of potential duplicate 
MetricRegistryInfos */
   private final RefCountingMap<MetricRegistryInfo, RatisMetricRegistry> 
registries;
-  // TODO: enable ratis metrics after verifying its correctness and efficiency
+  /** TODO: enable ratis metrics after verifying its correctness and 
efficiency */
   private final AbstractMetricService service = new DoNothingMetricService();
 
+  private String consensusGroupType;
+
   public MetricRegistryManager() {
     this.registries = new RefCountingMap<>();
   }
@@ -53,7 +55,8 @@ public class MetricRegistryManager extends MetricRegistries {
   @Override
   public RatisMetricRegistry create(MetricRegistryInfo metricRegistryInfo) {
     return registries.put(
-        metricRegistryInfo, () -> new IoTDBMetricRegistry(metricRegistryInfo, 
service));
+        metricRegistryInfo,
+        () -> new IoTDBMetricRegistry(metricRegistryInfo, service, 
consensusGroupType));
   }
 
   @Override
@@ -95,4 +98,8 @@ public class MetricRegistryManager extends MetricRegistries {
     throw new UnsupportedOperationException(
         "Console Reporter is disabled from RatisMetricRegistries");
   }
+
+  public void setConsensusGroupType(String consensusGroupType) {
+    this.consensusGroupType = consensusGroupType;
+  }
 }
diff --git 
a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/RatisMetricSet.java
 
b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/RatisMetricSet.java
index a376490ec7..732f4015d4 100644
--- 
a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/RatisMetricSet.java
+++ 
b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/RatisMetricSet.java
@@ -18,6 +18,7 @@
  */
 package org.apache.iotdb.consensus.ratis.metrics;
 
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
 import org.apache.iotdb.metrics.AbstractMetricService;
 import org.apache.iotdb.metrics.metricsets.IMetricSet;
 
@@ -25,10 +26,19 @@ import org.apache.ratis.metrics.MetricRegistries;
 
 public class RatisMetricSet implements IMetricSet {
   private MetricRegistries manager;
+  private final String consensusGroupType;
+
+  public RatisMetricSet(TConsensusGroupType consensusGroupType) {
+    super();
+    this.consensusGroupType = consensusGroupType.toString();
+  }
 
   @Override
   public void bindTo(AbstractMetricService metricService) {
     manager = MetricRegistries.global();
+    if (manager instanceof MetricRegistryManager) {
+      ((MetricRegistryManager) 
manager).setConsensusGroupType(consensusGroupType);
+    }
   }
 
   @Override
diff --git 
a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/TimerProxy.java
 
b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/TimerProxy.java
index c5f16e8e76..67f6c192af 100644
--- 
a/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/TimerProxy.java
+++ 
b/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/TimerProxy.java
@@ -29,8 +29,8 @@ public class TimerProxy extends com.codahale.metrics.Timer {
     this.timer = timer;
   }
 
+  /** time() method is used as a user time clock. Will reuse the dropwizard 
implementation. */
   @Override
-  // time() method is used as a user time clock. Will reuse the dropwizard 
implementation.
   public Context time() {
     return super.time();
   }
diff --git 
a/consensus/src/test/java/org/apache/iotdb/consensus/iot/ReplicateTest.java 
b/consensus/src/test/java/org/apache/iotdb/consensus/iot/ReplicateTest.java
index a36db6e3ea..0c9321fc2b 100644
--- a/consensus/src/test/java/org/apache/iotdb/consensus/iot/ReplicateTest.java
+++ b/consensus/src/test/java/org/apache/iotdb/consensus/iot/ReplicateTest.java
@@ -19,6 +19,7 @@
 
 package org.apache.iotdb.consensus.iot;
 
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
 import org.apache.iotdb.common.rpc.thrift.TEndPoint;
 import org.apache.iotdb.commons.consensus.ConsensusGroupId;
 import org.apache.iotdb.commons.consensus.DataRegionId;
@@ -93,6 +94,7 @@ public class ReplicateTest {
                           .setThisNodeId(peers.get(i).getNodeId())
                           .setThisNode(peers.get(i).getEndpoint())
                           .setStorageDir(peersStorage.get(i).getAbsolutePath())
+                          
.setConsensusGroupType(TConsensusGroupType.DataRegion)
                           .build(),
                       groupId -> stateMachines.get(finalI))
                   .orElseThrow(
diff --git 
a/consensus/src/test/java/org/apache/iotdb/consensus/iot/StabilityTest.java 
b/consensus/src/test/java/org/apache/iotdb/consensus/iot/StabilityTest.java
index ea7c15c8f1..5f04af6ee0 100644
--- a/consensus/src/test/java/org/apache/iotdb/consensus/iot/StabilityTest.java
+++ b/consensus/src/test/java/org/apache/iotdb/consensus/iot/StabilityTest.java
@@ -19,6 +19,7 @@
 
 package org.apache.iotdb.consensus.iot;
 
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
 import org.apache.iotdb.common.rpc.thrift.TEndPoint;
 import org.apache.iotdb.commons.consensus.ConsensusGroupId;
 import org.apache.iotdb.commons.consensus.DataRegionId;
@@ -56,6 +57,7 @@ public class StabilityTest {
                     .setThisNodeId(1)
                     .setThisNode(new TEndPoint("0.0.0.0", 9000))
                     .setStorageDir(storageDir.getAbsolutePath())
+                    .setConsensusGroupType(TConsensusGroupType.DataRegion)
                     .build(),
                 gid -> new TestStateMachine())
             .orElseThrow(
diff --git 
a/consensus/src/test/java/org/apache/iotdb/consensus/ratis/RatisConsensusTest.java
 
b/consensus/src/test/java/org/apache/iotdb/consensus/ratis/RatisConsensusTest.java
index cb86a0f79d..6a5bbde7da 100644
--- 
a/consensus/src/test/java/org/apache/iotdb/consensus/ratis/RatisConsensusTest.java
+++ 
b/consensus/src/test/java/org/apache/iotdb/consensus/ratis/RatisConsensusTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.iotdb.consensus.ratis;
 
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
 import org.apache.iotdb.common.rpc.thrift.TEndPoint;
 import org.apache.iotdb.commons.consensus.ConsensusGroupId;
 import org.apache.iotdb.commons.consensus.DataRegionId;
@@ -99,6 +100,7 @@ public class RatisConsensusTest {
                       .setThisNode(peers.get(i).getEndpoint())
                       .setRatisConfig(config)
                       .setStorageDir(peersStorage.get(i).getAbsolutePath())
+                      .setConsensusGroupType(TConsensusGroupType.DataRegion)
                       .build(),
                   groupId -> stateMachines.get(finalI))
               .orElseThrow(
diff --git 
a/consensus/src/test/java/org/apache/iotdb/consensus/simple/RecoveryTest.java 
b/consensus/src/test/java/org/apache/iotdb/consensus/simple/RecoveryTest.java
index 83fc2414b9..496f24e0a7 100644
--- 
a/consensus/src/test/java/org/apache/iotdb/consensus/simple/RecoveryTest.java
+++ 
b/consensus/src/test/java/org/apache/iotdb/consensus/simple/RecoveryTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.iotdb.consensus.simple;
 
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
 import org.apache.iotdb.common.rpc.thrift.TEndPoint;
 import org.apache.iotdb.commons.consensus.ConsensusGroupId;
 import org.apache.iotdb.commons.consensus.SchemaRegionId;
@@ -51,6 +52,7 @@ public class RecoveryTest {
                     .setThisNodeId(1)
                     .setThisNode(new TEndPoint("0.0.0.0", 9000))
                     .setStorageDir("target" + java.io.File.separator + 
"recovery")
+                    .setConsensusGroupType(TConsensusGroupType.SchemaRegion)
                     .build(),
                 gid -> new EmptyStateMachine())
             .orElseThrow(
diff --git 
a/server/src/main/java/org/apache/iotdb/db/consensus/DataRegionConsensusImpl.java
 
b/server/src/main/java/org/apache/iotdb/db/consensus/DataRegionConsensusImpl.java
index 905a8275c5..aa648e95c9 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/consensus/DataRegionConsensusImpl.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/consensus/DataRegionConsensusImpl.java
@@ -19,6 +19,7 @@
 
 package org.apache.iotdb.db.consensus;
 
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
 import org.apache.iotdb.common.rpc.thrift.TEndPoint;
 import org.apache.iotdb.commons.consensus.ConsensusGroupId;
 import org.apache.iotdb.commons.consensus.DataRegionId;
@@ -71,6 +72,7 @@ public class DataRegionConsensusImpl {
                           new TEndPoint(
                               conf.getInternalAddress(), 
conf.getDataRegionConsensusPort()))
                       .setStorageDir(conf.getDataRegionConsensusDir())
+                      .setConsensusGroupType(TConsensusGroupType.DataRegion)
                       .setIoTConsensusConfig(
                           IoTConsensusConfig.newBuilder()
                               .setRpc(
diff --git 
a/server/src/main/java/org/apache/iotdb/db/consensus/SchemaRegionConsensusImpl.java
 
b/server/src/main/java/org/apache/iotdb/db/consensus/SchemaRegionConsensusImpl.java
index eec582234d..d6e710ef12 100644
--- 
a/server/src/main/java/org/apache/iotdb/db/consensus/SchemaRegionConsensusImpl.java
+++ 
b/server/src/main/java/org/apache/iotdb/db/consensus/SchemaRegionConsensusImpl.java
@@ -19,6 +19,7 @@
 
 package org.apache.iotdb.db.consensus;
 
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
 import org.apache.iotdb.common.rpc.thrift.TEndPoint;
 import org.apache.iotdb.commons.consensus.SchemaRegionId;
 import org.apache.iotdb.consensus.ConsensusFactory;
@@ -135,6 +136,7 @@ public class SchemaRegionConsensusImpl {
                                       .build())
                               .build())
                       .setStorageDir(conf.getSchemaRegionConsensusDir())
+                      .setConsensusGroupType(TConsensusGroupType.SchemaRegion)
                       .build(),
                   gid ->
                       new SchemaRegionStateMachine(

Reply via email to