mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-08 06:13:14 +00:00
fix(object-data-cache): make the GET key write-unique and dedup the lookup (#4693)
fix(object-data-cache): make GET body cache key write-unique and dedup lookups Address four object-data-cache GET-path findings (backlog#1107 batch): ODC-06 (backlog#1111): the cache key was content-unique, not write-unique. Extend ObjectDataCacheKey with the resolved version's modification time (i128 unix nanoseconds, None -> 0), derived once in the shared planner so the ecstore hook and the usecase layer produce an identical key. An unversioned overwrite advances mod_time, so a stale node can no longer serve old bytes for up to the TTL under an MD5 collision; etag + size stay as belt-and-braces. ODC-16 (backlog#1121): every cacheable GET planned and looked up twice (once in the ecstore hook, once in the usecase layer), double-counting hits, hit_bytes and lookups. GetObjectReader now carries a GetObjectBodySource marker (Unprobed / HookMissed / HookServed); the hook stamps it, and build_get_object_body_with_cache serves a hook-served body directly and skips its lookup whenever the hook already probed. One hook-served GET now records exactly one lookup. ODC-19 (backlog#1124): ENABLE=true with no explicit mode defaulted to HitOnly, which never fills and keeps a permanent 0% hit rate. Default to FillBufferedOnly, log the resolved mode at startup, and warn when HitOnly is selected explicitly. ODC-24 (backlog#1129): max_entry_bytes above the in-memory GET fill limits was silently inert. Clamp the planner's size eligibility to min(max_entry_bytes, seek-support threshold, 64 MiB buffer cap) so ineligible sizes plan SkipTooLarge instead of being reported eligible, and warn at startup when the excess is inert. Co-authored-by: heihutu <heihutu@gmail.com>
This commit is contained in:
@@ -45,6 +45,16 @@ pub struct ObjectDataCache {
|
||||
backend: ObjectDataCacheBackendKind,
|
||||
config: Arc<ObjectDataCacheConfig>,
|
||||
stats: Arc<ObjectDataCacheStats>,
|
||||
/// Effective fill ceiling in bytes: a body larger than this is planned
|
||||
/// `SkipTooLarge` even when it fits `max_entry_bytes`. The app layer sets it
|
||||
/// to `min(max_entry_bytes, seek-support threshold, 64 MiB buffer cap)` so a
|
||||
/// `max_entry_bytes` above the in-memory GET fill limits is not reported as
|
||||
/// eligible while fill could never materialize it (backlog#1129 / ODC-24).
|
||||
/// `0` means "no additional clamp" — eligibility rests on `max_entry_bytes`
|
||||
/// alone (the default, used by engine-level tests). The concurrency-driven
|
||||
/// shrink of the fill threshold is dynamic and cannot be captured at plan
|
||||
/// time; this static ceiling is the conservative floor.
|
||||
fill_ceiling_bytes: u64,
|
||||
/// Monotonic origin for the cache-state publish debounce.
|
||||
created_at: Instant,
|
||||
/// Millis since `created_at` of the last cache-state gauge publish, or `0`
|
||||
@@ -62,6 +72,7 @@ impl ObjectDataCache {
|
||||
backend: ObjectDataCacheBackendKind::Noop(NoopBackend),
|
||||
config,
|
||||
stats,
|
||||
fill_ceiling_bytes: 0,
|
||||
created_at: Instant::now(),
|
||||
last_entry_publish_ms: AtomicU64::new(0),
|
||||
}
|
||||
@@ -82,11 +93,32 @@ impl ObjectDataCache {
|
||||
backend,
|
||||
config: Arc::new(config),
|
||||
stats,
|
||||
fill_ceiling_bytes: 0,
|
||||
created_at: Instant::now(),
|
||||
last_entry_publish_ms: AtomicU64::new(0),
|
||||
})
|
||||
}
|
||||
|
||||
/// Sets the effective fill ceiling (backlog#1129 / ODC-24). The app layer
|
||||
/// applies this at startup so a body above the in-memory GET fill limits is
|
||||
/// planned `SkipTooLarge` instead of being reported eligible. `0` disables
|
||||
/// the extra clamp.
|
||||
#[must_use]
|
||||
pub fn with_fill_ceiling_bytes(mut self, fill_ceiling_bytes: u64) -> Self {
|
||||
self.fill_ceiling_bytes = fill_ceiling_bytes;
|
||||
self
|
||||
}
|
||||
|
||||
/// Effective size above which a body is planned `SkipTooLarge`:
|
||||
/// `max_entry_bytes` clamped by the fill ceiling when one is set.
|
||||
fn effective_size_ceiling(&self) -> u64 {
|
||||
if self.fill_ceiling_bytes == 0 {
|
||||
self.config.max_entry_bytes
|
||||
} else {
|
||||
self.config.max_entry_bytes.min(self.fill_ceiling_bytes)
|
||||
}
|
||||
}
|
||||
|
||||
/// Produces a lightweight GET plan from request metadata.
|
||||
pub fn plan_get(&self, request: ObjectDataCacheGetRequest<'_>) -> ObjectDataCacheGetPlan {
|
||||
if self.config.is_disabled() {
|
||||
@@ -100,7 +132,11 @@ impl ObjectDataCache {
|
||||
return ObjectDataCacheGetPlan::Disabled;
|
||||
}
|
||||
|
||||
if request.size > self.config.max_entry_bytes {
|
||||
// ODC-24: clamp eligibility to the effective fill ceiling, not just
|
||||
// `max_entry_bytes`. A body in the gap between `max_entry_bytes` and the
|
||||
// in-memory GET fill limits could never fill, so admitting it here would
|
||||
// report it "eligible" while it kept a permanent 0% hit rate.
|
||||
if request.size > self.effective_size_ceiling() {
|
||||
record_plan_decision(self.backend.as_metric_label(), self.config.mode, "skip", "too_large", request.size);
|
||||
return ObjectDataCacheGetPlan::SkipTooLarge;
|
||||
}
|
||||
@@ -108,12 +144,13 @@ impl ObjectDataCache {
|
||||
record_plan_decision(self.backend.as_metric_label(), self.config.mode, "cacheable", "eligible", request.size);
|
||||
|
||||
ObjectDataCacheGetPlan::Cacheable {
|
||||
key: ObjectDataCacheKey::new(
|
||||
key: ObjectDataCacheKey::with_mod_time(
|
||||
request.bucket,
|
||||
request.object,
|
||||
request.version_id.as_deref(),
|
||||
request.etag,
|
||||
request.size,
|
||||
request.mod_time_unix_nanos,
|
||||
request.body_variant,
|
||||
),
|
||||
}
|
||||
@@ -308,6 +345,10 @@ pub struct ObjectDataCacheGetRequest<'a> {
|
||||
pub etag: &'a str,
|
||||
/// Object size in bytes.
|
||||
pub size: u64,
|
||||
/// Resolved version's modification time as Unix nanoseconds, or `0` when
|
||||
/// absent. Carried into the key so it is write-unique (backlog#1111 /
|
||||
/// ODC-06).
|
||||
pub mod_time_unix_nanos: i128,
|
||||
/// Supported response body variant.
|
||||
pub body_variant: ObjectDataCacheBodyVariant,
|
||||
}
|
||||
@@ -466,6 +507,7 @@ mod tests {
|
||||
version_id: None,
|
||||
etag,
|
||||
size,
|
||||
mod_time_unix_nanos: 0,
|
||||
body_variant: ObjectDataCacheBodyVariant::FullObjectPlainV1,
|
||||
}
|
||||
}
|
||||
@@ -645,6 +687,7 @@ mod tests {
|
||||
version_id: None,
|
||||
etag: "etag",
|
||||
size: 5,
|
||||
mod_time_unix_nanos: 0,
|
||||
body_variant: ObjectDataCacheBodyVariant::FullObjectPlainV1,
|
||||
});
|
||||
|
||||
@@ -654,4 +697,56 @@ mod tests {
|
||||
assert_eq!(fill, ObjectDataCacheFillResult::SkippedSizeMismatch);
|
||||
assert!(matches!(lookup, ObjectDataCacheLookup::Miss));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plan_clamps_size_eligibility_to_fill_ceiling() {
|
||||
// ODC-24 (backlog#1129): a body in the gap between `max_entry_bytes` and
|
||||
// the effective fill ceiling must plan `SkipTooLarge`, not `Cacheable`,
|
||||
// so it stops being reported as eligible while it can never fill.
|
||||
let config = ObjectDataCacheConfig {
|
||||
mode: ObjectDataCacheMode::HitOnly,
|
||||
max_bytes: 32 * 1024 * 1024,
|
||||
max_memory_percent: 0,
|
||||
max_entry_bytes: 16 * 1024 * 1024,
|
||||
..ObjectDataCacheConfig::default()
|
||||
};
|
||||
let cache = ObjectDataCache::new(config)
|
||||
.expect("clamped cache config should initialize")
|
||||
.with_fill_ceiling_bytes(4 * 1024 * 1024);
|
||||
|
||||
// Within the ceiling: eligible.
|
||||
assert!(matches!(
|
||||
cache.plan_get(plain_request("bucket", "object", "etag", 4 * 1024 * 1024)),
|
||||
ObjectDataCacheGetPlan::Cacheable { .. }
|
||||
));
|
||||
// In the gap (ceiling < size <= max_entry_bytes): skipped as too large.
|
||||
assert_eq!(
|
||||
cache.plan_get(plain_request("bucket", "object", "etag", 4 * 1024 * 1024 + 1)),
|
||||
ObjectDataCacheGetPlan::SkipTooLarge
|
||||
);
|
||||
assert_eq!(
|
||||
cache.plan_get(plain_request("bucket", "object", "etag", 16 * 1024 * 1024)),
|
||||
ObjectDataCacheGetPlan::SkipTooLarge
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plan_carries_mod_time_into_key() {
|
||||
// ODC-06: the resolved modification time must reach the key so two
|
||||
// writes with identical etag + size derive different keys.
|
||||
let cache = hit_only_cache();
|
||||
let request = ObjectDataCacheGetRequest {
|
||||
bucket: "bucket",
|
||||
object: "object",
|
||||
version_id: None,
|
||||
etag: "etag",
|
||||
size: 5,
|
||||
mod_time_unix_nanos: 42,
|
||||
body_variant: ObjectDataCacheBodyVariant::FullObjectPlainV1,
|
||||
};
|
||||
let ObjectDataCacheGetPlan::Cacheable { key } = cache.plan_get(request) else {
|
||||
panic!("plan should be cacheable");
|
||||
};
|
||||
assert_eq!(key.mod_time_unix_nanos, 42);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,16 @@ pub enum ObjectDataCacheBodyVariant {
|
||||
}
|
||||
|
||||
/// Stable cache key for a reusable object body.
|
||||
///
|
||||
/// The key is *write-unique*, not merely *content-unique* (backlog#1111 /
|
||||
/// ODC-06). `etag + size` alone identify content: for an unversioned overwrite
|
||||
/// two same-length payloads that collide on MD5 would derive the identical key,
|
||||
/// so a GET on a node that never observed the overwrite could serve the old
|
||||
/// bytes for up to the TTL, and the same collision turns the
|
||||
/// fill-after-invalidation race (backlog#1118) into a serving bug. Including
|
||||
/// the resolved version's modification time distinguishes two writes even under
|
||||
/// an MD5 collision, because an overwrite advances `mod_time`. `etag + size`
|
||||
/// stay in the key as belt-and-braces.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct ObjectDataCacheKey {
|
||||
/// Bucket name.
|
||||
@@ -38,6 +48,11 @@ pub struct ObjectDataCacheKey {
|
||||
pub etag: Arc<str>,
|
||||
/// Object size in bytes.
|
||||
pub size: u64,
|
||||
/// Resolved version's modification time as Unix nanoseconds
|
||||
/// (`OffsetDateTime::unix_timestamp_nanos`), or `0` when absent. This is the
|
||||
/// write-unique component: an overwrite advances `mod_time`, so the key
|
||||
/// changes even when `etag + size` are unchanged (an MD5 collision).
|
||||
pub mod_time_unix_nanos: i128,
|
||||
/// Cached body semantics.
|
||||
pub body_variant: ObjectDataCacheBodyVariant,
|
||||
}
|
||||
@@ -52,13 +67,17 @@ pub struct ObjectDataCacheIdentity {
|
||||
}
|
||||
|
||||
impl ObjectDataCacheKey {
|
||||
/// Creates a new stable object data cache key.
|
||||
pub fn new(
|
||||
/// Creates a stable object data cache key with an explicit modification
|
||||
/// time. This is the full constructor; the production GET planner uses it so
|
||||
/// the key is write-unique (backlog#1111 / ODC-06).
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn with_mod_time(
|
||||
bucket: impl Into<Arc<str>>,
|
||||
object: impl Into<Arc<str>>,
|
||||
version_id: Option<&str>,
|
||||
etag: impl Into<Arc<str>>,
|
||||
size: u64,
|
||||
mod_time_unix_nanos: i128,
|
||||
body_variant: ObjectDataCacheBodyVariant,
|
||||
) -> Self {
|
||||
Self {
|
||||
@@ -67,10 +86,25 @@ impl ObjectDataCacheKey {
|
||||
version_id: version_id.map_or_else(|| Arc::<str>::from(NULL_VERSION_ID), Arc::<str>::from),
|
||||
etag: etag.into(),
|
||||
size,
|
||||
mod_time_unix_nanos,
|
||||
body_variant,
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a stable object data cache key with no modification time
|
||||
/// (`mod_time_unix_nanos == 0`). Retained for callers that key purely by
|
||||
/// content identity (index/backend tests).
|
||||
pub fn new(
|
||||
bucket: impl Into<Arc<str>>,
|
||||
object: impl Into<Arc<str>>,
|
||||
version_id: Option<&str>,
|
||||
etag: impl Into<Arc<str>>,
|
||||
size: u64,
|
||||
body_variant: ObjectDataCacheBodyVariant,
|
||||
) -> Self {
|
||||
Self::with_mod_time(bucket, object, version_id, etag, size, 0, body_variant)
|
||||
}
|
||||
|
||||
/// Returns true when this key targets the canonical unversioned body variant.
|
||||
///
|
||||
/// Only exercised by tests; gated so it is not compiled into the shipping
|
||||
@@ -112,6 +146,51 @@ mod tests {
|
||||
assert_ne!(latest, versioned);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn key_distinguishes_writes_by_mod_time() {
|
||||
// ODC-06 (backlog#1111): two writes with identical etag + size (an MD5
|
||||
// collision on an unversioned overwrite) must derive different keys once
|
||||
// the modification time differs, so a stale node cannot serve old bytes.
|
||||
let old = ObjectDataCacheKey::with_mod_time(
|
||||
"bucket",
|
||||
"object",
|
||||
None,
|
||||
"etag",
|
||||
42,
|
||||
1_000,
|
||||
ObjectDataCacheBodyVariant::FullObjectPlainV1,
|
||||
);
|
||||
let new = ObjectDataCacheKey::with_mod_time(
|
||||
"bucket",
|
||||
"object",
|
||||
None,
|
||||
"etag",
|
||||
42,
|
||||
2_000,
|
||||
ObjectDataCacheBodyVariant::FullObjectPlainV1,
|
||||
);
|
||||
|
||||
assert_ne!(old, new, "keys differing only by mod_time must not collide");
|
||||
// Same mod_time still collapses to one key (a true re-read of one write).
|
||||
let new_again = ObjectDataCacheKey::with_mod_time(
|
||||
"bucket",
|
||||
"object",
|
||||
None,
|
||||
"etag",
|
||||
42,
|
||||
2_000,
|
||||
ObjectDataCacheBodyVariant::FullObjectPlainV1,
|
||||
);
|
||||
assert_eq!(new, new_again);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn key_new_defaults_mod_time_to_zero() {
|
||||
let key = ObjectDataCacheKey::new("bucket", "object", None, "etag", 42, ObjectDataCacheBodyVariant::FullObjectPlainV1);
|
||||
|
||||
assert_eq!(key.mod_time_unix_nanos, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn identity_new_preserves_bucket_and_object() {
|
||||
let identity = ObjectDataCacheIdentity::new("bucket", "object");
|
||||
|
||||
Reference in New Issue
Block a user