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


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueue.java:
##########
@@ -3128,6 +3139,108 @@ private void applySeekResetUnderWriteLock(final 
PendingSeekRequest request) {
         seekGeneration.get());
   }
 
+  private boolean commitAndRefreshWalRetention(
+      final WriterId writerId, final WriterProgress writerProgress) {
+    final boolean committed =
+        commitManager.commit(
+            consumerGroupId, topicName, consensusGroupId, writerId, 
writerProgress);
+    if (committed) {
+      refreshCommittedWalRetentionBoundAndNotify();
+    }
+    return committed;
+  }
+
+  private boolean commitWithoutOutstandingAndRefreshWalRetention(
+      final WriterId writerId, final WriterProgress writerProgress) {
+    final boolean committed =
+        commitManager.commitWithoutOutstanding(
+            consumerGroupId, topicName, consensusGroupId, writerId, 
writerProgress);
+    if (committed) {
+      refreshCommittedWalRetentionBoundAndNotify();
+    }
+    return committed;
+  }
+
+  private long getCommittedRetainedMinVersionId() {
+    refreshCommittedWalRetentionBound();
+    return committedRetainedMinVersionId;
+  }
+
+  private void refreshCommittedWalRetentionBoundAndNotify() {
+    if (refreshCommittedWalRetentionBound()) {
+      serverImpl.checkAndUpdateSafeDeletedSearchIndex();
+    }
+  }
+
+  private boolean refreshCommittedWalRetentionBound() {
+    final RegionProgress committedRegionProgress =
+        commitManager.getCommittedRegionProgress(consumerGroupId, topicName, 
consensusGroupId);
+    final long currentWalVersion = 
consensusReqReader.getCurrentWALFileVersion();
+
+    synchronized (committedRetentionLock) {
+      if (Objects.equals(lastCommittedProgressForRetention, 
committedRegionProgress)
+          && lastCurrentWalVersionForRetention == currentWalVersion) {
+        return false;
+      }
+
+      final long newRetainedMinVersionId =
+          computeCommittedRetainedMinVersionId(committedRegionProgress, 
currentWalVersion);
+      final boolean changed = committedRetainedMinVersionId != 
newRetainedMinVersionId;
+      committedRetainedMinVersionId = newRetainedMinVersionId;
+      lastCommittedProgressForRetention = committedRegionProgress;
+      lastCurrentWalVersionForRetention = currentWalVersion;
+      walFileCommitRequirements.keySet().removeIf(versionId -> versionId < 
newRetainedMinVersionId);
+      return changed;
+    }
+  }
+
+  private long computeCommittedRetainedMinVersionId(
+      final RegionProgress committedRegionProgress, final long 
currentWalVersion) {
+    if (!(consensusReqReader instanceof WALNode)) {
+      return 0L;
+    }
+
+    final WALNode walNode = (WALNode) consensusReqReader;
+    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;
+      }
+
+      WalFileCommitRequirement requirement = 
walFileCommitRequirements.get(versionId);

Review Comment:
   The metadata itself is already cached in walFileCommitRequirements, and 
refreshCommittedWalRetentionBound short-circuits when both committed progress 
and the current WAL version are unchanged. I kept the directory listing when 
either changes because a roll can introduce a new boundary and WAL cleanup can 
remove the previously visited file; reusing a single file id without checking 
that state could retain or release the wrong prefix.



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