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 me first get an overview.
- Assistant
Let me search for managed-ledger offload related files.
- Assistant
Let me look for managed ledger related files and specifically offloading scheduling/triggering.
- Assistant
The issue is about automatic managed-ledger offload triggers creating redundant retry work. This is likely in the managed-ledger or broker code. Let me search for more specific terms like "offload trigger", "automatic offload", etc.
- Assistant
Let me focus on
ManagedLedgerImpl.javasince that's likely where automatic offload triggers happen. - Assistant
Let me look at the
maybeOffloadmethod in detail. - Assistant
Now let me look at the maybeOffload method more closely:
- Assistant
Now I understand the problem. When the
offloadMutexis already locked (another offload is running), the code schedules a retry after 100ms. But if multiple automatic triggers arrive, each one schedules a separate retry, creating redundant work.Let me understand the full context by looking at how offloads are triggered:
- Assistant
Let me look at where
maybeOffloadInBackgroundis called. - Assistant
Let me look at line 1971 to see what calls
maybeOffloadInBackground(NULL_OFFLOAD_PROMISE). - Assistant
Let me look at where line 1971 is, and how it's called.
- Assistant
Let me understand the full implementation of the methods:ᕤNow let me look at the
offloadMutexand theCallbackMutexclass to understand the synchronization.Let me understand the full implementation of the methods: - Assistant
Let me look at the full maybeOffload method and understand the current retry behavior, then design the fix.
- Assistant
Now I understand the issue. Let me check how the manual/explicit offload is triggered:
- Assistant
Let me look for the
offloadPrefixmethod which is the manual offload path: - Assistant
Let me look at the
offloadPrefixmethod to understand the manual offload path. - Assistant
Now I understand the issue. Let me look at the rest of the
asyncOffloadPrefixmethod: - Assistant
Now let me look at
NULL_OFFLOAD_PROMISEand understand the promise handling: - Assistant
Now let me look at the area where
offloadMutex.unlock()happens to understand the full flow: - Assistant
Let me look at how
NULL_OFFLOAD_PROMISEis used and how the offload flows work: - Assistant
Now let me look at the OffloadPrefixTest to understand the expected behavior:
- Assistant
Let me look at the test to understand the existing behavior and what tests exist:
- Assistant
Let me look more carefully at the full
maybeOffloadmethod, and understand the automatic vs manual paths: - Assistant
Now I understand the issue clearly. The problem is in
maybeOffload:- When
offloadMutex.tryLock()fails, it schedules a retry viascheduledExecutor.schedule(() -> maybeOffloadInBackground(finalPromise), 100, TimeUnit.MILLISECONDS) - Every time
maybeOffloadInBackgroundis called (e.g., when a ledger is closed), if the mutex is locked, it schedules another retry - This can lead to many redundant retry tasks being scheduled
The fix should coalesce automatic offload triggers: at most one active automatic run, and at most one follow-up. Let me look at the full
maybeOffloadandmaybeOffloadInBackgroundmethods: - When
- Assistant
Now let me check for any other callers of
maybeOffloadInBackground: - Assistant
Let me check the ManagedLedgerFactoryImpl caller:
- Assistant
Now let me check the test that calls
maybeOffloadInBackground: - Assistant
Now let me understand the full picture. Let me look at the executor used:
- Assistant
Now let me look at the existing offload tests to understand what behavior is expected:
- Assistant
No results found. Let me search for tests related to automatic offloading.
- Assistant
Let me look at the imports and field declarations area:
- Assistant
Let me check what
CallbackMutexis: - Assistant
Now let me look at the
offloadLoopmethod to understand how the promise is completed: - Assistant
Now I have a thorough understanding of the problem. Let me summarize:
Problem: When
maybeOffloadcan't acquireoffloadMutex, it schedules a retry viascheduledExecutor.schedule(() -> maybeOffloadInBackground(finalPromise), 100, TimeUnit.MILLISECONDS). Every trigger while the mutex is locked creates another scheduled retry, leading to redundant work.Solution: Coalesce automatic offload triggers (those using
NULL_OFFLOAD_PROMISE):- Add an
AtomicBoolean automaticOffloadPendingflag - When mutex is locked for an automatic trigger, set the flag instead of scheduling a retry
- When any offload completes and releases the mutex, check the flag and schedule exactly one follow-up
Let me check for existing AtomicBoolean imports:
- Add an
- Assistant
Good,
AtomicBooleanis already imported. Now let me implement the fix. I need to:- Add an
AtomicBoolean automaticOffloadPendingfield near theoffloadMutex - Modify
maybeOffloadto coalesce automatic triggers - Modify the
unlockingPromise.whenCompletecallback to trigger follow-up
Let me first open the file and make the changes:
- Add an
- Assistant
Now let me add the
automaticOffloadPendingfield after theoffloadMutexandNULL_OFFLOAD_PROMISEdeclarations: - Assistant
Good. Now let me modify the
maybeOffloadmethod. First, let me look at the exact lines: - Assistant
Now let me make the key changes. I need to modify the
if (!offloadMutex.tryLock())block and theunlockingPromise.whenCompletecallback: - Assistant
Good. The changes look correct. Now let me verify the
asyncOffloadPrefixmethod is unchanged - manual offloads should retain their existing behavior: - Assistant
Good. The manual offload path via
asyncOffloadPrefixis unchanged and still fails withOffloadInProgressExceptionif the mutex is locked. Now let me compile the managed-ledger module to verify the changes compile: - Assistant
Let me find the correct module name.
- Assistant
Let me find the pom.xml for the managed-ledger module.
- Assistant
Let me find the build structure:
- Assistant
Let me look at the directory structure more carefully.
- Assistant
Let me check the build system:
- Assistant
It's a Gradle project. Let me try compiling with Gradle:
- Assistant
Let me try to compile with Gradle.
- Assistant
Compilation successful. Now let me run the offload tests to verify the changes don't break anything:
- Assistant
There's a test failure. Let me look at the failing test to understand what it expects:
- Assistant
Let me look at the full test:
- Assistant
Let me see the rest of this test method, particularly the case that failed (100, null):
- Assistant
Let me see the test parameters and the
assertEventuallyTruemethod: - Assistant
Let me look at the
assertEventuallyTrueand the specific test case that fails:(100, null) - Assistant
Let me understand the test flow. The issue is that
asyncOffloadPrefixuses its own mutex release logic that doesn't checkautomaticOffloadPending. Let me look at that: - Assistant
I see the issue. When the manual offload completes (in
asyncOffloadPrefix), it doesn't checkautomaticOffloadPending. TheautomaticOffloadPendingflag was set by the automatic triggers that arrived during the manual offload, but no one checks it when the manual offload completes. I need to add the same check inasyncOffloadPrefix's completion handler.Let me fix this:
- Assistant
I need to add the
automaticOffloadPendingcheck in theasyncOffloadPrefixmethod's promise completion callback as well: - Assistant
I see there's a duplicate
});on line 3671. Let me fix that: - Assistant
Good. Now let me compile and run the tests again:
- Assistant
Good. Now let me run the specific failing test again:
- Assistant
All OffloadPrefixTest tests passed. Let me also run the other offload-related tests:
- Assistant
Let me run all the managed-ledger tests to make sure nothing else is broken:
- Assistant
All managed-ledger tests pass. Let me now review the changes I've made:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.