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

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

commit 32b9ba58fb09d3807071845f7fc12193bd0ed52a
Author: OneSizeFitQuorum <[email protected]>
AuthorDate: Sat Mar 30 13:49:58 2024 +0800

    finish
    
    Signed-off-by: OneSizeFitQuorum <[email protected]>
---
 .../src/main/java/org/apache/iotdb/consensus/IConsensus.java |  8 ++++++++
 .../java/org/apache/iotdb/consensus/iot/IoTConsensus.java    |  7 +++++++
 .../org/apache/iotdb/consensus/ratis/RatisConsensus.java     | 12 ++++++++++++
 .../org/apache/iotdb/consensus/simple/SimpleConsensus.java   |  5 +++++
 4 files changed, 32 insertions(+)

diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/IConsensus.java 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/IConsensus.java
index 32dae2c0ff5..d8a405c70e4 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/IConsensus.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/IConsensus.java
@@ -184,6 +184,14 @@ public interface IConsensus {
    */
   boolean isLeader(ConsensusGroupId groupId);
 
+  /**
+   * Returns the logic clock of the current consensus group
+   *
+   * @param groupId the consensus group
+   * @return long
+   */
+  long getLogicalClock(ConsensusGroupId groupId);
+
   /**
    * Determine if the current peer is the leader and already able to provide 
services in the
    * corresponding consensus group.
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensus.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensus.java
index dc857dc18cd..e95f3a87eca 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensus.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensus.java
@@ -379,6 +379,13 @@ public class IoTConsensus implements IConsensus {
     return true;
   }
 
+  @Override
+  public long getLogicalClock(ConsensusGroupId groupId) {
+    return Optional.ofNullable(stateMachineMap.get(groupId))
+        .map(IoTConsensusServerImpl::getSearchIndex)
+        .orElse(0L);
+  }
+
   @Override
   public Peer getLeader(ConsensusGroupId groupId) {
     if (!stateMachineMap.containsKey(groupId)) {
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java
index 5fa4eb19b06..c441b15818c 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/ratis/RatisConsensus.java
@@ -615,6 +615,18 @@ class RatisConsensus implements IConsensus {
     }
   }
 
+  @Override
+  public long getLogicalClock(ConsensusGroupId groupId) {
+    RaftGroupId raftGroupId = Utils.fromConsensusGroupIdToRaftGroupId(groupId);
+    try {
+      return server.getDivision(raftGroupId).getInfo().getCurrentTerm();
+    } catch (IOException exception) {
+      // if the read fails, simply return 0
+      logger.info("getLogicalClock request failed with exception: ", 
exception);
+      return 0;
+    }
+  }
+
   private boolean waitUntilLeaderReady(RaftGroupId groupId) {
     DivisionInfo divisionInfo;
     try {
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/simple/SimpleConsensus.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/simple/SimpleConsensus.java
index d23086b51a7..308f07ed3f4 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/simple/SimpleConsensus.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/simple/SimpleConsensus.java
@@ -230,6 +230,11 @@ class SimpleConsensus implements IConsensus {
     return true;
   }
 
+  @Override
+  public long getLogicalClock(ConsensusGroupId groupId) {
+    return 0;
+  }
+
   @Override
   public Peer getLeader(ConsensusGroupId groupId) {
     if (!stateMachineMap.containsKey(groupId)) {

Reply via email to