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

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

commit eb29cf15ffe9f07b3d8265ea8d098dd996effc5c
Author: Xuanwo <[email protected]>
AuthorDate: Wed Nov 25 07:53:19 2026 +0100

    feat(services/lakefs): support ETag conditions on reads
    
    The lakeFS GET endpoint does not enforce If-Match. Check the ETag on the 
same response before exposing its stream. Forward If-None-Match to save a 
download on newer servers, and use the same response check for older servers.
    
    Refs: #5486
---
 core/services/lakefs/src/backend.rs |  2 ++
 core/services/lakefs/src/core.rs    | 15 +++++++-
 core/services/lakefs/src/docs.md    |  3 ++
 core/services/lakefs/src/reader.rs  | 68 ++++++++++++++++++++++++++-----------
 4 files changed, 68 insertions(+), 20 deletions(-)

diff --git a/core/services/lakefs/src/backend.rs 
b/core/services/lakefs/src/backend.rs
index 70e9ccaa47..06070f2f93 100644
--- a/core/services/lakefs/src/backend.rs
+++ b/core/services/lakefs/src/backend.rs
@@ -161,6 +161,8 @@ impl Builder for LakefsBuilder {
                     list: true,
 
                     read: true,
+                    read_with_if_match: true,
+                    read_with_if_none_match: true,
                     read_with_suffix: true,
                     write: true,
                     delete: true,
diff --git a/core/services/lakefs/src/core.rs b/core/services/lakefs/src/core.rs
index 13fbf102ca..1977dac4eb 100644
--- a/core/services/lakefs/src/core.rs
+++ b/core/services/lakefs/src/core.rs
@@ -15,6 +15,7 @@
 // specific language governing permissions and limitations
 // under the License.
 
+use std::borrow::Cow;
 use std::collections::HashMap;
 use std::fmt::Debug;
 
@@ -85,7 +86,7 @@ impl LakefsCore {
         ctx: &OperationContext,
         path: &str,
         range: BytesRange,
-        _args: &OpRead,
+        args: &OpRead,
     ) -> Result<Response<HttpBody>> {
         let p = build_absolute_path(&self.root, path)
             .trim_end_matches('/')
@@ -104,6 +105,9 @@ impl LakefsCore {
         let auth_header_content = 
format_authorization_by_basic(&self.username, &self.password)?;
         req = req.header(header::AUTHORIZATION, auth_header_content);
 
+        if let Some(etag) = args.if_none_match() {
+            req = req.header(header::IF_NONE_MATCH, 
format_etag(etag).as_ref());
+        }
         if !range.is_full() {
             req = req.header(header::RANGE, range.to_header());
         }
@@ -301,6 +305,15 @@ impl LakefsCore {
     }
 }
 
+// lakeFS returns bare checksums in object metadata and quoted ETags in GET 
headers.
+pub(super) fn format_etag(etag: &str) -> Cow<'_, str> {
+    if etag.starts_with('"') && etag.ends_with('"') {
+        Cow::Borrowed(etag)
+    } else {
+        Cow::Owned(format!("\"{etag}\""))
+    }
+}
+
 #[derive(Deserialize, Eq, PartialEq, Debug)]
 pub(super) struct LakefsStatus {
     pub path: String,
diff --git a/core/services/lakefs/src/docs.md b/core/services/lakefs/src/docs.md
index 71551098a7..25168ec435 100644
--- a/core/services/lakefs/src/docs.md
+++ b/core/services/lakefs/src/docs.md
@@ -17,6 +17,9 @@ Depending on its configuration and the backing system, this 
service can expose:
 - [ ] rename
 - [ ] ~~presign~~
 
+Reads support `if_match` and `if_none_match` with an ETag returned by lakeFS.
+The `if_match` check uses the download response ETag before exposing the body.
+
 Inspect the effective capability set with [`opendal_core::Operator::info`] and
 [`opendal_core::OperatorInfo::capability`] after building an operator.
 
diff --git a/core/services/lakefs/src/reader.rs 
b/core/services/lakefs/src/reader.rs
index c1732d83e4..2b7c03c9b3 100644
--- a/core/services/lakefs/src/reader.rs
+++ b/core/services/lakefs/src/reader.rs
@@ -16,7 +16,7 @@
 // under the License.
 
 use super::backend::*;
-use super::core::{ErrorContext, parse_error};
+use super::core::{ErrorContext, format_etag, parse_error};
 use http::Response;
 use http::StatusCode;
 use opendal_core::raw::*;
@@ -48,31 +48,61 @@ impl LakefsReader {
 
 impl oio::StreamRead for LakefsReader {
     async fn open(&self, range: BytesRange) -> Result<(RpRead, Box<dyn 
oio::ReadStreamDyn>)> {
-        let backend = &self.backend;
         let path = self.path.as_str();
-        let args = self.args.clone();
-        let resp = backend
+        let args = &self.args;
+        let resp = self
+            .backend
             .core
-            .get_object_content(&self.ctx, path, range, &args)
+            .get_object_content(&self.ctx, path, range, args)
             .await?;
-
         let status = resp.status();
+        let if_match = args.if_match();
+        let if_none_match = args.if_none_match();
+
+        if status == StatusCode::NOT_MODIFIED && if_none_match.is_some() {
+            let (parts, _) = resp.into_parts();
+            let err = Error::new(
+                ErrorKind::ConditionNotMatch,
+                "lakeFS object matched the If-None-Match ETag",
+            )
+            .with_context("service_operation", "GetObject");
+            return Err(with_error_response_context(err, parts));
+        }
 
-        let (rp, stream) = match status {
-            StatusCode::OK | StatusCode::PARTIAL_CONTENT => (
-                RpRead::new(parse_into_metadata(path, resp.headers())?),
-                resp.into_body(),
-            ),
-            _ => {
-                let (part, mut body) = resp.into_parts();
-                let buf = body.to_buffer().await?;
-                return Err(parse_error(
-                    ErrorContext::new(ServiceOperation("GetObject")),
-                    Response::from_parts(part, buf),
+        let serves_content = matches!(status, StatusCode::OK | 
StatusCode::PARTIAL_CONTENT);
+        if (serves_content || status == StatusCode::RANGE_NOT_SATISFIABLE)
+            && (if_match.is_some() || if_none_match.is_some())
+        {
+            // The lakeFS GET API ignores If-Match, and older servers also
+            // ignore If-None-Match. Compare against the same response that
+            // serves the content or rejects the range.
+            if let Some(etag) = parse_etag(resp.headers())? {
+                if if_match.is_some_and(|expected| etag != 
format_etag(expected))
+                    || if_none_match.is_some_and(|excluded| etag == 
format_etag(excluded))
+                {
+                    return Err(Error::new(
+                        ErrorKind::ConditionNotMatch,
+                        "lakeFS object did not satisfy the ETag condition",
+                    ));
+                }
+            } else if serves_content {
+                return Err(Error::new(
+                    ErrorKind::Unexpected,
+                    "lakeFS response is missing an ETag",
                 ));
             }
-        };
+        }
+
+        if serves_content {
+            let meta = parse_into_metadata(path, resp.headers())?;
+            return Ok((RpRead::new(meta), Box::new(resp.into_body())));
+        }
 
-        Ok((rp, Box::new(stream) as Box<dyn oio::ReadStreamDyn>))
+        let (parts, mut body) = resp.into_parts();
+        let buffer = body.to_buffer().await?;
+        Err(parse_error(
+            ErrorContext::new(ServiceOperation("GetObject")),
+            Response::from_parts(parts, buffer),
+        ))
     }
 }

Reply via email to