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 locate relevant files related to managed-ledger offload triggers. Let's first get an overview.
- Assistant
Let me look for the managed-ledger offload-related classes more specifically. The issue mentions "managed-ledger offload triggers", so let me search for classes related to offloading in the managed-ledger module.
- Assistant
Let me look for the main classes that handle managed ledger offloading and the trigger mechanisms. The issue mentions "automatic managed-ledger offload triggers" and "coalesce automatic offload triggers". Let me search for the relevant classes in the managed ledger module.
- Assistant
No results. Let me search for managed ledger related files more broadly.
- Assistant
Let me search more broadly for managed-ledger related files.
- Assistant
Let me look for the managed ledger module specifically - it's likely in a BookKeeper directory or a managed ledger directory.
- Assistant
Great, there's a managed-ledger directory. Let me explore its structure.
- Assistant
Let me search for the key classes that handle offload triggers:
- Assistant
Let me examine the ManagedLedgerImpl.java file which likely contains the offload trigger logic:
- Assistant
Now let me look at the
maybeOffloadmethod and related trigger code in ManagedLedgerImpl.java to understand how automatic offloads are triggered. - Assistant
Let me look at the
maybeOffloadmethod in detail: - Assistant
Let me see where
maybeOffloadInBackgroundis called: - Assistant
Now let me understand the problem. The issue is:
- Automatic offload triggers (from
maybeOffloadInBackground) can be called multiple times. - When an automatic offload is already running (the mutex is locked), new triggers are scheduled as retries (line 2837:
scheduledExecutor.schedule(() -> maybeOffloadInBackground(finalPromise), 100, TimeUnit.MILLISECONDS)). - This means each trigger creates a new retry work item, leading to many redundant retries.
The desired behavior is:
- At most one active automatic run
- At most one follow-up run when additional triggers arrive during an active run
- Explicit/manual offload futures must retain their existing behavior
Let me also look at what calls
maybeOffloadInBackground: - Automatic offload triggers (from
- Assistant
Let me now look at the
offloadPrefixmethod (manual offload path): - Assistant
Let me look at line 1971 to understand how automatic offload is triggered:
- Assistant
Let me look at
NULL_OFFLOAD_PROMISE: - Assistant
Let me see how this promise is used:
- Assistant
Now let me look at the existing tests for offload functionality:
- Assistant
Let me examine the specific area around the automatic offload logic more closely:
- Assistant
Let me see the full method and understand the structure better. Let me look at lines 2830-2900.
- Assistant
Let me check what the executor is and whether operations are single-threaded:
- Assistant
Let me check what
scheduledExecutoris: - Assistant
Now let me look at the full
maybeOffloadInBackgroundandmaybeOffloadmethods together: - Assistant
Now let me look at the offload tests to understand the test patterns:
- Assistant
Now let me understand the full flow. Let me look at how
asyncOffloadPrefixinteracts with the mutex: - Assistant
Now I have a good understanding of the issue. Let me analyze:
-
Automatic offload path:
maybeOffloadInBackground→maybeOffload→ if mutex is locked, schedules anothermaybeOffloadInBackgroundafter 100ms delay. This creates a chain of retries where each new trigger creates another scheduled task. -
Manual offload path:
asyncOffloadPrefix→ if mutex is locked, immediately returnsOffloadInProgressException.
The issue is: When multiple automatic offload triggers arrive while an automatic offload is running, each trigger creates a new scheduled retry task. These retry tasks pile up and create redundant work. There should be at most one active automatic run and at most one follow-up run.
The fix should:
- Track whether a follow-up automatic offload is already pending
- When a new automatic offload trigger arrives while one is running, just mark that a follow-up is needed (instead of scheduling a new retry)
- When the current automatic offload completes, check if a follow-up is needed and schedule exactly one
- Manual/explicit offload futures should retain their existing behavior (they already return OffloadInProgressException immediately)
-
- Assistant
Now I understand the full problem and solution. Let me implement the fix:
- Add an
AtomicBooleanfieldautomaticOffloadFollowUpto track pending automatic offloads - When
maybeOffloadcan't acquire the mutex, set the flag instead of scheduling a new retry - When the current offload completes, check the flag and schedule a single follow-up if needed
- Manual offloads (
asyncOffloadPrefix) remain unchanged
- Add an
- Assistant
Good, I can see that
AtomicBooleanis already imported. Let me now look at the field declarations to see where to add the new field: - Assistant
Now I have a clear understanding. Let me implement the solution:
- Add an
AtomicBoolean automaticOffloadFollowUpfield to track if a follow-up automatic offload is needed - When
maybeOffloadcan't acquire the mutex, set the flag instead of scheduling a retry - When the current offload completes, check the flag and schedule a single follow-up
- Add an
- Assistant
Now I have a clear understanding of the issue and solution. Let me implement the changes:
Problem: When an automatic offload is running and new triggers arrive (e.g., from
maybeOffloadInBackground), each trigger schedules a newmaybeOffloadInBackgroundretry, creating a cascade of redundant retry work.Solution:
- Add an
AtomicBooleanflag to track if a follow-up automatic offload is needed - When the mutex can't be acquired, set the flag instead of scheduling a retry
- When the current automatic offload completes, check the flag and schedule exactly one follow-up
Let me first add the field:
- Add an
- Assistant
Now I'll add the
automaticOffloadFollowUpPendingfield after theoffloadMutex: - Assistant
Let me use the replace command instead:
- Assistant
Good. Now I need to modify the
maybeOffloadmethod to implement the coalescing logic. Let me replace the retry mechanism: - Assistant
Let me look at the exact content at that line:
- Assistant
Now let me use the exact text from the file:
- Assistant
Good, the changes look correct. Now let me verify the
asyncOffloadPrefixmethod doesn't need changes (it should retain its existing behavior for manual offloads):Good, the changes look correct. Now let me verify the
asyncOffloadPrefixmethod doesn't need changes (it should retain its existing behavior for manual offloads): - Assistant
Good, the
asyncOffloadPrefixmethod (manual offload) doesn't need changes - it already has the correct behavior of immediately returningOffloadInProgressExceptionwhen the mutex is locked.Now let me run the existing offload tests to make sure nothing is broken:
- Assistant
Let me find the build files:
- Assistant
It's a Gradle project. Let me check the build configuration.
- Assistant
Let me try to compile the managed-ledger module first:
- Assistant
Good, the compilation is successful. Now let me run the offload tests:
- Assistant
Let me look at the failing test:
- Assistant
There's a failing test
autoTriggerWhileManualInProgress. Let me look at this test to understand what it's testing and what my change broke. - Assistant
I see the issue. The test
autoTriggerWhileManualInProgressexpects that when a manual offload completes, any pending automatic offload triggers that arrived during the manual offload should be processed. My change only checks the follow-up flag inmaybeOffload's completion handler, but not inasyncOffloadPrefix's completion handler.I need to also check the flag in
asyncOffloadPrefix's completion handler. Let me update that method: - Assistant
I see - the test
autoTriggerWhileManualInProgressexpects that automatic offload triggers deferred during a manual offload will eventually run. The problem is that theasyncOffloadPrefixcompletion handler doesn't check the follow-up flag. I need to add the follow-up check there too. - Assistant
Let me navigate to the exact location first:
- Assistant
Now let me use the edit command to modify lines 3661-3674:
- Assistant
Let me use sed to do the replacement since the replace command is having issues with whitespace matching:
- Assistant
Let me verify the change was applied correctly:
- Assistant
Good, the change looks correct. Now let me run the tests again to see if the failing test passes:
- Assistant
Wrong project name. Let me fix it.
- Assistant
All tests pass now. Let me run all the offload tests to make sure nothing else is broken:
- Assistant
All offload tests pass. Let me now verify the complete diff of changes to make sure everything is correct:
- Assistant
The diff looks clean and correct. Let me now review the changes to ensure they're complete and correct. Let me also check for any other tests that might be affected:
- Assistant
All tests pass. Let me now go through the checklist to confirm everything is done correctly.
- Assistant
Now let me verify the final state of the changes by looking at the key parts of the diff:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.