Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initial implementation of AlterDatabaseProcedure #4808

Conversation

NiwakaDev
Copy link
Collaborator

@NiwakaDev NiwakaDev commented Oct 10, 2024

I hereby agree to the terms of the GreptimeDB CLA.

Refer to a related PR or issue link (optional)

Related to #4394

What's changed and what's your intention?

This PR implements initial AlterDatabaseProcedure.

Checklist

  • I have written the necessary rustdoc comments.
  • I have added the necessary unit tests and integration tests.
  • This PR requires documentation updates.

Summary by CodeRabbit

  • New Features

    • Introduced a new module for altering databases, enhancing database management capabilities.
    • Added a new procedure for altering databases within a distributed system.
  • Bug Fixes

    • Minor formatting adjustments and comments added for clarity, improving overall code readability.

Copy link
Contributor

coderabbitai bot commented Oct 10, 2024

Note

Reviews paused

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Walkthrough

The changes introduce a new module named alter_database within the src/common/meta/src/ddl.rs file, which includes a new struct AlterDatabaseProcedure designed for handling database alterations in a distributed system. This struct features methods for initialization, JSON deserialization, and state management during the procedure's execution. The DdlManager is updated to register this new procedure, enhancing its capabilities to manage database alterations alongside existing DDL procedures.

Changes

File Path Change Summary
src/common/meta/src/ddl.rs Added new module: pub mod alter_database;
src/common/meta/src/ddl/alter_database.rs Introduced AlterDatabaseProcedure struct with various methods for managing database alterations, including new, from_json, on_prepare, on_update_metadata, execute, lock_key, and dump. Added enums and structs: AlterDatabaseState, AlterDatabaseData.
src/common/meta/src/ddl_manager.rs Updated register_loaders method to include AlterDatabaseProcedure in the DDL manager.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant DdlManager
    participant AlterDatabaseProcedure

    User->>DdlManager: Request Alter Database
    DdlManager->>AlterDatabaseProcedure: Create new instance
    AlterDatabaseProcedure->>AlterDatabaseProcedure: Initialize with context
    AlterDatabaseProcedure->>AlterDatabaseProcedure: Execute procedure
    AlterDatabaseProcedure-->>DdlManager: Return status
    DdlManager-->>User: Return result
Loading

🐰 In the meadow, changes bloom,
A new procedure clears the gloom.
Alter databases with ease,
As the code flows like a breeze.
Hopping forward, we embrace,
A brighter, more efficient space! 🌼


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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration 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 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 for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the docs-not-required This change does not impact docs. label Oct 10, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (7)
src/common/meta/src/ddl_manager.rs (1)

130-130: LGTM! Consider updating tests.

The addition of AlterDatabaseProcedure to the list of registered procedures is correct and follows the existing pattern. This change enhances the DDL capabilities of the DdlManager by allowing it to handle alterations to databases.

Consider updating the test case in the test_try_new function at the bottom of this file to include AlterDatabaseProcedure::TYPE_NAME in the expected_loaders vector. This will ensure that the new procedure is properly registered and tested.

src/common/meta/src/ddl/alter_database.rs (6)

27-30: Add documentation comments for the public struct AlterDatabaseProcedure

The struct AlterDatabaseProcedure is public but lacks documentation comments. Adding Rust doc comments will enhance code readability and maintainability, and assist other developers in understanding its purpose and usage.


35-49: Add documentation comments for the method new

The new method is public and should have a documentation comment explaining its purpose, parameters, and return value.


51-58: Add documentation comments for the method from_json

The from_json method is public but lacks documentation. Providing doc comments will help users understand how to deserialize the procedure from JSON.


60-62: Implement the on_prepare method

The on_prepare method is currently unimplemented (todo!();). Implementing this method is necessary for the procedure's preparation phase to function correctly.

Would you like assistance in implementing this method or creating a task to track this work?


64-66: Implement the on_update_metadata method

The on_update_metadata method contains a placeholder (todo!();). This method is essential for updating metadata during the procedure's execution.

Do you need help with the implementation, or should we open a GitHub issue to ensure it's addressed?


70-92: Add documentation comments for the Procedure implementation

The methods type_name, execute, lock_key, and dump are part of the Procedure trait implementation but lack documentation. Adding doc comments to these methods will improve clarity and assist others in understanding their roles within the procedure lifecycle.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between a8ed3db and 5f3885b.

📒 Files selected for processing (3)
  • src/common/meta/src/ddl.rs (1 hunks)
  • src/common/meta/src/ddl/alter_database.rs (1 hunks)
  • src/common/meta/src/ddl_manager.rs (2 hunks)
🧰 Additional context used
🔇 Additional comments (1)
src/common/meta/src/ddl_manager.rs (1)

Line range hint 1-1000: Consider adding a handler for AlterDatabaseProcedure

While the AlterDatabaseProcedure has been added to the list of registered procedures, there doesn't appear to be a corresponding handler function in this file. Other procedures like CreateDatabase and DropDatabase have their own handler functions (e.g., handle_create_database_task, handle_drop_database_task).

Consider adding a handle_alter_database_task function to maintain consistency with the existing code structure. This function should be implemented similarly to other handler functions and called from the submit_ddl_task method when an AlterDatabase task is received.

Here's a suggested outline for the new function:

async fn handle_alter_database_task(
    ddl_manager: &DdlManager,
    cluster_id: ClusterId,
    alter_database_task: AlterDatabaseTask,
) -> Result<SubmitDdlTaskResponse> {
    let (id, _) = ddl_manager
        .submit_alter_database(cluster_id, alter_database_task.clone())
        .await?;

    let procedure_id = id.to_string();
    info!(
        "Database {}.{} is altered via procedure_id {id:?}",
        alter_database_task.catalog, alter_database_task.schema
    );

    Ok(SubmitDdlTaskResponse {
        key: procedure_id.into(),
        ..Default::default()
    })
}

Don't forget to add the corresponding case in the match statement within the submit_ddl_task method:

match request.task {
    // ... existing cases ...
    AlterDatabase(alter_database_task) => {
        handle_alter_database_task(self, cluster_id, alter_database_task).await
    }
    // ... other cases ...
}

To ensure this suggestion is valid, let's check if the AlterDatabase variant exists in the task enum:

If the AlterDatabase variant is not found, it might need to be added to the appropriate enum definition.

@@ -31,6 +31,7 @@ use crate::rpc::ddl::{SubmitDdlTaskRequest, SubmitDdlTaskResponse};
use crate::rpc::procedure::{MigrateRegionRequest, MigrateRegionResponse, ProcedureStateResponse};
use crate::{ClusterId, DatanodeId};

pub mod alter_database;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Issue: AlterDatabaseProcedure is defined but not registered in DdlManager.

  • The AlterDatabaseProcedure is imported and used in ddl_manager.rs, but there is no registration found.
🔗 Analysis chain

LGTM: New alter_database module added.

The addition of the alter_database module aligns with the PR objectives and is consistent with the structure of other DDL-related modules in this file.

To ensure proper integration, let's verify if the new module is being used in the codebase:


Comment on lines +84 to +85
CatalogLock::Read(&self.data.catalog).into(),
SchemaLock::write(&self.data.catalog, &self.data.schema).into(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure consistent naming conventions for lock constructors

There is an inconsistency in naming conventions between CatalogLock::Read and SchemaLock::write:

  • CatalogLock::Read uses PascalCase.
  • SchemaLock::write uses snake_case.

To maintain consistency and adhere to Rust naming conventions:

  • If these are constructors or associated functions, consider using snake_case for both.
  • If they are enum variants, they should both use PascalCase.

Consider applying one of the following changes:

-            CatalogLock::Read(&self.data.catalog).into(),
+            CatalogLock::read(&self.data.catalog).into(),

or

-            SchemaLock::write(&self.data.catalog, &self.data.schema).into(),
+            SchemaLock::Write(&self.data.catalog, &self.data.schema).into(),

Ensure that the naming reflects their respective types appropriately.

Committable suggestion was skipped due to low confidence.

Copy link

codecov bot commented Oct 10, 2024

Codecov Report

Attention: Patch coverage is 2.00000% with 49 lines in your changes missing coverage. Please review.

Project coverage is 84.15%. Comparing base (e39f49f) to head (5f3885b).
Report is 417 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4808      +/-   ##
==========================================
- Coverage   85.15%   84.15%   -1.01%     
==========================================
  Files        1074     1125      +51     
  Lines      191979   204809   +12830     
==========================================
+ Hits       163483   172349    +8866     
- Misses      28496    32460    +3964     
---- 🚨 Try these New Features:

@waynexia
Copy link
Member

@coderabbitai pause

Copy link
Contributor

coderabbitai bot commented Oct 15, 2024

✅ Actions performed

Reviews paused.

@CookiePieWw
Copy link
Collaborator

Hi @NiwakaDev, I've noticed the pr has been stuck for a while. If you're busy recently, I'd like to help to push forward this based on this pr :)

@NiwakaDev
Copy link
Collaborator Author

@CookiePieWw

I've noticed the pr has been stuck for a while. If you're busy recently, I'd like to help to push forward this based on this pr :)

I've already implemented the demo. I'll divide into multiple PRs and then going to submit them in turn.

@CookiePieWw
Copy link
Collaborator

I've already implemented the demo. I'll divide into multiple PRs and then going to submit them in turn.

Got you :) call me if you need any help

@WenyXu
Copy link
Member

WenyXu commented Nov 20, 2024

Hi @NiwakaDev,

Apologies for bringing this up again, but we’d like to include this feature in the upcoming v0.10 release. If you’re busy recently, I’d be happy to help push it forward based on your work. Please let me know if that works for you!

@NiwakaDev
Copy link
Collaborator Author

@WenyXu

Is it better to submit a full PR?
I'm not sure how the initial PR get merged.

@WenyXu
Copy link
Member

WenyXu commented Nov 20, 2024

@WenyXu

Is it better to submit a full PR? I'm not sure how the initial PR get merged.

Hi @NiwakaDev,

Thanks for your quick reply! Apologies for the rush—this has become a priority due to a last-minute request from our CEO. Submitting a full PR sounds great, but since we’re aiming to release v0.10 tomorrow, the timeline is tight. Would you be open to collaborating with @CookiePieWw to finalize the changes together?

Let us know your thoughts!

@CookiePieWw CookiePieWw mentioned this pull request Nov 20, 2024
3 tasks
@NiwakaDev
Copy link
Collaborator Author

Thanks for your quick reply! Apologies for the rush—this has become a priority due to a last-minute request from our CEO.

@WenyXu @CookiePieWw @killme2008

Sorry for blocking your business. I'd like to contribute something if any chances and if you don't mind.

@NiwakaDev NiwakaDev closed this Nov 21, 2024
@NiwakaDev NiwakaDev deleted the feat/initial_implementation_alterdatabase_procedure branch November 21, 2024 12:47
@WenyXu
Copy link
Member

WenyXu commented Nov 21, 2024

Thanks for your quick reply! Apologies for the rush—this has become a priority due to a last-minute request from our CEO.

@WenyXu @CookiePieWw @killme2008

Sorry for blocking your business. I'd like to contribute something if any chances and if you don't mind.

Never mind🥰

@killme2008
Copy link
Contributor

Thanks for your quick reply! Apologies for the rush—this has become a priority due to a last-minute request from our CEO.

@WenyXu @CookiePieWw @killme2008

Sorry for blocking your business. I'd like to contribute something if any chances and if you don't mind.

@NiwakaDev
Thank you for your understanding, it really hasn't affected anything, so please don't take it to heart. @WenyXu was also joking; this is not a personal decision of mine. We just hope to include both alter database options and alter table options in version v0.10.0. They are important features for some users.
I sincerely apologize for the premature termination of your PR. In the future, we still warmly welcome you to continue contributing; your participation is very important to us. We look forward to your continued contributions!

@killme2008
Copy link
Contributor

@NiwakaDev
For similar cases in the future, we will communicate with the contributors in advance, rather than rushing like this. Thank you again for your understanding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs-not-required This change does not impact docs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants