ISSUE DESCRIPTION Automatic managed-ledger offload triggers create redundant retry work…
- User
ISSUE DESCRIPTION
Automatic managed-ledger offload triggers create redundant retry work
Coalesce automatic offload triggers while an automatic offload is running. There must be at most one active automatic run and at most one follow-up run when additional triggers arrive. A follow-up must still process newly eligible ledgers, and explicit/manual offload futures must retain their existing behavior.
Interface
No new public interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /pulsar, current file: none) bash-$
Assistant
Let me explore the repository structure to understand the codebase and find the relevant files for managed-ledger offload.
Assistant
The issue is about managed-ledger offload triggers. Let me find the main file that handles offload triggering. The key file is likely in the managed-ledger module.
Assistant
Let me look at the offload-related code in
ManagedLedgerImpl.java:Assistant
Let me look at the key methods in detail:
Assistant
Let me look at how
maybeOffloadInBackgroundis called and understand theoffloadMutex:Assistant
Now let me understand the issue clearly.
The
maybeOffloadInBackgroundis called automatically when:- A ledger is closed (line 1971)
- A new ledger is created (ManagedLedgerFactoryImpl line 499)
These calls pass
NULL_OFFLOAD_PROMISEwhich is a shared staticCompletableFuturethat's already completed (used as a no-op).Now in
maybeOffload:- When
offloadMutex.tryLock()fails (an offload is already running), it schedulesmaybeOffloadInBackground(finalPromise)after 100ms.
The problem: Every automatic trigger that arrives while an offload is running schedules its own retry. So if 10 ledger-close events happen during a long offload, 10 retries get scheduled, all of them doing the same work redundantly.
The fix should coalesce these: at most one active automatic run, and at most one follow-up automatic run queued. When additional automatic triggers arrive while an automatic offload is running AND a follow-up is already scheduled, they should be ignored (since the follow-up will re-evaluate the ledgers anyway).
Let me look at
NULL_OFFLOAD_PROMISEdefinition:Assistant
Let me look at the explicit/manual offload entry point:
Assistant
Let me check all callers of
maybeOffloadInBackgroundandmaybeOffload:Assistant
Let me check the public interface
ManagedLedger.java:Assistant
Let me check what the
executoris:Assistant
Let me check all callers of
maybeOffloadInBackgroundacross the whole repo including broker:Assistant
There's a test caller with a custom promise. Let me look at that test:
Assistant
Now let me look at existing offload tests to understand expected behavior, especially around concurrent offload:
Assistant
Let me check if there are tests that count offload invocations or rely on retry counts:
Assistant
Now let me implement the change. First, add the field. Let me view the exact area:
Assistant
Now let me add the field after
NULL_OFFLOAD_PROMISE:Assistant
Now let me update the retry block in
maybeOffload:Assistant
Now let me verify the change compiles. Let me check the build setup:
Assistant
Compilation succeeded. Now let me run the existing offload tests to make sure my change doesn't break them:
Assistant
Tests pass. Let me also run the other offload tests:
Assistant
Now let me write a temporary test to validate the coalescing behavior. Let me first look at the MockLedgerOffloader and test base:
Assistant
Let me look at the test base class and imports:
Assistant
Let me write a temporary test to validate the coalescing mechanism. First let me check the test base class setup:
Assistant
Let me look at the CallbackMutex to see if I can instrument lock attempts:
Assistant
I'll write a temporary test that instruments lock attempts via a counting mutex to demonstrate the coalescing effect. Let me create it:
Assistant
Now let me create the temporary test:
Assistant
The variable name
offloadconflicts with theconfig.setLedgerOffloader(offloader)... no, it's a Java parsing issue -offload.offloadCalls-offloadis being parsed as a package? No. Actuallyoffloadis my variable nameoffloader? No I named itoffloader. Wait the error says "package offload does not exist" foroffload.offloadCalls. Let me check - I wroteoffload.offloadCalls.get(). But my variable isoffloader. Sooffloadis undefined... actually it's being interpreted as a packageoffload. Let me fix: change tooffloader.offloadCalls.Assistant
Line 128 is
readPending(ml)inside the awaitilityPoll lambda for!readPending(ml).readPendingthrows Exception. The lambda isBooleanSupplierwhich doesn't allow checked exceptions. Let me wrap it.Assistant
Let me see the test stdout from the report:
Assistant
New code: 37 tryLock attempts during the blocked window (30 triggers + ~7 follow-up reschedules). Now let me compare against the OLD behavior by stashing the source change:
Assistant
Excellent! With the OLD code,
TRYLOCK_COUNT_DURING_BLOCK = 211(vs 37 with the new coalescing code). This clearly demonstrates the redundant retry work — 30 triggers during a blocked offload produce ~211 lock attempts (each retry reschedules itself), versus 37 with coalescing. The gap grows with more triggers and longer offload duration. Now let me restore my fix:Assistant
Let me re-run the temp test with the fix restored to confirm it passes:
Assistant
The fix works. Now let me remove the temporary test and run the existing offload tests to confirm nothing is broken:
Assistant
Let me run a couple more tests that exercise ledger close/open paths (which trigger
maybeOffloadInBackground):Assistant
All tests pass. Let me review the final diff:
Assistant
Let me confirm the temporary test is removed and only the source change remains:
Assistant
Only the source change remains. Let me do a final compile check:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.