Skip to content

Cherry pick #102033 to 26.6: Fix LOGICAL_ERROR crash in IcebergMetadata::iterate when datalake_table_state is missing - #114386

Merged
robot-ch-test-poll merged 21 commits into
backport/26.6/102033from
cherrypick/26.6/102033
Aug 12, 2026
Merged

robot-ch-test-poll merged 21 commits into
backport/26.6/102033from
cherrypick/26.6/102033

Conversation

@robot-clickhouse-ci-2

Copy link
Copy Markdown
Contributor

Original pull-request #102033

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 20 commits May 18, 2026 14:49
… isDataSortedBySortingKey()

Instead of adding fallback hacks inside IcebergMetadata::iterate() and
IcebergMetadata::isDataSortedBySortingKey(), fix the callers to ensure
datalake_table_state is present in the storage metadata before calling
these functions.

The state is normally set by updateExternalDynamicMetadataIfExists()
during query analysis, but can be missing due to race conditions between
concurrent queries (TOCTOU between setInMemoryMetadata and
getInMemoryMetadataPtr), or when called from code paths that bypass
the analyzer/interpreter (schema inference, cluster functions).

Two call sites fixed:
- createFileIterator(): the funnel for all iterate() calls — when the
  metadata lacks datalake_table_state, obtain it from the configuration
  and create a patched metadata copy before calling iterate().
- requestReadingInOrder(): entry point for isDataSortedBySortingKey() —
  same approach.

This preserves the LOGICAL_ERROR assertions inside iterate() and
isDataSortedBySortingKey() as safety nets for any unknown future code
paths, while fixing the actual root cause at the call sites.

Fixes STID 2606-4a47 (43 hits in 30 days, 3 on master).
Issue #93278 (reopened): `IcebergMetadata::iterate` and
`IcebergMetadata::isDataSortedBySortingKey` throw `LOGICAL_ERROR`
"Can't extract iceberg table state from storage snapshot" when
`datalake_table_state` is missing from the storage metadata snapshot
(STID 2606-4a47, 11 master + 45 PR hits over 30 days).

The PR's fix in `StorageObjectStorageSource::createFileIterator` and
`ReadFromObjectStorageStep::requestReadingInOrder` re-fetches the
snapshot from the configuration when it is missing. PR #101577 already
covered the materialized-view-target path
(`04077_iceberg_mv_select_crash`); this new test exercises the
concurrent-normal-SELECT path that the same fix has to keep stable.

The test runs many parallel `SELECT`s (both plain and `ORDER BY c0`)
against an `IcebergLocal` table while a writer thread produces new
snapshots, then asserts that the
"Can't extract iceberg table state" `LOGICAL_ERROR` never appears in
captured stderr and that the table remains readable.
Previously each consumer of the storage metadata in the object-storage read
step re-fetched the datalake table state independently when it was missing:
the file iterator (via createFileIterator), the sorting-key check (via
requestReadingInOrder/isDataSortedBySortingKey), and the Parquet column mapper
(via getColumnMapperForCurrentSchema). Under concurrent Iceberg commits these
independent fetches can resolve to different snapshots, so the file iterator
and the column mapper may pin divergent field-id mappings.

Compute the patched StorageMetadataPtr once at the read-step boundary
(getMetadataWithDatalakeState, cached) and reuse it for every consumer so they
all observe the same table state. Also harden
04141_iceberg_concurrent_no_logical_error.sh to capture each background PID and
wait on it explicitly, so a crashed or errored concurrent client surfaces a
non-zero exit instead of being silently swallowed by a bare wait.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The probabilistic concurrent test 04141 cannot guarantee a reader observes
metadata without datalake_table_state, so a build with the original bug can
pass. Add a REGULAR failpoint datalake_simulate_missing_table_state that, in
StorageObjectStorage::read, strips datalake_table_state from the snapshot
reaching the read step. A pre-fix build then throws the LOGICAL_ERROR
"Can't extract iceberg table state" deterministically from both the iterate
and the isDataSortedBySortingKey paths; the post-fix read step re-fetches the
state once for every pipeline consumer, so both queries succeed.

New test 04305_iceberg_missing_table_state_failpoint.sh enables the failpoint,
runs count() (iterate) and ORDER BY (read-in-order), then disables it.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Move the datalake_table_state fallback up into read(), before
prepareReadingFromFormat, and rebuild metadata from that state via
buildStorageMetadataFromState when schema reload is enabled. This keeps
requested columns, the field-id mapping used to list/read files, and the
read-in-order sorting key all derived from the same Iceberg snapshot.

Previously the fallback lived in ReadFromObjectStorageStep and re-fetched
the state per consumer, after read_from_format_info had already been built
from the original snapshot, so a concurrent schema or sort-order change
between analysis and read could mix columns from one table state with the
field-id mapping or sorting key from another. The step now uses the single
pinned snapshot directly.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The fallback added in 7612950 calls `getTableStateSnapshot` early in
`StorageObjectStorage::read`, but on cluster table function workers and
other paths that bypass the analyzer the underlying datalake configuration
has never had `update`/`updateExternalDynamicMetadataIfExists` run on it
yet. `getTableStateSnapshot` then hits `assertInitialized` and throws
`Metadata is not initialized` instead of the expected
`PATH_ACCESS_DENIED` (or normal read result), regressing
`test_local_table_safety`, `test_cluster_table_function*`, and stateless
flaky-check / bugfix-validation runs on PR #102033 head 2d39c85
(STID 2377-2862).

Initialize the configuration the same way the surrounding analyzer paths
do (`lazyInitializeIfNeeded` for table functions, `update` for ENGINE
tables) before invoking `getTableStateSnapshot`, so the fallback works
for all callers and any underlying error such as `PATH_ACCESS_DENIED`
keeps propagating with its original code.
The flaky-check (50x) on amd_debug/amd_msan/amd_tsan caught
`Code: 117 INCORRECT_DATA: JSON object/array should start with corresponding opening bracket`
from a `SELECT c0 FROM ... ORDER BY c0 LIMIT 5` race against a concurrent
INSERT-side iceberg manifest write. That is a separate (manifest-read mid-write)
race, not the `LOGICAL_ERROR: Can't extract iceberg table state` class this
test exists to gate. The previous `grep -iE "error|exception"` final guard
plus the `status=0` exit-on-any-non-zero check folded both classes together
and turned this test into a generic flake detector.

This test only exists to catch the specific LOGICAL_ERROR thrown by
`IcebergMetadata::iterate()` and `isDataSortedBySortingKey()` when
`datalake_table_state` is missing under concurrent commits. The
deterministic counterpart `04305_iceberg_missing_table_state.sh` already
covers the LOGICAL_ERROR path via `datalake_simulate_missing_table_state`
failpoint; 04141 is the noisy concurrent-workload counterpart that should
only fail when the LOGICAL_ERROR resurfaces.

Tighten the LOG_FILE check to a literal `Can't extract iceberg table state`
match. Stop exiting on any non-zero background-PID status; transient
non-LOGICAL_ERROR failures (e.g. INCORRECT_DATA on manifest reads,
randomized-setting validation errors) are not in scope.

PedroTadim directive on PR #102033 2026-06-09:
#102033 (comment)
The test exceeded the 180s test-runtime limit in the sync branch on a debug
build with randomized settings and --max_threads 1 (reported by PedroTadim on
PR #102033).

Root cause: the test spawned a fresh clickhouse-client process per query
(12 readers x 10 iterations x 2 queries + 24 writer inserts = ~264 spawns).
On a contended debug runner each spawn re-parses ~200 randomized settings,
which dominated the runtime.

Fix: each reader/writer now runs its whole loop inside a single client
connection (7 spawns total instead of ~264). Every statement still
re-resolves the Iceberg table-state snapshot independently, so the
concurrent-commit TOCTOU race the test exists to detect is fully preserved
(verified: a later SELECT in the same connection sees rows the writer
committed after an earlier SELECT, and a live run shows reader ORDER BY
queries overlapping with writer commits). Reader/writer counts were also
right-sized (6 readers x 15 read-pairs, 20 writer commits) so the workload
keeps the readers active across the writer's commit window without the
hundreds of expensive read-in-order queries that each trigger a full Iceberg
manifest re-parse via totalRows().

Also redirect stdin from /dev/null on the client invocations: a backgrounded
clickhouse-client running INSERT ... VALUES reads stdin for trailing insert
data, so an inherited non-EOF stdin would block the insert.

The LOGICAL_ERROR guard string is unchanged, so the test still fails on the
specific "Can't extract iceberg table state" error and ignores unrelated
transient errors. Deterministic coverage stays in 04305_iceberg_missing_table_state.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The iterate-path probe used SELECT count(), which the trivial-count
optimization answers from IcebergMetadata::totalRows before
StorageObjectStorage::read builds a read step, so the failpoint in read
was never reached. Replace it with SELECT sum(c0) so the read pipeline
runs, and keep the separate ORDER BY query that deterministically hits
isDataSortedBySortingKey under the failpoint.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Covers issue #107334: reading a persistent DeltaLake table whose first
access is through merge() reached DeltaLakeMetadataDeltaKernel::iterate()
with a storage snapshot that had no pinned datalake_table_state and aborted
the server with LOGICAL_ERROR 'No version found in table state snapshot'.
merge() builds the child storage snapshot without
updateExternalDynamicMetadataIfExists(), so the analyzer never pins the
state; the fix in this PR pins it in StorageObjectStorage::read() instead,
which covers all read-pipeline consumers (merge, cluster) engine-agnostically.

The test uses merge() as the sole first access and a non-trivial subcolumn
read (count() is bypassed by the trivial-count optimization and would not
reach iterate()).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The DeltaLakeLocal engine comes from delta-kernel-rs (Rust), which is not
built under Memory Sanitizer, so the test failed the amd_msan flaky check with
Code: 56 UNKNOWN_STORAGE. The other sanitizer flaky checks (debug, tsan, asan)
already pass. Mirrors the existing no-msan tag on sibling DeltaLakeLocal tests
(04050, 03635).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The regression test reproduced the missing-datalake-state bug via a real
merge() read, which genuinely aborts the unfixed master binary during Bugfix
validation (abort_on_logical_error is forced in debug/sanitizer builds). A
server abort is reported as a runner-level ERROR that the bugfix inverter
cannot flip to a reproduced-bug verdict, so the check went red.

Switch to the datalake_simulate_missing_table_state failpoint plus a direct
subcolumn read, mirroring 04305_iceberg_missing_table_state. The failpoint
strips datalake_table_state from the snapshot reaching the read step, so the
read deterministically hits DeltaLakeMetadataDeltaKernel::iterate() without a
pinned version. The failpoint exists only in this build, so on the master
binary SYSTEM ENABLE FAILPOINT fails cleanly and the read is unaffected, giving
Bugfix validation a clean invertible result instead of a crash.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The wait loop used `wait "$pid" || true`, so a background reader or
writer that exited non-zero was swallowed and the only remaining gate was
the exact grep for "Can't extract iceberg table state". On debug and
sanitizer builds the LOGICAL_ERROR aborts the server, so the failing client
exits non-zero but that string never reaches the client log (it goes to the
server log), meaning the grep alone can miss the regression on exactly the
builds where abort_on_logical_error is forced on.

Capture each background PID's exit status and fail the test on any non-zero
exit, keeping the grep as a descriptive second gate. A single writer and
trivial reads over a pinned snapshot do not error under CI-randomized
settings, so a non-zero exit is signal, not noise.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…lient exit

The flaky check failed because the previous gate treated any non-zero
background client exit as a failure. Under concurrent metadata commits a
reader can hit a benign, catchable INCORRECT_DATA error reading a partially
written metadata.json, which is not the regression under test and does not
crash the server.

Detect the guarded 'Can't extract iceberg table state' LOGICAL_ERROR by its
two real manifestations instead: a server abort on debug/sanitizer builds
(where abort_on_logical_error is forced on), caught by a liveness probe, and
the exact error string in the client log on release builds. This still fails
on the real regression (verified in both directions) while ignoring unrelated
concurrent-read noise.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…s cap

Under msan the concurrent read/write loop that drives the TOCTOU race can run
past the 180s per-run limit enforced only in flaky-check mode, so a single slow
iteration was marked FAIL (24 OK, 1 FAIL @ 212.96s). The loop volume is the race
budget; shrinking it would weaken what the test guards. The long tag exempts the
180s flaky-check cap and throttles the flaky-check repeat count, which is the
idiomatic handling for a correct-but-heavy test.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…datalake-state

Fix LOGICAL_ERROR crash in IcebergMetadata::iterate when datalake_table_state is missing
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Aug 11, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! labels Aug 11, 2026
Resolve conflict in src/Common/FailPoint.cpp: keep the 26.6 failpoint list
and add the new `datalake_simulate_missing_table_state` failpoint from #102033.

Related: #114386
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants