This is an automated email from the ASF dual-hosted git repository.

Xuanwo pushed a commit to branch xuanwo/fix-foyer-conditional-read-bypass
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit 5417e0bf362206ff23a8f7aba555db1312a52dfa
Author: Xuanwo <[email protected]>
AuthorDate: Mon Nov 23 04:37:39 2026 +0100

    fix(layers/foyer): bypass cache for conditional reads
    
    `get_or_fetch` deduplicates concurrent fills. When a conditional read leads 
a fill, an unconditional read that joins it receives the same 
`ConditionNotMatch` error even though its request has no precondition. A 
conditional read can also bypass its own precondition by sharing an 
unconditional fill.
    
    Bypass the cache entirely for any read with preconditions so the origin 
evaluates the original args. Conditional reads no longer populate the cache, 
and the fill path only carries the version used in the cache key.
---
 core/layers/foyer/src/full.rs |  34 ++---
 core/layers/foyer/src/lib.rs  | 298 +++++++++++++++++++++++++++++++++++++++---
 2 files changed, 293 insertions(+), 39 deletions(-)

diff --git a/core/layers/foyer/src/full.rs b/core/layers/foyer/src/full.rs
index c7a82dbeae..4dfd87a112 100644
--- a/core/layers/foyer/src/full.rs
+++ b/core/layers/foyer/src/full.rs
@@ -81,17 +81,11 @@ impl FullReader {
             .contains(key)
     }
 
-    /// Derive the `OpStat` used for cache fill from the reader's `OpRead`, so 
the
-    /// stat observes the same conditions (version, if-match, ...) as the 
request.
+    /// Derive the `OpStat` used for cache fill from the reader's `OpRead`, so 
a
+    /// versioned read stats the same version it caches.
     fn stat_args(&self) -> OpStat {
         options::StatOptions {
             version: self.args.version().map(str::to_owned),
-            if_match: self.args.if_match().map(str::to_owned),
-            if_none_match: self.args.if_none_match().map(str::to_owned),
-            if_version_match: self.args.if_version_match().map(str::to_owned),
-            if_version_not_match: 
self.args.if_version_not_match().map(str::to_owned),
-            if_modified_since: self.args.if_modified_since(),
-            if_unmodified_since: self.args.if_unmodified_since(),
             ..Default::default()
         }
         .into()
@@ -133,22 +127,16 @@ impl FullReader {
         }
 
         // A cached entry carries no precondition, so a conditional read must
-        // not be served from it: fall back to the origin, which observes the
-        // original args. On a miss the fill below still validates the
-        // condition at the origin, so only the hit path is skipped.
+        // never use the cache. Bypass both the hit path and `get_or_fetch`:
+        // `get_or_fetch` deduplicates concurrent fills, so a conditional read
+        // that joins an unconditional fill would receive its data without the
+        // condition being checked, and an unconditional read that joins a
+        // conditional fill would inherit a condition failure that does not
+        // apply to it. Send the read straight to the origin, which observes
+        // the original args.
         // `version` is not listed here because it is part of the cache key.
-        if self.args.if_match().is_some()
-            || self.args.if_none_match().is_some()
-            || self.args.if_version_match().is_some()
-            || self.args.if_version_not_match().is_some()
-            || self.args.if_modified_since().is_some()
-            || self.args.if_unmodified_since().is_some()
-        {
-            match self.inner.cache.get(&key).await {
-                Ok(Some(_)) => return Ok(None),
-                Ok(None) => {}
-                Err(e) => return Err(extract_err(e)),
-            }
+        if self.args.is_conditional() {
+            return Ok(None);
         }
 
         let result = self
diff --git a/core/layers/foyer/src/lib.rs b/core/layers/foyer/src/lib.rs
index 93d3b7f038..2046f7e47b 100644
--- a/core/layers/foyer/src/lib.rs
+++ b/core/layers/foyer/src/lib.rs
@@ -160,6 +160,9 @@ impl Code for FoyerValue {
 /// - `read`: [`FoyerLayer`] checks the cache first. On a cache miss, it reads
 ///   and caches the entire object if its size is within the configured
 ///   [`FoyerLayer::with_size_limit`] range, even when only a byte range is 
requested.
+///   Reads with preconditions such as `if_match` or `if_none_match` bypass the
+///   cache and read directly from the service, which evaluates the condition.
+///   A conditional read never populates the cache.
 /// - `delete`: [`FoyerLayer`] removes cached data after a successful delete 
when the deleter
 ///   closes. A failed delete call is not invalidated. Exact-key invalidation 
happens before the
 ///   underlying deleter is closed, so the data remains invalidated if closing 
the deleter fails.
@@ -452,6 +455,8 @@ mod tests {
     use std::sync::Arc;
     use std::sync::atomic::{AtomicUsize, Ordering};
 
+    use tokio::sync::{Semaphore, oneshot};
+
     use super::*;
     use crate::error::extract_err;
 
@@ -478,6 +483,11 @@ mod tests {
     #[derive(Debug)]
     struct MockReadState {
         data: Buffer,
+        etag: String,
+        stat_gate: Mutex<Option<Arc<Semaphore>>>,
+        stat_started: Arc<Semaphore>,
+        condition_gate: Mutex<Option<Arc<Semaphore>>>,
+        condition_started: Arc<Semaphore>,
         stat_calls: AtomicUsize,
         open_calls: AtomicUsize,
         read_calls: AtomicUsize,
@@ -489,6 +499,11 @@ mod tests {
         fn new(data: Buffer) -> Self {
             Self {
                 data,
+                etag: "\"etag-1\"".to_string(),
+                stat_gate: Mutex::new(None),
+                stat_started: Arc::new(Semaphore::new(0)),
+                condition_gate: Mutex::new(None),
+                condition_started: Arc::new(Semaphore::new(0)),
                 stat_calls: AtomicUsize::new(0),
                 open_calls: AtomicUsize::new(0),
                 read_calls: AtomicUsize::new(0),
@@ -497,11 +512,55 @@ mod tests {
             }
         }
 
-        fn metadata(&self) -> Metadata {
+        fn gate_stat(&self) -> Arc<Semaphore> {
+            let gate = Arc::new(Semaphore::new(0));
+            *self.stat_gate.lock().unwrap() = Some(gate.clone());
+            gate
+        }
+
+        fn gate_condition(&self) -> Arc<Semaphore> {
+            let gate = Arc::new(Semaphore::new(0));
+            *self.condition_gate.lock().unwrap() = Some(gate.clone());
+            gate
+        }
+
+        async fn check_conditions(
+            &self,
+            if_match: Option<&str>,
+            if_none_match: Option<&str>,
+        ) -> Result<()> {
+            if if_match.is_some() || if_none_match.is_some() {
+                let gate = self.condition_gate.lock().unwrap().clone();
+                if let Some(gate) = gate {
+                    self.condition_started.add_permits(1);
+                    gate.acquire().await.unwrap().forget();
+                }
+            }
+
+            if let Some(if_match) = if_match
+                && if_match != "*"
+                && if_match != self.etag
+            {
+                return Err(Error::new(
+                    ErrorKind::ConditionNotMatch,
+                    format!("if-match {if_match} does not match {}", 
self.etag),
+                ));
+            }
+            if let Some(if_none_match) = if_none_match
+                && (if_none_match == "*" || if_none_match == self.etag)
             {
-                let metadata = MetadataBuilder::file(self.data.len() as _);
-                metadata.build()
+                return Err(Error::new(
+                    ErrorKind::ConditionNotMatch,
+                    format!("if-none-match {if_none_match} matches {}", 
self.etag),
+                ));
             }
+            Ok(())
+        }
+
+        fn metadata(&self) -> Metadata {
+            let mut metadata = MetadataBuilder::file(self.data.len() as _);
+            metadata.etag(self.etag.clone());
+            metadata.build()
         }
 
         fn rp_read(&self) -> RpRead {
@@ -529,11 +588,15 @@ mod tests {
     #[derive(Debug)]
     struct MockReadReader {
         state: Arc<MockReadState>,
+        args: OpRead,
     }
 
     impl oio::Read for MockReadReader {
         async fn open(&self, range: BytesRange) -> Result<(RpRead, Box<dyn 
oio::ReadStreamDyn>)> {
             self.state.open_calls.fetch_add(1, Ordering::Relaxed);
+            self.state
+                .check_conditions(self.args.if_match(), 
self.args.if_none_match())
+                .await?;
             let buffer = self.state.read_range(range);
             Ok((
                 self.state.rp_read(),
@@ -543,6 +606,9 @@ mod tests {
 
         async fn read(&self, range: BytesRange) -> Result<(RpRead, Buffer)> {
             self.state.read_calls.fetch_add(1, Ordering::Relaxed);
+            self.state
+                .check_conditions(self.args.if_match(), 
self.args.if_none_match())
+                .await?;
             if range.size().is_none() {
                 return Err(Error::new(
                     ErrorKind::Unsupported,
@@ -591,14 +657,25 @@ mod tests {
 
         async fn stat(&self, _: &OperationContext, _: &str, args: OpStat) -> 
Result<RpStat> {
             self.state.stat_calls.fetch_add(1, Ordering::Relaxed);
-            *self.state.last_stat_args.lock().unwrap() = Some(args);
+            *self.state.last_stat_args.lock().unwrap() = Some(args.clone());
+
+            let gate = self.state.stat_gate.lock().unwrap().clone();
+            if let Some(gate) = gate {
+                self.state.stat_started.add_permits(1);
+                gate.acquire().await.unwrap().forget();
+            }
+
+            self.state
+                .check_conditions(args.if_match(), args.if_none_match())
+                .await?;
             Ok(RpStat::new(self.state.metadata()))
         }
 
         fn read(&self, _ctx: &OperationContext, _: &str, args: OpRead) -> 
Result<Self::Reader> {
-            *self.state.last_read_args.lock().unwrap() = Some(args);
+            *self.state.last_read_args.lock().unwrap() = Some(args.clone());
             Ok(MockReadReader {
                 state: self.state.clone(),
+                args,
             })
         }
 
@@ -782,18 +859,17 @@ mod tests {
     }
 
     #[tokio::test]
-    async fn test_cache_fill_preserves_read_args() {
+    async fn test_cache_fill_preserves_version() {
         let cache = memory_cache().await;
         let source = Arc::new(MockReadService::new("0123456789"));
         let state = source.state.clone();
-        let service = FoyerLayer::new(cache)
+        let service = FoyerLayer::new(cache.clone())
             .with_size_limit(0..100)
             .apply_service(source);
         let ctx = service_context(&service);
 
         let (_, args, _) = options::ReadOptions {
             version: Some("v1".to_owned()),
-            if_match: Some("etag-1".to_owned()),
             ..Default::default()
         }
         .into();
@@ -803,11 +879,23 @@ mod tests {
 
         let stat_args = state.last_stat_args.lock().unwrap().clone().unwrap();
         assert_eq!(stat_args.version(), Some("v1"));
-        assert_eq!(stat_args.if_match(), Some("etag-1"));
 
         let read_args = state.last_read_args.lock().unwrap().clone().unwrap();
         assert_eq!(read_args.version(), Some("v1"));
-        assert_eq!(read_args.if_match(), Some("etag-1"));
+
+        assert!(cache.contains(&FoyerKey {
+            path: "test".to_owned(),
+            version: Some("v1".to_owned()),
+        }));
+    }
+
+    fn conditional_read_args(if_match: &str) -> OpRead {
+        let (_, args, _) = options::ReadOptions {
+            if_match: Some(if_match.to_owned()),
+            ..Default::default()
+        }
+        .into();
+        args
     }
 
     #[tokio::test]
@@ -830,12 +918,9 @@ mod tests {
 
         // A conditional read must not be served from the cache: the cached
         // entry carries no condition, so only the origin can observe it.
-        let (_, args, _) = options::ReadOptions {
-            if_match: Some("etag-1".to_owned()),
-            ..Default::default()
-        }
-        .into();
-        let reader = service.read(&ctx, "test", args).unwrap();
+        let reader = service
+            .read(&ctx, "test", conditional_read_args("\"etag-1\""))
+            .unwrap();
         let (_, mut stream) = 
reader.open(BytesRange::from(0_u64..2)).await.unwrap();
         let buffer = stream.read_all().await.unwrap();
         assert_eq!(buffer.to_vec(), b"01");
@@ -849,6 +934,187 @@ mod tests {
         assert_eq!(state.open_calls.load(Ordering::Relaxed), 2);
     }
 
+    #[tokio::test]
+    async fn test_conditional_read_checks_precondition_on_cache_miss() {
+        let cache = memory_cache().await;
+        let source = Arc::new(MockReadService::new("0123456789"));
+        let service = FoyerLayer::new(cache)
+            .with_size_limit(0..100)
+            .apply_service(source);
+        let ctx = service_context(&service);
+
+        let reader = service
+            .read(&ctx, "test", conditional_read_args("\"wrong-etag\""))
+            .unwrap();
+        let err = reader
+            .open(BytesRange::new(0, None))
+            .await
+            .err()
+            .expect("a mismatched if-match must fail");
+        assert_eq!(err.kind(), ErrorKind::ConditionNotMatch);
+    }
+
+    #[tokio::test]
+    async fn test_conditional_read_checks_precondition_on_cache_hit() {
+        let cache = memory_cache().await;
+        let source = Arc::new(MockReadService::new("0123456789"));
+        let service = FoyerLayer::new(cache)
+            .with_size_limit(0..100)
+            .apply_service(source);
+        let ctx = service_context(&service);
+
+        let reader = service.read(&ctx, "test", OpRead::default()).unwrap();
+        let (_, mut stream) = reader.open(BytesRange::new(0, 
None)).await.unwrap();
+        stream.read_all().await.unwrap();
+
+        let reader = service
+            .read(&ctx, "test", conditional_read_args("\"wrong-etag\""))
+            .unwrap();
+        let err = reader
+            .open(BytesRange::new(0, None))
+            .await
+            .err()
+            .expect("a cached entry must not satisfy a mismatched if-match");
+        assert_eq!(err.kind(), ErrorKind::ConditionNotMatch);
+    }
+
+    #[tokio::test]
+    async fn test_conditional_read_allows_matching_precondition() {
+        let cache = memory_cache().await;
+        let source = Arc::new(MockReadService::new("0123456789"));
+        let state = source.state.clone();
+        let service = FoyerLayer::new(cache)
+            .with_size_limit(0..100)
+            .apply_service(source);
+        let ctx = service_context(&service);
+
+        let reader = service
+            .read(&ctx, "test", conditional_read_args("\"etag-1\""))
+            .unwrap();
+        let (_, mut stream) = reader.open(BytesRange::new(0, 
None)).await.unwrap();
+        let buffer = stream.read_all().await.unwrap();
+        assert_eq!(buffer.to_vec(), b"0123456789");
+        assert_eq!(state.open_calls.load(Ordering::Relaxed), 1);
+        assert_eq!(
+            state
+                .last_read_args
+                .lock()
+                .unwrap()
+                .as_ref()
+                .unwrap()
+                .if_match(),
+            Some("\"etag-1\"")
+        );
+    }
+
+    #[tokio::test]
+    async fn test_conditional_read_does_not_join_in_flight_fill() {
+        let cache = memory_cache().await;
+        let source = Arc::new(MockReadService::new("0123456789"));
+        let state = source.state.clone();
+        let gate = state.gate_stat();
+        let service = FoyerLayer::new(cache)
+            .with_size_limit(0..100)
+            .apply_service(source);
+        let ctx = service_context(&service);
+
+        // Start an unconditional read and stop it inside the cache fill.
+        let fill = tokio::spawn({
+            let service = service.clone();
+            let ctx = ctx.clone();
+            async move {
+                let reader = service.read(&ctx, "test", 
OpRead::default()).unwrap();
+                let (_, mut stream) = reader.open(BytesRange::new(0, 
None)).await.unwrap();
+                stream.read_all().await.unwrap()
+            }
+        });
+        state.stat_started.acquire().await.unwrap().forget();
+
+        // Start a conditional read while the unconditional fill is in flight.
+        let (started_tx, started_rx) = oneshot::channel();
+        let conditional = tokio::spawn({
+            let service = service.clone();
+            let ctx = ctx.clone();
+            async move {
+                let reader = service
+                    .read(&ctx, "test", 
conditional_read_args("\"wrong-etag\""))
+                    .unwrap();
+                started_tx.send(()).unwrap();
+                reader.open(BytesRange::new(0, None)).await
+            }
+        });
+        started_rx.await.unwrap();
+        tokio::task::yield_now().await;
+
+        gate.add_permits(1);
+
+        let fill_buffer = fill.await.unwrap();
+        assert_eq!(fill_buffer.to_vec(), b"0123456789");
+
+        let err = conditional
+            .await
+            .unwrap()
+            .err()
+            .expect("a conditional read must not receive an unconditional 
fill");
+        assert_eq!(err.kind(), ErrorKind::ConditionNotMatch);
+    }
+
+    #[tokio::test]
+    async fn test_unconditional_read_does_not_join_conditional_fill() {
+        let cache = memory_cache().await;
+        let source = Arc::new(MockReadService::new("0123456789"));
+        let state = source.state.clone();
+        let gate = state.gate_condition();
+        let service = FoyerLayer::new(cache)
+            .with_size_limit(0..100)
+            .apply_service(source);
+        let ctx = service_context(&service);
+
+        // Start a conditional read with a mismatched if-match and stop it
+        // inside the origin precondition check.
+        let conditional = tokio::spawn({
+            let service = service.clone();
+            let ctx = ctx.clone();
+            async move {
+                let reader = service
+                    .read(&ctx, "test", 
conditional_read_args("\"wrong-etag\""))
+                    .unwrap();
+                reader.open(BytesRange::new(0, None)).await
+            }
+        });
+        state.condition_started.acquire().await.unwrap().forget();
+
+        // Start an unconditional read while the conditional fill is in flight.
+        let (started_tx, started_rx) = oneshot::channel();
+        let fill = tokio::spawn({
+            let service = service.clone();
+            let ctx = ctx.clone();
+            async move {
+                let reader = service.read(&ctx, "test", 
OpRead::default()).unwrap();
+                started_tx.send(()).unwrap();
+                reader.open(BytesRange::new(0, None)).await
+            }
+        });
+        started_rx.await.unwrap();
+        tokio::task::yield_now().await;
+
+        gate.add_permits(1);
+
+        let err = conditional
+            .await
+            .unwrap()
+            .err()
+            .expect("the conditional read must fail on its precondition");
+        assert_eq!(err.kind(), ErrorKind::ConditionNotMatch);
+
+        let (_, mut stream) = fill
+            .await
+            .unwrap()
+            .expect("an unconditional read must not inherit the conditional 
failure");
+        let buffer = stream.read_all().await.unwrap();
+        assert_eq!(buffer.to_vec(), b"0123456789");
+    }
+
     #[tokio::test]
     async fn test() {
         let dir = tempfile::tempdir().unwrap();

Reply via email to