Repository navigation
fix(group): an allocation failure on the dense-array group route answers oom, not a table short of a column or a write through NULL - #1812
Conversation
291fbc5 to
7bbcfb1
Compare
…ers oom, not a table short of a column or a write through NULL The dense-array route of exec_group_run skipped a key column it could not allocate, and emit_agg_columns, which builds the aggregate columns of this and several other routes, skipped an aggregate column the same way: the answer lacked that column, and the failed allocation's error was dropped. The route used its dense_* scratch buffers unchecked and wrote through NULL when one failed to allocate. The route now answers the allocation's failure through one exit that frees its scratch, its shared buffers and the result: a failed key column, a failed ray_table_add_col, or a missing scratch buffer. emit_agg_columns makes *result the failed column's own error; its callers return the result as before, and the whole-table route, which recomputes columns after it, takes it as its recompute error. The sequential fallback the radix route takes when its buffer table fails to allocate had the same two skips in its own emit, a key column and an aggregate column; both now answer the failed column's error. Its group_ht_init failing part-way kept the slots it had allocated; single_ht is zeroed first and freed on that exit. A failed group_ht_rehash kept the old slot table, which was resized only after the insert that took it past half full: the fallback table starts at two slots, so that insert filled it, and the next probe for a new key found no empty slot and looped forever. The two probes that insert (group_probe_entry and group_merge_row) now rehash before that insert and probe again, and a failed rehash marks the table oom, as a failed grow of its rows does, so it takes no more groups and the group answers oom. A DEBUG seam, ray_select_keys_alloc_fail_for_test, fails each allocation of the keys-only select-by result build in ray_select_body; aware/group_alloc_fail sweeps it, and the group seam of #1809 now also sweeps the dense-array route, and, with a second DEBUG seam (ray_group_test_radix_bufs_fail) failing the radix buffer table, the sequential fallback. Fixes #1811
7bbcfb1 to
80ce008
Compare
singaraiona
left a comment
There was a problem hiding this comment.
Review of 80ce008: READY
My #1809 finding is closed. Every route I injected into answers the unfailed result or oom with nothing kept, and the rehash change is right as a hash-table change. Two observations below are for separate issues, not this PR.
Injection (local seam, never pushed)
The seam fails the nth allocation process-wide, on any thread, while exec_group runs. One mode then fails every following allocation too (a lasting failure). Every position was swept, except one cell noted below. Each run must give the unfailed answer byte for byte (ser), or oom with net_bytes <= 0 by ray_mem_trace (every heap) after release and GC. The table is 70,000 rows with 4 pool threads, and each query runs with v2 on and v2 off:
| Shapes | Outcome |
|---|---|
dense route, (select {s: (sum v) n: (min v) from: F by: j}) (19 positions with v2 off) and sum/avg/max/count by j |
same or oom, 0 kept |
keyless sum/avg and max/count |
same or oom, 0 kept |
by: d (DATE), by: w (I64 over 50,000 values: 34 positions v2 on, 106 v2 off) |
same or oom, 0 kept |
where: + by: j |
same or oom, 0 kept |
two failures on the radix fallback (ray_group_test_radix_bufs_fail, then every position): max by k, max/sum by [k j], min/max by [k d] |
33, 41 and 45 positions: same or oom, 0 kept, no short answer |
lasting failure (every allocation from the nth on fails): the fallback max by k, the dense route, by: w with v2 on and off |
always oom (or same before the first failure), 0 kept, no hang |
The lasting-failure rows exercise the new oom mark: a failed rehash no longer leaves a full table that a probe can loop in.
The rehash change, read as a hash-table change
- Same fill. The threshold
(grp_count + 1) * 2 > ht_capbefore the insert is dev'sgrp_count * 2 > ht_capafter it, so a group does the same number of rehashes. - Restarting the probe. After a successful rehash the probe restarts in the new table, which is now at most half full, so it always meets an empty slot.
- Keys and tombstones. An existing (duplicate) key is found before the empty-slot branch, so the change never touches it. There are no tombstones: groups are never deleted.
- After a failed rehash the table keeps its old slots and is marked
oom. The new-key branch then refuses further groups, so the table stays at most half full.- Every caller checks the flag: the phase-2 and v2 worker loops after each probe,
single_ht.oomaftergroup_rows_range(group.c:22642), andseq->oomin the pivot path.
- Every caller checks the flag: the phase-2 and v2 worker loops after each probe,
- The flag only goes up on failure. It is cleared in
group_ht_init(ht->oom = 0) and set only on a failed grow or rehash. - Other
group_ht_initcallers after a part-way failure: the v2 worker tables are freed unconditionally (#1806) andpivot_ingest_freefrees on slots or rows, so the fallback was the only site that leaked.
Uninjected results and cost
Release builds of this head and the base 4b60b75, -c 1, 1,048,576 rows: a 500,000-group key, a 16-group key and both together (by: [hi lo]).
- The
serof the results is identical. - Callgrind totals are exactly equal: 815,816,036 on both.
Runs (gcc)
make -j4 test TEST_ARGS="-j 4": 5893 of 5897 passed, 4 skipped, 0 failed.- ASan+UBSan test binary:
aware/group_alloc_fail1/1,-f cancel87/88 (1 skip),-f group/98/98, no sanitizer report. - TSan:
aware/group_alloc_fail1/1 and-f cancel87/88 (1 skip), 0 reports.
Observations for separate issues (pre-existing, outside this PR's routes)
- The parted per-partition group crashes on an allocation failure.
(select {s: (sum v) c: (count v) from: P by: j})over a 3-partition table takes a SIGSEGV inray_group_buildatgraph.c:1128(key_ids[i] = keys[i]->id,keys[i]NULL) fromexec_group_per_partition(group.c~14322). A scan node that failed to allocate is passed on unchecked. This code is untouched here; it is the graph builder of that route. - The group order can change under an injected failure. On
where:+by: j(v2 on and off) and onby: wwith v2 off, a few failure positions give an answer with the same groups and the same values, but in first-seen order instead of key order. The fallback emits a hash table, where the unfailed run takes the dense or sorted route. My byte-for-byte check counts these as "different". The content is complete and right. Whether group order is part of the contract is the owner's call; if it is, the fallback should sort as the route it replaces does.
Not covered (about 45 minutes)
exec_group_slicesand the whole-table recompute route by an injection known to reach them: my queries may not have taken every one of the sevenemit_agg_columnscallers.- Wall-time timing of the rehash move at 20 million rows (instruction counts only).
- SYM-keyed and parted routes beyond the crash above.
- Two failures outside the fallback.
Beyond the title
Besides the dense-array route of #1811, this pull request fixes three defects on the radix route's sequential fallback, the path a group takes when the radix buffer table
radix_bufsfails to allocate:radix_bufsfailed, a failed key or aggregate column was skipped, so the answer was short of a column (raised in the review of fix(group): an allocation failure in a group on the radix route answers oom, not a crash or an answer short of a column or groups #1809).group_ht_init. The fallback's table init kept the slots it had allocated when a later allocation failed.The defect
An allocation that failed on the dense-array group route, or on the radix route's sequential fallback, answered a wrong result, kept memory or hung (#1811, found while fixing #1808):
emit_agg_columns, which builds the aggregate columns of this and several other routes, skipped an aggregate column the same way. The answer lacked that column, and the failed allocation's error object was dropped.radix_bufsfails to allocate) had the same two skips in its own emit: a key column and an aggregate column. This was raised in the review of fix(group): an allocation failure in a group on the radix route answers oom, not a crash or an answer short of a column or groups #1809.group_ht_initfailed part-way it kept the slots it had already allocated (64 bytes).group_ht_rehashkept the old slot table. The table was resized only after the insert that took it past half full, and the fallback table starts at two slots, so that insert filled it. The next probe for a new key then found no empty slot and looped forever.dense_*scratch buffers unchecked, and wrote through NULL when one failed to allocate (SIGSEGV).The fix
The dense-array emit answers each failure through one exit (
da_emit_done), which frees its scratch, the route's shared buffers and the result: a failed key column (its own error), a failedray_table_add_col, or any scratch buffer the route asked for and did not get (oom). The scratch headers are declared ahead of the key loop, so that exit can free them from any point.emit_agg_columnsmakes*resultthe failed column's own error, releasing the table. Six of its seven callers returnresultright away, as before. The whole-table route recomputes columns after it, so it takes that error as itsrecompute_err, which skips the recomputes and answers it.The sequential fallback's emit answers a failed key or aggregate column with that column's error. On the aggregate exit, it releases the holistic columns that are not yet attached.
The fallback's init zeroes
single_htfirst and frees it when the init fails.The two probes that insert a group (
group_probe_entry,group_merge_row) rehash before the insert that would take the table past half full, then probe again in the new table. A failed rehash marks the tableoom, as a failed grow of its rows already did. The probes then take no more groups, the table stays at most half full, and the callers answeroom(they already checked the flag for the grow).The keys-only select-by result build
#1808 listed it as keeping 128 bytes on a few
oomexits. A seam over that build (ray_select_keys_alloc_fail_for_test: first rows, the table, the gathered columns ofray_select_body) shows every position answeringoomkeeping nothing, or the reference. The 128 bytes were the radix emit's dropped error, which #1809 fixed. Nothing changes in that code; the seam stays as coverage.Test
aware/group_alloc_failnow also:sweeps the dense-array route,
(select {s: (sum v) n: (min v) from: F by: j})with the v2 engine off (it would take the shape otherwise), with the group seam of fix(group): an allocation failure in a group on the radix route answers oom, not a crash or an answer short of a column or groups #1809, every position;sweeps the keys-only result build of
(select {from: F by: k})and(select {from: F by: j}), every position.sweeps two failures: a DEBUG seam (
ray_group_test_radix_bufs_fail) fails the radix route's buffer table, sending the group to its sequential fallback, and then the group seam fails the nth allocation, every position. The queries are(select {m: (max v) from: G by: k})(F64 key) and(select {m: (max v) s: (sum v) from: G by: [k j]}), over 70,000 rows.Each run must answer byte for byte the unfailed answer, or
oomkeeping nothing by the process's allocation trace (every heap).Without the fix:
group.cas on dev:(select {s: (sum v) n: (min v) from: F by: j}), allocation 14 of 19 fails: an answer, not the reference;(select {m: (max v) from: G by: k}), buffers and allocation 20 fail: an answer, not the reference; with only its aggregate-column skip restored: the same at allocation 21;buffers and allocation 4 fail: 64 bytes kept;oommark, it passes only because the seam fails one allocation, and the retry succeeds. Under a lasting failure that probe would retry the rehash forever.aw_teardownnow also turns the v2 engine back on, in case a failing test turned it off.Cost
Callgrind, release (gcc),
-c 1, 1,048,576 rows, against #1809's head, which is dev's group code (setup alone +22):(select {s: (sum v) n: (min v) from: F by: j})(select {s: (sum v) a: (avg v) from: F})(count (select {from: F by: j}))That is +0, +11 and +0 instructions once setup is taken out. Nothing is added per row; the seams are DEBUG only.
The rehash now runs before the insert instead of after, at the same fill, so a group does the same number of rehashes. Release (gcc), 20,000,000 rows, 5,000,000 groups, wall time of three runs each, against dev's probe and rehash:
-c(select {s: (sum v) from: T by: a})(select {s: (sum v) c: (count v) from: T by: [a b]})(select {s: (sum v) from: T by: a})(select {s: (sum v) c: (count v) from: T by: [a b]})That is within run-to-run noise.
Checks
make -j4 test TEST_ARGS="-j 4": 5,893 of 5,897 passed, 4 skipped.aware/group_alloc_failpasses; filtercancel: 87 of 88 passed (1 skipped); no sanitizer report.clang-23 -fsyntax-onlyongroup.candquery.c(with and without-DDEBUG) andtest_aware.c(-DDEBUG): clean.Fixes #1811