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


##########
iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/pipe/metric/PipeConsensusSyncLagManager.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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()) {

Review Comment:
   Is this code necessary?



##########
iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/pipe/metric/PipeConsensusSyncLagManager.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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);
+  }
+
+  /**
+   * 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();

Review Comment:
   Since you update "userWriteProgress" before "replicateProgress", the 
userWriteProgress may be less than the "replicateProgress" and may generate 
negative value. Either swap the sequence, or set the result to "0" if negative.



##########
iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/pipe/metric/PipeConsensusSyncLagManager.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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) {

Review Comment:
   If we only add connector without any removal, is it OK when any peers are 
removed?



##########
iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/pipe/metric/PipeConsensusSyncLagManager.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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) {

Review Comment:
   And if we applied "removePeer", previous "isEmpty" may make a false 
"non-empty" value. Check if there are indexOutOfBounds in 
"updateUserWriteProgress".



##########
iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/pipe/metric/PipeConsensusSyncLagManager.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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);
+  }
+
+  /**
+   * 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();

Review Comment:
   The latter is more encouraged since if there are removals, the user write 
may also become "0" if you calculate it later....



-- 
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: reviews-unsubscr...@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to