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 first locate the managed-ledger offload trigger paths and relevant tests, then identify how automatic versus explicit requests are scheduled. I’ll reproduce the redundant scheduling behavior with the closest existing test (or a temporary harness), implement coalescing without changing manual futures, and run the focused test suite.
- Assistant
The scheduling logic is in
ManagedLedgerImpl; I’ll inspect that implementation, its call sites, and offload tests around automatic triggering to understand concurrency and executor assumptions before editing. - Assistant
Currently every lock collision schedules its own 100 ms retry, which explains the redundant work. Automatic calls use a shared sentinel promise, while callers can also pass a real future; I’ll inspect field declarations, rollover/factory call sites, and lock/manual-offload implementation to preserve explicit request semantics precisely.
- Assistant
The shared
NULL_OFFLOAD_PROMISEis only used for fire-and-forget automatic triggers; real futures are used by explicit/test invocations. I’ll inspect its construction and callback mutex behavior, then design a small atomic state machine for automatic requests only: idle → running → follow-up-pending, with completion launching exactly one fresh eligibility scan. Manual requests will retain the existing retry loop and individual completion. - Assistant
A key race is a trigger arriving exactly as a run completes; a CAS-based three-state machine handles it without losing the trigger. The follow-up must reread policies and rescan ledgers, not reuse the first run’s queue. Lock contention with a manual run remains a retry of the same automatic run, while manual
CompletableFutures continue through the old per-request retry path. I’ll check imports/style and then apply this localized change. - Assistant
I’ll implement the coalescer around the existing sentinel-based automatic path. Each automatic run gets a private completion future so state advances on success or failure; retries caused by mutex contention remain part of that one run. A pending follow-up invokes the full entry method again internally, ensuring current policies and newly eligible ledgers are reevaluated.
- Assistant
python3is unavailable, so no file was changed. I’ll use the repository shell’s patch utility instead, applying the same localized field and method changes. - Assistant
Neither Python nor
apply_patchis installed. I’ll use the provided noninteractiveeditcommand directly, first inserting the state fields and then replacing the scheduling method. - Assistant
The fields are inserted correctly. I’ll now replace lines 2814–2827 with the automatic state transitions plus a shared scheduler. I’ll ensure policy-disabled/no-offloader cases complete the private automatic future so the state cannot remain stuck; this only changes completion behavior for the internal private future, not explicit requests in enabled configurations.
- Assistant
Review found one behavior regression: explicit futures previously remained untouched when offload was disabled or unavailable, whereas the shared scheduler currently completes them. To satisfy the requirement that explicit futures retain existing behavior, I’ll make scheduling report whether work was submitted; only the private automatic run will self-complete when no work can be scheduled. Explicit paths will remain exactly as before.
- Assistant
The sequential line edits shifted offsets and corrupted the middle block (visible missing CAS argument/braces). I’ll repair the entire
automaticOffloadCompleteandscheduleOffloadregion in one replacement, then compile immediately to catch structural issues. - Assistant
The source block is now structurally correct. Maven’s reactor does not expose the directory name as
managed-ledger; I’ll inspect module metadata and use the module’s own POM for compilation. Before testing, I’ll also consider a subtle synchronous-completion recursion when policies are disabled and a pending trigger exists; it is bounded to the single follow-up and safe. - Assistant
This checkout uses Gradle rather than Maven. I’ll list available tasks/modules and run the managed-ledger compile task, then inspect existing offload test utilities for a focused concurrency test that can be exercised without modifying tests.
- Assistant
The correct task is
managed-ledger:compileJava. I’ll compile and run Checkstyle; if successful, I’ll run focused offload tests. - Assistant
Compilation and Checkstyle pass. For behavioral verification, I’ll run the existing automatic-offload coverage (
OffloadLedgerDeleteTest) and broader prefix-offload tests, which exercise both the real-futuremaybeOffloadInBackgroundpath and explicit/manualoffloadPrefixlocking behavior. - Assistant
Focused tests pass. I’ll now perform a final concurrency audit and repository diff review. In particular: (1) completion unlocks
offloadMutexbefore advancing automatic state, so the follow-up can start immediately; (2) a concurrent trigger racing RUNNING→IDLE either creates PENDING before the CAS or starts a new RUNNING run after it—no trigger is lost; (3) repeated triggers while PENDING are dropped; and (4) each follow-up rescans configuration and ledgers. Then I’ll check only the intended source file changed. - Assistant
The final diff is limited to
ManagedLedgerImpl, introduces no public API, and preserves the explicit path’s prior disabled-policy behavior. To broaden regression confidence beyond focused offload classes, I’ll run the completemanaged-ledgertest suite before submission. - Assistant
All managed-ledger tests pass. Final checklist:
- If an error was reported, reproduce it: No concrete exception was reported; the defect was reproduced by tracing the existing lock-contention path, where every automatic trigger independently scheduled another 100 ms retry against
offloadMutex. - Confirm the fix resolves it: The automatic sentinel path now uses atomic IDLE/RUNNING/PENDING states, so bursts produce one active run and at most one pending follow-up. Triggers while pending do not create retry work.
- Consider edge cases: Completion/failure both advance state; completion races do not lose triggers; lock contention remains one retry chain per automatic run; follow-ups reread policies and rescan ledgers; disabled policies reset automatic state; explicit futures and manual
offloadPrefixbehavior remain unchanged. - Run affected tests:
compileJavaandcheckstyleMainpassed; focused and passed; the complete suite passed.
- If an error was reported, reproduce it: No concrete exception was reported; the defect was reproduced by tracing the existing lock-contention path, where every automatic trigger independently scheduled another 100 ms retry against
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.