Repository navigation
perf(checkpoint): shorten the rollout-snapshot barrier hold - #5241
Conversation
c6a6d77 to
ce8a591
Compare
ce8a591 to
006bcf6
Compare
terrykong
left a comment
There was a problem hiding this comment.
Please rebase first. This PR conflicts with its base. #5139 gained c98a1322fb (a recovery-metadata refactor in snapshots.py and test_checkpointing.py) after this PR was rebased. That commit does not touch _run_capture_phases, so C1 still applies.
Running the barrier phases side by side is a clean way to cut the hold, and moving the staging listing into RL removes Gym's dp_client dependency. The overlap and release tests fail if the phases run one after another or the view is never released, which is the right kind of guard.
- (C1)
_run_capture_phasescan drop the capture'sCheckpointReleaseErrorwhen another phase also fails or the snapshot is cancelled. The fix and two regression tests are in the comment.
I reviewed only this PR's own commits (68bd259c70..96c48d5095). Tests ran on CPU only (no GPU run). Ruff passes on the changed files.
Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-5241-shorter-barrier-hold.html
Visual explainer: https://terrykong.github.io/gh-pages-poc/terryk/pr-5241-hidden-release-error.html
Generated by Claude Code
96c48d5 to
8373572
Compare
8373572 to
540e43d
Compare
|
/ok to test 540e43d |
|
/ok to test 0c0196d |
The rollout-snapshot inventory checks compare key sets only, but list_sample_ids used TQ's KV_LIST, which ships every key's custom metadata (route plans and other tags). Add nrl_list_keys to the patched controller, returning just one partition's keys, and use it from list_sample_ids. On a CPU TQ, the inventory check at 300k keys drops from 0.41s to 0.09s with 64-byte tags and from 0.72s to 0.08s with 1 KB tags; at 2.6M keys it drops from 4.84s to 1.02s. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Anish Mahishi <[email protected]>
Inside the barrier, run the replay and staging inventory checks, the recovery-state build and the TQ capture concurrently. Every ledger mutation needs the barrier; the controller maps the build reads are copied on the event loop before it moves to a thread. A view captured beside a failed or cancelled phase is released before the error propagates, with CheckpointReleaseError if the release cannot be confirmed. The barrier phases now overlap, so the hold is close to the longest of them rather than their sum. Serializing the recovery state after resume is already in the base branch. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Anish Mahishi <[email protected]>
A rollout snapshot listed the token-capture staging partition twice under one barrier: once in Gym's check_against_cut, which ran before the concurrent capture phases and so added to the hold, and again in the recovery staging check. Restore listed it twice too. List it once in the staging phase and pass the listing to both checks. Gym's check now runs in that phase, beside the recovery-state build and the TQ capture. Restore shares one listing between its two checks; only the ledger changes between them. The TQ adapter no longer sorts list_sample_ids: every caller compares key sets, and sorting 2.6M staging keys took about 1.25 s per listing. The interface no longer promises an order. The staging check also skips sorting unreferenced rows it will not clear. GymEnvironmentCheckpoint and the environment-checkpoint builders lose the list_staging_keys hook and dp_client parameter that only fed the listing. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Anish Mahishi <[email protected]>
…ase errors Review follow-up. CheckpointReleaseError means storage actors may still hold a captured view, so checkpointing must stop. With the capture phases running concurrently, _run_capture_phases could hide it: a retryable error from another phase re-raised first (tq_capture is the last phase), or a cancellation re-raised CancelledError after the capture task failed its own late release. The snapshot pump then retried, or the failed release went unreported. Raise a phase's CheckpointReleaseError before any other error or the cancellation. Tests, as proposed by the reviewer, cover both cases. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Anish Mahishi <[email protected]>
…s on Listing the staging partition once per restore cut moved the token_capture.staging_partition read above the token_capture.enabled check, so a restore with token capture off read a setting it never uses. Read it inside that branch again. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Anish Mahishi <[email protected]>
0c0196d to
9e6e97b
Compare
|
/ok to test 9e6e97b |
Stack
PR 3, based on #5139 (#5137 is merged). Review against
amahishi/async-tq-rollout-snapshots-main; once #5139 merges, retarget to main. Independent of #5185, which shrinks the remaining ledger build further.Rebased onto #5139 at
1c6f7c4d57, which is onmainatbf50129f25(2026-11-19). The only conflict withmainwas thetq_controller_patch.pymodule docstring, which #5141 rewrote; it now keeps #5141's text, adds thelist_keysparagraph, and notes thatlist_keyshas no upstream counterpart. #5139 now serializes and hashes the recovery state after resume (a211758851), has thepersist(extra_metadata=...)API (12da50adad), and carries its review follow-ups: one recovery-metadata binding and separate capture logs (f053944698), trainer-sidecar and deferred-timing tests (cc5761ee46), and a shortasync_checkpointusage section in place of the README's internals section (1c6f7c4d57). That serialization half of this PR's second commit was dropped here, and this PR's README changes shrink to one paragraph in the new usage section; what remains is below.Summary
Shortens the time a rollout snapshot holds the data-plane barrier (and keeps Gym parked) with a retained SimpleStorage view. Four commits:
perf(data-plane): list TQ keys without their custom metadata— the replay and staging inventory checks compare key sets only, butlist_sample_idsused TQ'sKV_LIST, which ships every key's custom metadata. Addsnrl_list_keysto the patched controller (the perf(data_plane): cheaper TQ kv_retrieve_meta and clear on the controller #4769 patch, applied for every TQ backend) and uses it fromlist_sample_ids. No behavior change.perf(checkpoint): overlap the rollout-snapshot capture phasesCheckpointReleaseErrorif the release cannot be confirmed).perf(checkpoint): list the staging partition once per checkpoint cutcheck_against_cut, which ran before the concurrent phases and so added to the hold, and again in the recovery staging check. The staging phase now lists once, then runs Gym's check and the recovery check on that listing, beside the build and the TQ capture. Restore shares one listing between its two staging checks.list_sample_ids(about 1.25 s per listing at 2.6M keys); every caller compares key sets, and the interface no longer promises an order. The staging check also skips sorting unreferenced rows it will not clear.GymEnvironmentCheckpointand the environment-checkpoint builders drop thelist_staging_keyshook anddp_clientparameter that only fed Gym's listing.fix(checkpoint): let an unconfirmed capture release win over other phase errors(review follow-up)CheckpointReleaseErrormeans storage actors may still hold a captured view, so checkpointing must stop. With concurrent phases,_run_capture_phasescould hide it: a retryable error from another phase re-raised first (tq_captureis the last phase), or a cancellation re-raisedCancelledErrorafter the capture failed its own late release. It now raises a phase'sCheckpointReleaseErrorbefore any other error or the cancellation.Flow
A rollout snapshot with a retained SimpleStorage view, with #5139 and this PR. The shaded block is the data-plane barrier: TQ mutations are drained and blocked, and Gym stays parked. Serialization after resume comes from #5139.
sequenceDiagram participant S as RolloutSnapshotter<br/>(controller event loop) participant G as Gym participant T as Worker threads participant C as TQ controller participant U as TQ storage units S->>G: Close admission, prepare, export episodes, drain replies rect rgba(255, 170, 0, 0.15) Note over S,U: Data-plane barrier held, Gym parked par Replay inventory S->>C: nrl_list_keys(replay), keys only and Recovery-state build S->>T: Walk the recovery ledger into a detached state and Staging inventory S->>C: nrl_list_keys(staging), keys only, once S->>S: Gym's check, then RL's staging check, on that listing and TQ capture S->>C: Serialize controller metadata to bytes S->>U: Retain shard views end Note over S: A failed or cancelled phase releases a captured view end S->>G: Release the barrier, resume Gym and dispatch Note over S,U: Rollouts run again from here S->>T: torch.save + sha256 of the recovery state (from PR 5139) par Persist TQ S->>C: Write controller bytes and S->>U: Write retained shards end S->>S: Write metadata.json with the recovery digest (persist extra_metadata) S->>C: Release views S->>U: Release views S->>S: Write sidecars and manifest, publish the snapshotWhat this PR changes on top of #5139, at 131k rollouts × 20 calls (CPU measurement below; serialization after resume takes about 6.5 s in both):
flowchart LR subgraph base["PR 5139: one after another under the barrier (15.2 s)"] direction LR b0["Gym check<br/>KV_LIST with tags"] --> b1["Replay inventory<br/>KV_LIST with tags"] --> b2["Ledger build"] --> b3["Staging inventory<br/>KV_LIST with tags"] --> b4["TQ capture"] --> b5(["Barrier released,<br/>Gym resumes"]) --> b6["torch.save + sha256"] end subgraph after["This PR: concurrent under the barrier (9.8 s)"] direction LR a0(["Barrier taken"]) --> a1["Replay inventory<br/>keys only"] & a2["Ledger build<br/>(worker thread)"] & a3["Staging listing, once<br/>Gym + RL checks"] & a4["TQ capture"] a1 & a2 & a3 & a4 --> a5(["Barrier released,<br/>Gym resumes"]) --> a6["torch.save + sha256"] endBehavior changes
exclusive_hold_secondsis close to the longest phase, not their sum. The data-plane README is updated.check_against_cutruns inside the concurrent staging phase, so a Gym ownership mismatch can also be found while the TQ capture is retained; that view is released and the snapshot fails as before.staging_inventory_secondsnow includes Gym's check.list_sample_idsreturns keys in no particular order.CheckpointReleaseError, the snapshot fails with it even when another phase also failed or the snapshot was cancelled, so the pump stops checkpointing instead of retrying.Measurements
CPU only, one node, real Ray + TQ 0.1.11 SimpleStorage (2 storage units), synthetic sealed ledger: 8,192 groups × 16 siblings × 20 model calls (131k rollouts, 2.6M staging keys, 131k replay keys), 64-byte tags per key. A script reproduces the barrier phases with the real ledger, TQ listing and capture code; it is not a GPU run. Median of 3 rounds with the variants run in rotating order; ranges in parentheses. Measured on 2026-11-18, before the review follow-ups and the rebase onto current
main; not re-measured since.The hold drops 5.4 s (35%). In the same setup, before #5139's serialization commit (
e95dbedb8d), the hold was 22.82 s.Keys-only listing alone (inventory check incl. sort and comparison):
KV_LISTnrl_list_keysAt 131k rollouts the remaining hold is dominated by the ledger build (~9 s on main's copier; #5185 roughly halves it). Real-run numbers come from the phase metrics in #5139.
Validation
At
540e43d73f(onmainbf50129f25): 350 tests across every test file this PR touches and the Gym-marked lane (7) passed, withmain's Gym pin9e2e5d0ad0on the path. These include the overlap, failed-phase, cancelled-phase and single-listing tests, the two release-error tests from540e43d73f(which fail before its fix), #5139's deferred-path tests, all Gym-checkpoint tests, and the keys-only listing and SimpleStorage tests. Mutations checked earlier: making_run_capture_phasessequential fails the overlap test, and listing the staging partition twice fails the single-listing test. Ruff and Pyrefly passed. The hold benchmark does not model Gym's check.Before the rebases:
--noconftest, pinned Gym5911d628on the path). The same 32 failures and 3 fixture errors occur unchanged on perf(checkpoint): defer rollout and trainer data-plane persistence #5139's head: the 32 are an environmentAttributeError(capture_key) from the borrowed Gym checkout, the 3 need the skipped conftest. No new failures.KV_LISTkeys on a real in-process controller;extra_metadatareaches the completion marker and is rejected (repeated key, unserializable value) before any write; rollout snapshots serialize after resume and the published sidecar's sha256 equals the persisted digest; the trainer path binds the same digest; the build and the TQ capture provably overlap (a sequential mutation of_run_capture_phasesfails this test); a failed inventory or build phase, and a cancelled capture, release the captured view.test_tq_lifecycle.pyandtest_metrics.pywith their conftest (real TQ fixtures): 82 passed.tests/functional/data_plane_simple_checkpoint_cpu.py(real Ray/ZMQ, concurrent mutations, fresh-actor restore) passed, now also checkingnrl_list_keysagainstKV_LISTon the real actor.🤖 Generated with Claude Code