Repository navigation
Do not read part sizes while committing under the parts lock - #112691
alexey-milovidov merged 4 commits into
Conversation
`MergeTreeData::Transaction::commit` updates the table-level column and secondary index size aggregates from inside a `NOEXCEPT_SCOPE`, whose catch-all calls `std::terminate`. Accounting for a part whose own sizes were not computed yet called the part's size getters, which compute them from the part storage; on packed storage that reads the skip-indices archive through the thread-pool reader. When scheduling that read fails, the resulting `CANNOT_SCHEDULE_TASK` is not a memory-tracker exception, so `LockMemoryExceptionInThread` does not hold it back: it escapes the scope and aborts the server. A part arrives with its sizes uncomputed whenever it was produced by a path that calls neither warm site, which on the default `columns_and_secondary_indices_sizes_lazy_calculation = 1` includes the untouched-part mutation shortcut and ordinary part load. Account for such a part by dropping the table aggregates instead of reading its sizes, and let the next reader rebuild them from the active parts, which is already the state before the first size query on that default. A part whose sizes are known keeps the existing arithmetic and answers from its cached maps, so it performs no IO either. Also drop the `hasSecondaryIndex` check in the subtract path: it resolves an index file through the part storage, which reads on packed storage, while `getSecondaryIndexSize` already returns an empty size for an index the part does not have. Clear `primary_index_size` in both recompute paths alongside the two size maps. It has a single writer and neither path cleared it, so a recompute double-counted the primary index; this change makes recompute reachable on a new path. Verified with probes at the scope boundary and at every storage entry point below it: before this change one mutation performs 1 lazy size calculation, 2 index-existence resolutions, 12 storage overlay probes, 1 archive read and 1 thread-pool submit inside the scope; after it, zero of each, with identical query results and identical size aggregates.
The fix removes a part-storage read from MergeTreeData::Transaction::commit's NOEXCEPT_SCOPE. On packed storage that read resolves the skip-index archive index, and on object storage it is handed to a thread pool; a pool that cannot schedule throws CANNOT_SCHEDULE_TASK, which is not a memory-tracker exception, so it escapes the scope and terminates the server. The test measures the read through the MutatePart row of system.part_log. That row is usable because ProfileEventsScope is opened at function scope in MutatePlainMergeTreeTask::executeStep, so it covers transaction.commit, and write_part_log runs after the commit on the same thread. An object-storage disk is required: on a local disk the same read is served from the OS page cache and never reaches the pool, because PackedFilesReader::patchSettings clears direct_io_threshold, so the scheduling call short-circuits and the arms cannot be told apart. The cold arm is compared against a warm control that computed its sizes at load time, so the assertion needs no absolute counts. The comparison is strictly fewer rather than no more than: the unfixed build reads exactly as much as the control, so a no-more-than assertion would hold on both builds. Verified against five binaries: it passes on the fix, fails on the unfixed build, fails with only the invalidation reverted and with the predicate made to read, and passes with only the hasSecondaryIndex probe restored, so it pins the lazy-size carrier rather than that probe. 50 randomized runs pass with no tags beyond no-fasttest, and a 198-test sweep shows no other verdict change. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The correctness assertion in the regression test only required data_compressed_bytes > 0 for the two secondary indices, which also passes for a double-counted or a truncated aggregate, while the claim being sold is that invalidate-and-rebuild is equivalent to the incremental update. It also said nothing at all about column_sizes, the other aggregate the fix clears. Replace it with two rows that compare the cold table against the warm control exactly, on all three size fields, over both system.data_skipping_indices and system.columns. The warm control never takes the invalidation branch, so it holds the incrementally updated answer while the cold table holds the rebuilt one. A count and a positivity guard keep the rows from passing on an empty join or an all-zero answer. A relative comparison rather than absolute byte counts matters more than it looks: tests/config/install.sh draws default_compression_codec per install, so the same fixture writes 7549 compressed bytes under LZ4, 4577 under ZSTD(1) and 3947 under ZSTD(3). Both tables are written by one server under one draw, so the comparison is stable where absolute values would be flaky. The fewer_pool_read comment claimed the counter measures the scheduled hand-off to the read thread pool. It does not. ThreadpoolReaderReadBytes is incremented inside ThreadPoolRemoteFSReader::execute, and on this path AsynchronousBoundedReadBuffer::nextImpl has no prefetch future in flight, so it calls readSync, which runs execute directly on the mutation thread. That is why the counter is visible to the MutatePart scope at all: on the scheduled path the increment happens on a pool worker under its own thread group, which the part-log snapshot cannot collect. The assertion is a valid second read-volume signal and is kept unchanged; only the comment is corrected. Also backtick literal names in the test comments, per .claude/CLAUDE.md. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…comment The guard read `MutationUntouchedParts` from `system.events`, whose counters are process-wide and lifetime-cumulative (`StorageSystemEvents::fillData` reads `ProfileEvents::global_counters`). The test is deliberately parallel-safe, so a concurrent stateless test taking the same shortcut satisfied the assertion for it, and a value produced before the test started satisfied it too. That row is the fixture's anti-vacuity guard, so failing open would let the fixture silently stop exercising the clone-and-keep-block-range path while still reporting OK. Read the counter per table from the `MutatePart` row of `system.part_log` instead, for both arms in one row. The increment happens inside `mutate_task->execute()` under the `ProfileEventsScope` that `MutatePlainMergeTreeTask::executeStep` opens, and `write_part_log` runs after it on the same thread, so it lands in each table's own row. Measured: pointing the cold arm's ALTER at a row that exists takes the value from 1 to 0 and reddens the test, while the old row still reads 1 on that same state once another database has taken the shortcut. The opening comment also attributed the thread-pool scheduling to object storage, which contradicted the comment above the read-volume assertion and did not match the reproduced abort: those frames are the local-fs `pread_threadpool` reader (`AsynchronousReadBufferFromFileDescriptor::asyncReadInto` -> `ThreadPoolImpl::scheduleImpl`), and this fixture's own read runs synchronously. State the scheduling under the default `local_filesystem_read_method`, and explain the object-storage disk separately as what makes the extra access measurable as read volume. Co-Authored-By: Claude Fable 5 <[email protected]>
Internal second-model review (4 review rounds, 3 fix rounds; 10 automated findings, 13 self-review findings). Click to expand.Before opening this PR I put the change through repeated independent review: a cold code review by ❌ Agreed and fixed
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-review-slot-10:20260730-232100 |
|
cc @Avogar @CheSema, could you review this? |
|
Workflow [PR], commit [af3a507] Summary: ✅
AI ReviewSummaryThis PR moves column and secondary-index size I/O out of Final Verdict✅ No new findings. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 33/35 (94.29%) · Uncovered code |
CI finish ledger - af3a507Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260803-180000 |
|
While verifying the ordering hazard around this change I also noticed that |
|
This fix is not in 26.8, and the abort it fixes now shows up there.
Could this PR get |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes a server abort while committing a mutated part. Accounting for a part whose column and secondary index sizes were not computed yet read them from the part storage inside a critical section documented as non-throwing, so a failure to start a thread for that read terminated the server instead of being reported.
Description
MergeTreeData::Transaction::commitupdates the table-level column and secondary index size aggregates from inside aNOEXCEPT_SCOPE, whose catch-all callsstd::terminate. Accounting for a part whose own sizes were not computed yet called the part's size getters, which compute them from the part storage; on packed storage that reads the skip-indices archive. Under the defaultlocal_filesystem_read_method = 'pread_threadpool'that read is submitted to a thread pool, so a failure to schedule it raisesCANNOT_SCHEDULE_TASK. That is not a memory-tracker exception, soLockMemoryExceptionInThreaddoes not hold it back and it escapes the scope.Such a part now invalidates the table aggregates instead of having its sizes read, and the next reader rebuilds them from the active parts. That is already the state before the first size query on the default
columns_and_secondary_indices_sizes_lazy_calculation = 1, and the consumers are advisory statistics. A part whose sizes are known keeps the existing arithmetic. This also drops ahasSecondaryIndexcheck that resolved an index file through the part storage, whilegetSecondaryIndexSizealready returns an empty size for an absent index. Separately, both recompute paths cleared only the two size maps, soprimary_index_sizewas double counted on every rebuild; it is now cleared too. The change is inMergeTreeData, so it covers plain and replicated tables and everyTransaction::commitcall site.Observed on
master(Stress test (amd_msan), commit 27b5773,Received signal 6 (internal) (STID: 3563-40df), CI report).The new test measures the extra storage access itself, through the
MutatePartrow ofsystem.part_log, against a warm control; the scheduling step is established by the stack above rather than by the test. It pins the lazy size calculation. A sweep of related stateless tests found no failure that is unique to the fixed build.