Xuanwo opened a new pull request, #8890:
URL: https://github.com/apache/opendal/pull/8890

   # Which issue does this PR close?
   
   Part of #7107.
   
   # Rationale for this change
   
   Auditing and benchmarking `CacheLayer` against in-memory (`Memory`, `Moka`, 
`Foyer`), hybrid disk (`Foyer`), and simulated remote/network cache backends 
surfaced several concurrency, correctness, and performance issues:
   
   1. **TTL-expiry and cold `Suffix` stampede:** Expired-pointer `stat` 
revalidations and cold `BytesRange::Suffix` length-discovery `stat` calls did 
not coalesce through singleflight, so concurrent readers all issued origin 
`stat` and `publish` calls simultaneously.
   2. **`observe_stat` cancelling unrelated or matching cold publishes:** 
Whenever `op.stat()` missed the cache, `observe_stat` unconditionally 
incremented the 64-shard `epoch`, silently dropping in-flight cold-read 
`publish` calls for unrelated keys in the same shard (1/64 probability) or 
concurrent same-key reads whose metadata matched.
   3. **Mutex held across async cache I/O blocking hot reads:** `State::epochs` 
stored a `tokio::sync::Mutex<u64>` locked across cache `read`/`write`/`delete` 
in `publish`, `invalidate`, `observe_stat`, and `discard`. Every hot read 
awaited this mutex just to read `epoch`, preventing multi-core scaling on hot 
keys and causing head-of-line blocking across 1/64 of the keyspace on slower 
cache backends.
   4. **Per-read `"v1/gen"` lookup:** Every read and `stat` issued a cache 
`read("v1/gen")`, creating a 100%-traffic global hotspot key on remote caches 
and a per-read disk miss on hybrid `Foyer` caches.
   5. **Serial and redundant chunk I/O:** `CachedStream::fetch` probed trailing 
chunks serially and discarded cache hits on partial misses (re-reading them on 
the next chunk), `store_unit` wrote fill-unit chunks sequentially on the 
critical path, and reusing a `Reader` across multiple range reads re-fetched 
and re-decoded `v1/meta` on every range.
   
   # What changes are included in this PR?
   
   - **Singleflight revalidation & cold suffix `stat` (`state.rs`, 
`reader.rs`):** Add `State::revalidations` (`Flights<RevalidationKey, 
RevalidationResult>`) so concurrent expired-TTL readers and concurrent cold 
`Suffix` readers coalesce into one origin `stat` and one `publish`.
   - **Lock-free hot-read `epoch` & targeted cold-read stat guard (`state.rs`, 
`service.rs`, `mutation.rs`, `reader.rs`):** Split shards (expanded to 256) 
into `{ epoch: AtomicU64, meta_io: Mutex<()>, cold_reads }` so hot reads and 
`begin_mutation` never lock `meta_io`, while `publish`/`invalidate`/`discard` 
still serialize metadata writes against deletes. Track active in-flight cold 
reads per shard so `observe_stat` only suppresses a cold-read `publish` when a 
newer `stat` for the same subject contradicts it.
   - **In-process `generation` cache (`state.rs`):** Cache `v1/gen` with 
`AtomicU64` + timestamp, refreshing at `min(ttl, 1s)` (always refreshing on 
first access, `ttl == 0`, `invalidate`, and `bump_generation`).
   - **Batched concurrent chunk I/O & per-`Reader` pointer cache (`reader.rs`, 
`Cargo.toml`):**
     - Batch warm multi-chunk cache reads up to one fill unit with 
`futures::future::join_all`, retain contiguous trailing hits in 
`CachedStream::units` (only when aligned with the filled unit boundary) instead 
of discarding them, and avoid redundant trailing probes on freshly discovered 
cold streams.
     - Write multi-chunk fill units concurrently in `store_unit` via `join_all`.
     - Cache the last resolved `(Snapshot, Pointer)` on `CachedReader` under 
`RwLock`, invalidated immediately on epoch/generation change, TTL expiry, or 
`FillError::Changed`.
   - **Regression tests (`tests/read_through.rs`):** Add deterministic tests 
covering expired-TTL and cold-suffix `stat` singleflight, `observe_stat` 
preserving unrelated/matching cold publishes while blocking contradicting stale 
publishes, partial-miss chunk reuse without redundant cache reads, and `Reader` 
pointer reuse + invalidation on mutation.
   
   # Benchmark
   
   Measured in release mode on an 8-worker Tokio runtime comparing 
`origin/main` (`ba745d308`) against this PR (`91d6c777c`):
   
   | Workload | Before (`ba745d308`) | After (`91d6c777c`) | Change |
   |---|---:|---:|---:|
   | Concurrent 4 KiB hot `op.read()` (32 tasks, 1 key, `Moka`) | 183k ops/s | 
1,471k ops/s | **8.0x** |
   | Concurrent 4 KiB hot `op.read()` (32 tasks, 1 key, `Foyer` 16 shards) | 
169k ops/s | 1,189k ops/s | **7.0x** |
   | Concurrent 4 KiB hot `op.read()` (32 tasks, 128 keys, `Foyer` 16 shards) | 
381k ops/s | 1,421k ops/s | **3.7x** |
   | Single-task 4 KiB hot `op.read()` (`Foyer` hybrid memory+disk) | 11.79 µs 
(85k/s) | 3.84 µs (261k/s) | **3.1x** |
   | Reused `Reader` 4 KiB range reads (`Foyer`, 10k ranges) | 8.21 µs (122k/s) 
| 2.39 µs (419k/s) | **3.4x** |
   | Simulated remote cache (1 ms RTT) hot 16 MiB `op.read()` | 39.42 ms | 
10.44 ms | **3.8x** |
   | Simulated remote cache (1 ms RTT) cold 16 MiB `op.read()` (0 ms origin) | 
67.99 ms | 23.06 ms | **2.9x** |
   | Expired-TTL stampede (32 concurrent readers, 20 ms origin RTT) | 32 origin 
`stat`s | 1 origin `stat` | **32x fewer** |
   
   # Are there any user-facing changes?
   
   `CacheLayer` scales across concurrent hot reads, coalesces TTL-expiry and 
cold-suffix `stat` calls, and reduces cache round trips on remote/hybrid 
backends. No public API changes.
   
   # Breaking changes
   
   # AI Usage Statement
   
   - Harness: Pi (T3 Code)
   - Model: Gemini 4 Neon
   - Effort: high
   - Role: Audited `CacheLayer`, ran before/after release benchmarks against 
in-memory/hybrid/simulated-latency backends, implemented the singleflight, 
lock-free epoch, generation cache, batched chunk I/O, and regression tests, and 
ran formatting, clippy, locked check, and `opendal-layer-cache` tests.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to