Repository navigation
Make h3kRing and its siblings observe cancellation while expanding a block - #115893
Conversation
…block
`h3kRing`, `h3HexRing`, `h3Line` and `h3ToChildren` expand every row of a block
inside one `IFunction::executeImpl` call, and the executor evaluates
`max_execution_time` and `KILL QUERY` only between blocks, so nothing observed
the query's `QueryStatus` until the whole block was done. The size of a row's
result is driven by the arguments rather than by the number of rows read, so
the per-row caps these functions already have bound one row, not the block.
`h3kRing` is the worst of the four. A row costs `maxGridDiskSize(k)` regardless
of how many cells exist at the cell's resolution, and on a coarse cell
`gridDiskDistancesUnsafe` fails on the pentagons, after which h3 clears that
whole buffer and walks the grid recursively. So `h3kRing` on a resolution 0
cell with `k = 1023` allocates and zeroes 25 MB per row to produce 122 cells:
it never comes near a memory limit, it just runs. The other three produce what
they cost, so they are additionally bounded by `max_memory_usage`.
Measured on a release build:
SELECT sum(length(h3kRing(materialize(579205133326352383), toUInt16(1023))))
FROM numbers(65409)
SETTINGS max_block_size = 65409, max_execution_time = 1
before: 250088 ms after: 1004 ms
The checkpoint uses `makeCancellationCheck` and `CancellationBudget`, which are
already on master and used by `countSubstrings`, `replaceString`, `countMatches`
and `isProbablePrime`, so this adds no new constant. Work is charged in the
bytes a row expands into: the check then fires once per row while `k` is large
and about every sixteen thousand rows while it is 1. `h3Line` charges in both of
its loops because `gridPathCellsSize` walks the grid rather than being
arithmetic; `h3HexRing`'s sizing loop is `6 * k` arithmetic and is left alone.
Reported by Stress test (amd_msan) on master, where this query was still running
993 s after cancellation against its 10 s limit:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=d3d7dc9bb6b2dfe932910da82715bcbe7d0d6806&name_0=MasterCI&name_1=Stress%20test%20%28amd_msan%29
Related: #112203
|
Workflow [PR], commit [19bac1b] Summary: ✅
AI ReviewSummaryThis PR adds cancellation checkpoints to Findings
Final VerdictChanges requested: LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 46/51 (90.20%) · Uncovered code |
| "Line cannot be computed between start H3 index {} and end H3 index {}, error: {}", | ||
| start, end, err); | ||
|
|
||
| budget.charge(static_cast<size_t>(size) * sizeof(H3Index)); |
There was a problem hiding this comment.
gridPathCellsSize is still entered without any pre-call checkpoint. If the query is killed or times out after the previous row, this loop will still do one full path walk before budget.charge(...) runs, so h3Line can still ignore cancellation for the entire sizing pass of that row.
The other three functions charge before their expensive H3 call for exactly this reason. Here I think you need a direct check_cancellation() (or equivalent pre-call checkpoint) before gridPathCellsSize, and then keep the size-based budget.charge(...) after it so later checks still scale with the line length.
There was a problem hiding this comment.
will still do one full sizing walk before budget.charge(...) runs
N.p.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units10 translation units recompiled, 14 s compile time in total, 10 of them have a recent master baseline. |
Reproducer-- max_execution_time is ignored: ~250 s on 26.7 release, ~1 s once fixed
SELECT sum(length(h3kRing(materialize(579205133326352383), toUInt16(1023))))
FROM numbers(65409)
SETTINGS max_block_size = 65409, max_execution_time = 1, max_memory_usage = 0;Results:
Backport the fix to CC component owner: @alexey-milovidov @scanhex12 Analysis metadata
Introducing change: not identified
|
Related: #112203
Related: #113924
Related: #112273
Related: #115773
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
h3kRing,h3HexRing,h3Lineandh3ToChildrenignoringmax_execution_timeandKILL QUERYwhile expanding a block of rows.Description
Stress test (amd_msan)on master failed the hung check with a single query that had beencancelled 993 s earlier, against its own
max_execution_time = 10:report,
is_cancelled: 1,peak_memory_usage: 127 MB, the working thread insideFunctionH3KRing::executeImpl.The
arm_asan_ubsanmaster run 1 h earlier hung on the same shape.These four functions expand every row of a block inside one
IFunction::executeImplcall, and theexecutor evaluates
max_execution_timeandKILL QUERYonly between blocks, so nothing observes thequery's
QueryStatusuntil the whole block is done. The per-row caps these functions already havebound one row, not the sum over the block, and the size of a row's result is driven by the arguments
rather than by the number of rows read.
h3kRingis the worst of the four, and it is the reason this shows up as a hang rather than as anerror. A row costs
maxGridDiskSize(k)regardless of how many cells exist at the cell's resolution,and on a coarse cell
gridDiskDistancesUnsafefails on the pentagons, after which h3 clears thatwhole buffer and walks the grid recursively.
579205133326352383is a resolution 0 cell, where thewhole grid is 122 cells, so at
k = 1023each row allocates and zeroes 25 MB to produce 122 cells:the query never comes near a memory limit, it just runs. The other three produce what they cost, so
they are additionally bounded by
max_memory_usage.Measured on a release build, the query the fuzzer produced, one block:
Implementation
The checkpoint uses
makeCancellationCheckandCancellationBudget, which are already on master andused by
countSubstrings,replaceString,countMatchesandisProbablePrime, so this adds no newconstant and no new mechanism. Work is charged in the bytes a row expands into rather than in rows, so
the check fires once per row while
kis large and about every sixteen thousand rows while it is 1.h3Linecharges in both of its loops:gridPathCellsSizewalks the grid rather than being arithmetic,so its sizing pass is expensive on its own.
h3HexRing's sizing loop is6 * karithmetic plus a cellvalidation and is left alone.
This is a narrower version of #113924, which was closed. The objection there was the four hand-written
items_between_cancellation_checks = 10'000constants; the shared helper that removes them landed onmaster in #113369 and has been used by merged work since (#114032). If the preferred answer is still a
single generic check before
IFunction::executeImpl, note that it cannot bound these four, because thewhole block is inside one such call.
Testing
05029_h3_kring_cancellation.shruns the reported shape undermax_execution_time = 1inside an outertimeout 60, so the oracle is the outer timeout rather than a measured elapsed time, and asserts theresults are unchanged. It takes 1.7 s fixed and cannot finish unfixed. All 59 existing
h3tests passagainst the patched binary with no hung queries.
Workflow [PR]
Sync PR [sync-upstream/pr/115893]
Version info
26.8.1.1908(included in26.8and later)