You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "codelipenghui (via GitHub)" <gi...@apache.org> on 2026/08/12 06:03:00 UTC

Re: [PR] [fix][ml] Make inflight reads limiter work even if managed ledger cache is disabled [pulsar]

codelipenghui commented on code in PR #26296:
URL: https://github.com/apache/pulsar/pull/26296#discussion_r3763835745


##########
managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheDisabled.java:
##########
@@ -70,7 +78,63 @@ public void clear() {
     @Override
     public void asyncReadEntry(ReadHandle lh, long firstEntry, long lastEntry, IntSupplier expectedReadCount,
                                final AsyncCallbacks.ReadEntriesCallback callback, Object ctx) {
-        ReadEntryUtils.readAsync(ml, lh, firstEntry, lastEntry).thenAcceptAsync(
+        if (inflightReadsLimiter == null || inflightReadsLimiter.isDisabled()) {
+            readEntries(lh, firstEntry, lastEntry, callback, ctx);
+            return;
+        }
+
+        long estimatedReadSize = (lastEntry - firstEntry + 1) * getEstimatedEntrySize(lh);
+        Optional<InflightReadsLimiter.Handle> optionalHandle = inflightReadsLimiter.acquire(estimatedReadSize, handle ->
+                ml.getExecutor().execute(() -> readEntriesWithAcquiredPermits(lh, firstEntry, lastEntry, callback,
+                        ctx, handle)));
+        optionalHandle.ifPresent(handle -> readEntriesWithAcquiredPermits(lh, firstEntry, lastEntry, callback, ctx,
+                handle));
+    }
+
+    private void readEntriesWithAcquiredPermits(ReadHandle lh, long firstEntry, long lastEntry,
+                                                AsyncCallbacks.ReadEntriesCallback callback, Object ctx,
+                                                InflightReadsLimiter.Handle handle) {
+        if (!handle.success()) {
+            long estimatedReadSize = (lastEntry - firstEntry + 1) * getEstimatedEntrySize(lh);

Review Comment:
   The estimatedReadSize has been calculated twice with line:86?



##########
managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheDisabled.java:
##########
@@ -70,7 +78,63 @@ public void clear() {
     @Override
     public void asyncReadEntry(ReadHandle lh, long firstEntry, long lastEntry, IntSupplier expectedReadCount,
                                final AsyncCallbacks.ReadEntriesCallback callback, Object ctx) {
-        ReadEntryUtils.readAsync(ml, lh, firstEntry, lastEntry).thenAcceptAsync(
+        if (inflightReadsLimiter == null || inflightReadsLimiter.isDisabled()) {
+            readEntries(lh, firstEntry, lastEntry, callback, ctx);
+            return;
+        }
+
+        long estimatedReadSize = (lastEntry - firstEntry + 1) * getEstimatedEntrySize(lh);
+        Optional<InflightReadsLimiter.Handle> optionalHandle = inflightReadsLimiter.acquire(estimatedReadSize, handle ->
+                ml.getExecutor().execute(() -> readEntriesWithAcquiredPermits(lh, firstEntry, lastEntry, callback,
+                        ctx, handle)));
+        optionalHandle.ifPresent(handle -> readEntriesWithAcquiredPermits(lh, firstEntry, lastEntry, callback, ctx,
+                handle));
+    }
+
+    private void readEntriesWithAcquiredPermits(ReadHandle lh, long firstEntry, long lastEntry,
+                                                AsyncCallbacks.ReadEntriesCallback callback, Object ctx,
+                                                InflightReadsLimiter.Handle handle) {
+        if (!handle.success()) {
+            long estimatedReadSize = (lastEntry - firstEntry + 1) * getEstimatedEntrySize(lh);
+            String message = String.format(
+                    "Couldn't acquire enough permits on the max reads in flight limiter to read from ledger "
+                            + "%d, %s, estimated read size %d bytes for %d entries (check "
+                            + "managedLedgerMaxReadsInFlightPermitsAcquireQueueSize (direct config), "
+                            + "managedLedgerMaxReadsInFlightPermitsAcquireTimeoutMillis and "
+                            + "managedLedgerMaxReadsInFlightSizeInMB)", lh.getId(), getName(), estimatedReadSize,
+                    (int) (lastEntry - firstEntry + 1));
+            callback.readEntriesFailed(new ManagedLedgerException.TooManyRequestsException(message), ctx);
+            return;
+        }
+        readEntries(lh, firstEntry, lastEntry, new AsyncCallbacks.ReadEntriesCallback() {
+            @Override
+            public void readEntriesComplete(List<Entry> entries, Object callbackCtx) {
+                if (entries.isEmpty()) {
+                    inflightReadsLimiter.release(handle);
+                } else {
+                    AtomicInteger remainingEntries = new AtomicInteger(entries.size());
+                    for (Entry entry : entries) {
+                        ((EntryImpl) entry).onDeallocate(() -> {
+                            if (remainingEntries.decrementAndGet() == 0) {
+                                inflightReadsLimiter.release(handle);
+                            }
+                        });
+                    }
+                }

Review Comment:
   ```java  
   AtomicInteger remainingEntries = new AtomicInteger(entries.size());                                                                                                                                                 
     Runnable releaseWhenAllDeallocated = () -> {                                                                                                                                                                        
         if (remainingEntries.decrementAndGet() == 0) {                                                                                                                                                                  
             inflightReadsLimiter.release(handle);                                                                                                                                                                       
         }                                                                                                                                                                                                               
     };                                                                                                                                                                                                                  
     for (Entry entry : entries) {                                                                                                                                                                                       
         ((EntryImpl) entry).onDeallocate(releaseWhenAllDeallocated);                                                                                                                                                    
     }  
   ```
   
   It should be able to reduce the object creation by lambda



##########
managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/EntryCacheDisabled.java:
##########
@@ -70,7 +78,63 @@ public void clear() {
     @Override
     public void asyncReadEntry(ReadHandle lh, long firstEntry, long lastEntry, IntSupplier expectedReadCount,
                                final AsyncCallbacks.ReadEntriesCallback callback, Object ctx) {
-        ReadEntryUtils.readAsync(ml, lh, firstEntry, lastEntry).thenAcceptAsync(
+        if (inflightReadsLimiter == null || inflightReadsLimiter.isDisabled()) {
+            readEntries(lh, firstEntry, lastEntry, callback, ctx);
+            return;
+        }
+
+        long estimatedReadSize = (lastEntry - firstEntry + 1) * getEstimatedEntrySize(lh);
+        Optional<InflightReadsLimiter.Handle> optionalHandle = inflightReadsLimiter.acquire(estimatedReadSize, handle ->
+                ml.getExecutor().execute(() -> readEntriesWithAcquiredPermits(lh, firstEntry, lastEntry, callback,
+                        ctx, handle)));
+        optionalHandle.ifPresent(handle -> readEntriesWithAcquiredPermits(lh, firstEntry, lastEntry, callback, ctx,
+                handle));
+    }
+
+    private void readEntriesWithAcquiredPermits(ReadHandle lh, long firstEntry, long lastEntry,

Review Comment:
   The method name is confusing since it might not actually read the entries if the there is no enough queue size. Probably we can consider to move the handle check out of this method. And the branch of line:88 also doesn't need to check the handle again.



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org