Repository navigation
Iceberg: invalidate metadata cache on ALTER - #104419
PedroTadim merged 8 commits into
Conversation
Closes ClickHouse#104295. `Iceberg::alter` writes a new metadata file but never invalidated the metadata files cache. With `iceberg_metadata_staleness_ms` non-zero, the cache kept serving the pre-`ALTER` schema for the staleness window. The follow-up `INSERT` then ran with two inconsistent views of the table: - `IcebergStorageSink` always reads fresh metadata (`force=true`), so it sees the post-`ALTER` schema (one column). - The upstream `INSERT` pipeline reads through the cached storage metadata, so it produces chunks shaped by the pre-`ALTER` schema (two columns). `DataFileStatistics::getColumnSizes` iterates `column_sizes.size()` (set from the chunk, two entries) and indexes `field_ids` (set from the sink's `current_schema`, one entry), causing an out-of-bounds vector access at `DataFileStatistics.cpp:68` and aborting the server. The fix mirrors the cache invalidation already present in `IcebergStorageSink::initializeMetadata`: after `Iceberg::alter` successfully writes a new metadata file, drop the table's entries from `metadata_cache` so subsequent operations re-read the fresh schema. Reproducer (from the issue, plus an entry in `config.d/iceberg_metadata_log.xml` to surface the staleness path): CREATE TABLE t0 (c0 Int, c1 Int) ENGINE = IcebergLocal('...'); INSERT INTO TABLE t0 (c1, c0) VALUES (1, 1); SET iceberg_metadata_staleness_ms = 60000; ALTER TABLE t0 DROP COLUMN c0; INSERT INTO TABLE t0 (c1) SELECT 2; -- aborted before the fix Added a stateless regression test (`04214_iceberg_alter_drop_column_stale_metadata_cache_104295`) that runs the exact reproducer, verifies the second `INSERT` succeeds, and checks the resulting rows.
|
Pre-PR validation gate (TASK.md Phase 4):
Session: |
|
cc @alesapin @Michicosun @scanhex12 @kssenii — could you review? Iceberg manager + comp-datalake experts. Also cc @PedroTadim per the directive on #104295. |
|
Workflow [PR], commit [c2f8daf] Summary: ❌
AI ReviewSummaryThe PR fixes stale Iceberg metadata cache state after Final VerdictStatus: ✅ Approve |
| if (persistent_table_components.metadata_cache) | ||
| { | ||
| persistent_table_components.metadata_cache->remove(persistent_table_components.table_path); | ||
| if (persistent_table_components.table_uuid) |
There was a problem hiding this comment.
| if (persistent_table_components.table_uuid) | |
| if (persistent_table_components.table_uuid.has_value()) |
There was a problem hiding this comment.
Done — applied in fix-up commit 0c911724100. Switched to has_value().
| if (persistent_table_components.metadata_cache) | ||
| { | ||
| persistent_table_components.metadata_cache->remove(persistent_table_components.table_path); | ||
| if (persistent_table_components.table_uuid) | ||
| persistent_table_components.metadata_cache->remove(*persistent_table_components.table_uuid); |
There was a problem hiding this comment.
I see the same code in IcebergWrites.cpp::1088-1095, can we unify it into a common function?
There was a problem hiding this comment.
Done — applied in fix-up commit 0c911724100. Added PersistentTableComponents::invalidateMetadataCache (inline in PersistentTableComponents.h) and routed both Iceberg::alter (Mutations.cpp) and IcebergStorageSink::initializeMetadata (IcebergWrites.cpp) through it. Pure refactor — observable behavior is unchanged.
Diff: +25/-20 across 3 files (PersistentTableComponents.h +17, Mutations.cpp -8, IcebergWrites.cpp -3).
Verification: regression test 04214_iceberg_alter_drop_column_stale_metadata_cache_104295 passes 10/10 with full randomization and 3/3 without.
…ents Address @kssenii's review feedback on PR ClickHouse#104419: 1. Use `has_value()` for the `table_uuid` `optional` check. 2. Unify the duplicated metadata-cache-invalidation block between `Iceberg::alter` (`Mutations.cpp`) and `IcebergStorageSink::initializeMetadata` (`IcebergWrites.cpp:1088-1095`) into a single helper. Adds `PersistentTableComponents::invalidateMetadataCache` and routes both call sites through it. Pure refactor — behavior is unchanged. Regression test `04214_iceberg_alter_drop_column_stale_metadata_cache_104295` passes 10/10 with full randomization and 3/3 without.
The test uses a fully randomized table name (`t_${CLICKHOUSE_DATABASE}_${RANDOM}`)
and a `TABLE_PATH` scoped to that same name under `USER_FILES_PATH`, so it
shares no state with other parallel tests. Per `CLAUDE.md`: "do not add no-*
tags unless strictly necessarily." Confirmed locally — runs in the parallel
queue alongside other Iceberg tests without interference.
Addresses inline review feedback from `clickhouse-gh[bot]` and @kssenii on
PR ClickHouse#104419.
|
The |
…etadata-cache-104295
|
@alexey-milovidov done — merged Diff vs master is unchanged (5 files, the same set @kssenii approved): Session: cron:clickhouse-ci-task-worker:20260512-174500 |
`Iceberg::alter` used `while (i++ < MAX_TRANSACTION_RETRIES)` with the post-increment in the loop condition. Two consequences: 1. On retry exhaustion, `i` becomes `MAX_TRANSACTION_RETRIES + 1`, so the subsequent `if (i == MAX_TRANSACTION_RETRIES)` check never fires and `alter` silently returns as if the metadata write had succeeded — even though every retry actually failed. 2. When the final iteration `i == MAX_TRANSACTION_RETRIES` succeeds, the same check incorrectly fires after the loop, raising `LIMIT_EXCEEDED` despite a successful write. Replace the post-increment-in-condition with an explicit `bool succeeded` flag and `++i` at the end of the loop body. This matches the existing pattern in `IcebergStorageSink::finalizeBuffers` (`IcebergWrites.cpp`). Spotted by `clickhouse-gh[bot]` review on PR ClickHouse#104419. Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
Pushed c8267c5c7e6 addressing Bug.
Fix. Replaced with an explicit Verification. Existing regression test cc @alesapin (Iceberg manager) — diff vs master is now 6 files; the original 5 @kssenii approved plus this one-method tweak in Session: |
|
@groeneai what is the AST fuzzer (amd_tsan) failure |
|
@PedroTadim — investigated; this is unrelated to PR #104419. Cross-PR data (CIDB, 90-day window) for
Two hits across 90 days, two unrelated PRs, two different sanitizer variants, 56 days apart. Zero master / release-branch failures in the same window. Rare chronic trunk bug. Stack trace points squarely at the KILL QUERY interpreter (same line on both hits):
PullingPipelineExecutor executor(io.pipeline);
while (res.empty() && executor.pull(res));
while (executor.pull(tmp_block));
if (!tmp_block.empty())
throw Exception(LOGICAL_ERROR, "Expected one block from input stream");The 2026-03-17 hit (PR #98856) preserved the fuzzed query: @alexey-milovidov already touched this exact function in #99881 ("Fix exception in PR #104419 diff vs origin/master (still the 5 files @kssenii approved + the 1-method Mutations.cpp tweak): No Proposal: treat the AST fuzzer (amd_tsan) check on this PR as a flake from this STID and don't gate on it. I'll file a separate follow-up to investigate fixing Session: cron:clickhouse-ci-task-worker:20260513-091500 |
|
@groeneai, bugfix validation check Bugfix validation (functional tests) (pull_request) failed, meaning that this test, added in this PR, does not reproduce the bug, please check the initial issue's repro again. |
The reproducer in `04214_iceberg_alter_drop_column_stale_metadata_cache_104295`
runs the buggy `INSERT` through `${CLICKHOUSE_CLIENT}`, which means the
`SIGABRT` in `DataFileStatistics::getColumnSizes` lands inside the
long-running test server. The functional-tests runner's hung-check then
detects "server is not responding" and terminates the runner before any
test result is written to `test_result.txt`. `FTResultsProcessor` only
treats the exact strings `Server died, terminating all processes` or
`Server does not respond to health check` as `server_died`; the runner's
actual `Stopping tests, terminating all processes...` message is not
matched, so the run falls into the `not success_finish` branch with
status `ERROR` and an empty `test_results` list. The `Bugfix validation`
inversion loop only flips `FAIL` -> `OK` (it iterates `test_result.results`),
so with an empty list `has_failure` stays `false` and the framework
reports `Failed to reproduce the bug` even though the bug did fire and
left a `core.QueryPipelineEx.*` core dump.
Switch the reproducer to `clickhouse local` (multi-query) so the abort
is contained to a single short-lived subprocess. The test runner then
observes a normal non-zero exit code (134) and an empty stdout; the
diff against `.reference` produces a regular `FAIL` for this test, which
`Bugfix validation` then correctly inverts to `OK`.
Verified locally:
- Binary without the fix (`Iceberg::alter` does not invalidate the cache):
test FAILs with exit 134 and `libc++ Hardening assertion __n < size()`
in stderr, runner records a normal `FAIL`.
- Binary with the fix (`Iceberg::alter` invalidates the cache): 10/10
passes (`SELECT c1 FROM t0 ORDER BY c1` returns `1\n2`).
CI report for the failing bugfix-validation run on `c8267c5`:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104419&sha=c8267c5c7e69922ec96a5837cc51e5bc710bf9cb&name_0=PR&name_1=Bugfix+validation+%28functional+tests%29
|
@kssenii thanks for catching this. Pushed Root cause of The bug is reproduced correctly (the master-HEAD binary did
elif not s.success_finish:
state = Result.Status.ERROR
info = "The test runner was terminated unexpectedly"The Fix Run the reproducer in Verified locally:
Session: cron:clickhouse-ci-task-worker:20260513-224500 |
LLVM Coverage Report
Changed lines: 81.82% (18/22) · Uncovered code |
|
The |
567fd39
Closes #104295.
Iceberg::alterwrites a new metadata file but never invalidated the metadata files cache. Withiceberg_metadata_staleness_msnon-zero, the cache kept serving the pre-ALTERschema for the staleness window. The follow-upINSERTthen ran with two inconsistent views of the table:IcebergStorageSinkalways reads fresh metadata (force=true), so it sees the post-ALTERschema (one column).INSERTpipeline reads through the cached storage metadata, so it produces chunks shaped by the pre-ALTERschema (two columns).DataFileStatistics::getColumnSizesiteratescolumn_sizes.size()(set from the chunk, two entries) and indexesfield_ids(set from the sink'scurrent_schema, one entry), causing an out-of-bounds vector access atDataFileStatistics.cpp:68and aborting the server.The fix mirrors the cache invalidation already present in
IcebergStorageSink::initializeMetadata: afterIceberg::altersuccessfully writes a new metadata file, drop the table's entries frommetadata_cacheso subsequent operations re-read the fresh schema.Reproducer (from the issue, plus an
iceberg_metadata_logconfig entry to surface the staleness path):Stack trace from the abort (matches the issue):
Validated locally with the debug build:
04033,04042,04043,04061) keep passing.Added a stateless regression test (
04214_iceberg_alter_drop_column_stale_metadata_cache_104295) that runs the exact reproducer.A few related write paths (
Iceberg::mutate::writeMetadataFiles,Iceberg::Compaction::writeMetadataFiles) also write new metadata files without invalidating the cache. Those don't change schema, so they don't trigger the column-count mismatch path, but they could still leave stale snapshot/manifest information cached. Left as a follow-up to keep this PR focused on the crash fix.Session: cron:clickhouse-ci-task-worker:20260508-191500
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a server abort in
IcebergLocal/IcebergS3writes whenALTER TABLE ... DROP COLUMNis followed by anINSERTwithiceberg_metadata_staleness_msgreater than zero.ALTERnow invalidates the local Iceberg metadata files cache so subsequent reads and writes see the new schema.Documentation entry for user-facing changes
Version info
26.5.1.642