Pengzna commented on code in PR #12723:
URL: https://github.com/apache/iotdb/pull/12723#discussion_r1638475970


##########
iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/pipe/metric/PipeConsensusSyncLagManager.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.iotdb.consensus.pipe.metric;
+
+import org.apache.iotdb.consensus.pipe.consensuspipe.ConsensusPipeConnector;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+/**
+ * This class is used to aggregate the write progress of all Connectors to 
calculate the minimum
+ * synchronization progress of all follower copies, thereby calculating 
syncLag.
+ *
+ * <p>Note: every consensusGroup/dataRegion has and only has 1 instance of 
this class.
+ */
+public class PipeConsensusSyncLagManager {
+  long userWriteProgress = 0;
+  long minReplicateProgress = Long.MAX_VALUE;
+  List<ConsensusPipeConnector> consensusPipeConnectorList = new 
CopyOnWriteArrayList<>();
+
+  private void updateReplicateProgress() {
+    minReplicateProgress = Long.MAX_VALUE;
+    // if there isn't a consensus pipe task, replicate progress is 
Long.MAX_VALUE.
+    if (consensusPipeConnectorList.isEmpty()) {
+      return;
+    }
+    // else we find the minimum progress in all consensus pipe task.
+    consensusPipeConnectorList.forEach(
+        consensusPipeConnector ->
+            minReplicateProgress =
+                Math.min(
+                    minReplicateProgress,
+                    
consensusPipeConnector.getConsensusPipeReplicateProgress()));
+  }
+
+  private void updateUserWriteProgress() {
+    // if there isn't a consensus pipe task, user write progress is 0.
+    if (consensusPipeConnectorList.isEmpty()) {
+      userWriteProgress = 0;
+      return;
+    }
+    // since the user write progress of different consensus pipes on the same 
DataRegion is the
+    // same, we only need to take out one Connector to calculate
+    ConsensusPipeConnector connector = consensusPipeConnectorList.get(0);
+    userWriteProgress = connector.getConsensusPipeCommitProgress();
+  }
+
+  public void addConsensusPipeConnector(ConsensusPipeConnector 
consensusPipeConnector) {
+    consensusPipeConnectorList.add(consensusPipeConnector);
+  }
+
+  public void removeConsensusPipeConnector(ConsensusPipeConnector connector) {
+    consensusPipeConnectorList.remove(connector);
+  }
+
+  /**
+   * SyncLag represents the difference between the current replica users' 
write progress and the
+   * minimum synchronization progress of all other replicas. The semantics is 
how much data the
+   * leader has left to synchronize.
+   */
+  public long calculateSyncLag() {
+    updateUserWriteProgress();
+    updateReplicateProgress();
+    // if there isn't a consensus pipe task, the syncLag is userWriteProgress 
- 0
+    if (minReplicateProgress == Long.MAX_VALUE) {
+      return userWriteProgress;
+    } else {
+      // since we first update userWriteProgress then update 
replicateProgress, there may have some
+      // cases that userWriteProgress is less than replicateProgress. In these 
cases, we return 0.
+      if (userWriteProgress < minReplicateProgress) {
+        return 0;
+      }
+      return userWriteProgress - minReplicateProgress;
+    }
+  }
+
+  private PipeConsensusSyncLagManager() {
+    // do nothing
+  }
+
+  private static class PipeConsensusSyncLagManagerHolder {
+    private static Map<String, PipeConsensusSyncLagManager> 
CONSENSU_GROUP_ID_2_INSTANCE_MAP;

Review Comment:
   fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to