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 start by exploring the repository structure to understand what we're working with.
Assistant
This is the Apache Pulsar repository. The issue is about managed-ledger offload and automatic triggers. Let me explore the managed-ledger directory.
Assistant
Let me look at the key section in
ManagedLedgerImpl.javathat handles themaybeOffloadandmaybeOffloadInBackground:Assistant
Now I understand the code. Let me also look at the call site at line 1971:
Assistant
Let me look at where the offload mutex and the call to
maybeOffloadInBackgroundis used elsewhere:Assistant
Let me look at the
CallbackMutexclass:Assistant
Let me look at the full context of
maybeOffloadand understand the flow more:Assistant
Let me look at the existing tests for the offload functionality:
Assistant
Let me look at the dataProvider for
manualTriggerWhileAutoInProgresstest:Assistant
Let me look at the full test for
autoTriggerWhileManualInProgress:Assistant
Let me look at the
MockLedgerOffloaderclass:Assistant
Now I have a good understanding of the issue. Let me trace through the issue:
- When a new ledger is closed,
maybeOffloadInBackground(NULL_OFFLOAD_PROMISE)is called. - The current code at line 2836-2839 schedules a retry via
scheduledExecutor.schedule()if the mutex is held:if (!offloadMutex.tryLock()) { scheduledExecutor.schedule(() -> maybeOffloadInBackground(finalPromise), 100, TimeUnit.MILLISECONDS); return; } - Problem: When multiple new ledgers close while offload is running, each call schedules another retry, creating redundant work.
Let me verify this by writing a reproducer to demonstrate the issue:
- When a new ledger is closed,
Assistant
Let me look at the imports in
ManagedLedgerImpl.java:Assistant
Let me look at the
offloadPrefixmethod which is the explicit path:Assistant
Let me look at the existing tests and run them first to make sure I understand the expected behavior:
Assistant
Now let me trace through the existing test logic to understand what the issue is. Let me look at the
multipleAutoTriggerstest:Assistant
Now I have a clear understanding of the issue. Let me check how
LedgerOffloaderis used in the test and understand the flow:Assistant
Now I have a clear understanding. Let me implement the fix. The plan is:
- Add a
volatile boolean followupOffloadScheduledfield toManagedLedgerImpl. - In
maybeOffload, when the mutex cannot be acquired:- If the flag is false, set it to true and schedule a follow-up.
- If the flag is true, do nothing (a follow-up is already scheduled).
- When the follow-up fires, reset the flag and call
maybeOffloadInBackground(NULL_OFFLOAD_PROMISE).
This way:
- At most one active automatic run (already guaranteed by the mutex).
- At most one follow-up run when additional triggers arrive.
- A follow-up processes newly eligible ledgers.
- Explicit/manual offload futures retain their existing behavior (the explicit path at line 3644 is not modified).
Let me make the changes:
- Add a
Assistant
Now let me implement the fix. First, I'll add the field for tracking the follow-up state:
Assistant
Now let me add the field for tracking the follow-up state:
Assistant
I see there's a duplicate. Let me check and fix it:
Assistant
I see the duplicate. Let me fix it:
Assistant
Now let me update the
maybeOffloadmethod to use the flag:Assistant
Now let me update the
maybeOffloadmethod to use the flag:Assistant
Now let me verify the changes look correct:
Assistant
Let me look at the full updated method:
Assistant
Let me also check the field declaration:
Assistant
Assistant
Now let me think more carefully about the fix. The current implementation could have an issue where the follow-up's
maybeOffloadcould also schedule another follow-up if the lock is still held. Let me think about this:Looking at the flow:
- Original
maybeOffloadis called on the executor thread, acquires the lock, starts the offload, returns. - Additional
maybeOffloadis called on the executor thread, finds lock held, schedules a follow-up with 100ms delay, returns. - After 100ms, the follow-up fires, calls
maybeOffloadInBackground, which callsexecutor.execute(() -> maybeOffload(...)).
- Original
Assistant
Let me look at the rest of the
offloadLoopmethod to understand the threading:Assistant
Now let me look at the build configuration to understand how to run the tests:
Assistant
Let me first compile to verify the code is correct:
Assistant
The compilation succeeded. Now let me run the offload-related tests to verify my fix:
Assistant
tests pass. Let me also run the related OffloadPrefixReadTest and OffloadEvictUnusedLedgersTest:
Assistant
All tests pass. Let me also write a temporary reproducer to verify the fix addresses the redundant retry work issue:
Assistant
It seems the OffloadPrefixTest results weren't generated. Let me re-run them:
Assistant
All the relevant tests pass. Let me now run the full offload test suite to make sure nothing is broken:
Assistant
Let me force a re-run to verify the tests pass:
Assistant
Let me check the test results:
Assistant
All 44 tests pass. Now let me also write a temporary reproducer to verify the fix addresses the issue:
Assistant
All tests pass. Now let me also run the broader managed-ledger tests to make sure my fix doesn't break anything else:
Assistant
All 136 ManagedLedgerTest tests pass. Let me also check the diff of my changes:
Assistant
Let me look at the diff to confirm everything is in order:
Assistant
Assistant
All tests pass. Let me also check the ShadowManagedLedgerImpl to make sure my fix doesn't break anything:
Assistant
Let me also look at the code that we changed once more to verify everything is in order:
Assistant
Let me also verify the field declaration:
Assistant
Let me also create a temporary script to verify the fix works as expected. I'll create a simple test that demonstrates the redundant work issue and confirms my fix:
Assistant
Let me check the manual offload path:
Assistant
The manual offload path is preserved. Let me verify the explicit path is intact:
Assistant
The fix is complete. Let me also verify the fix by running a broader test:
Assistant
Let me check the test results:
Assistant
All 58 offload tests pass. Let me also check the final diff one more time:
Assistant
Let me also verify by running a quick set of tests in the pulsar-broker module to ensure no broker code is broken:
Assistant
The
maybeOffloadInBackgroundandmaybeOffloadmethods are only used inManagedLedgerImpl.java. My fix is isolated.Let me also check the other places where offload is used:
Assistant
Let me do a final verification by looking at the code one more time:
Assistant
Let me also write a simple verification script to confirm the fix works:
Assistant
Let me clean up the temporary files:
Assistant
Let me also check that my fix doesn't impact the case when the ledger is closed or fenced. Let me look at the offloadLoop:
Assistant
Let me also run the offload tests one more time to confirm:
Assistant
Let me check the final diff:
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.