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 find the relevant files related to managed-ledger offload functionality.
Assistant
Let me search for the offload-related methods in this file to understand the current implementation.
Assistant
Let me continue looking at the offload methods to understand the full implementation.
Assistant
Now let me look at the key methods in detail -
maybeOffloadand related methods:Assistant
Now let me look at where
maybeOffloadInBackgroundis called:Assistant
Let me see the context where
maybeOffloadInBackgroundis called on line 1971:Assistant
Let me understand the issue better. The problem is about coalescing automatic offload triggers while an automatic offload is running. Let me look at how the mutex works and what happens when triggers arrive:
Assistant
Let me see the
CallbackMutexclass to understand how it works:Assistant
Now let me look at the offload method to understand explicit/manual offloads:
Assistant
Now let me understand the issue better. Looking at the code:
- When
maybeOffloadInBackground(NULL_OFFLOAD_PROMISE)is called (line 1971) after a ledger is closed, it triggers automatic offload. - If an automatic offload is already running, line 2836-2838 schedules a retry after 100ms - this creates redundant retry work.
- The issue says: "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."
Let me check if there are any tests related to this:
- When
Assistant
Now let me look at the test file to understand how the offload functionality is tested:
Assistant
Let me search for tests that specifically test the coalescing behavior:
Assistant
Now let me understand the problem better. The current behavior is:
- When
maybeOffloadInBackgroundis called and offload mutex is locked, it schedules a retry after 100ms. - If multiple triggers arrive during an active offload, each one schedules its own retry, creating redundant work.
- The fix should coalesce these triggers so that:
- At most one active automatic run
- At most one follow-up run when additional triggers arrive during an active run
Let me look more closely at the current retry mechanism:
- When
Assistant
Now I understand the issue. When multiple automatic offload triggers arrive while an offload is already running, each one schedules a separate retry after 100ms, creating redundant work. The solution is to track whether a follow-up has already been scheduled and coalesce subsequent triggers.
Let me look at the full file structure to understand what fields already exist:
Assistant
Now let me check how the NULL_OFFLOAD_PROMISE is used vs when a real promise is passed (for manual/explicit offloads):
Assistant
Now let me understand the issue better by looking at the issue description again:
The key requirements are:
- Coalesce automatic offload triggers while an automatic offload is running
- At most one active automatic run - already achieved with
offloadMutex - At most one follow-up run when additional triggers arrive - THIS IS THE FIX NEEDED
- A follow-up must still process newly eligible ledgers - The retry should re-evaluate
- Explicit/manual offload futures must retain their existing behavior - Manual offloads should still fail with
OffloadInProgressException
Let me create a solution. I need to:
- Add a flag to track if a follow-up offload has been scheduled
- When
maybeOffloadInBackgroundis called and mutex is locked, check if follow-up is already scheduled - If not scheduled, schedule one and mark it as scheduled
- After the current offload completes, check if follow-up was requested and schedule it
Let me first look for any existing tests to understand how they work:
Assistant
Let me look at the
testAutoTriggerOffloadtest:Assistant
Now I have a good understanding of the current behavior. Let me implement the fix. The solution is to:
- Add a field to track if a follow-up automatic offload has been scheduled
- When
maybeOffloadInBackgroundis called and mutex is locked (meaning an offload is already running), check if:- This is an automatic trigger (promise == NULL_OFFLOAD_PROMISE)
- If a follow-up is already scheduled, skip scheduling another one
- Otherwise, schedule one follow-up and mark it as scheduled
- After the current automatic offload completes, if follow-up was requested, trigger another automatic offload
Let me implement this:
Assistant
Now let me create the fix. First, I'll add a field to track if a follow-up automatic offload is pending:
Assistant
}
Now let me look at the current retry logic more carefully to understand how to properly implement the coalescing:
Assistant
I'll use sed to make the changes instead:
Assistant
Good. Now let me modify the
maybeOffloadmethod to implement the coalescing logic:Assistant
Now I need to modify the
maybeOffloadmethod to implement coalescing. The key change is in theif (!offloadMutex.tryLock())block. Instead of always scheduling a retry, we should:- Check if this is an automatic offload (i.e.,
finalPromise == NULL_OFFLOAD_PROMISE) - If it's automatic and a follow-up is already scheduled, do nothing
- If it's automatic and no follow-up is scheduled, mark it as scheduled and schedule the retry
- If it's manual/explicit (not automatic), keep the existing behavior (schedule retry with the promise)
Let me update the code:
- Check if this is an automatic offload (i.e.,
Assistant
Let me check if the edit worked properly:
Assistant
It looks like the sed command duplicated some lines. Let me revert and try a different approach:
Assistant
Let me use a proper editor approach:
Assistant
Let me create a script to make the changes properly:
Assistant
Good! The field is added. Now I need to modify the
maybeOffloadmethod. Let me find the exact line numbers:Assistant
Now let me create a script to replace those lines:
Assistant
Good! Now I need to add the logic to reset
isOffloadFollowUpScheduledafter the offload completes and trigger a follow-up if needed. Let me look at the unlockingPromise.whenComplete block:Assistant
Now I need to modify the
whenCompletecallback to:- Reset
isOffloadFollowUpScheduledto false - Check if a follow-up was requested and if so, schedule it (but only for automatic offloads)
- Reset
Assistant
I see there's a duplicate line
CompletableFuture<Position> unlockingPromise = new CompletableFuture<>();. Let me fix that:Assistant
Wait, the duplicate is still there. Let me check:
Assistant
Good! Now let me verify the changes look correct:
Assistant
I notice there's a formatting issue - the
if (!offloadMutex.tryLock())block is missing an empty line beforeCompletableFuture<Position> unlockingPromise. Let me also check if there are any compilation issues by trying to build the project:Assistant
...
Assistant
This is a Gradle project, not Maven. Let me try to compile with Gradle:
Assistant
I see the issue - my awk script also modified another location where
unlockingPromiseis used (in a different method). The second location usesCompletableFuture<Void>instead ofCompletableFuture<Position>. Let me check what happened:Assistant
I see the problem. The awk script modified both occurrences. I need to revert the file and apply the changes more carefully.
Assistant
Let me use a more precise approach with sed:
Assistant
Good. Now let me find the exact lines for the maybeOffload method and use line-specific sed commands:
Assistant
Let me create a precise sed script:
Assistant
Good! Now let me find the whenComplete block and replace it:
Assistant
...
Assistant
I need to add an empty line after the closing
});. Let me fix the formatting:Assistant
Let me add the empty line properly:
Assistant
Now let's compile to check if the code is correct:
Assistant
The code compiles successfully. Now let me run the existing tests to make sure they still pass:
Assistant
The tests passed. Let me also run the OffloadLedgerDeleteTest to make sure those tests pass as well:
Assistant
Now let me view the complete changes I made to the file:
Assistant
Let me verify that the changes are correct by reviewing the logic more carefully. Let me also run more tests to make sure everything is working:
Assistant
All offload tests pass. Now let me create a simple test script to verify the coalescing behavior works correctly:
Assistant
Now let me verify the changes again by looking at the full context and make sure the logic is correct:
Assistant
The logic looks correct. Let me trace through the behavior:
Assistant
Now I need to update the whenComplete block to handle the follow-up:
Assistant
Let me add a blank line after the
});:Assistant
...
Assistant
Good. Now let me compile and run tests:
Assistant
Now let me run the tests:
Assistant
There's a test failure. Let me look at the failing test:
Assistant
Let me see more of the test:
Assistant
Let me understand the test scenario. In
autoTriggerWhileManualInProgress:- A manual offload is started (via
asyncOffloadPrefix) - While the manual offload is running (blocked on
slowOffload), automatic triggers fire (from ledger rolls) - The test expects that after the manual offload completes, automatic offloads should still run
The issue is that my implementation only schedules follow-ups for automatic offloads (when
finalPromise == NULL_OFFLOAD_PROMISE). But in this case, the mutex is held by a manual offload, and automatic triggers setisOffloadFollowUpScheduled = true. When the manual offload completes, thewhenCompletecallback checksfinalPromise == NULL_OFFLOAD_PROMISE, but since it's a manual offload,finalPromiseis notNULL_OFFLOAD_PROMISE, so it doesn't trigger the follow-up.I need to fix this. The follow-up should be scheduled if
hadFollowUpScheduledis true, regardless of what type of offload just completed. The flag is specifically for tracking automatic offload follow-ups.- A manual offload is started (via
Assistant
Now let me run the tests again:
Assistant
The test is still failing. Let me look at the test more closely to understand what's happening:
Assistant
Let me check the test report for more details:
Assistant
Let me look at the test output more carefully to understand what's happening.
Assistant
Let me think about the issue more carefully. The test is
autoTriggerWhileManualInProgress[4](100, null), which meanssizeThreshold=100andtimeThreshold=null.Looking at the test:
- Manual offload is started via
asyncOffloadPrefix - During manual offload, automatic triggers fire (from adding more entries)
- After manual offload completes, automatic offloads should still run
The problem is that the original code scheduled retries for cases when
tryLock()fails. My implementation changed this behavior for automatic offloads - instead of scheduling a retry, it just sets the flag and returns.However, the issue is that when a manual offload is running and automatic triggers come in:
- The automatic triggers can't acquire the mutex
- Manual offload is started via
Assistant
I see the issue now. The
asyncOffloadPrefixmethod has its ownpromise.whenCompletecallback that only unlocks the mutex but doesn't handle theisOffloadFollowUpScheduledflag. I need to update that callback as well.Assistant
I need to update the
asyncOffloadPrefixmethod'swhenCompletecallback to also handle the follow-up scheduling. Let me update lines 3671-3678:Assistant
Now let me compile and run the tests again:
Assistant
All offload tests are passing now. Let me verify the changes by looking at the full diff and make sure everything is correct.
Assistant
The changes look good. Let me run a broader set of tests to make sure nothing is broken:
Assistant
All tests pass. Let me review the changes I made and make sure they align with the issue description:
Assistant
Now let me verify my solution is complete by reviewing the checklist:
Checklist Review
-
If an error was reported in the issue description, I have successfully reproduced it.
- The issue description didn't report a specific error, but described a behavioral problem: "Automatic managed-ledger offload triggers create redundant retry work". My fix addresses this by coalescing automatic offload triggers.
-
If an error was the issue description, I have confirmed that my fix resolves the error.
- Since there was no specific error, I confirmed the fix by:
- Code compiles successfully
- All existing offload-related tests pass
- All managed-ledger tests pass
- Since there was no specific error, I confirmed the fix by:
-
I have thought about edge cases and made sure my fix handles them as well.
- Edge cases handled:
- Multiple automatic triggers during one automatic offload → coalesced into one follow-up
- Edge cases handled:
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.