Files
rustfs/crates/object-data-cache/src/memory.rs
T
houseme 1553dc3f62 Address P2 follow-ups from the 2026-07-10..12 merged-PR review (backlog#1210-1220) (#4783)
* fix(obs): open cleaner compression source with O_NOFOLLOW

The compressor opened the source log via File::open, which follows a
symlink at the final path component. Between the scanner selecting a
regular file and this open, an attacker with write access to the log
directory could swap the entry for a symlink (TOCTOU) pointing at, say,
/etc/shadow, whose contents would then be copied into an archive. Open
the source with O_NOFOLLOW on Unix so such a swap fails with ELOOP; the
temp/archive path already refused symlinks, this closes the source side.

Refs rustfs/backlog#1210
Co-Authored-By: heihutu <heihutu@gmail.com>

* fix(obs): recompress instead of trusting leftover cleaner archives

archive_header_ok only checked the first 2-4 magic bytes before treating
an existing .gz/.zst as a completed prior result and letting the caller
delete the source log. A file with valid magic but a truncated or forged
body passes that check, so an attacker with write access to the log
directory (or a crashed prior run) could plant such a stub and make the
cleaner delete the real log without ever producing a usable archive —
silent audit-data loss.

Chosen fix: stop trusting cross-process leftovers entirely and always
recompress the source in this pass, rather than fully decoding every
leftover to validate it. Full-decode validation would add real CPU cost
and decode-bug surface for a rare crash-recovery case; the existing
atomic create_new+rename already overwrites whatever sits at the archive
path (a planted symlink is replaced, never followed) with a freshly
written, fsync'd archive, so a partial/forged leftover can never gate
source deletion. This is the lowest-regression option.

Refs rustfs/backlog#1211
Co-Authored-By: heihutu <heihutu@gmail.com>

* fix(object-data-cache): cap memory-gate reservation at cache growth headroom

The memory gate subtracts `admitted_since_refresh` from the snapshot's
available bytes so a burst arriving faster than the 5 s refresh cannot
over-allocate. That counter is GROSS: it only rolls over on the refresh and
never rolls back when a fill is later evicted, cancelled, or loses the
invalidation race. Under sustained high-throughput churn (net footprint flat
and far below `max_capacity`) the raw counter balloons past the memory the
cache actually holds, so `effective_available` collapses and the gate reports
false memory pressure — skipping the hottest fills with SkippedMemoryPressure
until the next 5 s refresh. This only lowers hit rate; it never returns wrong
data and self-heals each refresh.

Fix direction 1 (minimal regression): cap the reservation deduction at the
cache's own growth headroom (`max_capacity - weighted_size()`) instead of
letting the unbounded gross counter shrink the system-available budget. The
cache can never hold more than `max_capacity`, so a burst adds at most that
headroom of real memory before moka evicts to stay bounded (net-zero churn
beyond that point) — capping the deduction there keeps the reservation honest
without treating gross churn as growth. Chosen over net-accounting (direction
2, releasing bytes on every failure/cancel/eviction path) because that only
plugs the leak on failed fills and would not address the core defect: churn of
*successful* insert/evict fills over the 5 s window. It also touches only the
gate plus one call site rather than every failure path in moka_backend.

The cap only ever raises `effective_available`, so real memory pressure (a low
snapshot at refresh) still suppresses fills; when the cache is at capacity the
headroom is 0 and the deduction vanishes, correctly reflecting net-zero churn.
`MokaBackend` now stores `max_capacity` and passes the live headroom into
`allows_fill`. Adds targeted gate tests: gross churn far above headroom no
longer falsely suppresses, yet the reservation still bounds a burst while the
cache can genuinely grow.

Refs rustfs/backlog#1212
Co-Authored-By: heihutu <heihutu@gmail.com>

* test(ecstore): assert native O_DIRECT path runs in uring read test

uring_preserves_o_direct_for_eligible_reads only compared bytes through
LocalDisk::read_file_mmap_copy. On a filesystem that rejects O_DIRECT the
read silently degrades to the buffered StdBackend fallback and the byte
check still passes, so the test could go green without the native
read_at_direct path ever executing -- a vacuous pass.

Add a per-disk native_direct_reads counter on UringBackend, incremented
only when pread_uring_direct completes, and rebuild the test to drive a
real UringBackend's pread_bytes and assert the counter is non-zero (every
eligible read went through the native tier). When io_uring or O_DIRECT is
unavailable on the host filesystem (restricted CI runners, tmpfs), the
test skips loudly via eprintln instead of asserting a tautology, while
still checking byte-correctness on whatever tier served the read.

The counter also gives a gray release a positive signal that the O_DIRECT
tier is serving reads, not just a fallback count.

Refs rustfs/backlog#1213
Co-Authored-By: heihutu <heihutu@gmail.com>

* fix(ecstore): warn + count read-time EINVAL on native O_DIRECT reads

classify_direct_read_error is only reached from the read side: the
O_DIRECT open in pread_uring_direct already succeeded (an open-time
refusal is handled earlier as DirectOpenError::ODirectRefused). So an
EINVAL/EOPNOTSUPP arriving here is a read-time error on an fd the kernel
accepted for O_DIRECT -- far more likely an alignment bug in the aligned
read path than an unsupported filesystem. The old code latched the disk's
native path off with only a once-per-disk debug trace, hiding a potential
correctness regression behind a silent buffered-read downgrade.

Diagnostics only: the fallback behaviour is unchanged (the native path is
still latched off and the caller still reads via StdBackend). This adds a
rustfs_io_uring_direct_read_einval_total counter and promotes the
once-per-disk trace from debug to warn so an operator can see an alignment
regression instead of an unexplained latency/CPU shift.

Refs rustfs/backlog#1214
Co-Authored-By: heihutu <heihutu@gmail.com>

* docs(ecstore): document data-blocks-first default and its tail-latency cost

DEFAULT_RUSTFS_GET_DATA_BLOCKS_FIRST_READER_SETUP is true and must stay
true: deferred-parity is the deliberate, already-rolled-out full-object
GET default from backlog#1159/#923. Flipping it back to false in code
would silently revert that rollout for every deployment that has not set
the env var, so this commit only documents -- no behaviour change.

The added notes explain what data-blocks-first does (schedule data shards
up front, engage parity lazily on a missing/corrupt data shard), the known
trade-off (parity is engaged late, so a slow-but-not-dead data drive
raises GET p99 because the faster parity shards are not raced against it
until a data shard is declared missing), and the operational rollback
switch (RUSTFS_GET_DATA_BLOCKS_FIRST_READER_SETUP=false), which is
intentionally an env override rather than a code default change.

No metric was added: the low-risk observability hook for "slow data drive
engaged deferred parity" would live at the deferred-stripe engage point,
which is out of this file's scope; this change stays documentation-only to
avoid touching the hot GET path.

Refs rustfs/backlog#1215
Co-Authored-By: heihutu <heihutu@gmail.com>

* docs(ecstore): document wide-directory walk stall hazard and tuning

list_dir enumerates a whole directory in one os::read_dir call (count =
-1), and the walk caller bounds that entire enumeration with the per-read
stall budget (default 5s) as if it were a single read. For a wide, flat
prefix -- one directory holding millions of immediate children -- a single
readdir can exceed the budget on a healthy disk, trip DiskError::Timeout,
and surface as a ListObjects 500 quorum failure though the drive is fine
(a #2999 sub-class).

This is documented, not rewritten: turning the one-shot readdir into a
streaming/batched enumeration that refreshes the stall deadline between
chunks is an architecture-level change with high regression surface
(ordering, the count contract, quorum merge) and belongs in a separate
follow-up. The supported mitigation today is operational, so the comments
point wide-directory deployments at RUSTFS_DRIVE_WALKDIR_STALL_TIMEOUT_SECS
and the high-latency drive-timeout profile, which widen the budget with no
code change. Notes were added at list_dir, the scan_dir call site, and
get_drive_walkdir_stall_timeout. No behaviour change.

Refs rustfs/backlog#1216
Co-Authored-By: heihutu <heihutu@gmail.com>

* docs(ecstore): document consumer-peek vs producer-stall coupling

In list_path_raw the consumer's peek_timeout is drawn from the same source
and same value (walkdir_stall_timeout, default 5s) as the producer-side
walk stall budget, but the two measure different things: the producer
stall bounds a single drive read, while the consumer peek bounds the gap
between two ADJACENT entries arriving from a reader. Because they share a
value, the consumer cannot wait meaningfully longer for the next entry
than the producer is allowed to spend producing one. Walking a region
dense with non-listable internal items can make a HEALTHY drive miss the
budget between visible entries; the consumer then declares it stalled and
detaches it, dropping a good drive from the merge and capping the "large
prefix succeeds" guarantee.

Documented, not decoupled: giving the consumer peek an independent,
strictly-larger budget would cut these false detaches but equally delays
detaching a genuinely dead drive and shifts listing tail-latency
semantics, so it wants soak data before changing the default. The comment
records the invariant any such follow-up must keep -- consumer peek >=
producer stall, never stricter -- so it can never fail a drive before the
producer would. No behaviour change.

Refs rustfs/backlog#1217
Co-Authored-By: heihutu <heihutu@gmail.com>

* fix(io-metrics): add time-based trigger for low-IOPS latency percentiles

Percentiles were recomputed only every 128 IOs and seeded to 0, so a
low-traffic deployment exported p95/p99 = 0/stale for a long time after
startup. Add a 10s wall-clock trigger alongside the count throttle so the
first recompute can fire before 128 samples accrue. Hot-path per-op mean
update is unchanged.

Refs rustfs/backlog#1218
Co-Authored-By: heihutu <heihutu@gmail.com>

* test(e2e): cover codec-streaming parity under fault injection and NoSuchKey

The codec-streaming compat A/B previously ran only against a healthy
4-disk EC set with successful full GETs: the DiskFaultHarness was
constructed but never faulted, the error path was untested, and the
range assertion silently compared legacy-vs-legacy (ranges always fall
back to the duplex path), overstating what it proved.

Add two genuinely-failable scenarios reusing the existing harness and
fixtures:

- Parity reconstruction A/B: take one data disk offline and re-run the
  full object matrix on both phases while the EC 2+2 set rebuilds each
  large object from the surviving shards. Assert codec == legacy
  byte-for-byte (sha256) and header-for-header, and assert the codec
  phase served the reconstructed objects with zero duplex-pipe fallback
  (the reader gate is drive-health-independent, so the codec fast path
  is really exercised through reconstruction).
- NoSuchKey negative path: compare the HTTP status + S3 error code of a
  missing-key GET across the legacy and codec phases and require them to
  be identical (404/NoSuchKey), guarding against the codec env
  perturbing the error path.

Also clarify the range-phase comment so it is not misread as
codec-range correctness coverage: both sides are served by the same
legacy range path, so the assertion only proves ranges keep working and
keep falling back to legacy with the gates open.

Verified: cargo check/--no-run pass and the test passes locally
(1 passed; dup_codec=0 confirms the codec path ran).

Refs rustfs/backlog#1219
Co-Authored-By: heihutu <heihutu@gmail.com>

* ci(ecstore): exercise native O_DIRECT read path on an ext4 loopback

The uring-integration leg ran on the runner's default TMPDIR, which may sit
on tmpfs/overlayfs where open(O_DIRECT) fails and the native read_at_direct
path silently latches off to the aligned StdBackend fallback. Mount a
dedicated ext4 loopback and point TMPDIR at it so the real io_uring dep
(bumped git->0.1.0->0.2.0->0.2.1) and the native O_DIRECT read path are
actually covered rather than validated only by signature diffing.

Refs rustfs/backlog#1220
Co-Authored-By: heihutu <heihutu@gmail.com>

---------

Co-authored-by: heihutu <heihutu@gmail.com>
2026-07-12 16:03:28 +00:00

564 lines
23 KiB
Rust

// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::config::ObjectDataCacheConfig;
use crate::metrics::record_memory_pressure;
use crate::stats::ObjectDataCacheStats;
use std::sync::Arc;
#[cfg(test)]
use std::sync::Mutex;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
use sysinfo::System;
const DEFAULT_REFRESH_INTERVAL: Duration = Duration::from_secs(5);
/// Source used to resolve the effective memory limits.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum MemoryBasis {
/// Limits come from host memory reported by sysinfo.
Host,
/// Limits come from a constraining cgroup (container) memory limit.
Cgroup,
}
impl MemoryBasis {
pub(crate) const fn as_str(self) -> &'static str {
match self {
Self::Host => "host",
Self::Cgroup => "cgroup",
}
}
}
/// Effective memory totals after reconciling host memory with cgroup limits.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct EffectiveMemory {
/// Effective total memory in bytes.
pub(crate) total_bytes: u64,
/// Effective available memory in bytes.
pub(crate) available_bytes: u64,
/// Whether the limits are host- or cgroup-derived.
pub(crate) basis: MemoryBasis,
}
/// Reconciles host memory with an optional cgroup limit.
///
/// `cgroup` carries `(total_memory, free_memory)` as reported for the cgroup
/// hierarchy (sysinfo already caps `total_memory` at the host total). A cgroup
/// only counts when it actually constrains below the host, so an unlimited
/// cgroup transparently falls back to host values.
pub(crate) fn select_effective_memory(host_total: u64, host_available: u64, cgroup: Option<(u64, u64)>) -> EffectiveMemory {
match cgroup {
Some((cgroup_total, cgroup_free)) if cgroup_total > 0 && cgroup_total < host_total => EffectiveMemory {
total_bytes: cgroup_total,
available_bytes: cgroup_free.min(cgroup_total),
basis: MemoryBasis::Cgroup,
},
_ => EffectiveMemory {
total_bytes: host_total,
available_bytes: host_available,
basis: MemoryBasis::Host,
},
}
}
/// Resolves the effective memory from an already-refreshed system handle.
///
/// `cgroup_limits()` is computed fresh on each call and is only implemented on
/// Linux (it returns `None` elsewhere), so non-Linux hosts always use host
/// values.
pub(crate) fn effective_memory_from_system(system: &System) -> EffectiveMemory {
let cgroup = system.cgroup_limits().map(|limits| (limits.total_memory, limits.free_memory));
select_effective_memory(system.total_memory(), system.available_memory(), cgroup)
}
/// Resolves the effective memory using a fresh, memory-refreshed system handle.
pub(crate) fn resolve_effective_memory() -> EffectiveMemory {
let mut system = System::new();
system.refresh_memory();
effective_memory_from_system(&system)
}
/// Immutable memory snapshot used by the cache fill gate.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ObjectDataCacheMemorySnapshot {
/// Total system memory in bytes.
pub total_bytes: u64,
/// Available system memory in bytes.
pub available_bytes: u64,
}
impl ObjectDataCacheMemorySnapshot {
/// Returns the available memory percentage.
pub fn available_percent(&self) -> u8 {
if self.total_bytes == 0 {
return 0;
}
let percent = self.available_bytes.saturating_mul(100) / self.total_bytes;
u8::try_from(percent.min(100)).unwrap_or(100)
}
}
/// Lock-free memory snapshot shared between the gate and its refresher task.
#[derive(Debug)]
struct MemorySnapshotCell {
total_bytes: AtomicU64,
available_bytes: AtomicU64,
/// Bytes admitted by the gate since the last snapshot refresh. The snapshot
/// is sampled at most every 5 s, so a burst that begins while it still reads
/// high could all pass a plain check-then-act gate and over-allocate before
/// the next refresh. Subtracting this running total from `available_bytes`
/// shrinks the effective budget as the burst proceeds, bounding cumulative
/// admission to the real headroom; the refresh resets it because the fresh
/// reading already reflects those allocations (backlog#1107).
admitted_since_refresh: AtomicU64,
}
impl MemorySnapshotCell {
fn new(snapshot: ObjectDataCacheMemorySnapshot) -> Self {
Self {
total_bytes: AtomicU64::new(snapshot.total_bytes),
available_bytes: AtomicU64::new(snapshot.available_bytes),
admitted_since_refresh: AtomicU64::new(0),
}
}
/// Stores a fresh snapshot and resets the admitted-bytes counter: the new
/// reading already accounts for whatever was admitted since the last one.
fn store(&self, snapshot: ObjectDataCacheMemorySnapshot) {
self.total_bytes.store(snapshot.total_bytes, Ordering::Relaxed);
self.available_bytes.store(snapshot.available_bytes, Ordering::Relaxed);
self.admitted_since_refresh.store(0, Ordering::Relaxed);
}
fn load(&self) -> ObjectDataCacheMemorySnapshot {
ObjectDataCacheMemorySnapshot {
total_bytes: self.total_bytes.load(Ordering::Relaxed),
available_bytes: self.available_bytes.load(Ordering::Relaxed),
}
}
fn admitted(&self) -> u64 {
self.admitted_since_refresh.load(Ordering::Relaxed)
}
fn reserve(&self, bytes: u64) {
self.admitted_since_refresh.fetch_add(bytes, Ordering::Relaxed);
}
}
/// Aborts the periodic refresher when the gate (and thus the cache) is dropped.
#[derive(Debug)]
struct RefresherGuard(tokio::task::JoinHandle<()>);
impl Drop for RefresherGuard {
fn drop(&mut self) {
self.0.abort();
}
}
/// Memory gate that keeps a cheap, lock-free snapshot for fill-path checks.
///
/// The snapshot is sampled off the fill path by a dedicated periodic refresher
/// (the private `spawn_refresher` task) that
/// runs the blocking `sysinfo` read on a `spawn_blocking` thread. `allows_fill`
/// only reads atomics, so it never blocks a tokio worker and concurrent fills
/// never serialize on a refresh.
#[derive(Debug)]
pub struct ObjectDataCacheMemoryGate {
snapshot: Arc<MemorySnapshotCell>,
min_free_memory_percent: u8,
stats: Arc<ObjectDataCacheStats>,
/// Kept alive so the refresher runs for the gate's lifetime; `None` when the
/// gate is opted out (`min_free_memory_percent == 0`) or no tokio runtime is
/// available at construction (e.g. synchronous unit tests).
_refresher: Option<RefresherGuard>,
#[cfg(test)]
test_override: Mutex<Option<ObjectDataCacheMemorySnapshot>>,
}
impl ObjectDataCacheMemoryGate {
/// Creates a new memory gate.
pub fn new(config: &ObjectDataCacheConfig, stats: Arc<ObjectDataCacheStats>) -> Self {
// Seed the snapshot once at construction. This is the only blocking
// sysinfo read on any caller's stack; all later refreshes run off-path.
let effective = resolve_effective_memory();
let snapshot = Arc::new(MemorySnapshotCell::new(ObjectDataCacheMemorySnapshot {
total_bytes: effective.total_bytes,
available_bytes: effective.available_bytes,
}));
let min_free_memory_percent = config.min_free_memory_percent;
// A zero floor opts out of the gate entirely, so there is nothing to
// refresh; skip the background task in that case.
let refresher = if min_free_memory_percent == 0 {
None
} else {
Self::spawn_refresher(Arc::clone(&snapshot), DEFAULT_REFRESH_INTERVAL)
};
Self {
snapshot,
min_free_memory_percent,
stats,
_refresher: refresher,
#[cfg(test)]
test_override: Mutex::new(None),
}
}
/// Spawns the periodic refresher, returning `None` when no tokio runtime is
/// available (the gate then keeps its construction-time seed snapshot).
fn spawn_refresher(snapshot: Arc<MemorySnapshotCell>, interval: Duration) -> Option<RefresherGuard> {
let handle = tokio::runtime::Handle::try_current().ok()?;
let task = handle.spawn(async move {
let mut ticker = tokio::time::interval(interval);
ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
// The first tick fires immediately; the seed snapshot is already in
// place, so refresh from the second tick onward.
ticker.tick().await;
loop {
ticker.tick().await;
// Run the blocking /proc/meminfo read off the async worker. A
// join error only happens if the runtime is shutting down; keep
// the last snapshot rather than clobbering it in that case.
if let Ok(effective) = tokio::task::spawn_blocking(resolve_effective_memory).await {
snapshot.store(ObjectDataCacheMemorySnapshot {
total_bytes: effective.total_bytes,
available_bytes: effective.available_bytes,
});
}
}
});
Some(RefresherGuard(task))
}
/// Returns the current atomic memory snapshot.
pub fn snapshot(&self) -> ObjectDataCacheMemorySnapshot {
#[cfg(test)]
{
if let Some(snapshot) = *lock_or_recover(&self.test_override) {
return snapshot;
}
}
self.snapshot.load()
}
/// Returns true when the fill path may proceed under current memory pressure.
///
/// This is lock-free and does no blocking sysinfo read: it only reads the
/// atomic snapshot maintained by the periodic refresher.
///
/// `cache_growth_headroom` is how many more bytes the cache itself can hold
/// before it is at capacity (`max_capacity - weighted_size()`). It caps how
/// far the in-window reservation may shrink the budget: see the reservation
/// note below.
pub fn allows_fill(&self, required_bytes: u64, cache_growth_headroom: u64) -> bool {
// A zero floor opts out of the gate, so fill admission never depends on
// a live memory reading — which differs between a host and a container.
// This must short-circuit before any snapshot read (see 51a97a81c).
if self.min_free_memory_percent == 0 {
return true;
}
let snapshot = self.snapshot();
if snapshot.total_bytes == 0 {
return true;
}
// Effective budget is the snapshot's available memory minus what the
// gate has already admitted since that snapshot was taken. This bounds a
// burst that arrives faster than the 5 s refresh: each admission shrinks
// the budget the next one sees, so cumulative admission cannot exceed the
// real headroom even though every fill reads the same (stale) snapshot.
//
// `admitted_since_refresh` counts GROSS admitted bytes and never rolls
// back on a fill that is later evicted, cancelled, or loses the
// invalidation race; it only resets on the 5 s refresh. Under sustained
// churn (net footprint flat, far below capacity) the raw counter would
// balloon past the real memory the cache consumes and falsely trip the
// gate, skipping the hottest fills until the next refresh. The cache can
// never hold more than `max_capacity`, so a burst adds at most
// `cache_growth_headroom` bytes of real memory before moka evicts to stay
// bounded (net-zero churn beyond that point). Capping the deduction there
// keeps the reservation honest without treating gross churn as growth
// (backlog#1212).
let reserved = self.snapshot.admitted().min(cache_growth_headroom);
let effective_available = snapshot.available_bytes.saturating_sub(reserved);
let min_free = u64::from(self.min_free_memory_percent);
let has_percent_budget = effective_available.saturating_mul(100) >= snapshot.total_bytes.saturating_mul(min_free);
let has_entry_budget = effective_available >= required_bytes;
let allowed = has_percent_budget && has_entry_budget;
if allowed {
// Reserve the admitted bytes so a concurrent fill sees a smaller
// budget; the reservation clears on the next snapshot refresh.
self.snapshot.reserve(required_bytes);
} else {
record_memory_pressure(&self.stats, "moka");
}
allowed
}
#[cfg(test)]
pub fn set_test_snapshot(&self, snapshot: Option<ObjectDataCacheMemorySnapshot>) {
*lock_or_recover(&self.test_override) = snapshot;
}
/// Writes the atomic snapshot directly, bypassing the `test_override` read
/// path so a test can observe whether `allows_fill` mutates the snapshot.
#[cfg(test)]
fn store_raw_snapshot_for_test(&self, snapshot: ObjectDataCacheMemorySnapshot) {
self.snapshot.store(snapshot);
}
/// Reads the atomic snapshot directly, bypassing `test_override`.
#[cfg(test)]
fn raw_snapshot_for_test(&self) -> ObjectDataCacheMemorySnapshot {
self.snapshot.load()
}
}
#[cfg(test)]
fn lock_or_recover<T>(mutex: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
match mutex.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
}
}
#[cfg(test)]
mod tests {
use super::{MemoryBasis, ObjectDataCacheMemoryGate, ObjectDataCacheMemorySnapshot, select_effective_memory};
use crate::config::ObjectDataCacheConfig;
use crate::stats::ObjectDataCacheStats;
use std::sync::Arc;
use std::time::Duration;
const GIB: u64 = 1024 * 1024 * 1024;
#[test]
fn select_effective_memory_prefers_constraining_cgroup() {
let effective = select_effective_memory(64 * GIB, 40 * GIB, Some((2 * GIB, GIB)));
assert_eq!(effective.basis, MemoryBasis::Cgroup);
assert_eq!(effective.total_bytes, 2 * GIB);
assert_eq!(effective.available_bytes, GIB);
}
#[test]
fn select_effective_memory_ignores_non_constraining_cgroup() {
// A cgroup total equal to (or above) the host total means no real limit.
let effective = select_effective_memory(64 * GIB, 40 * GIB, Some((64 * GIB, 10 * GIB)));
assert_eq!(effective.basis, MemoryBasis::Host);
assert_eq!(effective.total_bytes, 64 * GIB);
assert_eq!(effective.available_bytes, 40 * GIB);
}
#[test]
fn select_effective_memory_falls_back_to_host_without_cgroup() {
let effective = select_effective_memory(8 * GIB, 4 * GIB, None);
assert_eq!(effective.basis, MemoryBasis::Host);
assert_eq!(effective.total_bytes, 8 * GIB);
assert_eq!(effective.available_bytes, 4 * GIB);
}
#[test]
fn select_effective_memory_caps_available_at_total() {
let effective = select_effective_memory(64 * GIB, 40 * GIB, Some((2 * GIB, 3 * GIB)));
assert_eq!(effective.total_bytes, 2 * GIB);
assert_eq!(effective.available_bytes, 2 * GIB);
}
#[test]
fn gate_pauses_fill_when_container_memory_is_low() {
// Simulate a pod-sized snapshot (256 MiB total, 16 MiB free): below the
// default 20% free-memory floor, so fills must pause even though a node
// would have plenty of headroom.
let stats = Arc::new(ObjectDataCacheStats::default());
let gate = ObjectDataCacheMemoryGate::new(&ObjectDataCacheConfig::default(), Arc::clone(&stats));
gate.set_test_snapshot(Some(ObjectDataCacheMemorySnapshot {
total_bytes: 256 * 1024 * 1024,
available_bytes: 16 * 1024 * 1024,
}));
assert!(!gate.allows_fill(1024, u64::MAX));
assert_eq!(stats.snapshot().memory_pressure_events, 1);
}
#[test]
fn allows_fill_when_memory_snapshot_has_headroom() {
let stats = Arc::new(ObjectDataCacheStats::default());
let gate = ObjectDataCacheMemoryGate::new(&ObjectDataCacheConfig::default(), Arc::clone(&stats));
gate.set_test_snapshot(Some(ObjectDataCacheMemorySnapshot {
total_bytes: 1_000,
available_bytes: 500,
}));
assert!(gate.allows_fill(100, u64::MAX));
}
#[test]
fn zero_min_free_percent_disables_the_gate() {
// A pod-sized snapshot far below any floor: the gate is opted out, so
// fill admission stays independent of the live memory reading.
let stats = Arc::new(ObjectDataCacheStats::default());
let gate = ObjectDataCacheMemoryGate::new(
&ObjectDataCacheConfig {
min_free_memory_percent: 0,
..ObjectDataCacheConfig::default()
},
Arc::clone(&stats),
);
gate.set_test_snapshot(Some(ObjectDataCacheMemorySnapshot {
total_bytes: 1_000,
available_bytes: 1,
}));
assert!(gate.allows_fill(512, u64::MAX));
assert_eq!(stats.snapshot().memory_pressure_events, 0);
}
#[test]
fn blocks_fill_under_memory_pressure() {
let stats = Arc::new(ObjectDataCacheStats::default());
let gate = ObjectDataCacheMemoryGate::new(&ObjectDataCacheConfig::default(), Arc::clone(&stats));
gate.set_test_snapshot(Some(ObjectDataCacheMemorySnapshot {
total_bytes: 1_000,
available_bytes: 100,
}));
assert!(!gate.allows_fill(128, u64::MAX));
assert_eq!(stats.snapshot().memory_pressure_events, 1);
}
// backlog#1212: `admitted_since_refresh` counts GROSS admitted bytes and
// never rolls back on an evicted/cancelled/lost-race fill, so a churn window
// (net footprint flat, far below capacity) balloons the raw counter past the
// memory the cache actually holds. Deducting it wholesale falsely trips the
// gate; capping the deduction at the cache's growth headroom fixes it.
#[test]
fn reservation_deduction_capped_at_growth_headroom_avoids_false_pressure() {
let stats = Arc::new(ObjectDataCacheStats::default());
let gate = ObjectDataCacheMemoryGate::new(&ObjectDataCacheConfig::default(), Arc::clone(&stats));
gate.set_test_snapshot(Some(ObjectDataCacheMemorySnapshot {
total_bytes: 1_000_000,
available_bytes: 500_000, // 50% free, well above the 20% floor
}));
// A churn window admitted far more GROSS bytes than the cache can hold;
// repeated insert/evict never rolled the counter back, so it now dwarfs
// the real available memory even though the live footprint stays tiny.
gate.snapshot.reserve(10_000_000);
// Uncapped, the raw gross counter swamps the budget and falsely signals
// memory pressure even though the cache's net footprint is flat.
assert!(
!gate.allows_fill(1_000, u64::MAX),
"raw gross admitted-bytes deduction should falsely suppress (the bug being fixed)"
);
// Capping the deduction at the cache's growth headroom (net size flat,
// far below capacity) restores admission: gross churn is no longer
// mistaken for real memory growth.
assert!(
gate.allows_fill(1_000, 100_000),
"capping the reservation at cache growth headroom must not falsely suppress"
);
}
// backlog#1212: capping the deduction must not defeat the reservation under
// genuine cache growth. When the cache still has room to grow, the in-window
// reservation must still shrink the budget so a burst cannot over-admit.
#[test]
fn reservation_still_bounds_burst_within_growth_headroom() {
let stats = Arc::new(ObjectDataCacheStats::default());
let gate = ObjectDataCacheMemoryGate::new(&ObjectDataCacheConfig::default(), Arc::clone(&stats));
gate.set_test_snapshot(Some(ObjectDataCacheMemorySnapshot {
total_bytes: 1_000_000,
available_bytes: 300_000, // 30% free; floor is 20% = 200_000
}));
// The cache can still grow well past the reserved amount, so the cap does
// not bind and the reservation is deducted in full.
gate.snapshot.reserve(150_000);
// 300_000 available - 150_000 reserved = 150_000 effective, below the
// 200_000 floor: the reservation must still suppress the fill.
assert!(
!gate.allows_fill(1_000, u64::MAX),
"reservation must still bound a burst while the cache can genuinely grow"
);
}
// ODC-14: `allows_fill` must read the atomic snapshot without performing an
// inline (blocking) refresh. A synchronous test has no tokio runtime, so no
// refresher task exists; if `allows_fill` refreshed inline it would clobber
// the seeded atomic snapshot with the real host reading. Comparing exact
// byte values makes the assertion independent of the host's actual memory.
#[test]
fn allows_fill_reads_snapshot_without_refreshing_inline() {
let stats = Arc::new(ObjectDataCacheStats::default());
let gate = ObjectDataCacheMemoryGate::new(&ObjectDataCacheConfig::default(), Arc::clone(&stats));
let sentinel = ObjectDataCacheMemorySnapshot {
total_bytes: 4_242,
available_bytes: 2_121,
};
gate.store_raw_snapshot_for_test(sentinel);
// Exercise the gate on the atomic path (no test_override installed).
let _ = gate.allows_fill(1, u64::MAX);
let after = gate.raw_snapshot_for_test();
assert_eq!(
after, sentinel,
"allows_fill must not mutate the snapshot; an inline refresh would overwrite it"
);
}
// ODC-14: the periodic refresher samples memory off the fill path and
// updates the atomic snapshot, so a stale seed is replaced without any
// fill ever blocking on a refresh.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn periodic_refresher_updates_snapshot_off_path() {
let stats = Arc::new(ObjectDataCacheStats::default());
let snapshot = Arc::new(super::MemorySnapshotCell::new(ObjectDataCacheMemorySnapshot {
total_bytes: 1,
available_bytes: 1,
}));
let _guard = ObjectDataCacheMemoryGate::spawn_refresher(Arc::clone(&snapshot), Duration::from_millis(20))
.expect("a tokio runtime is available in an async test");
// The seed is a bogus 1/1; wait for the refresher to overwrite it with a
// real host reading (total memory is always far above 1 byte).
let mut refreshed = false;
for _ in 0..200 {
if snapshot.load().total_bytes > 1 {
refreshed = true;
break;
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
assert!(refreshed, "the periodic refresher must replace the seed snapshot off the fill path");
let _ = stats;
}
}