You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by lh...@apache.org on 2026/08/27 07:28:09 UTC

(pulsar) branch branch-4.2 updated: [fix][offload] Cache and reuse entry offsets discovered while skipping in BlobStoreBackedReadHandleImpl (#26424)

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

lhotari pushed a commit to branch branch-4.2
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/branch-4.2 by this push:
     new 3258ca54a04a [fix][offload] Cache and reuse entry offsets discovered while skipping in BlobStoreBackedReadHandleImpl (#26424)
3258ca54a04a is described below

commit 3258ca54a04ac316500f3b096e5099cb7c706beb
Author: Omar Yasin <om...@icloud.com>
AuthorDate: Thu Aug 27 07:25:05 2026 +0000

    [fix][offload] Cache and reuse entry offsets discovered while skipping in BlobStoreBackedReadHandleImpl (#26424)
    
    Co-authored-by: Omar Yasin <oy...@apple.com>
    (cherry picked from commit a0f3995097ce0238f74ae4b01433deb100f53e4b)
---
 .../jcloud/impl/BlobStoreBackedReadHandleImpl.java | 26 ++++++++++++++++------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/tiered-storage/jcloud/src/main/java/org/apache/bookkeeper/mledger/offload/jcloud/impl/BlobStoreBackedReadHandleImpl.java b/tiered-storage/jcloud/src/main/java/org/apache/bookkeeper/mledger/offload/jcloud/impl/BlobStoreBackedReadHandleImpl.java
index 77bcfa5cdd80..2ac167d4f86d 100644
--- a/tiered-storage/jcloud/src/main/java/org/apache/bookkeeper/mledger/offload/jcloud/impl/BlobStoreBackedReadHandleImpl.java
+++ b/tiered-storage/jcloud/src/main/java/org/apache/bookkeeper/mledger/offload/jcloud/impl/BlobStoreBackedReadHandleImpl.java
@@ -61,6 +61,11 @@ public class BlobStoreBackedReadHandleImpl implements ReadHandle, OffloadedLedge
     protected static final AtomicIntegerFieldUpdater<BlobStoreBackedReadHandleImpl> PENDING_READ_UPDATER =
             AtomicIntegerFieldUpdater.newUpdater(BlobStoreBackedReadHandleImpl.class, "pendingRead");
 
+    // Bound on how far seekToEntryOffset() probes backwards through entryOffsetsCache for an anchor
+    // before falling back to re-walking from the sparse index marker.
+    private static final long MAX_OFFSET_PROBE =
+            Long.getLong("pulsar.jclouds.readhandleimpl.offsetprobe.max", 1024);
+
     private final long ledgerId;
     private final OffloadIndexBlock index;
     private final BackedInputStream inputStream;
@@ -241,6 +246,7 @@ public class BlobStoreBackedReadHandleImpl implements ReadHandle, OffloadedLedge
                 }
                 long entryId = dataStream.readLong();
                 if (entryId == nextExpectedEntryId) {
+                    entryOffsetsCache.put(ledgerId, entryId, offset);
                     long skipped = inputStream.skip(len);
                     if (skipped != len) {
                         LedgerMetadata ledgerMetadata = getLedgerMetadata();
@@ -285,13 +291,19 @@ public class BlobStoreBackedReadHandleImpl implements ReadHandle, OffloadedLedge
                 inputStream.seek(indexOfNearestEntry.getDataOffset());
                 return;
             }
-            // 2. Try to use the previous index. Since the entry-0 must have a precise index, we can skip to check
-            //    whether "expectedEntryId" is larger than 0;
-            Long cachedPreviousKnownOffset = entryOffsetsCache.getIfPresent(ledgerId, expectedEntryId - 1);
-            if (cachedPreviousKnownOffset != null) {
-                inputStream.seek(cachedPreviousKnownOffset);
-                skipPreviousEntry(expectedEntryId - 1, expectedEntryId);
-                return;
+            // 2. Probe backwards for the nearest cached offset within a bounded window. Since entry-0
+            //    must have a precise index, we can skip checking whether "expectedEntryId" is larger
+            //    than 0. skipPreviousEntry() below caches every entry it walks past, so once a block
+            //    has been walked once, a later jump into the same block lands on that cached run
+            //    within "gap" probes instead of re-walking from the sparse index marker in step 3.
+            long probeFloor = Math.max(indexOfNearestEntry.getEntryId(), expectedEntryId - MAX_OFFSET_PROBE);
+            for (long probe = expectedEntryId - 1; probe >= probeFloor; probe--) {
+                Long cachedOffset = entryOffsetsCache.getIfPresent(ledgerId, probe);
+                if (cachedOffset != null) {
+                    inputStream.seek(cachedOffset);
+                    skipPreviousEntry(probe, expectedEntryId);
+                    return;
+                }
             }
             // 3. Use the persistent index of the nearest entry that is smaller than "expectedEntryId".
             //    Because it is a sparse index, some entries need to be skipped.