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 2aadeb4a93c [IOTDB-6259] Bump ratis version to 3.0.0 (#11618)
2aadeb4a93c is described below

commit 2aadeb4a93c9b6577945a11b6fabfa530e6e20a1
Author: William Song <[email protected]>
AuthorDate: Fri Dec 1 16:56:14 2023 +0800

    [IOTDB-6259] Bump ratis version to 3.0.0 (#11618)
---
 iotdb-core/consensus/pom.xml                       |  10 +-
 .../consensus/ratis/metrics/CounterProxy.java      |   6 +-
 .../iotdb/consensus/ratis/metrics/GaugeProxy.java  |  24 ++--
 .../ratis/metrics/IoTDBMetricRegistry.java         | 121 +++------------------
 .../ratis/metrics/MetricRegistryManager.java       |   2 +-
 .../ratis/metrics/RatisMetricsManager.java         |  20 ++--
 .../iotdb/consensus/ratis/metrics/TimerProxy.java  |  32 ++++--
 pom.xml                                            |   6 +-
 8 files changed, 71 insertions(+), 150 deletions(-)

diff --git a/iotdb-core/consensus/pom.xml b/iotdb-core/consensus/pom.xml
index 592c384493e..e4efab6a639 100644
--- a/iotdb-core/consensus/pom.xml
+++ b/iotdb-core/consensus/pom.xml
@@ -87,7 +87,7 @@
         </dependency>
         <dependency>
             <groupId>org.apache.ratis</groupId>
-            <artifactId>ratis-metrics</artifactId>
+            <artifactId>ratis-metrics-api</artifactId>
         </dependency>
         <dependency>
             <groupId>org.apache.ratis</groupId>
@@ -117,14 +117,6 @@
             <groupId>com.google.code.findbugs</groupId>
             <artifactId>jsr305</artifactId>
         </dependency>
-        <dependency>
-            <groupId>io.dropwizard.metrics</groupId>
-            <artifactId>metrics-core</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>io.dropwizard.metrics</groupId>
-            <artifactId>metrics-jmx</artifactId>
-        </dependency>
         <dependency>
             <groupId>org.apache.iotdb</groupId>
             <artifactId>tsfile</artifactId>
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/CounterProxy.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/CounterProxy.java
index fe86121e911..33d2470af20 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/CounterProxy.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/CounterProxy.java
@@ -21,8 +21,10 @@ package org.apache.iotdb.consensus.ratis.metrics;
 
 import org.apache.iotdb.metrics.type.Counter;
 
-/** A Proxy class using IoTDB Counter to replace the dropwizard Counter. */
-public class CounterProxy extends com.codahale.metrics.Counter {
+import org.apache.ratis.metrics.LongCounter;
+
+/** CounterProxy will route Ratis' internal counter metrics to our IoTDB 
{@link Counter} */
+public class CounterProxy implements LongCounter {
 
   /** IoTDB Counter. */
   private final Counter counter;
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/GaugeProxy.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/GaugeProxy.java
index 5f0ce546cde..8798debf2fd 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/GaugeProxy.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/GaugeProxy.java
@@ -19,27 +19,21 @@
 
 package org.apache.iotdb.consensus.ratis.metrics;
 
-import com.codahale.metrics.Gauge;
-import com.codahale.metrics.MetricRegistry;
+import java.util.function.Supplier;
 
 /** AutoGauge supplier holder class. */
-public class GaugeProxy implements Gauge {
+public class GaugeProxy<T> {
 
-  private final Gauge gauge;
+  private final Supplier<T> gaugeSupplier;
 
-  public GaugeProxy(MetricRegistry.MetricSupplier<Gauge> metricSupplier) {
-    this.gauge = metricSupplier.newMetric();
+  public GaugeProxy(Supplier<Supplier<T>> supplier) {
+    this.gaugeSupplier = supplier.get();
   }
 
-  @Override
-  public Object getValue() {
-    return gauge.getValue();
-  }
-
-  double getValueAsDouble() {
-    Object value = getValue();
-    if (value instanceof Number) {
-      return ((Number) value).doubleValue();
+  double getDoubleValue() {
+    Object latestValue = gaugeSupplier.get();
+    if (latestValue instanceof Number) {
+      return ((Number) latestValue).doubleValue();
     }
     return 0.0;
   }
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/IoTDBMetricRegistry.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/IoTDBMetricRegistry.java
index 3fdb9d951dc..df43be07050 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/IoTDBMetricRegistry.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/IoTDBMetricRegistry.java
@@ -24,25 +24,16 @@ import org.apache.iotdb.metrics.AbstractMetricService;
 import org.apache.iotdb.metrics.utils.MetricLevel;
 import org.apache.iotdb.metrics.utils.MetricType;
 
-import com.codahale.metrics.ConsoleReporter;
-import com.codahale.metrics.Counter;
-import com.codahale.metrics.Gauge;
-import com.codahale.metrics.Histogram;
-import com.codahale.metrics.Meter;
-import com.codahale.metrics.Metric;
-import com.codahale.metrics.MetricFilter;
-import com.codahale.metrics.MetricRegistry;
-import com.codahale.metrics.MetricSet;
-import com.codahale.metrics.Timer;
-import com.codahale.metrics.jmx.JmxReporter;
+import org.apache.ratis.metrics.LongCounter;
 import org.apache.ratis.metrics.MetricRegistryInfo;
 import org.apache.ratis.metrics.RatisMetricRegistry;
+import org.apache.ratis.metrics.Timekeeper;
 
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
-import java.util.SortedMap;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Supplier;
 
 public class IoTDBMetricRegistry implements RatisMetricRegistry {
 
@@ -52,7 +43,7 @@ public class IoTDBMetricRegistry implements 
RatisMetricRegistry {
   private final Map<String, String> metricNameCache = new 
ConcurrentHashMap<>();
   private final Map<String, CounterProxy> counterCache = new 
ConcurrentHashMap<>();
   private final Map<String, TimerProxy> timerCache = new ConcurrentHashMap<>();
-  private final Map<String, GaugeProxy> gaugeCache = new ConcurrentHashMap<>();
+  private final Map<String, Boolean> gaugeCache = new ConcurrentHashMap<>();
   /** Time taken to flush log. */
   public static final String RAFT_LOG_FLUSH_TIME = "flushTime";
   /** Size of SegmentedRaftLogCache::closedSegments in bytes. */
@@ -76,12 +67,6 @@ public class IoTDBMetricRegistry implements 
RatisMetricRegistry {
   /** Time taken to process write requests from client. */
   public static final String RAFT_CLIENT_WRITE_REQUEST = "clientWriteRequest";
 
-  private static final String METHOD_NOT_USED_EXCEPTION_MESSAGE =
-      "This method is not used in IoTDB project";
-
-  private static final String METER_NOT_USED_EXCEPTION_MESSAGE =
-      "Meter is not used in Ratis Metrics";
-
   private static final List<String> RATIS_METRICS = new ArrayList<>();
 
   static {
@@ -99,15 +84,14 @@ public class IoTDBMetricRegistry implements 
RatisMetricRegistry {
   IoTDBMetricRegistry(MetricRegistryInfo info, AbstractMetricService service) {
     this.info = info;
     this.metricService = service;
-    prefix =
-        MetricRegistry.name(
-            Utils.getConsensusGroupTypeFromPrefix(info.getPrefix()).toString(),
-            info.getApplicationName(),
-            info.getMetricsComponentName());
+    this.prefix =
+        Utils.getConsensusGroupTypeFromPrefix(info.getPrefix()).toString()
+            + info.getApplicationName()
+            + info.getMetricsComponentName();
   }
 
   private String getMetricName(String name) {
-    return metricNameCache.computeIfAbsent(name, n -> 
MetricRegistry.name(prefix, n));
+    return metricNameCache.computeIfAbsent(name, n -> this.prefix + n);
   }
 
   public MetricLevel getMetricLevel(String name) {
@@ -120,7 +104,7 @@ public class IoTDBMetricRegistry implements 
RatisMetricRegistry {
   }
 
   @Override
-  public Timer timer(String name) {
+  public Timekeeper timer(String name) {
     final String fullName = getMetricName(name);
     return timerCache.computeIfAbsent(
         fullName,
@@ -128,7 +112,7 @@ public class IoTDBMetricRegistry implements 
RatisMetricRegistry {
   }
 
   @Override
-  public Counter counter(String name) {
+  public LongCounter counter(String name) {
     final String fullName = getMetricName(name);
     return counterCache.computeIfAbsent(
         fullName,
@@ -161,93 +145,22 @@ public class IoTDBMetricRegistry implements 
RatisMetricRegistry {
   }
 
   @Override
-  public Gauge gauge(String name, MetricRegistry.MetricSupplier<Gauge> 
metricSupplier) {
-    final String fullName = getMetricName(name);
-    return gaugeCache.computeIfAbsent(
+  public <T> void gauge(String fullName, Supplier<Supplier<T>> supplier) {
+    gaugeCache.computeIfAbsent(
         fullName,
-        gaugeName -> {
-          final GaugeProxy gauge = new GaugeProxy(metricSupplier);
+        name -> {
+          final GaugeProxy<T> gauge = new GaugeProxy<>(supplier);
           metricService.createAutoGauge(
-              gaugeName, getMetricLevel(fullName), gauge, 
GaugeProxy::getValueAsDouble);
-          return gauge;
+              name, getMetricLevel(name), gauge, GaugeProxy::getDoubleValue);
+          return true;
         });
   }
 
-  @Override
-  public Timer timer(String name, MetricRegistry.MetricSupplier<Timer> 
metricSupplier) {
-    throw new UnsupportedOperationException(METHOD_NOT_USED_EXCEPTION_MESSAGE);
-  }
-
-  @Override
-  public SortedMap<String, Gauge> getGauges(MetricFilter metricFilter) {
-    throw new UnsupportedOperationException(METHOD_NOT_USED_EXCEPTION_MESSAGE);
-  }
-
-  @Override
-  public Counter counter(String name, MetricRegistry.MetricSupplier<Counter> 
metricSupplier) {
-    throw new UnsupportedOperationException(METHOD_NOT_USED_EXCEPTION_MESSAGE);
-  }
-
-  @Override
-  public Histogram histogram(String name) {
-    throw new UnsupportedOperationException("Histogram is not used in Ratis 
Metrics");
-  }
-
-  @Override
-  public Meter meter(String name) {
-    throw new UnsupportedOperationException(METER_NOT_USED_EXCEPTION_MESSAGE);
-  }
-
-  @Override
-  public Meter meter(String name, MetricRegistry.MetricSupplier<Meter> 
metricSupplier) {
-    throw new UnsupportedOperationException(METER_NOT_USED_EXCEPTION_MESSAGE);
-  }
-
-  @Override
-  public Metric get(String name) {
-    throw new UnsupportedOperationException(METER_NOT_USED_EXCEPTION_MESSAGE);
-  }
-
-  @Override
-  public <T extends Metric> T register(String name, T t) throws 
IllegalArgumentException {
-    throw new UnsupportedOperationException("register is not used in Ratis 
Metrics");
-  }
-
-  @Override
-  public MetricRegistry getDropWizardMetricRegistry() {
-    throw new UnsupportedOperationException(METHOD_NOT_USED_EXCEPTION_MESSAGE);
-  }
-
   @Override
   public MetricRegistryInfo getMetricRegistryInfo() {
     return info;
   }
 
-  @Override
-  public void registerAll(String s, MetricSet metricSet) {
-    throw new UnsupportedOperationException("registerAll is not used in Ratis 
Metrics");
-  }
-
-  @Override
-  public void setJmxReporter(JmxReporter jmxReporter) {
-    throw new UnsupportedOperationException("JmxReporter is not used in Ratis 
Metrics");
-  }
-
-  @Override
-  public JmxReporter getJmxReporter() {
-    throw new UnsupportedOperationException("JmxReporter is not used in Ratis 
Metrics");
-  }
-
-  @Override
-  public void setConsoleReporter(ConsoleReporter consoleReporter) {
-    throw new UnsupportedOperationException("ConsoleReporter is not used in 
Ratis Metrics");
-  }
-
-  @Override
-  public ConsoleReporter getConsoleReporter() {
-    throw new UnsupportedOperationException("ConsoleReporter is not used in 
Ratis Metrics");
-  }
-
   void removeAll() {
     counterCache.forEach((name, counter) -> 
metricService.remove(MetricType.COUNTER, name));
     gaugeCache.forEach((name, gauge) -> 
metricService.remove(MetricType.AUTO_GAUGE, name));
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/MetricRegistryManager.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/MetricRegistryManager.java
index 94c6a1805a2..dcb31f352f1 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/MetricRegistryManager.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/MetricRegistryManager.java
@@ -36,7 +36,7 @@ import java.util.function.Consumer;
 public class MetricRegistryManager extends MetricRegistries {
   /** Using RefCountingMap here because of potential duplicate 
MetricRegistryInfos. */
   private final RefCountingMap<MetricRegistryInfo, RatisMetricRegistry> 
registries;
-  /** TODO: enable ratis metrics after verifying its correctness and 
efficiency. */
+
   private final AbstractMetricService service = MetricService.getInstance();
 
   public MetricRegistryManager() {
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/RatisMetricsManager.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/RatisMetricsManager.java
index f91fdf89d60..bdf90f9328b 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/RatisMetricsManager.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/RatisMetricsManager.java
@@ -30,12 +30,12 @@ import java.util.function.BiConsumer;
 
 public class RatisMetricsManager {
 
-  static class TimeKeeper implements AutoCloseable {
+  public static class TimeReporter implements AutoCloseable {
     private final long startMoment;
     private final TConsensusGroupType groupType;
     private final BiConsumer<Long, TConsensusGroupType> reporter;
 
-    private TimeKeeper(
+    private TimeReporter(
         BiConsumer<Long, TConsensusGroupType> reporter, TConsensusGroupType 
groupType) {
       this.reporter = reporter;
       this.groupType = groupType;
@@ -55,16 +55,16 @@ public class RatisMetricsManager {
 
   private final MetricService metricService = MetricService.getInstance();
 
-  public TimeKeeper startWriteLocallyTimer(TConsensusGroupType 
consensusGroupType) {
-    return new TimeKeeper(this::recordWriteLocallyCost, consensusGroupType);
+  public TimeReporter startWriteLocallyTimer(TConsensusGroupType 
consensusGroupType) {
+    return new TimeReporter(this::recordWriteLocallyCost, consensusGroupType);
   }
 
-  public TimeKeeper startWriteRemotelyTimer(TConsensusGroupType 
consensusGroupType) {
-    return new TimeKeeper(this::recordWriteRemotelyCost, consensusGroupType);
+  public TimeReporter startWriteRemotelyTimer(TConsensusGroupType 
consensusGroupType) {
+    return new TimeReporter(this::recordWriteRemotelyCost, consensusGroupType);
   }
 
-  public TimeKeeper startReadTimer(TConsensusGroupType consensusGroupType) {
-    return new TimeKeeper(this::recordReadRequestCost, consensusGroupType);
+  public TimeReporter startReadTimer(TConsensusGroupType consensusGroupType) {
+    return new TimeReporter(this::recordReadRequestCost, consensusGroupType);
   }
 
   /** Record the time cost in write locally stage. */
@@ -114,6 +114,10 @@ public class RatisMetricsManager {
         RatisMetricSet.WRITE_STATE_MACHINE);
   }
 
+  private RatisMetricsManager() {
+    // empty constructor
+  }
+
   public static RatisMetricsManager getInstance() {
     return RatisMetricsManagerHolder.INSTANCE;
   }
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/TimerProxy.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/TimerProxy.java
index 17b54b1c381..07a01abe04a 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/TimerProxy.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/metrics/TimerProxy.java
@@ -21,23 +21,39 @@ package org.apache.iotdb.consensus.ratis.metrics;
 
 import org.apache.iotdb.metrics.type.Timer;
 
+import org.apache.ratis.metrics.Timekeeper;
+
 import java.util.concurrent.TimeUnit;
 
-public class TimerProxy extends com.codahale.metrics.Timer {
+/** TimerProxy will route Ratis' internal timer metrics to our IoTDB {@link 
Timer} */
+public class TimerProxy implements Timekeeper {
+
+  private static class TimerContext implements Timekeeper.Context {
+
+    private final Timer reporter;
+    private final long startTime;
+
+    TimerContext(Timer reporter) {
+      this.reporter = reporter;
+      this.startTime = System.nanoTime();
+    }
+
+    @Override
+    public long stop() {
+      final long elapsed = System.nanoTime() - startTime;
+      reporter.update(elapsed, TimeUnit.NANOSECONDS);
+      return elapsed;
+    }
+  }
+
   private final Timer timer;
 
   TimerProxy(Timer timer) {
     this.timer = timer;
   }
 
-  /** time() method is used as a user time clock. Will reuse the dropwizard 
implementation. */
   @Override
   public Context time() {
-    return super.time();
-  }
-
-  @Override
-  public void update(long duration, TimeUnit unit) {
-    timer.update(duration, unit);
+    return new TimerContext(timer);
   }
 }
diff --git a/pom.xml b/pom.xml
index 6c4036c75bf..4c15c949471 100644
--- a/pom.xml
+++ b/pom.xml
@@ -143,14 +143,14 @@
         <osgi.version>7.0.0</osgi.version>
         <pax-jdbc-common.version>1.5.6</pax-jdbc-common.version>
         <powermock.version>2.0.9</powermock.version>
-        <ratis-thirdparty-misc.version>1.0.4</ratis-thirdparty-misc.version>
+        <ratis-thirdparty-misc.version>1.0.5</ratis-thirdparty-misc.version>
         <!--
       This is an unreleased version of a custom branch. The 8-character part 
after the version number
       This is an unreleased version of a custom branch. The 8-character part 
after the version number
       is for ensuring the SNAPSHOT will stay available. We should however have 
the Ratis folks do a
       new release soon, as releasing with this version is more than sub-ideal.
     -->
-        <ratis.version>2.5.2-0f5f95d-SNAPSHOT</ratis.version>
+        <ratis.version>3.0.0</ratis.version>
         <reactive-streams.version>1.0.4</reactive-streams.version>
         <reactor-netty.version>1.1.13</reactor-netty.version>
         <reactor.version>3.5.10</reactor.version>
@@ -828,7 +828,7 @@
             </dependency>
             <dependency>
                 <groupId>org.apache.ratis</groupId>
-                <artifactId>ratis-metrics</artifactId>
+                <artifactId>ratis-metrics-api</artifactId>
                 <version>${ratis.version}</version>
             </dependency>
             <dependency>

Reply via email to