This is an automated email from the ASF dual-hosted git repository.

Caideyipi pushed a commit to branch fix/retired-writer-safe-frontier
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit c8cfeaa5f4167780cd69b6d9a85af24495b5544b
Author: Caideyipi <[email protected]>
AuthorDate: Mon Nov 30 15:21:58 2026 +0800

    [Subscription] Release retired writer barriers after catch-up
---
 .../consensus/ConsensusPrefetchingQueue.java       | 19 ++++--
 ...onsensusPrefetchingQueueWriterOrderingTest.java | 79 +++++++++++++++++++++-
 2 files changed, 90 insertions(+), 8 deletions(-)

diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueue.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueue.java
index cca8282e29d..4d2c27a9815 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueue.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueue.java
@@ -2111,12 +2111,10 @@ public class ConsensusPrefetchingQueue {
         lingerBatch.reset();
       }
     }
-    final Map<Integer, SafeFrontier> safeFrontiers =
+    final Map<Integer, SafeFrontier> initialSafeFrontiers =
         serverImpl.getWriterSafeFrontierTracker().snapshotSafeFrontiers();
-    for (final int writerNodeId : replayActiveWriterNodeIds) {
-      getOrCreateWriterReplay(writerNodeId).wasRuntimeActive = true;
-    }
-    safeFrontiers.keySet().forEach(this::getOrCreateWriterReplay);
+    replayActiveWriterNodeIds.forEach(this::getOrCreateWriterReplay);
+    initialSafeFrontiers.keySet().forEach(this::getOrCreateWriterReplay);
     recoveryWriterProgressByWriter.keySet().forEach(id -> 
getOrCreateWriterReplay(id.getNodeId()));
     realtimeEntriesByWriter.keySet().forEach(this::getOrCreateWriterReplay);
     for (final IndexedConsensusRequest request : pendingEntries) {
@@ -2150,6 +2148,14 @@ public class ConsensusPrefetchingQueue {
       if (orderedWriterReplay.size() != writerCount && remainingEntryBudget[0] 
> 0) {
         continue;
       }
+      final Map<Integer, SafeFrontier> safeFrontiers =
+          serverImpl.getWriterSafeFrontierTracker().snapshotSafeFrontiers();
+      final int writerCountBeforeFrontierRefresh = orderedWriterReplay.size();
+      safeFrontiers.keySet().forEach(this::getOrCreateWriterReplay);
+      if (orderedWriterReplay.size() != writerCountBeforeFrontierRefresh
+          && remainingEntryBudget[0] > 0) {
+        continue;
+      }
       if (replayActiveWriterNodeIds != activeWriterNodeIds) {
         return PrefetchRoundResult.rescheduleNow();
       }
@@ -2175,7 +2181,7 @@ public class ConsensusPrefetchingQueue {
               Objects.nonNull(safe) && state.consumedLocalSeq < 
safe.getLocalSeq();
           if (!isWriterRuntimeActive(replayActiveWriterNodeIds, writerNodeId)
               && state.caughtUp
-              && !(state.wasRuntimeActive && hasUnreadAppliedProgress)) {
+              && !hasUnreadAppliedProgress) {
             continue;
           }
           final long safePt =
@@ -2460,7 +2466,6 @@ public class ConsensusPrefetchingQueue {
     private IndexedConsensusRequest head;
     private boolean fromPending;
     private boolean caughtUp;
-    private boolean wasRuntimeActive;
 
     private WriterReplayState(final ProgressWALIterator iterator, final long 
consumedLocalSeq) {
       this.iterator = iterator;
diff --git 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueueWriterOrderingTest.java
 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueueWriterOrderingTest.java
index 983c254c207..cb0dc0dbde6 100644
--- 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueueWriterOrderingTest.java
+++ 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueueWriterOrderingTest.java
@@ -94,7 +94,7 @@ public class ConsensusPrefetchingQueueWriterOrderingTest {
   private int originalWalEntries;
   private int originalTablets;
   private int originalDelay;
-  private final WriterSafeFrontierTracker tracker = new 
WriterSafeFrontierTracker();
+  private final WriterSafeFrontierTracker tracker = spy(new 
WriterSafeFrontierTracker());
   private final AtomicInteger conversions = new AtomicInteger();
   private final AtomicInteger walReads = new AtomicInteger();
   private ConsensusPrefetchingQueue queue;
@@ -343,6 +343,83 @@ public class ConsensusPrefetchingQueueWriterOrderingTest {
     assertEquals(Set.of(1, 5), queue.getActiveWriterNodeIds());
   }
 
+  @Test
+  public void 
testLeaderOnlyReplayWaitsForUnreadRetiredWriterUntilWalVisibility() throws 
Exception {
+    final File directory = 
temporaryFolder.newFolder("leader-only-unseen-writer-wal");
+    newQueue(walNode(directory, 1), 1024 * 1024, 
TopicConstant.ORDER_MODE_LEADER_ONLY_VALUE);
+    queue.applyRuntimeState(new ConsensusRegionRuntimeState(2, 5, true, 
Set.of(1, 5, 7)));
+    queue.applyRuntimeState(new ConsensusRegionRuntimeState(3, 1, true, 
Set.of(1, 5, 7)));
+    tracker.recordAppliedProgress(0, 5, 0);
+    tracker.observePendingWriterSafeTimeBarrier(3000, 1, 0);
+    assertEquals(Set.of(1, 5), queue.getActiveWriterNodeIds());
+    assertTrue(pending().offer(request(1, 1, 2000)));
+
+    queue.drivePrefetchOnce();
+    assertEquals(0, conversions.get());
+    assertNull(queue.poll("consumer"));
+
+    queue.applyRuntimeState(ConsensusRegionRuntimeState.leaderOnly(4, 1, 
true));
+    assertEquals(Set.of(1), queue.getActiveWriterNodeIds());
+    final AtomicInteger safeFrontierSnapshots = new AtomicInteger();
+    doAnswer(
+            invocation -> {
+              final Object snapshot = invocation.callRealMethod();
+              if (safeFrontierSnapshots.getAndIncrement() == 0) {
+                tracker.recordAppliedProgress(0, 5, 1);
+              }
+              return snapshot;
+            })
+        .when(tracker)
+        .snapshotSafeFrontiers();
+
+    for (int round = 0; round < 10; round++) {
+      assertEquals(PrefetchRoundResult.Type.RESCHEDULE_LATER, 
queue.drivePrefetchOnce().getType());
+    }
+    assertTrue(safeFrontierSnapshots.get() >= 20);
+    assertEquals(0, conversions.get());
+    assertEquals(0L, queue.getRetainedTabletBytes());
+    assertNull(queue.poll("consumer"));
+
+    final File data =
+        new File(directory, WALFileUtils.getLogFileName(0, 0, 
WALFileStatus.CONTAINS_SEARCH_INDEX));
+    try (WALWriter writer = new WALWriter(data, WALFileVersion.V3)) {
+      writeEntry(writer, -1, 5, 1000, 1);
+    }
+    try (WALWriter ignored =
+        new WALWriter(
+            new File(
+                directory, WALFileUtils.getLogFileName(1, 1, 
WALFileStatus.CONTAINS_SEARCH_INDEX)),
+            WALFileVersion.V3)) {}
+    queue.drivePrefetchOnce();
+
+    assertEvent(5, 1000);
+    assertEvent(1, 2000);
+    assertNull(queue.poll("consumer"));
+    assertEquals(2, conversions.get());
+  }
+
+  @Test
+  public void 
testLeaderOnlyReplayReleasesRetiredCaughtUpWriterWithoutSafeTime() throws 
Exception {
+    newQueue(
+        mock(ConsensusReqReader.class), 1024 * 1024, 
TopicConstant.ORDER_MODE_LEADER_ONLY_VALUE);
+    queue.applyRuntimeState(new ConsensusRegionRuntimeState(2, 5, true, 
Set.of(1, 5)));
+    queue.applyRuntimeState(new ConsensusRegionRuntimeState(3, 1, true, 
Set.of(1, 5)));
+    tracker.recordAppliedProgress(0, 5, 0);
+    assertTrue(pending().offer(request(1, 1, 2000)));
+
+    queue.drivePrefetchOnce();
+    assertNull(queue.poll("consumer"));
+    assertEquals(0, conversions.get());
+
+    queue.applyRuntimeState(ConsensusRegionRuntimeState.leaderOnly(4, 1, 
true));
+    queue.drivePrefetchOnce();
+
+    assertEvent(1, 2000);
+    assertNull(queue.poll("consumer"));
+    assertEquals(1, conversions.get());
+    assertEquals(Set.of(1), queue.getActiveWriterNodeIds());
+  }
+
   @Test
   public void testDefaultPendingBatchBoundaryKeepsWriterOrdering() throws 
Exception {
     config.setSubscriptionConsensusBatchMaxDelayInMs(50);

Reply via email to