Skip to content

Packet job: read only the reusable forced rows, stage the window as it is measured - #670

Merged
rusholme merged 3 commits into
rebuildfrom
packet-window-lean/1-lean
Dec 1, 2026
Merged

rusholme merged 3 commits into
rebuildfrom
packet-window-lean/1-lean

Conversation

@rusholme

@rusholme rusholme commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

What and why

Run 277 lost 1,231 packet jobs: 813 ran out of disk, 400 were killed at 5,120 MiB and 18 could not start on a full node. Both causes are in rapidpipe/stages/packet.py, and this PR fixes both without any Batch change.

  • Memory: the reuse read. The read fetched every official forced row on the window's images for every object in the triggers' fields. For job 160585 that was 3.8 M rows, of which 19,650 were reused, and the result was held as dict rows twice. reuse_rows now reads each field child of forced for the triggers' objects only (REUSE_SQL, aid = ANY … AND pid = ANY …, served by the child's existing aid index) and builds the reuse dict one row at a time. A pair's forced_id hashes its object, so no reusable row is lost. The packets reads the manifest has always recorded (every official set with any row on the window's images) come from a second query, REUSE_SETS_SQL, which returns one row per set. The manifest's reads are therefore unchanged. No schema function is added; the packet_reuse_rows function stays in the schema, now unused by the stage.
  • Disk: the window staging. _read downloaded every window image before measuring anything, and _plain kept a decompressed copy of each until exit (about 191 MiB per image). WindowImage's file fields are now URLs. measure_forced fetches an image's files in fetch threads while the LOOKAHEAD images before it are measured. It removes each staged file once the last image measured on it is done: a file several images share, such as a science PSF, is reference-counted by URL. The trigger's three cutout images are fetched by _read and kept. A file:// product is never removed, and an image whose pairs are all reused is never fetched. The fetch threads download without entering a step (steps is single-threaded); the measuring thread's wait is charged to s3_read, and s3_read_bytes still counts every download.
  • LOOKAHEAD = 1, chosen from the measurement below.
  • When several sets hold one forced_id (sets that measured the same pair at the same time), the reuse read keeps the row last in the child's storage order (ORDER BY f.ctid). The copies differ only in their own time_proc. The old whole-field read took whichever row its scan met last, and that varied from run to run (see Identity).

No rapid_docs page states how the window is staged or what the reuse read scans, so no page needed correcting first. The module docstring carries the rule.

Files: rapidpipe/stages/packet.py; tests/unit/test_packet.py (WindowImage takes URLs; two new tests); tests/db/test_field_children.py (the reuse read compared with the shipped read for every case, and the new contract test); tests/db/test_packet_inputs.py (the statement probe now looks for the children check instead of packet_reuse_rows).

Contract tests:

  • test_the_reuse_read_returns_a_trigger_object_s_row_and_not_another_object_s (tests/db): one official set holds two rows on one image, one of the job's object and one of another object. The read returns the first, not the second, and records the set.
  • test_a_window_image_s_files_leave_the_cache_once_it_is_measured (tests/unit, lookahead 0, 1 and 2): before each measurement, the images already measured have no files left in the download cache, except the trigger's cutout images and the PSF a later image still reads. After the call returns, and before the job's cleanup, only the cutout images remain.
  • test_an_image_whose_pairs_are_all_reused_is_never_fetched.

Measurements

These come from the replay harness, read-only against the deployed database through its ro.py guard. Each job is rebuilt from its jobs row and launch inputs and run through _read and write_set, as of the job's start (--asof started). The process is pinned to one CPU to match Batch's 1 vCPU, with its temp directory on /scratch. Peak RSS is getrusage. Peak temp is a high-water byte count: the harness walks the temp directory before every unlink and at the end, and bytes there only fall at an unlink, so this is the true peak. The 3 s sampler's figure agrees with it on every run.

Baseline: unchanged code at the parent 8a5395c; rapidpipe/ is unchanged from the replay ledger's ce36bb4.

job wall peak RSS peak temp
158509 control 391.5 s 2.14 GiB 5.945 GiB
160585 910.4 s 17.00 GiB 15.681 GiB

Lookahead on job 160585 (83 window images, 164,186 forced rows, 19,650 reused), one arm at a time:

lookahead wall forced step peak RSS peak temp
0 756.6 s 611.7 s 2.69 GiB 0.449 GiB
1 588.0 s 442.9 s 2.72 GiB 0.636 GiB
2 585.8 s (fastest) 439.9 s 2.86 GiB 0.823 GiB
4 587.7 s 443.8 s 2.96 GiB 1.303 GiB

The rule is the smallest lookahead whose wall time is within 5 % of the fastest arm's, with peak temp under 4 GiB. That is lookahead 1 (0.4 % slower than lookahead 2).

In-memory decompression (astropy reads each .gz itself, no plain copy written) at lookahead 1: 619.9 s, 2.66 GiB, 0.532 GiB. It is slower, so it is not taken. Two dry reads overlapped its first two minutes, which may have slowed it a little.

The nine jobs on the changed code. 158509 and 160585 ran alone at the PR head d9f7f17. The other seven ran after the merge of rebuild at ba779cb, in parallel, each pinned to its own CPU.

job run 277 outcome window images forced rows (reused) wall peak RSS peak temp
158509 control published 31 61,627 (0) 306.8 s 2,256 MiB 0.620 GiB
158617 out of disk 228 428,262 (43) 1,827.7 s 3,062 MiB 0.601 GiB
160358 out of disk 87 35,846 (930) 537.7 s 2,800 MiB 0.608 GiB
160452 out of disk 200 96,897 (5,565) 1,206.7 s 2,862 MiB 0.610 GiB
160614 out of disk 232 167,044 (4,062) 1,560.0 s 2,860 MiB 0.611 GiB
160517 exit 137 116 230,048 (11,582) 914.0 s 2,761 MiB 0.636 GiB
160585 exit 137 83 164,186 (19,650) 581.2 s 2,781 MiB 0.636 GiB
161086 exit 137 259 216,511 (5,268) 1,810.0 s 2,834 MiB 0.629 GiB
160560 exit 137 544 366,832 (29,247) 3,217.2 s 3,075 MiB 0.626 GiB

All nine are under 5,120 MiB and under the 4 GiB temp ceiling. Wall time: the control 391.5 → 306.8 s and 160585 910.4 → 581.2 s.

Both reuse queries ran under EXPLAIN (ANALYZE, BUFFERS) through ro.py on 160585's fields. On the large child, the rows query is an index scan on forced_862362_aid_idx (1,013 rows, under 1 ms); on the small one the planner chose a 0.7 ms seq scan. The sets query is a 37 ms scan of the large child, with official_open_product_job serving the join. 160585's read took 7.8 s, against 212.4 s before (which included the downloads).

Identity. Same cutoff, record by record. Equal inputs are shown by the same as-of passed to both reads; the same window (pid hash), triggers (sid hash), members (hash) and reused pair ids (hash); and reused counts 19,650 = 19,650.

  • Control 158509 (reuses nothing at its cutoff):
    • Avro records: 2,000 identical.
    • Forced rows: 61,627 identical.
    • External rows: 1,838 identical.
    • Manifest reads: 930 identical in order.
    • Index: identical except byte_offset/byte_length, which shift by single bytes because each record's deflated block holds the set's write time (timeProcessedMjd).
  • 160585 (19,650 reused):
    • Manifest reads: 1,062 identical in order.
    • External rows: 1,334 identical.
    • Avro records and forced rows (164,186): identical except 47 reused rows' time_proc (3 records' prvDiaForcedSources[].timeProcessedMjd, and the index offsets that follow from them).
    • Those 47 are forced_ids that up to a dozen official sets each hold, with identical values and each copy's own time_proc. The unchanged code does not choose among them reproducibly: two old-code reads of 160585 on the same cutoff chose different copies for 66 pairs, and 25 and 41 against the baseline.
    • With every forced time masked (compare.py --copies), 160585 is identical on every product.

Raw evidence (harness, scripts, every result JSON and log, the comparison outputs and the EXPLAIN output): s3://roman-rapid-scratch/rapidpipe/foundation/qa/packet-window-lean/. It is under no run's prefix because this card made no run.

Divergences

  • Identity is shown up to which copy of a multiply-held pair is reused. This is not exact. Under the card's rule, "a difference with equal inputs" ends the task BLOCKED. The difference here is in time_proc alone, and the unchanged code itself differs from run to run on that same field, on equal inputs, by more than the change does. So I judged the patch to cause no difference, and merged. Ben may read the rule differently. If so, the fix is one choice of rule (for example, the earliest time_proc, the original measurement), not a revert.
  • Wall time is measured on one pinned CPU, which Batch's node contention may not match.
  • The seven jobs ran in parallel, so their wall times carry host contention; only their RSS and temp are claimed.
  • The final runs' code differs slightly: 158509 and 160585 ran at the PR head before the merge of rebuild (which brought Alert diaSourceId packs the xsource's set job and xsid into one long (for the 2026-12-01 review) #665's diaSourceId). The seven ran after it.

Improvements seen

  • packet_reuse_rows in database/schema.sql is now unused by the stage. Dropping it is a schema statement on SMDC, left for a schema PR.
  • The packets reads still list every official set with a row on the window's images (91 for 160585), not only the sets whose rows are reused. Narrowing them would change the manifest contract, so this PR keeps them.
  • Duplicate forced_ids across sets come from packet jobs measuring the same pair at the same time. Each set then stores its own copy, which grows forced and makes the reuse choice ambiguous.

Session notes

/scratch (S3 Files) refuses copystat with errno 524, so shutil.copytree fails there and the harness copies bytes only. Pytest under TMPDIR on EFS fails test_measured_with_a_set_falls* with .nfs busy files; CI on local disk is green.

Next and left

Run 277's failed packet jobs are the dataset owner's to rerun on an image holding this fix. No image is built here, and no job definition or Batch setting is touched.

🤖 Generated with Claude Code

rusholme and others added 3 commits December 1, 2026 01:36
…t is measured

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rusholme
rusholme merged commit cf365b9 into rebuild Dec 1, 2026
5 checks passed
@rusholme
rusholme deleted the packet-window-lean/1-lean branch December 1, 2026 04:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant