refactor(storage): remove object cache plumbing (#2422)

Co-authored-by: heihutu <heihutu@gmail.com>
This commit is contained in:
houseme
2026-04-07 22:00:53 +08:00
committed by GitHub
parent e3000f16e0
commit 79ffecbf14
22 changed files with 188 additions and 5011 deletions
-229
View File
@@ -12,116 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
/// Environment variable name to toggle object-level in-memory caching.
///
/// - Purpose: Enable or disable the object-level in-memory cache (moka).
/// - Acceptable values: `"true"` / `"false"` (case-insensitive) or a boolean typed config.
/// - Semantics: When enabled, the system keeps fully-read objects in memory to reduce backend requests; when disabled, reads bypass the object cache.
/// - Example: `export RUSTFS_OBJECT_CACHE_ENABLE=true`
/// - Note: Evaluate together with `RUSTFS_OBJECT_CACHE_CAPACITY_MB`, TTL/TTI and concurrency thresholds to balance memory usage and throughput.
pub const ENV_OBJECT_CACHE_ENABLE: &str = "RUSTFS_OBJECT_CACHE_ENABLE";
/// Environment variable name that specifies the object cache capacity in megabytes.
///
/// - Purpose: Set the maximum total capacity of the object cache (in MB).
/// - Unit: MB (1 MB = 1_048_576 bytes).
/// - Valid values: any positive integer (0 may indicate disabled or alternative handling).
/// - Semantics: When the moka cache reaches this capacity, eviction policies will remove entries; tune according to available memory and object size distribution.
/// - Example: `export RUSTFS_OBJECT_CACHE_CAPACITY_MB=512`
/// - Note: Actual memory usage will be slightly higher due to object headers and indexing overhead.
pub const ENV_OBJECT_CACHE_CAPACITY_MB: &str = "RUSTFS_OBJECT_CACHE_CAPACITY_MB";
/// Environment variable name for maximum object size eligible for caching in megabytes.
///
/// - Purpose: Define the upper size limit for individual objects to be considered for caching.
/// - Unit: MB (1 MB = 1_048_576 bytes).
/// - Valid values: any positive integer; objects larger than this size will not be cached.
/// - Semantics: Prevents caching of excessively large objects that could monopolize cache capacity; tune based on typical object size distribution.
/// - Example: `export RUSTFS_OBJECT_CACHE_MAX_OBJECT_SIZE_MB=50`
/// - Note: Setting this too low may reduce cache effectiveness; setting it too high may lead to inefficient memory usage.
pub const ENV_OBJECT_CACHE_MAX_OBJECT_SIZE_MB: &str = "RUSTFS_OBJECT_CACHE_MAX_OBJECT_SIZE_MB";
// =============================================================================
// L1/L2 Tiered Cache Configuration
// =============================================================================
/// Environment variable for L1 cache maximum size in megabytes.
///
/// L1 cache is for hot small objects (<1MB). Higher values improve hit rate for small objects.
pub const ENV_OBJECT_L1_CACHE_MAX_SIZE_MB: &str = "RUSTFS_OBJECT_L1_CACHE_MAX_SIZE_MB";
/// Environment variable for L1 cache maximum number of objects.
pub const ENV_OBJECT_L1_CACHE_MAX_OBJECTS: &str = "RUSTFS_OBJECT_L1_CACHE_MAX_OBJECTS";
/// Environment variable for L1 cache TTL (time-to-live) in seconds.
pub const ENV_OBJECT_L1_CACHE_TTL_SECS: &str = "RUSTFS_OBJECT_L1_CACHE_TTL_SECS";
/// Environment variable for L1 cache TTI (time-to-idle) in seconds.
pub const ENV_OBJECT_L1_CACHE_TTI_SECS: &str = "RUSTFS_OBJECT_L1_CACHE_TTI_SECS";
/// Environment variable for L1 cache maximum object size in megabytes.
pub const ENV_OBJECT_L1_MAX_OBJECT_SIZE_MB: &str = "RUSTFS_OBJECT_L1_MAX_OBJECT_SIZE_MB";
/// Environment variable for L2 cache maximum size in megabytes.
///
/// L2 cache is for standard objects (<10MB).
pub const ENV_OBJECT_L2_CACHE_MAX_SIZE_MB: &str = "RUSTFS_OBJECT_L2_CACHE_MAX_SIZE_MB";
/// Environment variable for L2 cache maximum number of objects.
pub const ENV_OBJECT_L2_CACHE_MAX_OBJECTS: &str = "RUSTFS_OBJECT_L2_CACHE_MAX_OBJECTS";
/// Environment variable for L2 cache TTL (time-to-live) in seconds.
pub const ENV_OBJECT_L2_CACHE_TTL_SECS: &str = "RUSTFS_OBJECT_L2_CACHE_TTL_SECS";
/// Environment variable for L2 cache TTI (time-to-idle) in seconds.
pub const ENV_OBJECT_L2_CACHE_TTI_SECS: &str = "RUSTFS_OBJECT_L2_CACHE_TTI_SECS";
// =============================================================================
// Adaptive TTL Configuration
// =============================================================================
/// Environment variable to enable adaptive TTL.
///
/// When enabled, hot objects (with high hit counts) get extended TTL.
pub const ENV_OBJECT_ADAPTIVE_TTL_ENABLE: &str = "RUSTFS_OBJECT_ADAPTIVE_TTL_ENABLE";
/// Environment variable for hot object hit threshold.
///
/// Objects with hit count >= this threshold are considered "hot" and get extended TTL.
pub const ENV_OBJECT_HOT_HIT_THRESHOLD: &str = "RUSTFS_OBJECT_HOT_HIT_THRESHOLD";
/// Environment variable for TTL extension factor.
///
/// Hot objects TTL is extended by this factor (e.g., 2.0 = 2x longer).
pub const ENV_OBJECT_TTL_EXTENSION_FACTOR: &str = "RUSTFS_OBJECT_TTL_EXTENSION_FACTOR";
/// Environment variable name for object cache TTL (time-to-live) in seconds.
///
/// - Purpose: Specify the maximum lifetime of a cached entry from the moment it is written.
/// - Unit: seconds (u64).
/// - Semantics: TTL acts as a hard upper bound; entries older than TTL are considered expired and removed by periodic cleanup.
/// - Example: `export RUSTFS_OBJECT_CACHE_TTL_SECS=300`
/// - Note: TTL and TTI both apply; either policy can cause eviction.
pub const ENV_OBJECT_CACHE_TTL_SECS: &str = "RUSTFS_OBJECT_CACHE_TTL_SECS";
/// Environment variable name for object cache TTI (time-to-idle) in seconds.
///
/// - Purpose: Specify how long an entry may remain in cache without being accessed before it is evicted.
/// - Unit: seconds (u64).
/// - Semantics: TTI helps remove one-time or infrequently used entries; frequent accesses reset idle timers but do not extend beyond TTL unless additional logic exists.
/// - Example: `export RUSTFS_OBJECT_CACHE_TTI_SECS=120`
/// - Note: Works together with TTL to keep the cache populated with actively used objects.
pub const ENV_OBJECT_CACHE_TTI_SECS: &str = "RUSTFS_OBJECT_CACHE_TTI_SECS";
/// Environment variable name for threshold of "hot" object hit count used to extend life.
///
/// - Purpose: Define a hit-count threshold to mark objects as "hot" so they may be treated preferentially near expiration.
/// - Valid values: positive integer (usize).
/// - Semantics: Objects reaching this hit count can be considered for relaxed eviction to avoid thrashing hot items.
/// - Example: `export RUSTFS_OBJECT_HOT_MIN_HITS_TO_EXTEND=5`
/// - Note: This is an optional enhancement and requires cache-layer statistics and extension logic to take effect.
pub const ENV_OBJECT_HOT_MIN_HITS_TO_EXTEND: &str = "RUSTFS_OBJECT_HOT_MIN_HITS_TO_EXTEND";
/// Environment variable name for high concurrency threshold used in adaptive buffering.
///
/// - Purpose: When concurrent request count exceeds this threshold, the system enters a "high concurrency" optimization mode to reduce per-request buffer sizes.
@@ -148,38 +38,6 @@ pub const ENV_OBJECT_MEDIUM_CONCURRENCY_THRESHOLD: &str = "RUSTFS_OBJECT_MEDIUM_
/// - Note: This setting may interact with OS-level I/O scheduling and should be tuned based on hardware capabilities.
pub const ENV_OBJECT_MAX_CONCURRENT_DISK_READS: &str = "RUSTFS_OBJECT_MAX_CONCURRENT_DISK_READS";
/// Default: object caching is enabled.
///
/// - Semantics: Caching is now enabled by default for improved performance. Hot objects are kept in memory
/// to reduce backend requests. Set RUSTFS_OBJECT_CACHE_ENABLE=false to disable if needed.
/// - Default is set to true (enabled).
pub const DEFAULT_OBJECT_CACHE_ENABLE: bool = true;
/// Environment variable to enable tiered cache (L1 + L2).
///
/// When enabled, uses two-level caching:
/// - L1: Hot small objects (<1MB) with short TTL
/// - L2: Standard objects (<10MB) with longer TTL
///
/// When enabled, provides L1 (hot small objects) and L2 (standard objects) caching.
/// When disabled, uses single-level cache for backward compatibility.
pub const ENV_OBJECT_TIERED_CACHE_ENABLE: &str = "RUSTFS_OBJECT_TIERED_CACHE_ENABLE";
/// Default: tiered cache is enabled for improved cache hit rates.
pub const DEFAULT_OBJECT_TIERED_CACHE_ENABLE: bool = true;
/// Default object cache capacity in MB.
///
/// - Default: 100 MB (can be overridden by `RUSTFS_OBJECT_CACHE_CAPACITY_MB`).
/// - Note: Choose a conservative default to reduce memory pressure in development/testing.
pub const DEFAULT_OBJECT_CACHE_CAPACITY_MB: u64 = 100;
/// Default maximum object size eligible for caching in MB.
///
/// - Default: 10 MB (can be overridden by `RUSTFS_OBJECT_CACHE_MAX_OBJECT_SIZE_MB`).
/// - Note: Balances caching effectiveness with memory usage.
pub const DEFAULT_OBJECT_CACHE_MAX_OBJECT_SIZE_MB: usize = 10;
/// Maximum concurrent requests before applying aggressive optimization.
///
/// When concurrent requests exceed this threshold (>8), the system switches to
@@ -209,33 +67,6 @@ pub const DEFAULT_OBJECT_MEDIUM_CONCURRENCY_THRESHOLD: usize = 4;
/// Default is set to 64 concurrent reads.
pub const DEFAULT_OBJECT_MAX_CONCURRENT_DISK_READS: usize = 64;
/// Time-to-live for cached objects (5 minutes = 300 seconds).
///
/// After this duration, cached objects are automatically expired by Moka's
/// background cleanup process, even if they haven't been accessed. This prevents
/// stale data from consuming cache capacity indefinitely.
///
/// Default is set to 300 seconds.
pub const DEFAULT_OBJECT_CACHE_TTL_SECS: u64 = 300;
/// Time-to-idle for cached objects (2 minutes = 120 seconds).
///
/// Objects that haven't been accessed for this duration are automatically evicted,
/// even if their TTL hasn't expired. This ensures cache is populated with actively
/// used objects and clears out one-time reads efficiently.
///
/// Default is set to 120 seconds.
pub const DEFAULT_OBJECT_CACHE_TTI_SECS: u64 = 120;
/// Minimum hit count to extend object lifetime beyond TTL.
///
/// "Hot" objects that have been accessed at least this many times are treated
/// specially - they can survive longer in cache even as they approach TTL expiration.
/// This prevents frequently accessed objects from being evicted prematurely.
///
/// Default is set to 5 hits.
pub const DEFAULT_OBJECT_HOT_MIN_HITS_TO_EXTEND: usize = 5;
/// Skip bitrot hash verification on GetObject reads.
///
/// When enabled, GetObject reads skip the per-shard hash
@@ -670,63 +501,3 @@ pub const ENV_OBJECT_IO_RANDOM_READAHEAD_DISABLE_CONCURRENCY: &str = "RUSTFS_OBJ
/// Default read-ahead disable concurrency threshold: 4.
pub const DEFAULT_OBJECT_IO_RANDOM_READAHEAD_DISABLE_CONCURRENCY: usize = 4;
// =============================================================================
// L1/L2 Tiered Cache Default Values
// =============================================================================
/// Default L1 cache maximum size: 50 MB.
///
/// L1 cache is for hot small objects (<1MB). Smaller values reduce memory usage.
pub const DEFAULT_OBJECT_L1_CACHE_MAX_SIZE_MB: u64 = 50;
/// Default L1 cache maximum number of objects: 1000.
pub const DEFAULT_OBJECT_L1_CACHE_MAX_OBJECTS: usize = 1000;
/// Default L1 cache TTL: 60 seconds (1 minute).
///
/// Shorter TTL for L1 cache ensures only very hot objects stay in L1.
pub const DEFAULT_OBJECT_L1_CACHE_TTL_SECS: u64 = 60;
/// Default L1 cache TTI: 30 seconds.
///
/// Shorter TTI means L1 evicts idle objects quickly.
pub const DEFAULT_OBJECT_L1_CACHE_TTI_SECS: u64 = 30;
/// Default L1 maximum object size: 1 MB.
///
/// Only objects smaller than 1MB are cached in L1.
pub const DEFAULT_OBJECT_L1_MAX_OBJECT_SIZE_MB: usize = 1;
/// Default L2 cache maximum size: 200 MB.
///
/// L2 cache is for standard objects (<10MB).
pub const DEFAULT_OBJECT_L2_CACHE_MAX_SIZE_MB: u64 = 200;
/// Default L2 cache maximum number of objects: 500.
pub const DEFAULT_OBJECT_L2_CACHE_MAX_OBJECTS: usize = 500;
/// Default L2 cache TTL: 300 seconds (5 minutes).
pub const DEFAULT_OBJECT_L2_CACHE_TTL_SECS: u64 = 300;
/// Default L2 cache TTI: 120 seconds (2 minutes).
pub const DEFAULT_OBJECT_L2_CACHE_TTI_SECS: u64 = 120;
// =============================================================================
// Adaptive TTL Default Values
// =============================================================================
/// Default: adaptive TTL is enabled.
///
/// When enabled, hot objects get extended TTL based on access patterns.
pub const DEFAULT_OBJECT_ADAPTIVE_TTL_ENABLE: bool = true;
/// Default hot object hit threshold: 3.
///
/// Objects with hit count >= 3 are considered "hot" and get extended TTL.
pub const DEFAULT_OBJECT_HOT_HIT_THRESHOLD: usize = 3;
/// Default TTL extension factor: 2.0.
///
/// Hot objects TTL is extended by 2x (e.g., 5 min TTL becomes 10 min).
pub const DEFAULT_OBJECT_TTL_EXTENSION_FACTOR: f64 = 2.0;