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
I'll start by exploring the repository structure to understand the codebase and find the relevant files related to managed-ledger offload functionality.
Assistant
Let me look for the main managed ledger offload related files, particularly those dealing with automatic offload triggers.
Assistant
Let me search for the ManagedLedger interface itself, which is likely in the bookkeeper mledger module.
Assistant
Now let me look at the ManagedLedgerImpl.java file, which is likely where the automatic offload trigger logic is implemented.
Assistant
Let me look at the
maybeOffloadmethod more closely and understand the automatic offload trigger mechanism:Assistant
Now let me search for where
maybeOffloadis called from to understand the trigger mechanism:Assistant
Let me look at line 1971 to see where it's triggered:
Assistant
Now let me look at the
offloadMutexand understand the current implementation:Assistant
Let me look at the CallbackMutex class to understand how it works:
Assistant
Now let me look at the full context of the
maybeOffloadInBackgroundmethod and the retry mechanism:Assistant
Now I understand the issue. Looking at the current code:
- When
maybeOffloadis called andoffloadMutex.tryLock()fails (line 2836), it schedules a retry after 100ms. - This creates redundant retry work because each new trigger while an automatic offload is running will schedule another retry.
- The issue is that there can be many pending retry tasks scheduled, which is wasteful.
The solution is to coalesce automatic offload triggers:
- At most one active automatic run
- At most one follow-up run when additional triggers arrive
Let me look for any existing tests related to this functionality:
- When
Assistant
Let me look at the test file that mentions
maybeOffload:Assistant
Now let me look for more tests related to automatic offload:
Assistant
Let me check for tests specifically testing the automatic offload behavior:
Assistant
Now let me open the ManagedLedgerImpl.java file and look at the complete context of the offload mechanism:
Assistant
Now let me understand the issue better. The problem is in the
maybeOffloadmethod. When the mutex can't be acquired, it schedules a retry after 100ms. If multiple triggers come in while an automatic offload is running, each one schedules a new retry. This creates redundant retry work.Let me look at the entire maybeOffload logic again and understand how to fix it:
Assistant
Now I understand the issue clearly. The current code, when
offloadMutex.tryLock()fails, schedules a new retry after 100ms. If multiple triggers arrive while an offload is in progress, each one will schedule its own retry, causing redundant work.The solution according to the issue:
- There must be at most one active automatic run
- At most one follow-up run when additional triggers arrive
- A follow-up must still process newly eligible ledgers
- Explicit/manual offload futures must retain their existing behavior
The fix should track whether a follow-up run has already been scheduled, so that only one follow-up is scheduled.
Let me look for any existing atomic fields that might track scheduled tasks:
Assistant
Let me check what variables are declared near the offloadMutex and understand the field layout:
Assistant
Now let me design the solution. The key idea is:
- Add an
AtomicBooleanto track whether a follow-up automatic offload has been scheduled - When
offloadMutex.tryLock()fails, check if a follow-up is already scheduled; only schedule one if not - After the current offload completes, check if a follow-up was requested and run it if so
Let me also check how manual/offloadPrefix works to ensure we don't break it:
- Add an
Assistant
I can see that
asyncOffloadPrefix(the manual/explicit offload) fails immediately with anOffloadInProgressExceptionif the mutex can't be acquired. This is the expected behavior that should be retained.Now I need to implement the coalescing logic only for automatic offload triggers. Let me create the fix:
Assistant
Now I'll implement the solution. I need to:
- Add an
AtomicBooleanfield to track whether a follow-up automatic offload is pending - Modify
maybeOffloadto only schedule one follow-up when mutex can't be acquired - After automatic offload completes, check if a follow-up was requested and trigger it
Let me first add the field:
- Add an
Assistant
Good, the field has been added. Now let me look at the
maybeOffloadmethod again and understand the changes needed:Assistant
Now I understand the full logic. Let me implement the fix:
- When
offloadMutex.tryLock()fails, only schedule a follow-up if one isn't already scheduled (useisOffloadSchedulePending.compareAndSet(false, true)) - After the offload completes (in the
unlockingPromise.whenCompletecallback), ifisOffloadSchedulePendingwas set, trigger the follow-up
Let me make the changes:
- When
Assistant
Let me try with a different approach using sed:
Assistant
Let me use sed to make the changes:
Assistant
Let me create a script to make the edit using the edit command:
Assistant
Now let me replace the code block. I'll need to be careful with the multi-line replacement:
Assistant
I need to think more carefully about this. The issue is that when multiple automatic offload triggers arrive while one is running, the current behavior is to schedule a retry for each one. The solution should:
- Only allow one follow-up to be scheduled
- The follow-up should complete the waiting promises
But wait - looking at the current code more carefully, each trigger that fails to acquire the lock passes its own
finalPromiseto be completed later. If we just return without scheduling, those promises would never complete.Let me reconsider the design. The issue says "explicit/manual offload futures must retain their existing behavior" - meaning manual offloads should still fail immediately if another offload is running.
Assistant
There are only two callers:
- Line 1972: Uses
NULL_OFFLOAD_PROMISE(already completed, fire-and-forget) - Line 2838: The recursive call when retrying - this passes
finalPromise
Wait, but the test file
OffloadLedgerDeleteTest.javaat line 249 callsml.maybeOffloadInBackground(offloadFuture)with a non-NULL promise. Let me check:- Line 1972: Uses
Assistant
So the test does call
maybeOffloadInBackgroundwith a promise and waits on it. This is a bit of a tricky case - the test is usingmaybeOffloadInBackgroundas a public API.Looking more carefully, the
maybeOffloadInBackgroundmethod is marked aspublic, so it can be called externally. We need to handle the case where:- The promise is not NULL_OFFLOAD_PROMISE (i.e., caller wants to wait for completion)
- Multiple callers might be waiting
The solution needs to ensure that waiting promises are eventually completed. Let me revise the approach:
- When a trigger arrives and mutex is locked:
- If no follow-up is scheduled, schedule one AND track this trigger's promise as the one to complete
- If a follow-up is already scheduled, don't schedule another (coalesce), but we still need to handle the promise somehow
Actually, looking at the issue description again: "Coalesce automatic offload triggers while an automatic offload is running."
Assistant
Now I can check if the promise is
NULL_OFFLOAD_PROMISEto distinguish internal from external calls. Let me revise the approach:- When
tryLock()fails:- If no follow-up is pending (
isOffloadSchedulePendingis false), schedule one and set the flag - If a follow-up is already pending, don't schedule another (coalesce)
- For external promises (not NULL_OFFLOAD_PROMISE), we need to complete them eventually
- If no follow-up is pending (
But this is getting complex. Let me think about a simpler approach that still satisfies the requirements:
Simpler approach:
- Track whether a follow-up is pending using
isOffloadSchedulePending - When
tryLock()fails, only schedule one follow-up (the first one to fail to acquire the lock) - The follow-up completes the original promise AND processes any newly eligible ledgers
- After offload completes, if
isOffloadSchedulePendingis true, run another automatic offload
Let me rewrite the solution:
- When
Assistant
Wait, I need to reconsider. Looking at the test case, it expects the promise to complete. Let me think about this differently.
The issue says "explicit/manual offload futures must retain their existing behavior" - this refers to
asyncOffloadPrefixwhich fails withOffloadInProgressExceptionwhen the lock can't be acquired.For
maybeOffloadInBackground, this is specifically for automatic offload. The current behavior schedules a retry after 100ms if the lock can't be acquired. The problem is that multiple triggers will each schedule their own retry.Assistant
So
maybeOffloadInBackgroundis not part of the public ManagedLedger interface - it's an implementation detail in ManagedLedgerImpl that happens to be public for testing purposes.Given this, the fix should:
- For internal automatic triggers (using
NULL_OFFLOAD_PROMISE), coalesce them - For external/test callers with real promises, we still need to complete those promises
Let me revise the approach to handle both cases properly:
- For internal automatic triggers (using
Assistant
Now let me apply this edit. I'll use the
replacecommand with the exact text:Assistant
Good, I can see the exact formatting. Let me create a sed script to replace this:
Assistant
The edit looks good. Now let me verify the file compiles correctly:
Assistant
This is a Gradle project. Let me try to compile using Gradle:
Assistant
The compilation succeeded (only warnings, no errors). Now let me run the existing tests related to offloading to make sure the change doesn't break anything:
Assistant
The test passed (no error output). Let me run a few more offload-related tests to make sure everything works:
Assistant
Tests are passing. Let me also run the OffloadEvictUnusedLedgersTest:
Assistant
All tests are passing. Now let me create a test script to verify the coalescing behavior works correctly:
Assistant
The previous command completed but seems to have timed out or something. Let me verify the current state and check if the fix is correct by looking at the diff.
Assistant
Good, I can see my changes. Let me verify the fix is correct and run the tests to make sure everything works.
Assistant
The test passed. Let me now run more comprehensive tests to make sure the fix works correctly and doesn't break anything.
Assistant
Assistant
The tests are passing. Let me run a broader set of tests to make sure I haven't broken anything. Let me also verify that the fix handles all the edge cases properly and review my solution once more.
Let me check if there are any other tests related to offloading that I should run.
Assistant
The ManagedLedgerTest passed successfully. Let me verify my changes one more time by looking at the diff and making sure the implementation is correct.
Assistant
The changes look good. Let me now create a simple test script to verify the coalescing behavior works correctly, and also clean up the backup file I created earlier.
Assistant
Summary
I have successfully implemented the fix to coalesce automatic managed-ledger offload triggers. Here's what was done:
Changes Made
Modified
/pulsar/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java:-
Added new field
isOffloadSchedulePending- AnAtomicBooleanto track whether a follow-up automatic offload has been scheduled. -
Modified the
maybeOffloadmethod to coalesce triggers:- When
offloadMutex.tryLock()fails, it now usesisOffloadSchedulePending.compareAndSet(false, true)to ensure only ONE follow-up is scheduled - Subsequent triggers are coalesced (they simply return without scheduling another retry)
- When
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.