|
| 1 | +.. Copyright 2026-present Alibaba Inc. |
| 2 | +
|
| 3 | +.. Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +.. you may not use this file except in compliance with the License. |
| 5 | +.. You may obtain a copy of the License at |
| 6 | +
|
| 7 | +.. http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +.. Unless required by applicable law or agreed to in writing, software |
| 10 | +.. distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +.. See the License for the specific language governing permissions and |
| 13 | +.. limitations under the License. |
| 14 | +
|
| 15 | +Manifest Cache |
| 16 | +============== |
| 17 | + |
| 18 | +Overview |
| 19 | +-------- |
| 20 | + |
| 21 | +paimon-cpp caches raw manifest file bytes at the ``ObjectsFile<T>::Read()`` |
| 22 | +layer. The cache uses the public ``Cache`` abstraction and is injected through |
| 23 | +``ScanContextBuilder`` or ``ReadContextBuilder``. The cache covers data |
| 24 | +manifests, manifest lists, and index manifests because they all read through |
| 25 | +``ObjectsFile<T>``. |
| 26 | + |
| 27 | +For repeated ``get``, ``scan``, or batch ``get/scan -f`` requests in the same |
| 28 | +process, the same snapshot often reads the same manifest files repeatedly. On a |
| 29 | +cache hit, the read path skips remote filesystem ``open/read``, builds an |
| 30 | +in-memory input stream from cached bytes, and still runs the format reader, |
| 31 | +Arrow decoding, and object deserialization. This design primarily reduces |
| 32 | +remote IO latency and bandwidth while keeping cache weight aligned with the |
| 33 | +actual cached bytes. |
| 34 | + |
| 35 | +Configuration |
| 36 | +------------- |
| 37 | + |
| 38 | +Manifest caching is disabled by default. Embedding applications that need it can |
| 39 | +provide a custom ``Cache`` implementation and inject it through ``WithCache``. |
| 40 | +The builder wraps the cache as ``CacheKind::Manifest`` internally, so callers do |
| 41 | +not need to pass the cache kind through scan or read contexts. The same cache |
| 42 | +instance can be reused across multiple scan or read contexts when process-local |
| 43 | +sharing is desired. |
| 44 | + |
| 45 | +Example: |
| 46 | + |
| 47 | +.. code-block:: cpp |
| 48 | +
|
| 49 | + auto manifest_cache = std::make_shared<MyCache>(); |
| 50 | +
|
| 51 | + paimon::ScanContextBuilder scan_builder(table_path); |
| 52 | + scan_builder.WithCache(manifest_cache); |
| 53 | +
|
| 54 | + paimon::ReadContextBuilder read_builder(table_path); |
| 55 | + read_builder.WithCache(manifest_cache); |
| 56 | +
|
| 57 | +Passing ``nullptr`` or omitting ``WithCache()`` leaves manifest caching disabled. |
| 58 | + |
| 59 | +Implementation Notes |
| 60 | +-------------------- |
| 61 | + |
| 62 | +- The cache key is an internal whole-file position key with ``path``, |
| 63 | + ``offset = 0``, and ``length = -1``, so cache hits do not need an extra |
| 64 | + file-status lookup. |
| 65 | +- The cache value stores full manifest file bytes as a ``MemorySegment``. |
| 66 | +- The cache instance is supplied by the embedding application and passed through |
| 67 | + read or scan contexts. |
| 68 | +- On a cache hit, ``ObjectsFile::Read()`` keeps the cached ``Bytes`` alive while |
| 69 | + ``ByteArrayInputStream`` reads from it without copying. Caller filters are |
| 70 | + applied after records are decoded. |
| 71 | +- ``Read()`` keeps its existing append semantics, which is required when base |
| 72 | + and delta manifests are loaded into the same result vector. |
| 73 | + |
| 74 | +Test Coverage |
| 75 | +------------- |
| 76 | + |
| 77 | +- ``ScanContextTest`` and ``ReadContextTest`` validate cache injection through |
| 78 | + ``WithCache(cache)``. |
| 79 | +- ``ManifestFileTest.TestReadUsesManifestCache`` validates cache hits on |
| 80 | + repeated reads with an injected manifest cache. |
| 81 | +- ``ManifestFileTest.TestManifestCacheIsDisabledWithoutInjectedCache`` validates |
| 82 | + that omitting an injected cache still reopens files for every read. |
| 83 | +- ``ManifestFileTest.TestManifestCacheCanBeDisabled`` validates that omitting |
| 84 | + the option still reopens files for every read. |
| 85 | + |
| 86 | +Future Optimizations |
| 87 | +-------------------- |
| 88 | + |
| 89 | +- Add hit, miss, bypass, and eviction metrics to read trace or metrics. |
| 90 | +- Add single-flight loading for high-concurrency misses on the same manifest |
| 91 | + path. |
| 92 | +- Evaluate a decoded-records second-level cache, configurable as a |
| 93 | + CPU-vs-memory tradeoff. |
0 commit comments