Skip to content

Add paimon metadata cache - #104657

Merged
hanfei1991 merged 77 commits into
ClickHouse:masterfrom
Binnn-MX:add_paimon_metadata_cache
Jul 16, 2026
Merged

hanfei1991 merged 77 commits into
ClickHouse:masterfrom
Binnn-MX:add_paimon_metadata_cache

Conversation

@Binnn-MX

@Binnn-MX Binnn-MX commented May 12, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Experimental Feature

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

Add an in-memory SLRU cache for deserialized Paimon metadata files (manifest lists and manifests). When enabled via the use_paimon_metadata_files_cache setting, repeated queries against the same Paimon table skip re-downloading and re-parsing metadata from object storage.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Motivation

Every Paimon query must read and deserialize manifest-list and manifest files from object storage (S3/Azure/HDFS/Local) to locate data files. For tables with many snapshots or partitions these metadata files can be large, and re-parsing them on every query adds noticeable overhead. The metadata cache keeps the deserialized results in memory so that subsequent queries can reuse them without any I/O.

Settings

Query-level setting:

Setting Type Default Description
use_paimon_metadata_files_cache Bool false Enable the in-memory Paimon metadata files cache. When enabled, the filesystem cache is automatically disabled for Paimon metadata reads to avoid double caching.

Server-level settings (config.xml):

Setting Type Default Description
paimon_metadata_files_cache_policy String SLRU Cache eviction policy.
paimon_metadata_files_cache_size UInt64 1073741824 (1 GiB) Maximum cache size in bytes.
paimon_metadata_files_cache_max_entries UInt64 1000 Maximum number of cached entries.
paimon_metadata_files_cache_size_ratio Double 0.5 SLRU protected-to-total size ratio.

Observability:

Type Name Description
ProfileEvent PaimonMetadataFilesCacheHits Number of cache hits.
ProfileEvent PaimonMetadataFilesCacheMisses Number of cache misses (loaded from object storage).
ProfileEvent PaimonMetadataFilesCacheWeightLost Bytes evicted from cache.
CurrentMetric PaimonMetadataFilesCacheBytes Current cache memory usage in bytes.
CurrentMetric PaimonMetadataFilesCacheFiles Current number of cached entries.

Example use

-- Enable the cache for the current session
SET use_paimon_metadata_files_cache = 1;

-- First query: cache miss, metadata loaded from object storage
SELECT count() FROM paimon_table;

-- Second query: cache hit, no I/O for metadata
SELECT count() FROM paimon_table;

-- Verify cache activity
SELECT event, value FROM system.events WHERE event LIKE 'PaimonMetadataFilesCache%';
SELECT metric, value FROM system.metrics WHERE metric LIKE 'PaimonMetadataFilesCache%';

Version info

  • Merged into: 26.7.1.1014 (included in 26.7 and later)

@clickhouse-gh

clickhouse-gh Bot commented May 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [82792c9]

Summary: ✅


AI Review

Summary

This PR adds an in-memory cache for deserialized Paimon manifest metadata, wires it into server/query settings, and adds a schema-0 identity check meant to fail closed when the upstream Paimon table is dropped and recreated in place. The cache plumbing and most of the race fixes look reasonable, but one recreate window is still open in the incremental/targeted-snapshot paths, and that window can silently advance the Keeper watermark instead of throwing.

Findings

❌ Blockers

  • [src/Storages/ObjectStorage/DataLakes/Paimon/PaimonMetadata.cpp:563] validateTableIdentity() now runs only once before the incremental and targeted-snapshot branches. If the upstream table is recreated after this check, the later loadStateForSnapshot() calls can read from the new table. In the incremental path, a missing snapshot-N is caught as a compaction gap, last_consumed_snapshot_id still advances, and iterate() commits that watermark to Keeper. That loses unread data from the old table instead of failing with the documented recreation exception. Suggested fix: revalidate table identity around every later loadStateForSnapshot() / getSnapshotsBetween() call, including the exception path that currently interprets missing snapshots as compaction.
Final Verdict

Changes required: the external-table recreation guard still does not fail closed for incremental and targeted-snapshot reads.

@clickhouse-gh clickhouse-gh Bot added the pr-experimental Experimental Feature label May 12, 2026
@JiaQiTang98 JiaQiTang98 added the can be tested Allows running workflows for external contributors label May 12, 2026
Comment thread src/Core/Settings.cpp Resolved
Comment thread src/Core/ServerSettings.cpp Resolved
Comment thread src/Interpreters/Context.h Resolved
Comment thread programs/server/Server.cpp Resolved
Comment thread src/Core/ServerSettings.cpp Resolved
@Binnn-MX
Binnn-MX force-pushed the add_paimon_metadata_cache branch from 03d9be0 to 5e210e0 Compare May 14, 2026 07:38
Comment thread .metals/metals.lock.db Resolved
Comment thread programs/server/Server.cpp Resolved
@Binnn-MX
Binnn-MX force-pushed the add_paimon_metadata_cache branch from a64935f to 07c0e1f Compare May 19, 2026 08:47
@Binnn-MX
Binnn-MX marked this pull request as ready for review May 26, 2026 05:49
@hanfei1991
hanfei1991 added this pull request to the merge queue Jul 10, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 10, 2026
Comment thread src/Interpreters/Context.cpp Resolved
/// analysis->execution window where an external DROP + re-CREATE would otherwise be reused stale.
/// iterate() runs once per query read (the iterator is shared across streams), so this single
/// remote schema-0 check is negligible relative to the data scan it guards (fail-close).
validateTableIdentity();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The single validateTableIdentity() here still does not protect the later loadStateForSnapshot() calls.

Concrete incremental trace:

  1. iterate() pins state at snapshot 10 and this check passes.
  2. The upstream Paimon table is then DROP + CREATEd.
  3. collectIncrementalDataFiles() calls getSnapshotsBetween(9, 10), and loadStateForSnapshot(10) now reads from the recreated table path. If snapshot-10 is missing there, the catch block in getSnapshotsBetween() treats that as a compaction gap.
  4. snapshots.empty() then advances last_consumed_snapshot_id to 10, and iterate() commits that watermark to Keeper.

So the new fail-close contract is still broken: after a recreate, incremental reads can silently skip unread snapshots instead of raising the documented schema-0 mismatch exception. The same gap exists for the paimon_target_snapshot_id branch if the recreated table reuses the requested snapshot id.

Please revalidate table identity around every later loadStateForSnapshot() / getSnapshotsBetween() call (including the exception path that currently assumes compaction), not only before consuming the already-pinned state.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is fundamentally a race with the external Paimon table being dropped/recreated during query execution. Without an external lock or transaction, no single schema-0 check can make the whole read consistent: the table can always be recreated immediately after the check.

So adding checks around every loadStateForSnapshot only moves the TOCTOU window; it does not close it. At most, we can do best-effort validation at key boundaries.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The current guard is a best-effort fail-close check intended to prevent stale schema reuse across refresh/analysis boundaries. It does not promise strong consistency if the external table is dropped and recreated during query execution.

@clickhouse-gh

clickhouse-gh Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 28.60% 85.90% +57.30%
Functions 39.40% 92.70% +53.30%
Branches 20.60% 78.00% +57.40%

Changed lines: Changed C/C++ lines covered: 424/451 (94.01%) · Uncovered code

Full report · Diff report

@hanfei1991
hanfei1991 added this pull request to the merge queue Jul 16, 2026
Merged via the queue into ClickHouse:master with commit e11350a Jul 16, 2026
176 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 16, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-experimental Experimental Feature 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.

5 participants