You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@paimon.apache.org by "discivigour (via GitHub)" <gi...@apache.org> on 2026/04/02 07:09:10 UTC

[PR] feat: implement token cache and FileIOProvider for token refresh [paimon-rust]

discivigour opened a new pull request, #192:
URL: https://github.com/apache/paimon-rust/pull/192

   ## Summary
   
   This PR introduces the `FileIOProvider` trait and implements token caching mechanism for REST catalog data access.
   
   ## Key Changes
   
   ### 1. FileIOProvider Trait (`file_io_provider.rs`)
   - New abstraction layer for file I/O operations
   - Enables dynamic FileIO implementation switching
   - Supports `Debug` trait for better observability
   
   ### 2. Token Cache Implementation
   - Global FileIO cache using `moka` (Caffeine-like)
   - Cache key: `RESTToken` (with custom Hash implementation)
   - TTL: 10 hours, max capacity: 1000 entries
   - Thread-safe via moka's internal synchronization
   
   ### 3. RESTTokenFileIO Enhancement
   - Implements `FileIOProvider` trait
   - Checks token validity before each operation
   - Automatic token refresh on expiration
   - FileIO instances cached per token
   
   ### 4. Architecture Updates
   - `FileSystemCatalog`: uses `Arc<dyn FileIOProvider>"
   - `RESTCatalog": supports dynamic FileIO with token refresh
   - All components migrated to use `FileIOProvider` trait
   
   ## Files Changed
   - 16 files modified
   - +318 lines, -141 lines
   - New file: `file_io_provider.rs`
   
   ## Testing
   - All existing tests updated to use async FileIOProvider methods
   - Clippy passes with no warnings


-- 
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: issues-unsubscribe@paimon.apache.org

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


Re: [PR] feat: implement token cache and FileIOProvider for token refresh [paimon-rust]

Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #192:
URL: https://github.com/apache/paimon-rust/pull/192#discussion_r3031705777


##########
crates/paimon/src/io/file_io.rs:
##########
@@ -65,11 +65,13 @@ impl FileIO {
             })?;
         Ok(FileIOBuilder::new(url.scheme()))
     }
+}
+// Implement FileIOProvider for FileIO
+use crate::io::FileIOProvider;
 
-    /// Create a new input file to read data.
-    ///
-    /// Reference: <https://github.com/apache/paimon/blob/release-0.8.2/paimon-common/src/main/java/org/apache/paimon/fs/FileIO.java#L76>
-    pub fn new_input(&self, path: &str) -> crate::Result<InputFile> {
+#[async_trait::async_trait]
+impl FileIOProvider for FileIO {
+    async fn new_input(&self, path: &str) -> Result<InputFile> {

Review Comment:
   Why need to do this refactor? sync to async?



-- 
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: issues-unsubscribe@paimon.apache.org

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


Re: [PR] feat: implement token cache and FileIOProvider for token refresh [paimon-rust]

Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #192:
URL: https://github.com/apache/paimon-rust/pull/192#discussion_r3031712833


##########
crates/paimon/src/catalog/rest/rest_token_file_io.rs:
##########
@@ -20,30 +20,63 @@
 //! This module provides a FileIO wrapper that supports getting data access
 //! tokens from a REST Server. It handles token caching, expiration detection,
 //! and automatic refresh.
+//!
+//! Unlike the previous implementation that only refreshed tokens during
+//! `build_file_io()`, this implementation implements `FileIOProvider` and
+//! checks token validity before each file operation, matching the Java
+//! implementation behavior.
 
 use std::collections::HashMap;
+use std::sync::OnceLock;
+use std::time::Duration;
 
+use moka::future::Cache;
 use tokio::sync::{OnceCell, RwLock};
 
 use crate::api::rest_api::RESTApi;
 use crate::api::rest_util::RESTUtil;
 use crate::catalog::Identifier;
 use crate::common::{CatalogOptions, Options};
 use crate::io::storage_oss::OSS_ENDPOINT;
-use crate::io::FileIO;
-use crate::Result;
+use crate::io::{FileIO, FileIOProvider, FileStatus, InputFile, OutputFile};
+use crate::{Error, Result};
 
 use super::rest_token::RESTToken;
 
 /// Safe time margin (in milliseconds) before token expiration to trigger refresh.
 const TOKEN_EXPIRATION_SAFE_TIME_MILLIS: i64 = 3_600_000;
 
+/// Maximum number of entries in the global FileIO cache.
+const FILE_IO_CACHE_MAX_CAPACITY: u64 = 1000;
+/// Time-to-live for cache entries in seconds (10 hours).
+const FILE_IO_CACHE_TTL_SECS: u64 = 10 * 60 * 60;
+
+/// Global static FileIO cache, similar to Java's Caffeine cache.
+///
+/// This cache stores FileIO instances keyed by their corresponding RESTToken.
+/// Features:
+/// - max_capacity: 1000 entries
+/// - time_to_live: 10 hours (entries expire after this duration)
+/// - thread-safe via moka's internal synchronization
+static FILE_IO_CACHE: OnceLock<Cache<RESTToken, FileIO>> = OnceLock::new();
+
+/// Get the global FileIO cache, initializing it if necessary.
+fn get_file_io_cache() -> &'static Cache<RESTToken, FileIO> {
+    FILE_IO_CACHE.get_or_init(|| {
+        Cache::builder()
+            .max_capacity(FILE_IO_CACHE_MAX_CAPACITY)
+            .time_to_live(Duration::from_secs(FILE_IO_CACHE_TTL_SECS))
+            .build()
+    })
+}
+
 /// A FileIO wrapper that supports getting data access tokens from a REST Server.
 ///
 /// This struct handles:
 /// - Token caching with expiration detection
 /// - Automatic token refresh via `RESTApi::load_table_token`
 /// - Merging token credentials into catalog options to build the underlying `FileIO`
+/// - FileIO caching based on token to avoid rebuilding FileIO unnecessarily
 pub struct RESTTokenFileIO {

Review Comment:
   No Clone for this?



-- 
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: issues-unsubscribe@paimon.apache.org

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


Re: [PR] feat: implement token cache and FileIOProvider for token refresh [paimon-rust]

Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #192:
URL: https://github.com/apache/paimon-rust/pull/192#discussion_r3031705777


##########
crates/paimon/src/io/file_io.rs:
##########
@@ -65,11 +65,13 @@ impl FileIO {
             })?;
         Ok(FileIOBuilder::new(url.scheme()))
     }
+}
+// Implement FileIOProvider for FileIO
+use crate::io::FileIOProvider;
 
-    /// Create a new input file to read data.
-    ///
-    /// Reference: <https://github.com/apache/paimon/blob/release-0.8.2/paimon-common/src/main/java/org/apache/paimon/fs/FileIO.java#L76>
-    pub fn new_input(&self, path: &str) -> crate::Result<InputFile> {
+#[async_trait::async_trait]
+impl FileIOProvider for FileIO {
+    async fn new_input(&self, path: &str) -> Result<InputFile> {

Review Comment:
   Why need to do this refactor? sync to async?



-- 
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: issues-unsubscribe@paimon.apache.org

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


Re: [PR] feat: implement token cache and FileIOProvider for token refresh [paimon-rust]

Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on PR #192:
URL: https://github.com/apache/paimon-rust/pull/192#issuecomment-4178474435

   Please rebase master.


-- 
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: issues-unsubscribe@paimon.apache.org

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


Re: [PR] feat: implement token cache and FileIOProvider for token refresh [paimon-rust]

Posted by "luoyuxia (via GitHub)" <gi...@apache.org>.
luoyuxia commented on code in PR #192:
URL: https://github.com/apache/paimon-rust/pull/192#discussion_r3032729061


##########
crates/paimon/src/io/file_io.rs:
##########
@@ -65,11 +65,13 @@ impl FileIO {
             })?;
         Ok(FileIOBuilder::new(url.scheme()))
     }
+}
+// Implement FileIOProvider for FileIO
+use crate::io::FileIOProvider;
 
-    /// Create a new input file to read data.
-    ///
-    /// Reference: <https://github.com/apache/paimon/blob/release-0.8.2/paimon-common/src/main/java/org/apache/paimon/fs/FileIO.java#L76>
-    pub fn new_input(&self, path: &str) -> crate::Result<InputFile> {
+#[async_trait::async_trait]
+impl FileIOProvider for FileIO {

Review Comment:
   I'm wondering whether we can refactor as this:
   1: Make FileIO a trait:
   ```
     pub trait FileIO: Send + Sync + Debug {
         async fn new_input(&self, path: &str) -> Result<InputFile>;
         async fn new_output(&self, path: &str) -> Result<OutputFile>;
         async fn get_status(&self, path: &str) -> Result<FileStatus>;
         async fn list_status(&self, path: &str) -> Result<Vec<FileStatus>>;
         async fn exists(&self, path: &str) -> Result<bool>;
         async fn delete_file(&self, path: &str) -> Result<()>;
         async fn delete_dir(&self, path: &str) -> Result<()>;
         async fn mkdirs(&self, path: &str) -> Result<()>;
         async fn rename(&self, src: &str, dst: &str) -> Result<()>;
     }
   ```
   2: Have a default/static file io
   ```
   #[derive(Clone, Debug)]
     pub struct DefaultFileIO {
         storage: Arc<Storage>,
     }
   
   impl DefaultFileIO {
         pub(crate) fn new(storage: Storage) -> Self {
             Self {
                 storage: Arc::new(storage),
             }
         }
     }
   
   #[async_trait::async_trait]
     impl FileIO for DefaultFileIO {
         async fn new_input(&self, path: &str) -> Result<InputFile> { ... }
         async fn new_output(&self, path: &str) -> Result<OutputFile> { ... }
         async fn get_status(&self, path: &str) -> Result<FileStatus> { ... }
         async fn list_status(&self, path: &str) -> Result<Vec<FileStatus>> { ... }
         async fn exists(&self, path: &str) -> Result<bool> { ... }
         async fn delete_file(&self, path: &str) -> Result<()> { ... }
         async fn delete_dir(&self, path: &str) -> Result<()> { ... }
         async fn mkdirs(&self, path: &str) -> Result<()> { ... }
         async fn rename(&self, src: &str, dst: &str) -> Result<()> { ... }
     }
   ```
   
   3: Have a RESTTokenFileIO file io which will load file io dynamically according to token refresh
   ```
   #[derive(Debug)]
     pub struct RESTTokenFileIO {
         identifier: Identifier,
         path: String,
         catalog_options: Options,
         api: OnceCell<RESTApi>,
         token: RwLock<Option<RESTToken>>,
         refresh_lock: Mutex<()>,
     }
   imple RESTTokenFileIO {
    // in here, we can load file io dynamically
   }
   
   impl FileIO for RESTTokenFileIO {
   
    // todo
   }
   ```
   
   4: optional:
   pub type FileIORef = Arc<dyn FileIO>; 
   
   in rust, we tend to type `Arc<dyn xxx>`as `xxxRef`. It's optinal, but may make code simple. 
   
   Since it's a little big of change, maybe we need to reach consensus with @JingsongLi  before move on.
   
   
   
   
   



##########
crates/paimon/src/io/file_io.rs:
##########
@@ -113,16 +109,9 @@ impl FileIO {
         })
     }
 
-    /// List the statuses of the files/directories in the given path if the path is a directory.
-    ///
-    /// References: <https://github.com/apache/paimon/blob/release-0.8.2/paimon-common/src/main/java/org/apache/paimon/fs/FileIO.java#L105>
-    ///
-    /// FIXME: how to handle large dir? Better to return a stream instead?
-    pub async fn list_status(&self, path: &str) -> Result<Vec<FileStatus>> {
+    async fn list_status(&self, path: &str) -> Result<Vec<FileStatus>> {
         let (op, relative_path) = self.storage.create(path)?;
         let base_path = &path[..path.len() - relative_path.len()];
-        // Opendal list() expects directory path to end with `/`.

Review Comment:
   why remve the comments? I think the comments are usefull. 
   The same for others. 



##########
crates/paimon/src/io/file_io.rs:
##########
@@ -65,11 +65,13 @@ impl FileIO {
             })?;
         Ok(FileIOBuilder::new(url.scheme()))
     }
+}
+// Implement FileIOProvider for FileIO
+use crate::io::FileIOProvider;
 
-    /// Create a new input file to read data.
-    ///
-    /// Reference: <https://github.com/apache/paimon/blob/release-0.8.2/paimon-common/src/main/java/org/apache/paimon/fs/FileIO.java#L76>
-    pub fn new_input(&self, path: &str) -> crate::Result<InputFile> {
+#[async_trait::async_trait]
+impl FileIOProvider for FileIO {

Review Comment:
   It's strange to me that FileIO implement FileIO**Provider**..



-- 
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: issues-unsubscribe@paimon.apache.org

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


Re: [PR] feat: implement token cache and FileIOProvider for token refresh [paimon-rust]

Posted by "discivigour (via GitHub)" <gi...@apache.org>.
discivigour closed pull request #192: feat: implement token cache and FileIOProvider for token refresh
URL: https://github.com/apache/paimon-rust/pull/192


-- 
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: issues-unsubscribe@paimon.apache.org

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