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 for the managed-ledger offload functionality. Let me first get an overview.
- Assistant
Let me search for files related to automatic offloading in the managed ledger.
- Assistant
Let me search for "automatic offload" or "managed ledger offload" in the codebase.
- Assistant
Let me look for the main classes related to managed ledger offloading. The key class would likely be PersistentTopic.java or something in the managed-ledger module.
- Assistant
Let me understand the offload mechanism in the managed ledger. Let me look at the PersistentTopic.java file, and also search for ManagedLedger or ManagedLedgerImpl to understand how offloading is triggered.
- Assistant
Let me search for the managed ledger offload implementation, particularly automatic offload triggers.
- Assistant
Let me look at the ManagedLedgerImpl.java file, focusing on offload-related code.
- Assistant
Let me look at the offload-related code in ManagedLedgerImpl.java more closely. I need to understand how automatic offloading is triggered.
- Assistant
Let me look at the
maybeOffloadmethod and theoffloadMutexto understand the current mechanism for automatic offloading. - Assistant
Now I see the pattern. When
offloadMutex.tryLock()fails, it schedules a retry after 100ms. This could create redundant work if many triggers arrive while an offload is already running. Let me trace who callsmaybeOffloadInBackground: - Assistant
Let me look at the automatic trigger call sites:
- Assistant
Let me look at where
maybeOffloadInBackgroundis called - that's the entry point for automatic offloading. I see two places:ManagedLedgerImpl.java:1971- after creating a new ledgerManagedLedgerFactoryImpl.java:499- probably when creating a new managed ledger
Let me look more at the code to understand the issue. The issue says: "Automatic managed-ledger offload triggers create redundant retry work. Coalesce automatic offload triggers while an automatic offload is running."
Looking at the current code in
maybeOffload:if (!offloadMutex.tryLock()) { scheduledExecutor.schedule(() -> maybeOffloadInBackground(finalPromise), 100, TimeUnit.MILLISECONDS); return; }When the mutex is already held (an offload is running), it schedules another attempt in 100ms. The problem is that if multiple automatic triggers come in while an offload is running, each one schedules another retry, creating a chain of redundant retries.
The solution should be:
- When an automatic offload trigger arrives and one is already running, mark that a follow-up is needed
- When the running offload completes, if a follow-up is needed, start exactly one more offload
- Assistant
Let me look at the
offloadPrefixmethod (manual/explicit offload) to understand its behavior: - Assistant
Now I understand the issue clearly. Let me look at the
offloadMutexfield and the broader context: - Assistant
Now I have a good understanding of the issue. Let me look at the
maybeOffloadInBackgroundmethod more carefully to understand the current behavior and the problem: - Assistant
Now I understand the issue. The problem is in
maybeOffload(line 2836-2838). When theoffloadMutexis already held (meaning an automatic offload is in progress), the code schedules another call tomaybeOffloadInBackgroundafter 100ms. This creates redundant retry work because:- Multiple automatic offload triggers can arrive while one offload is running
- Each trigger schedules its own retry 100ms later
- Those retries can also fail to acquire the lock and schedule more retries
- This creates an avalanche of redundant retries
The fix should:
- When an automatic offload trigger arrives while an automatic offload is running, simply mark that a follow-up is needed (at most one)
- When the running offload completes, check if a follow-up was requested and run exactly one more automatic offload
- Manual/explicit offload futures must retain their existing behavior
Let me first look at the test file to understand expected behavior:
- Assistant
Let me look for tests related to automatic offloading:
- Assistant
Let me look at the
OffloadLedgerDeleteTestto see howmaybeOffloadInBackgroundis tested: - Assistant
Now I have a clear understanding of the issue. Let me look at the full context around the
maybeOffloadmethod to understand the flow: - Assistant
Good,
AtomicBooleanis already imported. Now let me implement the fix. First, let me read the exact area where I need to add the field: - Assistant
Now let me look at the complete
maybeOffloadmethod and theasyncOffloadPrefixmethod to understand exactly how the offloadMutex is used: - Assistant
Let me also look at the
asyncOffloadPrefixmethod to understand the manual offload flow: - Assistant
Now I have a complete understanding. Let me also look at the
offloadLoopmethod to understand the complete offload flow: - Assistant
Now I have a thorough understanding of the problem. Let me look at the existing tests related to offloading to understand what tests might be affected:
- Assistant
Now I have a comprehensive understanding of the issue. Let me implement the fix:
Problem: In
maybeOffload, when theoffloadMutexis already held (automatic offload running), each new trigger schedules a new retry viascheduledExecutor.schedule(() -> maybeOffloadInBackground(finalPromise), 100, TimeUnit.MILLISECONDS). This creates a cascade of redundant retry work.Solution:
- Add an
AtomicBooleanflag to track if a follow-up automatic offload is needed - When
maybeOffloadfails to acquire the lock, set the flag instead of scheduling a retry - When the current offload completes, check the flag and schedule exactly one follow-up
- Add an
- Assistant
Now let me implement the changes:
- Add an
AtomicBooleanfield for tracking follow-up offload requests - Modify
maybeOffloadto set the flag instead of scheduling a retry when the mutex is held - Modify the
unlockingPromise.whenCompletecallback to check the flag and schedule a follow-up if needed
Let me open the file and make the changes.
- Add an
- Assistant
Now let me add the
AtomicBooleanfield after theoffloadMutex: - Assistant
Good. Now let me modify the
maybeOffloadmethod. First, let me see the exact current content: - Assistant
Now let me make the changes to the
maybeOffloadmethod: - Assistant
Now let me verify the changes look correct and check if there are any other places that need to be updated:
- Assistant
Good. The changes look correct. Now let me run the existing tests to verify nothing is broken:
- Assistant
Let me find the correct module name for the managed-ledger.
- Assistant
Let me find the pom.xml
- Assistant
Let me try another way.
- Assistant
It's a Gradle project. Let me check if there's a root build file.
- Assistant
Let me check if there's a gradle wrapper.
- Assistant
Let me try compiling with gradle.
- Assistant
Good, compilation succeeds. Now let me run the offload-related tests to ensure nothing is broken:
- Assistant
There's a failing test:
autoTriggerWhileManualInProgress. Let me look at this test to understand what it expects: - Assistant
Let me look at the full test from the beginning:
- Assistant
Let me look at the full test method.
- Assistant
I see! The test is failing because the
automaticOffloadFollowupPendingflag is only checked inmaybeOffload's own callback, but when a manual offload completes (viaasyncOffloadPrefix), it doesn't check this flag. Let me also look at theasyncOffloadPrefixcompletion handler: - Assistant
I need to add the follow-up check in the
asyncOffloadPrefix's completion handler as well, so that when a manual offload completes, it can trigger the pending automatic offload follow-up. - Assistant
I see the edit got messed up - there's a duplicate line and the else clause is broken. Let me fix this:
- Assistant
I see the edit introduced a duplicate line and broke the else block. Let me fix this:
- Assistant
Good, I reverted to the original. Let me redo both changes carefully:
- Assistant
Good, the file is back to original. Let me apply all three changes carefully. First, add the field:
- Assistant
Good. Now let me make the second change: modify the
maybeOffloadmethod to use the flag instead of scheduling retries: - Assistant
Now I need to replace lines 2839-2853 with the new logic:
- Assistant
Good, the first change is correct. Now let me also update the
asyncOffloadPrefixcallback to check for the follow-up flag when the manual offload completes: - Assistant
Now I need to update the
asyncOffloadPrefixcallback to also check for the follow-up flag: - Assistant
Good, both changes are applied correctly. Now let me compile and run the tests again:
- Assistant
Good, compilation succeeds. Now let me run the failing test:
- Assistant
The test passes. Now let me run all offload-related tests:
- Assistant
All OffloadPrefixTest tests pass. Let me also run the other offload-related tests:
- Assistant
Let me run the remaining offload tests:
- Assistant
All offload tests pass. Let me also run the ManagedLedgerTest to make sure nothing else is broken:
- Assistant
All tests pass. Let me verify the complete diff to make sure my changes are correct:
- Assistant
The diff looks clean and correct. Let me run the full managed-ledger test suite to ensure everything passes:
- Assistant
All managed-ledger tests pass. Let me now verify the final diff one more time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.