Skip to content
TheChatArchiveThe Chat Archive
GLMz-ai/glm-5.2· unknownHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION Automatic managed-ledger offload triggers create redundant retry work…

  1. 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 using git reset --hard command. Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to 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-$

  2. GLM Assistant

    Let me start by exploring the repository structure to understand what we're working with.

  3. GLM Assistant

    This is the Apache Pulsar repository. The issue is about managed-ledger offload. Let me find the relevant offload code.

  4. GLM Assistant

    Now I understand the issue. When maybeOffloadInBackground is called repeatedly (e.g., on every append, trim, or other event), and an offload is already running (offloadMutex.tryLock() fails), the current code schedules another maybeOffloadInBackground after 100ms. This creates redundant retry work — multiple scheduled retries piling up.

    Let me look at how maybeOffloadInBackground is called.

  5. GLM Assistant

    Now let me look at the manual offload entry point and the offloadLoop.

  6. GLM Assistant

    Now let me understand the full picture. The maybeOffloadInBackground is called with NULL_OFFLOAD_PROMISE for automatic triggers. When an offload is already in progress (!offloadMutex.tryLock()), it schedules a retry after 100ms. Multiple retries pile up.

    The fix:

    • For automatic triggers, coalesce them: only one active run and at most one follow-up run.
    • The follow-up must process newly eligible ledgers.
    • Explicit/manual offload futures must retain their existing behavior.

    Let me look at the NULL_OFFLOAD_PROMISE definition and the manual offload method (the one that uses offloadMutex.tryLock() and returns OffloadInProgressException).

    The manual offload entry point is the offload method with a callback. Let me find it.

  7. GLM Assistant

    Let me look at the test that uses maybeOffloadInBackground with a real promise.

  8. GLM Assistant

    Let me check the executor used by ManagedLedgerImpl.

  9. GLM Assistant

    executor is bookKeeper.getMainWorkerPool().chooseThread(name) — this is an ordered executor per name, so all operations for a given managed ledger run on the same thread, one at a time. This means operations submitted to executor are serialized. The maybeOffload runs on this executor. The offloadLoop callbacks (thenCompose etc.) run on... let me check. The offload callbacks use scheduledExecutor for retries. The whenComplete of the offload chain — which thread? CompletableFuture callbacks run on the thread that completes them, unless an executor is specified. The offload operation config.getLedgerOffloader().offload(...) completes on some thread (bookkeeper worker). Then the subsequent callbacks run on that thread. So offloadLoop recursion happens on various threads.

    But the unlockingPromise.whenComplete (which unlocks the mutex) — when is it completed? It's completed by offloadLoop's promise.complete/promise.completeExceptionally, which happens on whichever thread processes the last ledger. So the unlock happens on a bookkeeper worker thread, not the managed ledger executor.

    So thread safety matters. The offloadMutex is a CallbackMutex. Let me look at CallbackMutex.

  10. GLM Assistant

    Now let me look at the existing tests to understand expected behaviors.

  11. GLM Assistant

    Let me check the context where maybeOffloadInBackground is called at line 1971.

  12. GLM Assistant

    Let me search for any existing tests or references to coalescing/follow-up behavior.

  13. GLM Assistant

    Let me run the existing offload tests to establish a baseline.

  14. GLM Assistant

    Let me check all usages of offloadMutex to understand all the paths that hold it.

  15. GLM Assistant

    Two paths acquire offloadMutex:

    1. maybeOffload (automatic, line 2836) — unlocks in unlockingPromise.whenComplete (line 2844).
    2. Manual offload method (line 3644) — unlocks in promise.whenComplete (line 3651).

    Let me check ShadowManagedLedgerImpl for offload overrides.

  16. GLM Assistant

    I now have a clear understanding. Let me design the solution:

    1. Add a pendingAutoOffload flag, manipulated only on the serialized executor thread (in maybeOffload), so it's race-free.
    2. When an automatic trigger (NULL_OFFLOAD_PROMISE) finds the mutex held, set pendingAutoOffload = true instead of scheduling a redundant retry. (Explicit/manual futures keep the retry.)
    3. On every offload completion (both the automatic maybeOffload path and the manual offload path), schedule a follow-up check on the executor that, if pendingAutoOffload is set, clears it and triggers exactly one follow-up run.

    This ensures at most one active automatic run and at most one follow-up, while manual/explicit behavior is preserved. Let me re-read the exact code to edit.

  17. GLM Assistant

    Let me see the full manual offload method to find its exact boundaries.

  18. GLM Assistant

    Now I have a complete understanding. Let me implement the solution. First, add the pendingAutoOffload field near the other offload fields.

  19. GLM Assistant

    No python3. Let me use the edit command.

  20. GLM Assistant

    Now let me edit the maybeOffload method's tryLock-fail path and the completion callback. Let me find the current line numbers.

  21. GLM Assistant

    Now add the scheduleAutoOffloadFollowupIfNeeded helper method after maybeOffload.

  22. GLM Assistant

    I left out the closing brace of the new method. Let me fix it.

  23. GLM Assistant

    Let me add a blank line for readability between the methods.

  24. GLM Assistant

    Now update the manual offload (asyncOffloadPrefix) completion path to also schedule the follow-up check. Let me find the current line numbers.

  25. GLM Assistant

    There's a duplicate }); at line 3699. Let me remove it.

  26. GLM Assistant

    Now let me compile the managed-ledger module to check for errors.

  27. GLM Assistant

    Compiles. Now let me run the existing offload tests to verify nothing breaks.

  28. GLM Assistant

    All existing tests pass. Now let me write a temporary test to verify the coalescing behavior works correctly. Let me look at the test base class setup.

  29. GLM Assistant

    Let me look at the MockLedgerOffloader and buildEntry helpers in OffloadPrefixTest.

  30. GLM Assistant

    Let me create a temporary test to verify the coalescing behavior using reflection on the new pendingAutoOffload flag.

  31. GLM Assistant

    I referenced a non-existent public helper. Let me fix the test to define its own buildEntry.

  32. GLM Assistant

    Wait, the sed for adding the method might have messed up the class declaration indentation. Let me check the file.

  33. GLM Assistant

    The buildEntry method ended up outside the class. Let me rewrite the file cleanly.

  34. GLM Assistant

    The test passes. Let me also verify that an explicit future via maybeOffloadInBackground retains its retry behavior (completes after the running offload finishes). Let me add a second test method.

  35. GLM Assistant

    The appended method went outside the class. Let me rewrite the whole file with both methods properly inside.

  36. GLM Assistant

    Both tests pass. Now let me run the broader managed-ledger test suite to ensure nothing else breaks.

  37. GLM Assistant

    All pass. Let me also check for broker-level offload tests that might be affected.

  38. GLM Assistant

    Let me look at the test that exercises offload triggering in AdminApiOffloadTest, around line 447 (the offload test with threshold).

  39. GLM Assistant

    Let me run the full managed-ledger test suite to be thorough.

  40. GLM Assistant

    Full managed-ledger test suite passes. Now let me remove the temporary test file before submitting.

  41. GLM Assistant

    The full managed-ledger test suite passes. Now let me remove the temporary test file before submitting.

  42. GLM Assistant

    Only the source file is modified. Let me review the full diff to ensure correctness.

  43. GLM Assistant

    The diff looks clean and correct. Let me verify maybeOffload is only ever invoked via the executor (so the flag access is truly single-threaded).

  44. GLM Assistant

    Confirmed: maybeOffload is only ever invoked via executor.execute(...) (line 2822), and scheduleAutoOffloadFollowupIfNeeded's lambda also runs on the executor. So pendingAutoOffload is accessed only on the single per-ledger executor thread → race-free, as documented.

    The implementation is complete and verified. Let me summarize the solution and run through the checklist.

    Summary of changes

    In ManagedLedgerImpl.java:

    1. Added a pendingAutoOffload flag (accessed only on the managed-ledger's single-threaded executor) that records whether a follow-up automatic offload run is needed.

    2. maybeOffload (automatic trigger path): When tryLock() fails for an automatic trigger (NULL_OFFLOAD_PROMISE), instead of scheduling a redundant 100ms retry on scheduledExecutor (which previously cascaded into N retry chains when many triggers arrived during one running offload), it now just sets pendingAutoOffload = true (coalescing — idempotent, so any number of triggers collapse to a single pending follow-up). Explicit/manual futures (finalPromise != NULL_OFFLOAD_PROMISE) retain the existing retry behavior.

    3. scheduleAutoOffloadFollowupIfNeeded(): A new helper invoked from both offload completion paths (the automatic maybeOffload's unlockingPromise completion and the manual asyncOffloadPrefix's promise completion). It runs on the executor; if pendingAutoOffload is set it clears it and schedules exactly one maybeOffloadInBackground(NULL_OFFLOAD_PROMISE) run, which re-evaluates thresholds and processes any newly eligible ledgers.

    This guarantees at most one active automatic run (mutex) and at most one follow-up run when additional triggers arrive, while manual/explicit offload behavior is unchanged (manual offloadPrefix still throws OffloadInProgressException immediately; explicit futures still retry to completion).

    Checklist reasoning

    • Reproduced the issue: The issue describes redundant retry work (no stack trace). I wrote a temporary test using a slow offloader with many ledger rolls during a running automatic offload; it confirmed the new coalescing flag is set (coalescing occurred), the single follow-up processes newly eligible ledgers, and the flag clears afterward. (Removed before submitting.)
    • Confirmed the fix resolves it: The temp test passed with the fix; with the old code the pendingAutoOffload mechanism didn't exist (the old code scheduled a retry per trigger). Existing multipleAutoTriggers/offloadAsSoonAsClosed/manualTriggerWhileAutoInProgress/autoTriggerWhileManualInProgress tests confirm correctness is preserved.
    • Edge cases: explicit/manual futures retain retry; manual offload still fails fast with OffloadInProgressException; both completion paths schedule the follow-up; flag is single-threaded (race-free); early-return paths don't acquire the mutex; the chain terminates (an acquiring run never sets the flag, so no infinite follow-up loop); follow-up re-evaluates thresholds for newly eligible ledgers.
    • Ran affected tests: Full :managed-ledger:test suite passes (BUILD SUCCESSFUL), including all offload-related tests and ManagedLedgerTest.

    Let me do a final verification that the working tree only contains the intended change.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.