GreptimeTeam / greptimedb

An Open-Source, Cloud-Native, Unified Time Series Database for Metrics, Events, and Logs with SQL/PromQL supported. Available on GreptimeCloud.
https://greptime.com/
Apache License 2.0
4k stars 289 forks source link

refactor(puffin): adjust generic parameters #4279

Closed zhongzc closed 1 week ago

zhongzc commented 1 week ago

I hereby agree to the terms of the GreptimeDB CLA.

Refer to a related PR or issue link (optional)

https://github.com/GreptimeTeam/greptimedb/pull/4266#discussion_r1665121324

What's changed and what's your intention?

reduce generic parameters for FsPuffinManager

Checklist

Summary by CodeRabbit

coderabbitai[bot] commented 1 week ago

Walkthrough

The recent code changes primarily involve refactoring the puffin_manager.rs file and related modules to use the auto_impl::auto_impl(Arc) attribute for automatic Arc implementations. This includes updating the PuffinReader, BlobGuard, DirGuard, and Stager traits, as well as modifying type declarations and constructors in SstPuffinManager. These adjustments streamline the codebase and improve maintainability.

Changes

Files Change Summary
src/mito2/src/sst/index/puffin_manager.rs, src/puffin/src/puffin_manager.rs, src/puffin/src/puffin_manager/file_accessor.rs, src/puffin/src/puffin_manager/stager.rs Annotated traits PuffinReader, BlobGuard, DirGuard, PuffinFileAccessor, and Stager with #[auto_impl::auto_impl(Arc)]. Updated trait bounds and type declarations to use Arc. Adjusted constructors and type implementations accordingly.
src/puffin/Cargo.toml Added auto_impl dependency version "1.2.0".

Poem

Tedious lines now melt away,
Arc advances, here to stay.
auto_impl guards our keep,
Less to code, more to reap.
Puffin safe in stager's arms,
Grace and speed in all its charms.
🌟🚀


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `Generate unit testing code for this file.` - `Open a follow-up GitHub issue for this discussion.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit testing code for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai generate interesting stats about this repository and render them as a table.` - `@coderabbitai show all the console.log statements in this repository.` - `@coderabbitai read src/utils.ts and generate unit testing code.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` - `@coderabbitai help me debug CodeRabbit configuration file.` Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. ### CodeRabbit Commands (invoked as PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai full review` to do a full review from scratch and review all the files again. - `@coderabbitai summary` to regenerate the summary of the PR. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository. - `@coderabbitai help` to get help. Additionally, you can add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. ### CodeRabbit Configration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information. - If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json` ### Documentation and Community - Visit our [Documentation](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
codecov[bot] commented 1 week ago

Codecov Report

Attention: Patch coverage is 90.90909% with 1 line in your changes missing coverage. Please review.

Project coverage is 84.69%. Comparing base (03c933c) to head (37e39c6). Report is 3 commits behind head on main.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #4279 +/- ## ========================================== - Coverage 84.96% 84.69% -0.27% ========================================== Files 1058 1061 +3 Lines 188055 188107 +52 ========================================== - Hits 159780 159317 -463 - Misses 28275 28790 +515 ```
v0y4g3r commented 1 week ago

I wonder if we can change stager: S to stager: Arc<S> so that we don't have to derive Stager impl for Arc<T>/Rc<T>/Box<T>

https://github.com/GreptimeTeam/greptimedb/blob/81ea1b6ee4d135345edb1b02c27be8c18f107489/src/puffin/src/puffin_manager/fs_puffin_manager.rs#L31

diff --git a/src/mito2/src/sst/index/puffin_manager.rs b/src/mito2/src/sst/index/puffin_manager.rs
index f953ca5dd4..18cfd7f07a 100644
--- a/src/mito2/src/sst/index/puffin_manager.rs
+++ b/src/mito2/src/sst/index/puffin_manager.rs
@@ -36,8 +36,7 @@ type InstrumentedAsyncRead = store::InstrumentedAsyncRead<'static, FuturesAsyncR
 type InstrumentedAsyncWrite = store::InstrumentedAsyncWrite<'static, FuturesAsyncWriter>;

 pub(crate) type BlobReader = <Arc<FsBlobGuard> as BlobGuard>::Reader;
-pub(crate) type SstPuffinManager =
-    FsPuffinManager<Arc<BoundedStager>, ObjectStorePuffinFileAccessor>;
+pub(crate) type SstPuffinManager = FsPuffinManager<BoundedStager, ObjectStorePuffinFileAccessor>;

 const STAGING_DIR: &str = "staging";

@@ -71,7 +70,8 @@ impl PuffinManagerFactory {
     pub(crate) fn build(&self, store: ObjectStore) -> SstPuffinManager {
         let store = InstrumentedStore::new(store).with_write_buffer_size(self.write_buffer_size);
         let puffin_file_accessor = ObjectStorePuffinFileAccessor::new(store);
-        SstPuffinManager::new(self.stager.clone(), puffin_file_accessor)
+        let stager = self.stager.clone();
+        SstPuffinManager::new(stager, puffin_file_accessor)
     }
 }

diff --git a/src/puffin/src/puffin_manager/fs_puffin_manager.rs b/src/puffin/src/puffin_manager/fs_puffin_manager.rs
index 7c95532dc6..e9c3854e98 100644
--- a/src/puffin/src/puffin_manager/fs_puffin_manager.rs
+++ b/src/puffin/src/puffin_manager/fs_puffin_manager.rs
@@ -16,6 +16,8 @@ mod dir_meta;
 mod reader;
 mod writer;

+use std::sync::Arc;
+
 use async_trait::async_trait;
 pub use reader::FsPuffinReader;
 pub use writer::FsPuffinWriter;
@@ -28,14 +30,18 @@ use crate::puffin_manager::PuffinManager;
 /// `FsPuffinManager` is a `PuffinManager` that provides readers and writers for puffin data in filesystem.
 pub struct FsPuffinManager<S, F> {
     /// The stager.
-    stager: S,
+    stager: Arc<S>,
     /// The puffin file accessor.
     puffin_file_accessor: F,
 }

-impl<S, F> FsPuffinManager<S, F> {
+impl<S, F> FsPuffinManager<S, F>
+where
+    S: Stager,
+    F: PuffinFileAccessor + Clone,
+{
     /// Creates a new `FsPuffinManager` with the specified `stager` and `puffin_file_accessor`.
-    pub fn new(stager: S, puffin_file_accessor: F) -> Self {
+    pub fn new(stager: Arc<S>, puffin_file_accessor: F) -> Self {
         Self {
             stager,
             puffin_file_accessor,
@@ -46,7 +52,7 @@ impl<S, F> FsPuffinManager<S, F> {
 #[async_trait]
 impl<S, F> PuffinManager for FsPuffinManager<S, F>
 where
-    S: Stager + Clone,
+    S: Stager,
     F: PuffinFileAccessor + Clone,
 {
     type Reader = FsPuffinReader<S, F>;
diff --git a/src/puffin/src/puffin_manager/fs_puffin_manager/reader.rs b/src/puffin/src/puffin_manager/fs_puffin_manager/reader.rs
index a1b8d3a8ea..2430d5d2c3 100644
--- a/src/puffin/src/puffin_manager/fs_puffin_manager/reader.rs
+++ b/src/puffin/src/puffin_manager/fs_puffin_manager/reader.rs
@@ -12,6 +12,8 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.

+use std::sync::Arc;
+
 use async_compression::futures::bufread::ZstdDecoder;
 use async_trait::async_trait;
 use futures::future::BoxFuture;
@@ -36,14 +38,14 @@ pub struct FsPuffinReader<S, F> {
     puffin_file_name: String,

     /// The stager.
-    stager: S,
+    stager: Arc<S>,

     /// The puffin file accessor.
     puffin_file_accessor: F,
 }

 impl<S, F> FsPuffinReader<S, F> {
-    pub(crate) fn new(puffin_file_name: String, stager: S, puffin_file_accessor: F) -> Self {
+    pub(crate) fn new(puffin_file_name: String, stager: Arc<S>, puffin_file_accessor: F) -> Self {
         Self {
             puffin_file_name,
             stager,
diff --git a/src/puffin/src/puffin_manager/fs_puffin_manager/writer.rs b/src/puffin/src/puffin_manager/fs_puffin_manager/writer.rs
index ab7227606d..945cbbc236 100644
--- a/src/puffin/src/puffin_manager/fs_puffin_manager/writer.rs
+++ b/src/puffin/src/puffin_manager/fs_puffin_manager/writer.rs
@@ -14,6 +14,7 @@

 use std::collections::HashSet;
 use std::path::PathBuf;
+use std::sync::Arc;

 use async_compression::futures::bufread::ZstdEncoder;
 use async_trait::async_trait;
@@ -39,7 +40,7 @@ pub struct FsPuffinWriter<S, W> {
     puffin_file_name: String,

     /// The stager.
-    stager: S,
+    stager: Arc<S>,

     /// The underlying `PuffinFileWriter`.
     puffin_file_writer: PuffinFileWriter<W>,
@@ -49,7 +50,7 @@ pub struct FsPuffinWriter<S, W> {
 }

 impl<S, W> FsPuffinWriter<S, W> {
-    pub(crate) fn new(puffin_file_name: String, stager: S, writer: W) -> Self {
+    pub(crate) fn new(puffin_file_name: String, stager: Arc<S>, writer: W) -> Self {
         Self {
             puffin_file_name,
             stager,
diff --git a/src/puffin/src/puffin_manager/stager.rs b/src/puffin/src/puffin_manager/stager.rs
index fd2c09254c..0ec565c1c1 100644
--- a/src/puffin/src/puffin_manager/stager.rs
+++ b/src/puffin/src/puffin_manager/stager.rs
@@ -52,7 +52,6 @@ pub trait InitDirFn = Fn(DirWriterProviderRef) -> WriteResult;

 /// `Stager` manages the staging area for the puffin files.
 #[async_trait]
-#[auto_impl::auto_impl(Box, Rc, Arc)]
 pub trait Stager: Send + Sync {
     type Blob: BlobGuard;
     type Dir: DirGuard;
zhongzc commented 1 week ago

I wonder if we can change stager: S to stager: Arc<S> so that we don't have to derive Stager impl for Arc<T>/Rc<T>/Box<T>

What benefits can be gained from this?

zhongzc commented 1 week ago

@WenyXu a failed fuzz test, PTAL https://github.com/GreptimeTeam/greptimedb/actions/runs/9792344295/job/27038170891?pr=4279

v0y4g3r commented 1 week ago

I wonder if we can change stager: S to stager: Arc<S> so that we don't have to derive Stager impl for Arc<T>/Rc<T>/Box<T>

What benefits can be gained from this?

Just make the code simpler 😄 overall lgtm

WenyXu commented 1 week ago

@WenyXu a failed fuzz test, PTAL https://github.com/GreptimeTeam/greptimedb/actions/runs/9792344295/job/27038170891?pr=4279

A known issue, fixed in #4283