unknowntpo commented on code in PR #5905:
URL: https://github.com/apache/gravitino/pull/5905#discussion_r1917597636

##########
clients/filesystem-fuse/src/fuse_api_handle_debug.rs:
##########
@@ -0,0 +1,637 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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::AppConfig;
+use crate::filesystem::{FileSystemContext, RawFileSystem};
+use crate::fuse_api_handle::FuseApiHandle;
+use chrono::DateTime;
+use fuse3::path::prelude::{ReplyData, ReplyOpen, ReplyStatFs, ReplyWrite};
+use fuse3::path::Request;
+use fuse3::raw::prelude::{
+    FileAttr, ReplyAttr, ReplyCreated, ReplyDirectory, ReplyDirectoryPlus, 
ReplyEntry, ReplyInit,
+};
+use fuse3::raw::reply::{DirectoryEntry, DirectoryEntryPlus};
+use fuse3::raw::Filesystem;
+use fuse3::{Inode, SetAttr, Timestamp};
+use futures_util::stream::BoxStream;
+use std::ffi::OsStr;
+use std::fmt::Write;
+use tracing::{debug, error};
+
+/// Convert `ReplyAttr` to descriptive string.
+///
+/// Example: `ttl: 1s, FileAttr: { ino: 10000, size: 0, blocks: 0, atime: 
"2025-01-10 12:12:29.452650", mtime: "2025-01-10 12:12:29.452650", ctime: 
"2025-01-10 12:12:29.452650", crtime: "2025-01-10 12:12:29.452650", kind: 
RegularFile, perm: 384, nlink: 1, uid: 501, gid: 20, rdev: 0, flags: 0, 
blksize: 8192 }`
+fn reply_attr_to_desc_str(reply_attr: &ReplyAttr) -> String {
+    let mut output = String::new();
+
+    write!(output, "ttl: {:?}, ", reply_attr.ttl).unwrap();
+    write!(
+        output,
+        "FileAttr: {}",
+        file_attr_to_desc_str(&reply_attr.attr)
+    )
+    .unwrap();
+
+    output
+}
+
+/// Convert `ReplyEntry` to descriptive string.
+///
+/// Example: `ttl: 1s, FileAttr: { ino: 10001, size: 0, blocks: 1, atime: 
"2025-01-10 22:46:08.444791", mtime: "2025-01-10 22:46:08.444791", ctime: 
"2025-01-10 22:46:08.444791", crtime: "2025-01-10 22:46:08.444791", kind: 
Directory, perm: 448, nlink: 0, uid: 501, gid: 20, rdev: 0, flags: 0, blksize: 
8192 }, generation: 0`
+fn reply_entry_to_desc_str(reply_entry: &ReplyEntry) -> String {
+    let mut output = String::new();
+
+    write!(output, "ttl: {:?}, ", reply_entry.ttl).unwrap();
+    write!(
+        output,
+        "FileAttr: {}, ",
+        file_attr_to_desc_str(&reply_entry.attr)
+    )
+    .unwrap();
+    write!(output, "generation: {}", reply_entry.generation).unwrap();
+
+    output
+}
+
+/// Convert `ReplyCreated` to descriptive string.
+///
+/// Example: `ttl: 1s, FileAttr: { ino: 10000, size: 0, blocks: 1, atime: 
"2025-01-10 22:53:45.491337", mtime: "2025-01-10 22:53:45.491337", ctime: 
"2025-01-10 22:53:45.491337", crtime: "2025-01-10 22:53:45.491337", kind: 
RegularFile, perm: 384, nlink: 0, uid: 501, gid: 20, rdev: 0, flags: 0, 
blksize: 8192 }, generation: 0, fh: 1`
+fn reply_created_to_desc_str(reply_created: &ReplyCreated) -> String {
+    let mut output = String::new();
+
+    write!(output, "ttl: {:?}, ", reply_created.ttl).unwrap();
+    write!(
+        output,
+        "FileAttr: {}, ",
+        file_attr_to_desc_str(&reply_created.attr)
+    )
+    .unwrap();
+    write!(output, "generation: {}, ", reply_created.generation).unwrap();
+    write!(output, "fh: {}", reply_created.fh).unwrap();
+
+    output
+}
+
+/// Convert `FileAttr` to descriptive string.
+///
+/// Example: `{ ino: 10000, size: 0, blocks: 0, atime: "2025-01-10 
12:12:29.452650", mtime: "2025-01-10 12:12:29.452650", ctime: "2025-01-10 
12:12:29.452650", crtime: "2025-01-10 12:12:29.452650", kind: RegularFile, 
perm: 384, nlink: 1, uid: 501, gid: 20, rdev: 0, flags: 0, blksize: 8192 }`
+fn file_attr_to_desc_str(attr: &FileAttr) -> String {
+    let mut output = String::new();
+
+    write!(output, "{{ ").unwrap();
+
+    write!(output, "ino: {}, ", attr.ino).unwrap();
+    write!(output, "size: {}, ", attr.size).unwrap();
+    write!(output, "blocks: {}, ", attr.blocks).unwrap();
+    write!(
+        output,
+        "atime: {:?}, ",
+        timestamp_to_desc_string(attr.atime)
+    )
+    .unwrap();
+    write!(
+        output,
+        "mtime: {:?}, ",
+        timestamp_to_desc_string(attr.mtime)
+    )
+    .unwrap();
+    write!(
+        output,
+        "ctime: {:?}, ",
+        timestamp_to_desc_string(attr.ctime)
+    )
+    .unwrap();
+    #[cfg(target_os = "macos")]
+    {
+        write!(
+            output,
+            "crtime: {:?}, ",
+            timestamp_to_desc_string(attr.crtime)
+        )
+        .unwrap();
+    }
+    write!(output, "kind: {:?}, ", attr.kind).unwrap();
+    write!(output, "perm: {}, ", attr.perm).unwrap();
+    write!(output, "nlink: {}, ", attr.nlink).unwrap();
+    write!(output, "uid: {}, ", attr.uid).unwrap();
+    write!(output, "gid: {}, ", attr.gid).unwrap();
+    write!(output, "rdev: {}, ", attr.rdev).unwrap();
+    #[cfg(target_os = "macos")]
+    {
+        write!(output, "flags: {}, ", attr.flags).unwrap();
+    }
+    write!(output, "blksize: {}", attr.blksize).unwrap();
+
+    write!(output, " }}").unwrap();
+
+    output
+}
+
+/// Convert `Timestamp` to descriptive string.
+///
+/// Example output: "2025-01-07 23:01:30.531699"
+fn timestamp_to_desc_string(tstmp: Timestamp) -> String {
+    DateTime::from_timestamp(tstmp.sec, tstmp.nsec)
+        .unwrap()
+        .naive_utc()
+        .to_string()
+}
+
+pub(crate) struct FuseApiHandleDebug<T: RawFileSystem> {
+    inner: FuseApiHandle<T>,
+}
+
+impl<T: RawFileSystem> FuseApiHandleDebug<T> {
+    pub fn new(fs: T, _config: &AppConfig, context: FileSystemContext) -> Self 
{
+        Self {
+            inner: FuseApiHandle::new(fs, _config, context),
+        }
+    }
+}
+
+impl<T: RawFileSystem> Filesystem for FuseApiHandleDebug<T> {
+    async fn init(&self, req: Request) -> fuse3::Result<ReplyInit> {
+        debug!(req.unique, ?req, "init");
+        match self.inner.init(req).await {
+            Ok(reply) => {
+                debug!(req.unique, ?reply, "init");
+                Ok(reply)
+            }
+            Err(e) => {
+                error!(req.unique, ?req, "init");
+                Err(e)
+            }
+        }
+    }
+
+    async fn destroy(&self, req: Request) {
+        debug!(req.unique, ?req, "destroy started");
+        self.inner.destroy(req).await;
+        debug!(req.unique, "destroy completed");
+    }
+
+    async fn lookup(&self, req: Request, parent: Inode, name: &OsStr) -> 
fuse3::Result<ReplyEntry> {
+        let parent_stat = self
+            .inner
+            .get_modified_file_stat(parent, Option::None, Option::None, 
Option::None)
+            .await?;
+        debug!(req.unique, ?req, parent = ?parent_stat.name, ?name, "lookup 
started");
+
+        match self.inner.lookup(req, parent, name).await {
+            Ok(reply) => {
+                debug!(req.unique, ?reply, "lookup completed");
+                Ok(reply)
+            }
+            Err(e) => {
+                error!(req.unique, ?e, "lookup failed");
+                Err(e)
+            }
+        }
+    }
+
+    async fn getattr(
+        &self,
+        req: Request,
+        inode: Inode,
+        fh: Option<u64>,
+        flags: u32,
+    ) -> fuse3::Result<ReplyAttr> {
+        let stat = self
+            .inner
+            .get_modified_file_stat(inode, Option::None, Option::None, 
Option::None)
+            .await?;
+        debug!(req.unique, ?req, "filename" = ?stat.name, ?fh, ?flags, 
"getattr started");
+
+        match self.inner.getattr(req, inode, fh, flags).await {
+            Ok(reply) => {
+                debug!(req.unique, reply = %reply_attr_to_desc_str(&reply), 
"getattr completed");
+                Ok(reply)
+            }
+            Err(e) => {
+                error!(req.unique, ?e, "getattr failed");
+                Err(e)
+            }
+        }
+    }
+
+    async fn setattr(
+        &self,
+        req: Request,
+        inode: Inode,
+        fh: Option<u64>,
+        set_attr: SetAttr,
+    ) -> fuse3::Result<ReplyAttr> {
+        let stat = self
+            .inner
+            .get_modified_file_stat(inode, Option::None, Option::None, 
Option::None)
+            .await?;
+        debug!(req.unique, ?req, "filename" = ?stat.name, ?fh, ?set_attr, 
"setattr started");
+
+        match self.inner.setattr(req, inode, fh, set_attr).await {
+            Ok(reply) => {
+                debug!(req.unique, reply = %reply_attr_to_desc_str(&reply), 
"setattr completed");
+                Ok(reply)
+            }
+            Err(e) => {
+                error!(req.unique, ?e, "setattr failed");
+                Err(e)
+            }
+        }
+    }
+
+    async fn mkdir(
+        &self,
+        req: Request,
+        parent: Inode,
+        name: &OsStr,
+        mode: u32,
+        umask: u32,
+    ) -> fuse3::Result<ReplyEntry> {
+        let parent_stat = self
+            .inner
+            .get_modified_file_stat(parent, Option::None, Option::None, 
Option::None)
+            .await?;
+        debug!(
+            req.unique,
+            ?req,
+            parent = ?parent_stat.name,
+            ?name,
+            mode,
+            umask,
+            "mkdir started"
+        );
+
+        match self.inner.mkdir(req, parent, name, mode, umask).await {
+            Ok(reply) => {
+                debug!(req.unique, reply = %reply_entry_to_desc_str(&reply), 
"mkdir succeeded");
+                Ok(reply)
+            }
+            Err(e) => {
+                error!(req.unique, ?e, "mkdir failed");
+                Err(e)
+            }
+        }
+    }
+
+    async fn unlink(&self, req: Request, parent: Inode, name: &OsStr) -> 
fuse3::Result<()> {
+        let parent_stat = self
+            .inner
+            .get_modified_file_stat(parent, Option::None, Option::None, 
Option::None)
+            .await?;
+        debug!(req.unique, ?req, parent = ?parent_stat.name, ?name, "unlink 
started");
+
+        match self.inner.unlink(req, parent, name).await {
+            Ok(()) => {
+                debug!(req.unique, "unlink succeeded");
+                Ok(())
+            }
+            Err(e) => {
+                error!(req.unique, ?e, "unlink failed");
+                Err(e)
+            }
+        }
+    }
+
+    async fn rmdir(&self, req: Request, parent: Inode, name: &OsStr) -> 
fuse3::Result<()> {
+        let parent_stat = self
+            .inner
+            .get_modified_file_stat(parent, Option::None, Option::None, 
Option::None)
+            .await?;
+        debug!(req.unique, ?req, parent = ?parent_stat.name, ?name, "rmdir 
started");
+
+        match self.inner.rmdir(req, parent, name).await {
+            Ok(()) => {
+                debug!(req.unique, "rmdir succeeded");
+                Ok(())
+            }
+            Err(e) => {
+                error!(req.unique, ?e, "rmdir failed");
+                Err(e)
+            }
+        }
+    }
+
+    async fn open(&self, req: Request, inode: Inode, flags: u32) -> 
fuse3::Result<ReplyOpen> {
+        let stat = self
+            .inner
+            .get_modified_file_stat(inode, Option::None, Option::None, 
Option::None)
+            .await?;
+        debug!(req.unique, ?req, "filename" = ?stat.name, ?flags, "open 
started");
+
+        match self.inner.open(req, inode, flags).await {
+            Ok(reply) => {
+                debug!(req.unique, ?reply, "open succeeded");
+                Ok(reply)
+            }
+            Err(e) => {
+                error!(req.unique, ?e, "open failed");
+                Err(e)
+            }
+        }
+    }
+
+    async fn read(
+        &self,
+        req: Request,
+        inode: Inode,
+        fh: u64,
+        offset: u64,
+        size: u32,
+    ) -> fuse3::Result<ReplyData> {
+        let stat = self
+            .inner
+            .get_modified_file_stat(inode, Option::None, Option::None, 
Option::None)
+            .await?;
+        debug!(
+            req.unique,
+            ?req,
+            "filename" = ?stat.name,
+            ?fh,
+            ?offset,
+            ?size,
+            "read started"
+        );
+
+        match self.inner.read(req, inode, fh, offset, size).await {
+            Ok(reply) => {
+                debug!(req.unique, ?reply, "read succeeded");
+                Ok(reply)
+            }
+            Err(e) => {
+                error!(req.unique, ?e, "read failed");
+                Err(e)
+            }
+        }
+    }
+
+    async fn write(
+        &self,
+        req: Request,
+        inode: Inode,
+        fh: u64,
+        offset: u64,
+        data: &[u8],
+        write_flags: u32,
+        flags: u32,
+    ) -> fuse3::Result<ReplyWrite> {
+        let stat = self
+            .inner
+            .get_modified_file_stat(inode, Option::None, Option::None, 
Option::None)
+            .await?;
+        debug!(
+            req.unique,
+            ?req,
+            "filename" = ?stat.name,
+            ?fh,
+            ?offset,
+            data_len = data.len(),
+            ?write_flags,
+            ?flags,
+            "write started"
+        );
+
+        match self
+            .inner
+            .write(req, inode, fh, offset, data, write_flags, flags)
+            .await
+        {
+            Ok(reply) => {
+                debug!(req.unique, ?reply, "write succeeded");
+                Ok(reply)
+            }
+            Err(e) => {
+                error!(req.unique, ?e, "write failed");
+                Err(e)
+            }
+        }
+    }
+
+    async fn statfs(&self, req: Request, inode: Inode) -> 
fuse3::Result<ReplyStatFs> {
+        let stat = self
+            .inner
+            .get_modified_file_stat(inode, Option::None, Option::None, 
Option::None)
+            .await?;
+        debug!(req.unique, ?req, "filename" = ?stat.name, "statfs started");
+
+        match self.inner.statfs(req, inode).await {
+            Ok(reply) => {
+                debug!(req.unique, ?reply, "statfs succeeded");
+                Ok(reply)
+            }
+            Err(e) => {
+                error!(req.unique, ?e, "statfs failed");
+                Err(e)
+            }
+        }
+    }
+
+    async fn release(
+        &self,
+        req: Request,
+        inode: Inode,
+        fh: u64,
+        flags: u32,
+        lock_owner: u64,
+        flush: bool,
+    ) -> fuse3::Result<()> {
+        let stat = self
+            .inner
+            .get_modified_file_stat(inode, Option::None, Option::None, 
Option::None)
+            .await?;
+        debug!(
+            req.unique,
+            ?req,
+            "pathname" = ?stat.name,
+            ?fh,
+            ?flags,
+            ?lock_owner,
+            ?flush,
+            "release started"
+        );
+
+        match self
+            .inner
+            .release(req, inode, fh, flags, lock_owner, flush)
+            .await
+        {
+            Ok(()) => {
+                debug!(req.unique, "release succeeded");
+                Ok(())
+            }
+            Err(e) => {
+                error!(req.unique, ?e, "release failed");
+                Err(e)
+            }
+        }
+    }
+
+    async fn opendir(&self, req: Request, inode: Inode, flags: u32) -> 
fuse3::Result<ReplyOpen> {
+        let stat = self
+            .inner
+            .get_modified_file_stat(inode, Option::None, Option::None, 
Option::None)
+            .await?;
+        debug!(req.unique, ?req, "dirname" = ?stat.name, ?flags, "opendir 
started");
+
+        match self.inner.opendir(req, inode, flags).await {
+            Ok(reply) => {
+                debug!(req.unique, ?reply, "opendir succeeded");
+                Ok(reply)
+            }
+            Err(e) => {
+                error!(req.unique, ?e, "opendir failed");
+                Err(e)
+            }
+        }

Review Comment:
   I implemented a macro called `log_result`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to