Skip to content

HIVE-30277: Iceberg: cache table metadata files across queries in the Hive catalog's FileIO - #6984

Open
deniskuzZ wants to merge 7 commits into
apache:masterfrom
deniskuzZ:iceberg-metadata-cache
Open

deniskuzZ wants to merge 7 commits into
apache:masterfrom
deniskuzZ:iceberg-metadata-cache

Conversation

@deniskuzZ

@deniskuzZ deniskuzZ commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

  • MetadataCachingFileIO (iceberg-catalog): a HadoopFileIO that serves files in a table's metadata directory from
    Iceberg ContentCaches shared across the process, one per user, keyed by location:

    • metadata JSON
    • manifest lists and manifests
    • Puffin statistics (Iceberg's .stats, Hive's column-stats-*.puffin)
    • partition statistics (partition-stats-*)

    Data and delete files are never cached. Neither are a Hadoop catalog's v<N>.metadata.json (those names come back
    when a table is created again at the same location) or version-hint.text. "Metadata directory" means a directory
    named metadata, Iceberg's default: a table whose write.metadata.path points elsewhere is read uncached.

  • HiveCatalog.initialize creates it in place of HadoopFileIO when the catalog sets io.manifest.cache-enabled and
    uses the default FileIO (io-impl unset or HadoopFileIO).

    • As a subclass it keeps everything HadoopFileIO is: HadoopConfigurable, so serialized tables still skip the
      FileIO's Hadoop configuration (HIVE-25843), and DelegateFileIO, for bulk and prefix operations.
    • Nothing is kept in the FileIO: the cache is looked up on every metadata file open, by the reading user and the
      bounds. A table serialized into the Tez AM and the tasks therefore uses the same caches as the rest of the process.
  • A file opened without its length (the metadata JSON, manifest lists, partition statistics) is looked up in the cache
    first and, when cached, opened with the cached length. ContentCache.tryCache asks a file for its length before it
    looks in the cache, so otherwise every hit would cost a length request. The lookup relies on the file tryCache
    returns answering getLength() from the cache, which Iceberg does not promise; a test fails if that changes.

  • One cache per user. A cache hit never reaches storage, so no permission is checked on it.

    • With hive.server2.enable.doAs, each user's first read of a file goes through their own storage permissions, and
      a revoked permission takes effect when the entry expires.
    • The key is the short user name, which HDFS authorizes on. The FileSystem a file is opened with is resolved for
      the same user in the same call, so an entry in a user's cache was always read as that user.
    • With doAs off (LLAP) every read is made as the same user, and there is one cache.
    • A user's cache is dropped once it has gone unused for the expiry interval.
    • Storage credentials that differ between two configurations of the same user are not told apart, as Hadoop's own
      FileSystem cache does not tell them apart.
  • Bounds: io.manifest.cache.expiration-interval-ms (default here 1 h, Iceberg's 60 s expires entries between the
    queries of a session), max-total-bytes and max-content-length (Iceberg's defaults, 100 MB and 8 MB). They apply
    to each user's cache. The process keeps at most iceberg.io.manifest.cache.fileio-max caches (8 by default), the
    limit Iceberg sets on its own manifest caches, so the bytes held stay within 8 × max-total-bytes however many
    users read. One DEBUG line each time a cache is created.

  • The FileIO hides io.manifest.cache-enabled from Iceberg, which would otherwise wrap the same manifests in a
    second, per-instance cache.

  • IcebergStoredStats.openPuffin opens Puffin statistics files with the file and footer sizes the table metadata
    records, for the column statistics reads and for the writer, which carries earlier statistics forward. The FileIO
    gets the size too, so a cached file is served without asking the file system for its length.

Why are the changes needed?

Every query re-reads its tables' metadata from object storage several times. HiveServer2 reads the metadata JSON and
the statistics files at compile time. The Tez AM reads the metadata JSON, manifest lists and manifests again during
split planning.

Measured on a 40-node LLAP cluster (r5d.4xlarge, 12 executors; 1TRC part2, an Iceberg table with 21 manifests,
4.3 MiB): cache off / on / on / off, 3 runs each, same image, results identical in every run. With the cache on,
HiveServer2 and the Tez AM that planned the query each held the same 23 files; with it off, no process held a cache.
Measured before the cache was keyed by user, and while a hit on a file opened without its length still made a length
request; that cluster runs with doAs off, so it had one cache either way.

mean of 6 runs off on per mirrored half
split planning (Map 1 vertex init, Tez AM) 2.04 s 1.63 s (-20 %) -0.49 / -0.34 s
HiveServer2 compile 3.04 s 2.95 s -0.11 / -0.09 s

Query time did not change (the halves disagree: -4.4 / -0.2 s on ~95 s). The saving grows with the number of
manifests and with object-store latency, so tables with frequent small commits gain more. This is parity with Trino
and StarRocks.

A cache hit costs no storage request. A first read costs what it costs today, or less. Requests per read on S3A:

today first read cache hit
metadata JSON HEAD + read HEAD + read none
manifest list, partition statistics 2 HEAD + read HEAD + read none
manifests, Puffin files (opened with their length) read read none

Today the Avro and Parquet readers of manifest lists and partition statistics open the file, a HEAD, and then ask for
its length, another HEAD. With the cache, tryCache asks for the length first, and Hive's HadoopInputFile passes it
to openFile, so the open skips its HEAD. HDFS ignores that option, so there the first read of a metadata JSON costs
one getFileStatus call more than today. These counts come from the code (Iceberg 1.12.0, Parquet 1.18.1, Hadoop
3.4.3), not from a request trace.

Does this PR introduce any user-facing change?

Only when io.manifest.cache-enabled is set for a Hive catalog that uses the default FileIO. Table metadata files
are then cached across queries, per user, in every process that reads them: HiveServer2, the Tez AM, and the LLAP
daemons, which open metadata files only for metadata-table scans. The default expiry is 1 h instead of 60 s when
io.manifest.cache.expiration-interval-ms is unset. A process keeps at most iceberg.io.manifest.cache.fileio-max
caches (a system property, 8 by default). Nothing changes with the property unset (the default) or with a custom
io-impl.

How was this patch tested?

  • TestMetadataCachingFileIO:
    • every kind of metadata file is read once per process, by any instance, with its length or without it
    • a cached file opened without its length is still served after the file is deleted: no length request is made
    • data and delete files (including Puffin deletion vectors and a table named metadata) are read as they are
    • Hadoop-catalog version files and version-hint.text are read as they are
    • a deserialized FileIO uses the same cache
    • a user is never served a file another user read: two users share one FileIO; the second user's first read
      reaches the file system, and the first user is still served from the cache
    • Iceberg builds no second cache
    • default and catalog-set bounds; a file over the per-file bound is read as it is
    • an idle user's cache is dropped once its entries have expired, and kept when the expiry interval is 0
    • the process keeps at most iceberg.io.manifest.cache.fileio-max caches
  • TestHiveCatalog: the catalog's tables get the caching FileIO with the property (also with io-impl set to
    HadoopFileIO) and a plain HadoopFileIO without it. A second catalog loads a table whose metadata file was made
    unparseable, from the cache.
  • Each was checked by mutation (25, all caught):
    • either read overload not cached
    • the cache lookup for files opened without their length removed
    • the metadata-directory check or the Hadoop version-name exclusion dropped
    • Avro, Puffin, metadata JSON, .stats or partition-stats- files not admitted
    • a cache per instance instead of per process, or a FileIO that no longer serializes
    • the user or the catalog's cache configuration left out of the cache key, or the first user's cache kept in the
      FileIO
    • the per-file limit not passed on to ContentCache
    • the flag passed on to Iceberg
    • Iceberg's 60 s default
    • an idle user's cache never dropped, the cache ignoring a key's expiry, or an expiry interval of 0 expiring at once
    • the cap removed, or weak keys as Iceberg's own caches have (keys built on every open are then never found)
    • the catalog never using the caching FileIO, or not when io-impl names HadoopFileIO
  • A serialized table stays as small as before: 3,616 B with HadoopFileIO, 3,648 B with MetadataCachingFileIO
    (Hive's serializeTable, a ~130 KB vertex configuration).
  • TestIcebergColStatsFormat, TestIcebergColStatsWritePolicy, TestCatalogs.
  • Cluster: 40 x r5d.4xlarge, 1TRC part2, cache off / on / on / off, 3 runs each: results identical; with the cache on,
    HiveServer2 and the Tez AM hold the cached files (heap histogram), with it off no process holds a cache.

🤖 Generated with Claude Code

… Hive catalog's FileIO

- MetadataCachingFileIO: a HadoopFileIO serving a table's metadata dir from one ContentCache per process, by location
- cached: metadata JSON, manifest lists, manifests, Puffin and partition stats; never data or delete files
- not cached: Hadoop catalog v<N>.metadata.json and version-hint.text (names come back)
- HiveCatalog uses it with io.manifest.cache-enabled and the default FileIO; Iceberg's per-instance cache is not built
- cache field transient, looked up again by bounds after deserialization; default expiry 1 h instead of 60 s
- IcebergColStatsReader opens Puffin files with their known size
- tests: TestMetadataCachingFileIO, TestHiveCatalog
- a cache hit never reaches storage, so the cache is per user: with doAs, each user's first read of a file goes through their own permissions
- the key is the short user name, looked up on every open, so a FileIO serving several users cannot carry one user's entries to another
- the cache field is gone: nothing is kept in the FileIO, a deserialized table uses the same caches as the rest of the process
- a user's cache is dropped once unused for the expiry interval (Caffeine holder, system scheduler)
- bounds apply per user; with doAs off there is one cache
- test: two users share one FileIO; the second user's first read reaches the file system
…ts.openPuffin

- one helper opens a Puffin stats file with the file and footer sizes the table metadata records, the FileIO getting the size too
- replaces the three copies in IcebergColStatsReader
- the writer's carry-forward of earlier stats now passes the size as well, so a cached file is served without a length request
* again, so an idle user's cache would otherwise keep its bytes. The scheduler drops it on time even when no one
* opens a metadata file afterwards.
*/
private static final Cache<CacheKey, ContentCache> CONTENT_CACHES = Caffeine.newBuilder()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

matches Iceberg's own name: ManifestFiles declares Cache<FileIO, ContentCache> CONTENT_CACHES and reads it through contentCache(FileIO io)

@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants