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. This appears to be related to Apache Pulsar, based on the issue description mentioning "managed-ledger offload triggers".
Assistant
Let me search for files related to managed ledger and offload triggers.
<tool_call> <function=bash_terminal> <parameter=command> grep -rl "offload" /pulsar --include="*.java" | grep -i "managed|ledger" | head -20 </tool_call>
Assistant
Let me fix the command format to properly search for files related to managed ledger and offload.
Assistant
Let me search more specifically for files related to automatic offload triggers.
Assistant
Let me search for more specific terms related to the issue - automatic offload, coalesce, retry work, etc.
Assistant
Let me search more specifically for managed ledger related code that handles offload triggers.
Assistant
Let me search for files in the impl directory of managed-ledger which is likely where the offload trigger logic would be.
Assistant
Let me search for "trigger" and "offload" in the managed ledger implementation files.
Assistant
Let me look at the offload-related code in ManagedLedgerImpl.java more closely, especially the maybeOffload and offloadLoop methods.
Assistant
Let me search for "RetryWork" or similar patterns that might relate to the "retry work" mentioned in the issue.
Assistant
Let me search more specifically for the offload trigger logic. The issue mentions "automatic managed-ledger offload triggers" and "retry work". Let me look at the LedgerOffloader interface and its implementations.
Assistant
Let me examine the
maybeOffloadand related methods inManagedLedgerImpl.javamore closely.Assistant
Now I can see the issue. When
offloadMutex.tryLock()fails, it schedules a retry after 100ms. Let me see the full context of the offload loop and how triggers are handled.Assistant
Let me look at the
offloadLoopmethod and understand the full flow:Assistant
Let me look at the
triggerOffloadmethod in PersistentTopic to understand the difference between automatic and manual triggers:Assistant
Now I understand the issue better. Let me look at the test files and understand the current behavior:
Assistant
Let me look at how the automatic offload triggers work and where they're called from:
Assistant
Now I see the flow.
maybeOffloadInBackgroundis called fromaddEntryCompleteafter creating a new ledger. WhenoffloadMutex.tryLock()fails, it schedules a retry after 100ms. The issue is that:- Multiple automatic offload triggers can be scheduled simultaneously (each adding to the retry queue).
- When an automatic offload is running, the mutex will fail, and it will keep scheduling retries.
- The problem is these redundant retry works - when an automatic offload is already running, multiple triggers keep scheduling more retry work.
The fix should coalesce automatic offload triggers while an automatic offload is running. There should be at most:
- One active automatic run
- At most one follow-up run when additional triggers arrive
Let me look more at the issue. The key is in
maybeOffloadwhere the mutex is not acquired:if (!offloadMutex.tryLock()) { scheduledExecutor.schedule(() -> maybeOffloadInBackground(finalPromise), 100, TimeUnit.MILLISECONDS); return; }Assistant
Assistant
I need to continue exploring the code to understand the full context of the
maybeOffloadmethod before implementing the fix.Assistant
Now let me check the imports at the top of the file:
Assistant
Good,
AtomicBooleanis already imported. Now let me look at the area around line 232 whereoffloadMutexis defined:Assistant
Now I have a complete understanding of the code. Let me implement the fix. The issue is that when
offloadMutex.tryLock()fails inmaybeOffload, it schedules a new retry task every time without checking if one is already pending. This creates redundant retry work.The fix:
- Add an
AtomicBooleanflag to track if there's already a pending automatic offload trigger - When
offloadMutex.tryLock()fails, only schedule a retry if no retry is already pending - After the current offload completes, check the flag and start another offload if needed
- Add an
Assistant
Now let me add the
AtomicBooleanfield to track pending automatic offload triggers. I'll add it right afteroffloadMutex:Assistant
Now I need to add the AtomicBoolean field after line 233. Let me add it between the offloadMutex and NULL_OFFLOAD_PROMISE declarations.
Assistant
The edit didn't format correctly. Let me fix the formatting:
Assistant
Let me fix the formatting issue:
Assistant
Let me check what we have now:
Assistant
Let me fix this properly by replacing the problematic line:
Assistant
Let me try a different approach - I'll use the edit tool properly this time:
Assistant
It seems the sed command partially worked but left some issues. Let me check the full state of the file around lines 234-236.
Assistant
Good, the field was added correctly. Now I need to modify the
maybeOffloadmethod. Let me look at it again:Assistant
Now I need to modify two parts of the
maybeOffloadmethod:- The
if (!offloadMutex.tryLock())block - to coalesce retries - The
unlockingPromise.whenCompletecallback - to check for pending triggers after the offload completes
Let me first edit the retry scheduling part:
- The
Assistant
I see there's an extra
}at line 2850. Let me fix that and also update the unlock callback to check for pending triggers after the offload completes:Assistant
Now I need to modify the
unlockingPromisecallback to check for pending triggers after the offload completes. Let me first add the final variables needed for the recursive call and update the callback:Assistant
The changes look correct. Let me verify the code compiles and then check if there are any existing tests related to this functionality that I should run.
Assistant
I need to find the correct module name for the managed-ledger. Let me check the project structure.
Assistant
Let me find the correct project structure
Assistant
This project uses Gradle, not Maven. Let me compile with Gradle:
Assistant
The compilation succeeded. Now let me run the relevant tests to ensure the changes don't break existing functionality:
Assistant
Good, the tests ran without any failures (only warnings about deprecated JVM methods). Let me verify this more explicitly:
Assistant
Let me review the changes I've made to ensure they are correct:
Assistant
The diff looks correct. Let me verify the code compiles again and run a broader set of tests:
Assistant
Let me check what's happening - the compilation might have failed.
Assistant
Good, the compilation is successful. The "ERROR" in the previous output was from the grep not finding matches (exit code 1). Let me now run more comprehensive tests to make sure the changes don't break anything:
Assistant
Let me run more offload-related tests to make sure everything works:
Assistant
The fix looks good. Now let me think about a potential edge case. When the scheduled retry runs after the follow-up has started (from the unlock callback), it might create additional retry work. Let me verify this isn't an issue by examining the flow more carefully.
The issue is: when the scheduled retry (created because an offload was running) fires, the follow-up has already been triggered by the unlock callback. The scheduled retry then calls
maybeOffloadInBackground, which schedulesmaybeOffloadon the executor. ThismaybeOffloadwill fail to acquire the mutex (if the follow-up has it), and it will set the flag to true and schedule another retry. This creates redundant work.To properly prevent this, I should cancel the scheduled retry when the follow-up starts. Let me update the implementation:
Assistant
Looking at the code more carefully, I see a potential edge case. When the scheduled retry fires after the follow-up has already started (from the unlock callback), it might schedule another retry. Let me fix this by properly canceling the pending scheduled task when the follow-up starts.
Let me revise the implementation:
Assistant
Let me trace through the code more carefully to identify the edge case I mentioned. When the scheduled retry fires after the follow-up has already started, it might schedule another retry. I need to cancel the pending scheduled task when the follow-up starts.
Let me update the implementation to properly cancel pending scheduled retries:
Assistant
Good, the new field was added. Now I need to update the retry scheduling logic to store the ScheduledFuture and cancel it when the follow-up starts. Let me update the retry scheduling:
Assistant
Now I need to update the retry scheduling to store the ScheduledFuture and cancel it when the follow-up starts:
Assistant
I see there's an extra
}at line 2856. Let me fix that and also update the unlock callback to cancel the pending scheduled retry:Assistant
Now I need to update the unlock callback to also cancel the pending scheduled retry task when a follow-up starts:
Assistant
Good, now let me verify the code compiles correctly:
Assistant
Compilation successful. Let me verify the full diff of my changes:
Assistant
The diff looks good. Now let me run the existing tests to verify the changes don't break anything:
Assistant
Let me run a broader set of tests to make sure everything is working:
Assistant
All tests pass. Let me verify the solution once more by reviewing the complete diff and then check for potential edge cases:
Assistant
Let me review the complete solution and verify the final state:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.