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. Let's first get an overview.
- Assistant
Let me explore the repository structure more broadly and look for managed-ledger offload-related code.
- Assistant
Let me search for automatic offload triggers in the codebase.
- Assistant
Let me look for files related to offloading in the managed-ledger module, which is likely where the automatic offload triggers happen.
- Assistant
The issue is about automatic managed-ledger offload triggers creating redundant retry work. I need to find where automatic offloading is triggered and how we can coalesce those triggers.
Let me look at the
ManagedLedgerImpl.javafile first, as that's likely where the offloading logic is. - Assistant
Let me look at the offloading logic in more detail. The key seems to be in the
maybeOffloadmethod and how it handles automatic offloading. - Assistant
Let me continue reading the maybeOffload method and also look at the maybeOffloadInBackground method.
- Assistant
Now I can see the issue! When
offloadMutex.tryLock()fails (line 2836), it schedules a retry viamaybeOffloadInBackgroundafter 100ms. If multiple triggers come in while an offload is in progress, each one creates a separate retry, leading to redundant work.Let me find the
maybeOffloadInBackgroundmethod. - Assistant
Let me look at the maybeOffloadInBackground method:
- Assistant
Now I can see the core issue clearly. When
offloadMutex.tryLock()fails (line 2836-2839), it schedules a retry withmaybeOffloadInBackground(finalPromise)with 100ms delay. If multiple automatic triggers arrive while an offload is in progress, each one creates a separate delayed retry. This creates redundant work because:- Each trigger creates its own scheduled retry
- Each retry will independently try to acquire the mutex
- Once the original offload finishes, all the queued retries will run one after another
The fix should coalesce these automatic triggers so that there's at most one active automatic run and at most one follow-up run.
Let me also look at the
offloadPrefixmethod and its callers to understand the difference between automatic and manual offloads. - Assistant
Let me look at the
offloadPrefixmethod and how manual offloads work: - Assistant
Now I understand the problem. Let me look at where
maybeOffloadInBackgroundis called and examine the full flow: - Assistant
Let me look at the full context around the offload mutex and related fields:
- Assistant
Let me see where
maybeOffloadInBackgroundis called to understand the automatic triggers: - Assistant
Let me check around line 1971 to see what calls maybeOffloadInBackground.
- Assistant
Let me look at the existing tests for offloading:
- Assistant
Let me look at the OffloadPrefixTest to understand the existing test structure:
- Assistant
Let me look for tests related to automatic offloading:
- Assistant
Let me look at the test that calls maybeOffloadInBackground:
- Assistant
Let me look at where the offload mutex is used and the full picture of how automatic offloading works:
- Assistant
Now let me look at how the offload is triggered (where
maybeOffloadInBackgroundis called): - Assistant
Let me see how the offload is triggered and the full flow:
- Assistant
Let me understand the full picture. I need to look at the
asyncOffloadPrefixmethod: - Assistant
Now I have a good understanding of the issue. Let me see the full
maybeOffloadandmaybeOffloadInBackgroundmethods side by side, as well as how the manual offload path (asyncOffloadPrefix) works separately: - Assistant
Now I have a clear understanding of the issue and the fix. Let me trace through the full picture:
- Automatic offload triggers call
maybeOffloadInBackground(NULL_OFFLOAD_PROMISE)(lines 1971 and inManagedLedgerFactoryImpl) - This calls
maybeOffload(), which tries to acquireoffloadMutex - If the mutex is held (another offload in progress), it schedules a retry after 100ms:
scheduledExecutor.schedule(() -> maybeOffloadInBackground(finalPromise), 100, TimeUnit.MILLISECONDS) - Problem: Each new trigger while an offload is running creates a separate retry, leading to a chain of redundant retries
Fix: Add a
pendingAutomaticOffloadRetryflag. When the mutex is held and the flag is already true, skip scheduling another retry. When the retry fires, clear the flag before attempting again.Let me now look at the test infrastructure to understand how to verify the changes:
- Automatic offload triggers call
- Assistant
Now I have a clear understanding of the issue. Let me look at the
ManagedLedgerFactoryImplcall to understand the full picture: - Assistant
Now I have a clear understanding of the issue. Let me analyze the problem:
- Automatic offload triggers happen via
maybeOffloadInBackground(NULL_OFFLOAD_PROMISE)(lines 1971, 499 in ManagedLedgerFactoryImpl) - In
maybeOffload(), when the mutex is held, each trigger schedules a new retry after 100ms - If N triggers arrive during an offload, N retries are scheduled, creating redundant work
The fix: Track whether a pending automatic offload retry exists, and coalesce additional automatic triggers. Manual/explicit offloads (via
asyncOffloadPrefix) use a separate path and are unaffected.Let me now implement the fix:
- Automatic offload triggers happen via
- Assistant
Now let me implement the fix. I need to:
- Add a
volatile boolean pendingAutomaticOffloadRetryfield nearoffloadMutex - Modify the
maybeOffloadmethod to coalesce automatic triggers when the mutex is held
Let me first open the file to see the exact line numbers:
- Add a
- Assistant
Now I have a clear understanding of the issue. Let me implement the fix. I need to:
- Add a
volatile boolean pendingAutomaticOffloadRetryfield nearoffloadMutex - Modify the
maybeOffloadmethod to coalesce automatic triggers when the mutex is held
Let me first add the new field:
- Add a
- Assistant
Now let me add the
pendingAutomaticOffloadRetryfield afteroffloadMutex: - Assistant
Good. Now let me modify the
maybeOffloadmethod to coalesce automatic triggers: - Assistant
Now let me modify the
maybeOffloadmethod to coalesce automatic triggers: - Assistant
Good. Now let me verify the changes look correct by checking the diff:
- Assistant
The diff looks correct. Now let me run the existing tests to make sure nothing is broken. First, let me find and run the relevant tests:
- Assistant
Let me check the build system and run the relevant tests:
- Assistant
Let me check what build system is being used.
- Assistant
Let me try differently.
- Assistant
It's a Gradle project. Let me run the offload tests:
- Assistant
The
spotbugsMaintask wasn't found. Let me try without that option. - Assistant
The tests pass. Let me also run the OffloadLedgerDeleteTest:
- Assistant
All tests pass. Let me run a broader set of managed-ledger tests to make sure nothing is broken:
- Assistant
All managed-ledger tests pass. Let me also look at the
ShadowManagedLedgerImplto make sure it doesn't override any relevant methods: - Assistant
Good, the ShadowManagedLedgerImpl doesn't reference these methods. Let me also verify the
asyncOffloadPrefix(manual offload) path is completely separate and unaffected: - Assistant
Good. The manual offload path (line 3660) uses
offloadMutex.tryLock()directly and is completely separate frommaybeOffload. My changes only affect the automatic offload path inmaybeOffload.Let me verify the logic is correct by looking at the full flow once more:
- Assistant
The logic looks correct. Let me now write a simple test to verify the coalescing behavior works as expected:
- Assistant
Wait, I should NOT add new tests. Let me delete that temporary file and just make sure the existing tests pass:
- Assistant
Let me look at the existing tests more carefully and run them all:
- Assistant
Let me run a broader set of tests to confirm nothing is broken:
- Assistant
All managed-ledger tests pass. Let me now review the complete diff one more time to verify correctness:
- Assistant
Now let me verify the other call site in
ManagedLedgerFactoryImplto make sure it's also covered: - Assistant
Good. Both automatic trigger call sites pass
NULL_OFFLOAD_PROMISE, so the coalescing logic correctly identifies them. The test inOffloadLedgerDeleteTestpasses a customCompletableFuture(notNULL_OFFLOAD_PROMISE), so it retains the existing retry behavior.Let me verify there are no other callers of
maybeOffloadInBackgroundin the codebase: - Assistant
Let me also verify the
asyncOffloadPrefixpath is completely unaffected: - Assistant
Good.
asyncOffloadPrefixis at line 3590, and it directly callsoffloadLoopwithout going throughmaybeOffload. Manual offloads are completely unaffected.Let me now do a final comprehensive review of the changes and run the tests one more time to confirm:
- Assistant
Let me verify the complete final state of the changes:
- Assistant
Let me run the full managed-ledger test suite one more time to make sure everything is solid:
- Assistant
The tests pass (cached result since we already ran them). Let me force a re-run to make sure.
- Assistant
All tests pass. Let me now verify the full correctness of the solution by reviewing the complete flow once more:
- Assistant
Everything looks correct. Let me now verify the complete solution with a final comprehensive check:
- Assistant
Let me compile and run all tests one more time to make sure everything is clean.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.