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

jt2594838 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 43b531db38f Cache consensus group ID in subscription prefetch (#19420)
43b531db38f is described below

commit 43b531db38f378cc99108959f8e58fac189b1423
Author: Caideyipi <[email protected]>
AuthorDate: Fri Nov 27 16:48:55 2026 +0800

    Cache consensus group ID in subscription prefetch (#19420)
---
 .../consensus/ConsensusPrefetchingQueue.java       | 35 ++++++------
 .../consensus/ConsensusPrefetchingQueueTest.java   | 65 ++++++++++++++++++++++
 2 files changed, 83 insertions(+), 17 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 643018691f8..cca8282e29d 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
@@ -124,6 +124,7 @@ public class ConsensusPrefetchingQueue {
   private final String consumerGroupId;
   private final String topicName;
   private final ConsensusGroupId consensusGroupId;
+  private final String consensusGroupIdString;
 
   private final IoTConsensusServerImpl serverImpl;
 
@@ -713,6 +714,7 @@ public class ConsensusPrefetchingQueue {
     this.consumerGroupId = consumerGroupId;
     this.topicName = topicName;
     this.consensusGroupId = consensusGroupId;
+    this.consensusGroupIdString = consensusGroupId.toString();
     this.serverImpl = serverImpl;
     this.consensusReqReader = serverImpl.getConsensusReqReader();
     this.retentionPolicy = retentionPolicy;
@@ -1307,7 +1309,7 @@ public class ConsensusPrefetchingQueue {
   }
 
   private WriterId toWriterId(final IndexedConsensusRequest request) {
-    return new WriterId(consensusGroupId.toString(), request.getNodeId());
+    return new WriterId(consensusGroupIdString, request.getNodeId());
   }
 
   private WriterProgress toWriterProgress(final IndexedConsensusRequest 
request) {
@@ -2300,7 +2302,7 @@ public class ConsensusPrefetchingQueue {
         ignored -> {
           final WriterProgress recovered =
               recoveryWriterProgressByWriter.get(
-                  new WriterId(consensusGroupId.toString(), writerNodeId));
+                  new WriterId(consensusGroupIdString, writerNodeId));
           final WriterProgress materialized = 
materializedProgressByWriter.get(writerNodeId);
           return new WriterReplayState(
               createSubscriptionWALIterator(Long.MIN_VALUE),
@@ -2320,8 +2322,11 @@ public class ConsensusPrefetchingQueue {
       return true;
     }
     for (final IndexedConsensusRequest request : pendingEntries) {
-      if (shouldSkipForRecoveryProgress(request)
-          || shouldSkipForMaterializedProgress(request)
+      final boolean skipForRecoveryProgress = 
shouldSkipForRecoveryProgress(request);
+      final boolean skipForMaterializedProgress =
+          !skipForRecoveryProgress && 
shouldSkipForMaterializedProgress(request);
+      if (skipForRecoveryProgress
+          || skipForMaterializedProgress
           || (hasLocalSearchIndex(request)
               && request.getSearchIndex() < orderedReplayStartSearchIndex)) {
         if (pendingEntries.remove(request)) {
@@ -2330,8 +2335,6 @@ public class ConsensusPrefetchingQueue {
         continue;
       }
       if (request.getNodeId() == writerNodeId
-          && !shouldSkipForRecoveryProgress(request)
-          && !shouldSkipForMaterializedProgress(request)
           && (Objects.isNull(state.iterator)
               || request.getProgressLocalSeq() == state.consumedLocalSeq + 
1L)) {
         state.head = request;
@@ -2618,8 +2621,7 @@ public class ConsensusPrefetchingQueue {
       sample.put("materializedProgress", 
materializedProgressByWriter.get(writerNodeId));
       sample.put(
           "recoveryProgress",
-          recoveryWriterProgressByWriter.get(
-              new WriterId(consensusGroupId.toString(), writerNodeId)));
+          recoveryWriterProgressByWriter.get(new 
WriterId(consensusGroupIdString, writerNodeId)));
       final WriterReplayState replay = orderedWriterReplay.get(writerNodeId);
       if (Objects.nonNull(replay)) {
         sample.put("consumedLocalSeq", replay.consumedLocalSeq);
@@ -2776,7 +2778,7 @@ public class ConsensusPrefetchingQueue {
       final long physicalTime, final int nodeId, final long localSeq) {
     final WriterProgress candidate = new WriterProgress(physicalTime, 
localSeq);
     final WriterProgress recoveryProgress =
-        recoveryWriterProgressByWriter.get(new 
WriterId(consensusGroupId.toString(), nodeId));
+        recoveryWriterProgressByWriter.get(new 
WriterId(consensusGroupIdString, nodeId));
     if (Objects.nonNull(recoveryProgress)
         && compareWriterProgress(candidate, recoveryProgress) <= 0) {
       return true;
@@ -3503,7 +3505,7 @@ public class ConsensusPrefetchingQueue {
         batchWriterNodeId >= 0
             ? batchWriterNodeId
             : IoTDBDescriptor.getInstance().getConfig().getDataNodeId();
-    final WriterId writerId = new WriterId(consensusGroupId.toString(), 
effectiveNodeId);
+    final WriterId writerId = new WriterId(consensusGroupIdString, 
effectiveNodeId);
     final WriterProgress writerProgress = new 
WriterProgress(batchPhysicalTime, localSeq);
     return new SubscriptionCommitContext(
         IoTDBDescriptor.getInstance().getConfig().getDataNodeId(),
@@ -4386,7 +4388,7 @@ public class ConsensusPrefetchingQueue {
 
   private WriterId extractCommitWriterId(final SubscriptionCommitContext 
commitContext) {
     final WriterId writerId = commitContext.getWriterId();
-    return Objects.nonNull(writerId) ? writerId : new 
WriterId(consensusGroupId.toString(), -1);
+    return Objects.nonNull(writerId) ? writerId : new 
WriterId(consensusGroupIdString, -1);
   }
 
   private WriterProgress extractCommitWriterProgress(
@@ -5113,8 +5115,7 @@ public class ConsensusPrefetchingQueue {
       if (Objects.isNull(requirement)) {
         try (final ProgressWALReader reader = openProgressWALReader(walFile)) {
           requirement =
-              WalFileCommitRequirement.fromMetadata(
-                  consensusGroupId.toString(), reader.getMetaData());
+              WalFileCommitRequirement.fromMetadata(consensusGroupIdString, 
reader.getMetaData());
           walFileCommitRequirements.put(versionId, requirement);
         } catch (final IOException e) {
           LOGGER.warn(
@@ -5298,7 +5299,7 @@ public class ConsensusPrefetchingQueue {
         continue;
       }
 
-      final WriterId writerId = new WriterId(consensusGroupId.toString(), 
writerNodeId);
+      final WriterId writerId = new WriterId(consensusGroupIdString, 
writerNodeId);
       final WriterProgress candidateProgress = new 
WriterProgress(physicalTime, localSeq);
       final WriterProgress currentProgress = 
tailProgressByWriter.get(writerId);
       if (Objects.isNull(currentProgress)
@@ -5693,7 +5694,7 @@ public class ConsensusPrefetchingQueue {
         consumerGroupId,
         INVALID_COMMIT_ID,
         seekGeneration.get(),
-        consensusGroupId.toString());
+        consensusGroupIdString);
   }
 
   public boolean isCommitContextOutdated(final SubscriptionCommitContext 
commitContext) {
@@ -5920,7 +5921,7 @@ public class ConsensusPrefetchingQueue {
         IoTDBDescriptor.getInstance().getConfig().getDataNodeId(),
         consumerGroupId,
         topicName,
-        consensusGroupId.toString(),
+        consensusGroupIdString,
         isActive,
         prefetchInitialized,
         currentWalSearchIndex,
@@ -6267,7 +6268,7 @@ public class ConsensusPrefetchingQueue {
     final Map<String, String> result = new HashMap<>();
     result.put("consumerGroupId", consumerGroupId);
     result.put("topicName", topicName);
-    result.put("consensusGroupId", consensusGroupId.toString());
+    result.put("consensusGroupId", consensusGroupIdString);
     result.put("currentReadSearchIndex", 
String.valueOf(nextExpectedSearchIndex.get()));
     result.put("prefetchingQueueSize", 
String.valueOf(prefetchingQueue.size()));
     result.put("inFlightEventsSize", String.valueOf(inFlightEvents.size()));
diff --git 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueueTest.java
 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueueTest.java
index 9e92ec114ff..6af4205c4dd 100644
--- 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueueTest.java
+++ 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/subscription/broker/consensus/ConsensusPrefetchingQueueTest.java
@@ -129,6 +129,71 @@ public class ConsensusPrefetchingQueueTest {
 
   @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder();
 
+  @Test
+  public void testToWriterIdReusesConsensusGroupIdString() throws Exception {
+    final String originalSystemDir = 
IoTDBDescriptor.getInstance().getConfig().getSystemDir();
+    final CountingDataRegionId consensusGroupId = new CountingDataRegionId(1);
+    ConsensusPrefetchingQueue queue = null;
+    try {
+      final FakeConsensusReqReader reader = new FakeConsensusReqReader();
+      reader.currentSearchIndex = 1L;
+      final IoTConsensusServerImpl server = mock(IoTConsensusServerImpl.class);
+      when(server.getConsensusReqReader()).thenReturn(reader);
+      when(server.getWriterSafeFrontierTracker()).thenReturn(new 
WriterSafeFrontierTracker());
+      queue =
+          new ConsensusPrefetchingQueue(
+              "consumerGroup",
+              "topic",
+              TopicConstant.ORDER_MODE_MULTI_WRITER_VALUE,
+              consensusGroupId,
+              server,
+              new SubscriptionWalRetentionPolicy(
+                  "topic",
+                  SubscriptionWalRetentionPolicy.UNBOUNDED,
+                  SubscriptionWalRetentionPolicy.UNBOUNDED),
+              mock(ConsensusLogToTabletConverter.class),
+              newCommitManager(temporaryFolder.newFolder("writer-id-cache")),
+              new RegionProgress(Collections.emptyMap()),
+              1L,
+              1L,
+              false);
+
+      final int toStringCallCount = consensusGroupId.toStringCallCount;
+      final Method toWriterId =
+          ConsensusPrefetchingQueue.class.getDeclaredMethod(
+              "toWriterId", IndexedConsensusRequest.class);
+      toWriterId.setAccessible(true);
+      final IndexedConsensusRequest request =
+          new IndexedConsensusRequest(1L, 
Collections.emptyList()).setNodeId(7);
+      for (int i = 0; i < 1000; i++) {
+        final WriterId writerId = (WriterId) toWriterId.invoke(queue, request);
+        assertEquals("DataRegion[1]", writerId.getRegionId());
+        assertEquals(7, writerId.getNodeId());
+      }
+      assertEquals(toStringCallCount, consensusGroupId.toStringCallCount);
+    } finally {
+      if (queue != null) {
+        queue.close();
+      }
+      
IoTDBDescriptor.getInstance().getConfig().setSystemDir(originalSystemDir);
+    }
+  }
+
+  private static final class CountingDataRegionId extends DataRegionId {
+
+    private int toStringCallCount;
+
+    private CountingDataRegionId(final int id) {
+      super(id);
+    }
+
+    @Override
+    public String toString() {
+      toStringCallCount++;
+      return super.toString();
+    }
+  }
+
   @Test
   public void testReplayStallWarningIsThrottledAndResetsAfterProgress() throws 
Exception {
     final String originalSystemDir = 
IoTDBDescriptor.getInstance().getConfig().getSystemDir();

Reply via email to