Re: [PR] [Subscription] Protect retained WAL by committed progress [iotdb]

2026-08-24 Thread via GitHub


jt2594838 merged PR #18399:
URL: https://github.com/apache/iotdb/pull/18399


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



Re: [PR] [Subscription] Protect retained WAL by committed progress [iotdb]

2026-08-16 Thread via GitHub


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


##
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueue.java:
##
@@ -3192,6 +3305,84 @@ private LiveWALMetaDataSnapshot(final long versionId, 
final WALMetaData metadata
 }
   }
 
+  static final class WalFileCommitRequirement {
+
+private final Map requiredWriterProgress;
+private final boolean containsUnsupportedProgress;
+
+private WalFileCommitRequirement(
+final Map requiredWriterProgress,
+final boolean containsUnsupportedProgress) {
+  this.requiredWriterProgress = requiredWriterProgress;
+  this.containsUnsupportedProgress = containsUnsupportedProgress;
+}
+
+static WalFileCommitRequirement fromMetadata(
+final String regionId, final WALMetaData metadata) {
+  if (Objects.isNull(metadata)) {
+return new WalFileCommitRequirement(Collections.emptyMap(), true);
+  }
+
+  final List buffersSize = metadata.getBuffersSize();
+  final List physicalTimes = metadata.getPhysicalTimes();
+  final List nodeIds = metadata.getNodeIds();
+  final List localSeqs = metadata.getLocalSeqs();
+  if (physicalTimes.size() < buffersSize.size()
+  || nodeIds.size() < buffersSize.size()
+  || localSeqs.size() < buffersSize.size()) {
+return new WalFileCommitRequirement(Collections.emptyMap(), true);
+  }
+
+  final Map requiredWriterProgress = new 
LinkedHashMap<>();
+  for (int i = 0; i < buffersSize.size(); i++) {
+final int writerNodeId = nodeIds.get(i);
+final long physicalTime = physicalTimes.get(i);
+final long localSeq = localSeqs.get(i);
+if (writerNodeId < 0 && physicalTime == 0L && localSeq < 0L) {
+  // Non-search WAL entries do not participate in subscription 
progress.
+  continue;
+}
+if (writerNodeId < 0 || physicalTime < 0L || localSeq < 0L) {
+  // Legacy or incomplete writer metadata cannot be compared safely 
with RegionProgress.
+  return new WalFileCommitRequirement(Collections.emptyMap(), true);
+}
+
+final WriterId writerId = new WriterId(regionId, writerNodeId);
+final WriterProgress candidateProgress = new 
WriterProgress(physicalTime, localSeq);
+requiredWriterProgress.merge(
+writerId,
+candidateProgress,
+(currentProgress, candidate) ->
+compareProgress(candidate, currentProgress) > 0 ? candidate : 
currentProgress);
+  }
+  return new WalFileCommitRequirement(requiredWriterProgress, false);
+}
+
+boolean isCoveredBy(final RegionProgress committedRegionProgress) {
+  if (containsUnsupportedProgress || 
Objects.isNull(committedRegionProgress)) {
+return false;
+  }
+  for (final Map.Entry entry : 
requiredWriterProgress.entrySet()) {
+final WriterProgress committedWriterProgress =
+committedRegionProgress.getWriterPositions().get(entry.getKey());
+if (Objects.isNull(committedWriterProgress)
+|| compareProgress(committedWriterProgress, entry.getValue()) < 0) 
{
+  return false;
+}
+  }
+  return true;
+}
+
+private static int compareProgress(
+final WriterProgress leftProgress, final WriterProgress rightProgress) 
{
+  final int physicalTimeComparison =
+  Long.compare(leftProgress.getPhysicalTime(), 
rightProgress.getPhysicalTime());
+  return physicalTimeComparison != 0
+  ? physicalTimeComparison
+  : Long.compare(leftProgress.getLocalSeq(), 
rightProgress.getLocalSeq());
+}
+  }

Review Comment:
   Applied in 172ad9a2c16. I made the existing compareWriterProgress ordering 
reusable and removed the duplicate comparator from WalFileCommitRequirement, so 
WAL metadata aggregation and coverage checks use the same 
physical-time/local-sequence ordering as the rest of the queue.



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

Re: [PR] [Subscription] Protect retained WAL by committed progress [iotdb]

2026-08-09 Thread via GitHub


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


##
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueue.java:
##
@@ -3192,6 +3305,84 @@ private LiveWALMetaDataSnapshot(final long versionId, 
final WALMetaData metadata
 }
   }
 
+  static final class WalFileCommitRequirement {
+
+private final Map requiredWriterProgress;
+private final boolean containsUnsupportedProgress;
+
+private WalFileCommitRequirement(
+final Map requiredWriterProgress,
+final boolean containsUnsupportedProgress) {
+  this.requiredWriterProgress = requiredWriterProgress;
+  this.containsUnsupportedProgress = containsUnsupportedProgress;
+}
+
+static WalFileCommitRequirement fromMetadata(
+final String regionId, final WALMetaData metadata) {
+  if (Objects.isNull(metadata)) {
+return new WalFileCommitRequirement(Collections.emptyMap(), true);
+  }
+
+  final List buffersSize = metadata.getBuffersSize();
+  final List physicalTimes = metadata.getPhysicalTimes();
+  final List nodeIds = metadata.getNodeIds();
+  final List localSeqs = metadata.getLocalSeqs();
+  if (physicalTimes.size() < buffersSize.size()
+  || nodeIds.size() < buffersSize.size()
+  || localSeqs.size() < buffersSize.size()) {
+return new WalFileCommitRequirement(Collections.emptyMap(), true);
+  }
+
+  final Map requiredWriterProgress = new 
LinkedHashMap<>();
+  for (int i = 0; i < buffersSize.size(); i++) {
+final int writerNodeId = nodeIds.get(i);
+final long physicalTime = physicalTimes.get(i);
+final long localSeq = localSeqs.get(i);
+if (writerNodeId < 0 && physicalTime == 0L && localSeq < 0L) {
+  // Non-search WAL entries do not participate in subscription 
progress.
+  continue;
+}
+if (writerNodeId < 0 || physicalTime < 0L || localSeq < 0L) {
+  // Legacy or incomplete writer metadata cannot be compared safely 
with RegionProgress.
+  return new WalFileCommitRequirement(Collections.emptyMap(), true);
+}
+
+final WriterId writerId = new WriterId(regionId, writerNodeId);
+final WriterProgress candidateProgress = new 
WriterProgress(physicalTime, localSeq);
+requiredWriterProgress.merge(
+writerId,
+candidateProgress,
+(currentProgress, candidate) ->
+compareProgress(candidate, currentProgress) > 0 ? candidate : 
currentProgress);
+  }
+  return new WalFileCommitRequirement(requiredWriterProgress, false);
+}
+
+boolean isCoveredBy(final RegionProgress committedRegionProgress) {
+  if (containsUnsupportedProgress || 
Objects.isNull(committedRegionProgress)) {
+return false;
+  }
+  for (final Map.Entry entry : 
requiredWriterProgress.entrySet()) {
+final WriterProgress committedWriterProgress =
+committedRegionProgress.getWriterPositions().get(entry.getKey());
+if (Objects.isNull(committedWriterProgress)
+|| compareProgress(committedWriterProgress, entry.getValue()) < 0) 
{
+  return false;
+}
+  }
+  return true;
+}
+
+private static int compareProgress(
+final WriterProgress leftProgress, final WriterProgress rightProgress) 
{
+  final int physicalTimeComparison =
+  Long.compare(leftProgress.getPhysicalTime(), 
rightProgress.getPhysicalTime());
+  return physicalTimeComparison != 0
+  ? physicalTimeComparison
+  : Long.compare(leftProgress.getLocalSeq(), 
rightProgress.getLocalSeq());
+}
+  }

Review Comment:
   Is the ordering different from that in other places, and why?
   Otherwise, why is it necessary to keep it here?



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



Re: [PR] [Subscription] Protect retained WAL by committed progress [iotdb]

2026-08-09 Thread via GitHub


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


##
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:
   A new WAL file does not seem to have an impact on the current one.
   And WAL cleanup may be tested by the existence of the current file, which 
should be much cheaper than listing all files.



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



Re: [PR] [Subscription] Protect retained WAL by committed progress [iotdb]

2026-08-07 Thread via GitHub


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]



Re: [PR] [Subscription] Protect retained WAL by committed progress [iotdb]

2026-08-07 Thread via GitHub


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


##
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueue.java:
##
@@ -3192,6 +3305,84 @@ private LiveWALMetaDataSnapshot(final long versionId, 
final WALMetaData metadata
 }
   }
 
+  static final class WalFileCommitRequirement {
+
+private final Map requiredWriterProgress;
+private final boolean containsUnsupportedProgress;
+
+private WalFileCommitRequirement(
+final Map requiredWriterProgress,
+final boolean containsUnsupportedProgress) {
+  this.requiredWriterProgress = requiredWriterProgress;
+  this.containsUnsupportedProgress = containsUnsupportedProgress;
+}
+
+static WalFileCommitRequirement fromMetadata(
+final String regionId, final WALMetaData metadata) {
+  if (Objects.isNull(metadata)) {
+return new WalFileCommitRequirement(Collections.emptyMap(), true);
+  }
+
+  final List buffersSize = metadata.getBuffersSize();
+  final List physicalTimes = metadata.getPhysicalTimes();
+  final List nodeIds = metadata.getNodeIds();
+  final List localSeqs = metadata.getLocalSeqs();
+  if (physicalTimes.size() < buffersSize.size()
+  || nodeIds.size() < buffersSize.size()
+  || localSeqs.size() < buffersSize.size()) {
+return new WalFileCommitRequirement(Collections.emptyMap(), true);
+  }
+
+  final Map requiredWriterProgress = new 
LinkedHashMap<>();
+  for (int i = 0; i < buffersSize.size(); i++) {
+final int writerNodeId = nodeIds.get(i);
+final long physicalTime = physicalTimes.get(i);
+final long localSeq = localSeqs.get(i);
+if (writerNodeId < 0 && physicalTime == 0L && localSeq < 0L) {
+  // Non-search WAL entries do not participate in subscription 
progress.
+  continue;
+}
+if (writerNodeId < 0 || physicalTime < 0L || localSeq < 0L) {
+  // Legacy or incomplete writer metadata cannot be compared safely 
with RegionProgress.
+  return new WalFileCommitRequirement(Collections.emptyMap(), true);
+}
+
+final WriterId writerId = new WriterId(regionId, writerNodeId);
+final WriterProgress candidateProgress = new 
WriterProgress(physicalTime, localSeq);
+requiredWriterProgress.merge(
+writerId,
+candidateProgress,
+(currentProgress, candidate) ->
+compareProgress(candidate, currentProgress) > 0 ? candidate : 
currentProgress);
+  }
+  return new WalFileCommitRequirement(requiredWriterProgress, false);
+}
+
+boolean isCoveredBy(final RegionProgress committedRegionProgress) {
+  if (containsUnsupportedProgress || 
Objects.isNull(committedRegionProgress)) {
+return false;
+  }
+  for (final Map.Entry entry : 
requiredWriterProgress.entrySet()) {
+final WriterProgress committedWriterProgress =
+committedRegionProgress.getWriterPositions().get(entry.getKey());
+if (Objects.isNull(committedWriterProgress)
+|| compareProgress(committedWriterProgress, entry.getValue()) < 0) 
{
+  return false;
+}
+  }
+  return true;
+}
+
+private static int compareProgress(
+final WriterProgress leftProgress, final WriterProgress rightProgress) 
{
+  final int physicalTimeComparison =
+  Long.compare(leftProgress.getPhysicalTime(), 
rightProgress.getPhysicalTime());
+  return physicalTimeComparison != 0
+  ? physicalTimeComparison
+  : Long.compare(leftProgress.getLocalSeq(), 
rightProgress.getLocalSeq());
+}
+  }

Review Comment:
   WriterProgress is a payload value class and does not expose compareTo. The 
queue intentionally keeps compareProgress local so the ordering (physical time, 
then local sequence) is explicit and is shared by all retention checks. No 
behavior change is needed here.



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



Re: [PR] [Subscription] Protect retained WAL by committed progress [iotdb]

2026-08-05 Thread via GitHub


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


##
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:
   Is it possible to record the last visited WalFileCommitRequirement, and 
avoid listing files when it cannot still be covered by the committed progress.
   
   The overhead of computing the id after each commit concerns me.



##
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueue.java:
##
@@ -3192,6 +3305,84 @@ private LiveWALMetaDataSnapshot(final long versionId, 
final WALMetaData metadata
 }
   }
 
+  static final class WalFileCommitRequirement {
+
+private final Map requiredWriterProgress;
+private final boolean containsUnsupportedProgress;
+
+private WalFileCommitRequirement(
+final Map requiredWriterProgress,
+final boolean containsUnsupportedProgress) {
+  this.requiredWriterProgress = requiredWriterProgress;
+  this.containsUnsupportedProgress = containsUnsupportedProgress;
+}
+
+static WalFileCommitRequirement fromMetadata(
+final String regionId, final WALMetaData metadata) {
+  if (Objects.isNull(metadata)) {
+return new WalFileCommitRequirement(Collections.emptyMap(), true);
+  }
+
+  final List buffersSize = metadata.getBuffersSize();
+  final List physicalTimes = metadata.getPhysicalTimes();
+  final List nodeIds = metadata.getNodeIds();
+  final List localSeqs = metadata.getLocalSeqs();
+  if (physicalTimes.size() <