jt2594838 commented on code in PR #18718:
URL: https://github.com/apache/iotdb/pull/18718#discussion_r4128847398


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusSubscriptionWalRetention.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.db.subscription.broker.consensus;
+
+import org.apache.iotdb.commons.consensus.ConsensusGroupId;
+import org.apache.iotdb.consensus.iot.IoTConsensusServerImpl;
+import org.apache.iotdb.consensus.iot.SubscriptionWalRetentionPolicy;
+import org.apache.iotdb.consensus.iot.log.ConsensusReqReader;
+import org.apache.iotdb.db.storageengine.dataregion.wal.io.ProgressWALReader;
+import org.apache.iotdb.db.storageengine.dataregion.wal.node.WALNode;
+import org.apache.iotdb.db.storageengine.dataregion.wal.utils.WALFileUtils;
+import org.apache.iotdb.rpc.subscription.payload.poll.RegionProgress;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Objects;
+
+final class ConsensusSubscriptionWalRetention {
+
+  private ConsensusSubscriptionWalRetention() {}
+
+  static String generateRetentionId(
+      final String consumerGroupId, final String topicName, final 
ConsensusGroupId regionId) {
+    return consumerGroupId + "\u0000" + topicName + "\u0000" + regionId;
+  }
+
+  static long computeCommittedRetainedMinVersionId(
+      final ConsensusReqReader consensusReqReader,
+      final ConsensusGroupId consensusGroupId,
+      final RegionProgress committedRegionProgress) {
+    if (!(consensusReqReader instanceof WALNode)) {
+      return 0L;
+    }
+
+    final WALNode walNode = (WALNode) consensusReqReader;
+    final long currentWalVersion = walNode.getCurrentWALFileVersion();
+    final File[] walFiles = 
WALFileUtils.listAllWALFiles(walNode.getLogDirectory());

Review Comment:
   May use 
org.apache.iotdb.db.storageengine.dataregion.wal.node.WALNode#getSortedWalFiles



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusSubscriptionCommitManager.java:
##########
@@ -722,6 +722,46 @@ public void removeAllStatesForTopic(final String 
consumerGroupId, final String t
     }
   }
 
+  public void removeAllStatesForConsumerGroup(final String consumerGroupId) {
+    recoverAllTopicStatesIfNeeded();
+    final Set<String> topicNames = new java.util.HashSet<>();

Review Comment:
   Mind the import



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusSubscriptionWalRetention.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.db.subscription.broker.consensus;
+
+import org.apache.iotdb.commons.consensus.ConsensusGroupId;
+import org.apache.iotdb.consensus.iot.IoTConsensusServerImpl;
+import org.apache.iotdb.consensus.iot.SubscriptionWalRetentionPolicy;
+import org.apache.iotdb.consensus.iot.log.ConsensusReqReader;
+import org.apache.iotdb.db.storageengine.dataregion.wal.io.ProgressWALReader;
+import org.apache.iotdb.db.storageengine.dataregion.wal.node.WALNode;
+import org.apache.iotdb.db.storageengine.dataregion.wal.utils.WALFileUtils;
+import org.apache.iotdb.rpc.subscription.payload.poll.RegionProgress;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Objects;
+
+final class ConsensusSubscriptionWalRetention {
+
+  private ConsensusSubscriptionWalRetention() {}
+
+  static String generateRetentionId(
+      final String consumerGroupId, final String topicName, final 
ConsensusGroupId regionId) {
+    return consumerGroupId + "\u0000" + topicName + "\u0000" + regionId;
+  }
+
+  static long computeCommittedRetainedMinVersionId(
+      final ConsensusReqReader consensusReqReader,
+      final ConsensusGroupId consensusGroupId,
+      final RegionProgress committedRegionProgress) {
+    if (!(consensusReqReader instanceof WALNode)) {
+      return 0L;
+    }
+
+    final WALNode walNode = (WALNode) consensusReqReader;
+    final long currentWalVersion = walNode.getCurrentWALFileVersion();
+    final File[] walFiles = 
WALFileUtils.listAllWALFiles(walNode.getLogDirectory());
+    if (Objects.isNull(walFiles) || walFiles.length == 0) {
+      return Math.max(0L, currentWalVersion);
+    }
+
+    WALFileUtils.ascSortByVersionId(walFiles);
+    for (final File walFile : walFiles) {
+      final long versionId = WALFileUtils.parseVersionId(walFile.getName());
+      if (versionId >= currentWalVersion) {
+        return Math.max(0L, currentWalVersion);
+      }
+      if (ProgressWALIterator.isHeaderOnlyWalFile(walFile)) {
+        continue;
+      }
+
+      try (final ProgressWALReader reader = new ProgressWALReader(walFile)) {
+        if (!ConsensusPrefetchingQueue.WalFileCommitRequirement.fromMetadata(
+                consensusGroupId.toString(), reader.getMetaData())
+            .isCoveredBy(committedRegionProgress)) {
+          return versionId;
+        }
+      } catch (final IOException e) {
+        return versionId;
+      }
+    }
+    return Math.max(0L, currentWalVersion);
+  }

Review Comment:
   Since the progress is monotonic before any seek, may cache the last covered 
file version to reduce metadata read.



##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/subscription/meta/consumer/CommitProgressKeeper.java:
##########
@@ -116,6 +116,21 @@ public synchronized void removeTopicProgress(
                         && isValidRegionAndDataNodeProgressKey(key, 
legacyTopicKeyPrefix)));
   }
 
+  public synchronized void removeConsumerGroupProgress(final String 
consumerGroupId) {
+    final String versionedConsumerGroupKeyPrefix =
+        VERSIONED_KEY_PREFIX + encodeKeyComponent(consumerGroupId) + 
KEY_COMPONENT_SEPARATOR;
+    final String legacyConsumerGroupKeyPrefix = 
String.valueOf(consumerGroupId) + KEY_SEPARATOR;
+    final boolean legacyConsumerGroupKeyIsUnambiguous =
+        !String.valueOf(consumerGroupId).contains(KEY_SEPARATOR);
+    regionProgressMap
+        .keySet()
+        .removeIf(
+            key ->
+                key.startsWith(versionedConsumerGroupKeyPrefix)
+                    || (legacyConsumerGroupKeyIsUnambiguous
+                        && key.startsWith(legacyConsumerGroupKeyPrefix)));
+  }

Review Comment:
   May skip initializing legacyConsumerGroupKeyPrefix when 
legacyConsumerGroupKeyIsUnambiguous is false.



-- 
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