Skip to content

Use less s3 requests to get metadata file - #126267

Merged
scanhex12 merged 19 commits into
masterfrom
iceberg-cache-parsed-metadata-json
Oct 22, 2026
Merged

scanhex12 merged 19 commits into
masterfrom
iceberg-cache-parsed-metadata-json

Conversation

@scanhex12

@scanhex12 scanhex12 commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Reduce the number of S3 requests when reading Iceberg tables by reusing metadata lookups and caching parsed metadata.json files: one GET and one LIST instead of three GETs and two LISTs.


Workflow [PR]
Sync PR [sync-upstream/pr/126267]

Version info

  • Merged into: 26.11.1.544-master (included in 26.11 and later)

@clickhouse-gh

clickhouse-gh Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [24821f6]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_debug, sequential) FAIL
05339_iceberg_top_k_query_condition_cache FAIL cidb
Stateless tests (amd_debug, distributed plan, s3 storage, sequential) FAIL
05339_iceberg_top_k_query_condition_cache FAIL cidb
Stateless tests (arm_binary, sequential) FAIL
05339_iceberg_top_k_query_condition_cache FAIL cidb
Stateless tests (arm_asan_ubsan, azure, sequential, 2/2) FAIL
05339_iceberg_top_k_query_condition_cache FAIL cidb
Stateless tests (arm_binary, s3 storage, DBReplicated, sequential) FAIL
05339_iceberg_top_k_query_condition_cache FAIL cidb
Stateless tests (arm_binary, ParallelReplicas, s3 storage, sequential) FAIL
05339_iceberg_top_k_query_condition_cache FAIL cidb
Stateless tests (arm_binary, AsyncInsert, s3 storage, sequential) FAIL
05339_iceberg_top_k_query_condition_cache FAIL cidb
01168_mutations_isolation_3 NOT_FAILED cidb

AI Review

Summary

PR #126267, reviewed at 24821f6c4fc8aeff5a1fbf9694df2c822332ab2d, reduces Iceberg metadata requests by reusing the initialization lookup, caching parsed JSON, and counting rows from the query's pinned state. The earlier correctness findings are addressed. No new correctness findings warrant inline comments.

PR Metadata

Improvement is appropriate and requires a changelog entry. The current entry gives request counts but does not identify Iceberg and describes an intended change. Suggested replacement:

Reduce S3 requests when reading Iceberg tables by reusing metadata lookups and caching parsed metadata.json files.

Missing context / blind spots
  • ⚠️ The latest head's CI was still in progress. No local ClickHouse binary or build directory was available, so regression tests were reviewed but not executed locally. Completed functional-test results for this head would close this validation gap.
Findings

💡 Nits

  • PR description: make the required changelog entry user-facing with the replacement above. This is nonblocking.
Final Verdict

Status: ✅ Approve. No blocking changes requested; the changelog wording is a nonblocking improvement.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 16, 2026
…d-metadata-json

# Conflicts:
#	src/Storages/ObjectStorage/DataLakes/Iceberg/IcebergMetadata.cpp
@clickhouse-gh clickhouse-gh Bot added the comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. label Oct 16, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 24821f6c4 with master c743fe67b (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

307 object files changed (+358.75 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/ObjectStorage/DataLakes/Iceberg/IcebergMetadata.cpp.o 1.28 MiB 1.33 MiB +49.94 KiB (+3.80%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

717 translation units recompiled, 6214 s compile time in total, 717 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×1.06 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +238.1 s (+4%) in total before that adjustment.

Job report

scanhex12 and others added 2 commits October 19, 2026 09:16
The metadata found when `IcebergMetadata` was constructed was handed to the
first later `getRelevantState` call, however many queries later, so a
`SELECT` after `ALTER` could read the old schema. Use it only within the
query that constructed the object, and not when an explicit metadata file
path is pinned.

The cache now shares one parsed `Poco::JSON::Object` between callers, but
write paths pass it to `MetadataGenerator`, which mutates it in place. That
rewrote the cached entry of the previous metadata file and let concurrent
writers modify the same object. Deep-copy the object in the write paths.

#126267

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… ids

`IcebergMetadata::update` is called by `configuration->update` at the start
of every query on an Iceberg table, so it is the natural place to drop the
metadata remembered at construction. `setExplicitMetadataFilePath` drops it
as well, since a pinned file makes it irrelevant.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
scanhex12 and others added 3 commits October 19, 2026 10:46
`IcebergMetadata` remembered the metadata file found at construction for a
single `getRelevantState` call, so the next call in the same query, e.g.
from the file iterator, listed `metadata/` again. Keep it for all calls
until the next `IcebergMetadata::update`, which now refreshes it once per
query instead of only dropping it.

Add a test that a query reading an Iceberg table issues exactly one LIST,
and one GET of `metadata.json` (none for a table, whose metadata is cached).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A rejected `ALTER TABLE ... MODIFY COLUMN` must not change the cached
metadata, and the first `SELECT` from a table must see rows appended by
another writer after its `CREATE TABLE`. Both tests fail without
`deepCopy` in the write paths and without the refresh in
`IcebergMetadata::update`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@SmitaRKulkarni SmitaRKulkarni self-assigned this Oct 19, 2026

@SmitaRKulkarni SmitaRKulkarni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest all LGTM

@scanhex12
scanhex12 marked this pull request as draft October 19, 2026 12:29
scanhex12 and others added 3 commits October 19, 2026 19:19
… newer one

`IcebergMetadata::update` resolves the latest metadata file outside
`cached_latest_metadata_mutex` and publishes the result to a slot shared by
all queries. A query whose lookup started earlier could publish after a query
whose lookup started later, so the later query read the older metadata from
the slot and missed already committed rows, even with
`iceberg_metadata_staleness_ms = 0`.

Each lookup now takes an increasing id before it starts, and its result is
published only if no lookup that started later has published already.
`setExplicitMetadataFilePath` takes an id as well, so a lookup that was in
flight before the path was pinned cannot publish over it. Comparing lookup
ids rather than metadata versions keeps working when a table is recreated at
the same path and its versions start over.

Add pausable failpoints `iceberg_metadata_update_pause_before_publish` and
`iceberg_metadata_update_pause_after_publish` to reproduce the interleaving.

#126267

Co-Authored-By: Claude Opus 5.5 <[email protected]>
An older query is paused after resolving the metadata file and before
publishing it, another writer appends rows, and a newer query is paused right
after publishing its fresher result. The older query then resumes, and the
newer query must still see the appended rows.

#126267

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`Iceberg::deepCopy` serialized the whole object to a string and parsed it
back, which is slower than parsing the original `metadata.json`. Copy the
tree recursively instead: objects are copy-constructed (keeping key order and
flags) with nested objects and arrays replaced by their copies, arrays are
rebuilt, and scalar values are copied as is.

On a 268 KB `metadata.json` (300 snapshots, 5 schemas with nested structs)
this takes 1.41 ms instead of 8.29 ms; parsing the same file takes 4.03 ms.

#126267

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@scanhex12
scanhex12 marked this pull request as ready for review October 19, 2026 19:56
…lookup ids

`IcebergMetadata::update` now publishes the metadata file it found only if
its version is not lower than the version already cached, so a lookup that
started earlier and finished later cannot replace a newer result. This
removes `last_metadata_lookup_id` and `cached_latest_metadata_lookup_id`.

When the metadata file path is pinned (`iceberg_metadata_file_path` or
`setExplicitMetadataFilePath`), `getRelevantState` does not use the cached
result: resolving a pinned path does not list `metadata/`. A lookup that
started before the path was pinned therefore cannot affect it, and
`setExplicitMetadataFilePath` no longer resets the cached result.

#126267

Co-Authored-By: Claude Opus 5.5 <[email protected]>
scanhex12 and others added 4 commits October 21, 2026 10:47
Comparing metadata file versions in `IcebergMetadata::update` overrode the
resolver's selection order: with
`iceberg_recent_metadata_file_by_last_updated_ms_field = 1` the resolver picks
the file with the largest `last-updated-ms`, and with `iceberg_use_version_hint`
it follows the hint, so the selected file can have a lower version than the
cached one. Such a result was discarded and the table stayed on the old
metadata. `update` now publishes a result only if its lookup started after the
lookup of the cached result, which does not depend on how the resolver ranks
metadata files.

Add a test where a later lookup in timestamp-selection mode picks a
lower-numbered metadata file.

#126267 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`IcebergMetadata::update` looked up the latest metadata file and published it
for later `getRelevantState` calls. The `IcebergMetadata` of a table is shared
by concurrent queries, so a query could read a result published by another one,
and ordering the results by metadata version or by lookup start only narrowed
this down.

`update` now only drops the result of the lookup done at construction, and
`getRelevantState` either reuses that result or does its own lookup. A query
on a table calls `update` first and then pins the state it looked up into its
storage snapshot, which is where the rest of the query reads it from. A table
function creates its `IcebergMetadata` per query and never calls `update`, so
all its calls reuse the lookup done at construction.

The race reproduced by `05410_iceberg_older_metadata_refresh_does_not_replace_newer`
cannot happen anymore, so remove the test and its failpoints.

#126267 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The trivial count optimization called `StorageObjectStorage::totalRows`, which
updated the configuration and looked up the latest metadata file again, so
`SELECT count()` from an Iceberg table listed `metadata/` twice: once to pin
the query's state and once to count. The count could also be computed on
another state than the one the query was analyzed on.

Add `IStorage::totalRowsInSnapshot`, which receives the query's storage
snapshot and by default calls `totalRows`. The planner and the old analyzer
call it for the trivial count. `StorageObjectStorage` and
`StorageObjectStorageCluster` pass the pinned data lake state to
`IDataLakeMetadata::totalRows`, and `IcebergMetadata` counts the rows of that
state without looking up the metadata file again.

Extend `05407_iceberg_metadata_s3_requests` with `SELECT count()` from a table.

#126267

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@scanhex12
scanhex12 enabled auto-merge October 21, 2026 12:43
@scanhex12
scanhex12 added this pull request to the merge queue Oct 22, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Oct 22, 2026
…d-metadata-json

# Conflicts:
#	src/Storages/ObjectStorage/DataLakes/Iceberg/IcebergMetadata.cpp
@scanhex12

Copy link
Copy Markdown
Member Author

05339_iceberg_top_k_query_condition_cache is broken everywhere

@scanhex12
scanhex12 enabled auto-merge October 22, 2026 13:12
@scanhex12
scanhex12 disabled auto-merge October 22, 2026 13:13
@scanhex12
scanhex12 enabled auto-merge October 22, 2026 13:13
@scanhex12
scanhex12 disabled auto-merge October 22, 2026 13:14
@scanhex12
scanhex12 added this pull request to the merge queue Oct 22, 2026
Merged via the queue into master with commit c1146e5 Oct 22, 2026
172 of 180 checks passed
@scanhex12
scanhex12 deleted the iceberg-cache-parsed-metadata-json branch October 22, 2026 14:27
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 22, 2026
zvonand added a commit to Altinity/ClickHouse that referenced this pull request Nov 19, 2026
Antalya 26.8 Backport of ClickHouse#126267 - Use less s3 requests to get metadata file
@clickhouse-gh clickhouse-gh Bot added comp-iceberg Apache Iceberg tables: reading and writing Iceberg metadata (snapshots, manifests, schema evoluti... and removed comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. labels Nov 22, 2026
VighneshPath pushed a commit to VighneshPath/ClickHouse that referenced this pull request Nov 23, 2026
…rsed-metadata-json

Use less s3 requests to get metadata file

Conflicts committed as-is; the next commit resolves them:
- src/Planner/PlannerJoinTree.cpp (both modified)
- src/Storages/ObjectStorage/DataLakes/DeltaLakeMetadataDeltaKernel.h (both modified)
- src/Storages/ObjectStorage/DataLakes/Iceberg/IcebergMetadata.cpp (both modified)
- src/Storages/ObjectStorage/DataLakes/Iceberg/IcebergMetadata.h (both modified)
- src/Storages/ObjectStorage/DataLakes/Iceberg/IcebergMetadataFilesCache.h (both modified)
- src/Storages/ObjectStorage/DataLakes/Iceberg/IcebergWrites.cpp (both modified)
- src/Storages/ObjectStorage/DataLakes/Iceberg/MetadataGenerator.cpp (both modified)
- src/Storages/ObjectStorage/DataLakes/Iceberg/Mutations.cpp (both modified)
- src/Storages/ObjectStorage/DataLakes/Iceberg/Utils.cpp (both modified)
- src/Storages/ObjectStorage/StorageObjectStorageCluster.cpp (both modified)
VighneshPath pushed a commit to VighneshPath/ClickHouse that referenced this pull request Nov 23, 2026
Resolved 10 conflicts by keeping the 26.8 code and applying the PR's metadata-lookup and parsed-JSON caching; dropped the `ignore_metadata_pointer_overrides`, `explicit_metadata_file_path` and catalog-lookup code and the `createInitial` declaration, which 26.8 lacks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-iceberg Apache Iceberg tables: reading and writing Iceberg metadata (snapshots, manifests, schema evoluti... pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants