feat(cache): make FileCachePool thread-safe for multi-vCPU access (#1555)
- feat(cache): make FileCachePool thread-safe for multi-vCPU access
Guard all FileCachePool metadata (fileIndex_, lru_, cold tiers, totalUsed_, tuning state) with a coarse photon::mutex (m_lock_) so one pool can be shared across multiple photon vCPUs (OS threads).
Invariants:
- m_lock_ is held only across in-memory ops; never across open()/do_open() or forceRecycle()/eviction(), keeping the lock order rw_lock -> m_lock_ one-way (avoids ABBA with ObjectCache’s per-item mutex and non-reentrant self-deadlock).
- eviction/evict snapshot a victim under m_lock_, release it, do the I/O (open()+WLOCK truncate), then re-lock to finalize.
- the write path accounts size under the store rw_lock (updateSpace fstats under m_lock_) so it can’t drift against eviction’s WLOCK+truncate; forceRecycle() is deferred to do_pwritev2 after rw_lock is released.
- running_/exit_/isFull_ and LruEntry::truncate_done become std::atomic.
Add a multi-vCPU concurrency stress test (CachePool.concurrent_stress).
QuotaFilePool is left unchanged and documented as not-yet-thread-safe (it is currently unwired: the factory always builds a plain FileCachePool).
Co-Authored-By: Claude Opus 4.8 noreply@anthropic.com
- check total used in eviction loop
Co-authored-by: Claude Opus 4.8 noreply@anthropic.com
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802047560号
PhotonLibOS
PhotonlibOS.github.io
What’s New
DPDK. En / 中文.RocksDBfrom multi-threads to coroutines by only 200 lines of code? En / 中文.Click to show more history...
boostdependency anymore.thread_localkeyword. See doc.photon_stdnamespace is added. Developers can search forstd::thread,std::mutexin their own projects, and replace them all into the equivalents ofphoton_std::<xxx>. It’s a quick way to transform thread-based programs to coroutine-based ones.WorkPoolso it’s more friendly now to write multi-vcpu programs.Introduction
Photon is a C++ library designed to augment the operating system in terms of high-performance concurrent I/O programming. Photon is collected from Alibaba Cloud Storage team’s internal code base. It has been extensively tested and used in production, and is the same code they depend on in their daily lives.
Photon features a highly efficient user-space threading model, implemented as coroutines with multi-core CPU support, driven by a variety of asynchronous event engines such as
epoll,io_uring, andkqueue, etc. These event engines are abstracted behind a lightweight, unified interface.Built on this foundation, Photon provides a comprehensive set of high-level abstractions for networking and file I/O operations, leveraging both kernel-based system calls and high-performance user-space frameworks like DPDK and SPDK.
Furthermore, Photon includes entirely new, ground-up implementations of key distributed system components, including RPC, HTTP client and server, and Redis client, etc., delivering significantly higher performance and efficiency compared to existing solutions. The architecture of Photon is illustrated below:
Photon is well-suited for developing any I/O-intensive applications, including those involving disk I/O and/or network I/O.
Performance Photon can switch to a thread in just a few nanoseconds; this action corresponds to callback in a typical asynchronous framework. The cost of creating a new thread is primarily a simple malloc(), and the allocation can be pooled for even greater efficiency. Performance benchmarks on I/O workloads are available here.
Production Photon is the foundation of many production systems in (listing in alphabetical order) Alibaba Group, Ant Group, ByteDance, Xiaomi, XSKY, etc., and many open source projects in public domain.
overlaybd, a sub-project of containerd, which is deployed at scale in Alibaba’s own infrastructure as well as the public cloud platform.
OSSFSv2, a high-performance client for Alibaba Cloud’s object storage service (OSS) that provides a POSIX-compliant file system interface to the users.
Connector for AI/ML, a high-performance storage driver for popular AI frameworks, such as Pythorch, safetensors, vllm, etc., supporting dataset reading, checkpointing, model loading.
P2P Transport, a key component that addresses the scalability issue of large scale data serving, which is found in many scenarios like container image service, or AI model loading, etc.
Caching services, such as OSS Accelerator, a multi-tenant distributed caching service for high-performance object access; or EFC cache, a client-side caching module for elastic file client (EFC).
KTransformers, a flexible, Python-centric AI framework with advanced kernel optimizations and placement/parallelism strategies, designed with extensibility at its core. Photon is used to asynchronously load kvcache data from disk to memory, or vice versa.
The coroutine engine in VLang
Agility Photon is designed to support a wide range of applications — and is already deployed in production across diverse environments. We’ve gone beyond mere feasibility, engineering it to be not only highly adaptable, but also intuitive, convenient, and fast to use. This is made possible through a set of well-defined, modular abstractions that decouple core logic from underlying system specifics, enabling both flexibility and performance.
Portability Photon supports multiple platforms, i.e. the combinations of {Linux, macOS} * {x86_64, ARM64} * {gcc, clang}.
Inclusion in awesome-cpp
Community