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

refactor: refactor PgStore #5309

Merged
merged 10 commits into from
Jan 7, 2025
Merged

Conversation

WenyXu
Copy link
Member

@WenyXu WenyXu commented Jan 7, 2025

I hereby agree to the terms of the GreptimeDB CLA.

Refer to a related PR or issue link (optional)

What's changed and what's your intention?

minor refactoring PgStore

Bench results:

  1. Serializable
    image

  2. Read repeatable
    image

  3. etcd
    image

PR Checklist

Please convert it to a draft if some of the following conditions are not met.

  • I have written the necessary rustdoc comments.
  • I have added the necessary unit tests and integration tests.
  • This PR requires documentation updates.
  • API changes are backward compatible.
  • Schema or data changes are backward compatible.

Summary by CodeRabbit

  • New Features

    • Added support for specifying table names in PostgreSQL key-value store initialization
    • Enhanced PostgreSQL error handling with more detailed SQL context
  • Bug Fixes

    • Improved parameter handling and string conversion in database interactions
    • Updated error handling mechanisms for database operations
  • Refactor

    • Streamlined PostgreSQL store implementation with generic type parameters
    • Updated test function signatures to use references instead of ownership
    • Simplified SQL query parameter management
  • Chores

    • Added Default trait implementations for response structs
    • Standardized string conversion methods in database interactions

@WenyXu WenyXu requested review from v0y4g3r, MichaelScofield and a team as code owners January 7, 2025 03:08
Copy link
Contributor

coderabbitai bot commented Jan 7, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This pull request introduces a series of modifications across multiple files in the GrepTime project, focusing on enhancing PostgreSQL store configuration, error handling, and test infrastructure. The changes primarily involve updating method signatures, improving SQL query handling, and refactoring key-value backend interactions. The modifications aim to provide more flexible and robust database interactions, with a particular emphasis on PostgreSQL-related functionality.

Changes

File Change Summary
src/cli/src/bench.rs Updated PgStore::with_url() to include table name parameter
src/common/meta/src/error.rs Removed StrFromUtf8 error variant, enhanced PostgresExecution error context, added is_serialization_error method
src/common/meta/src/kv_backend/postgres.rs Introduced generic type parameter for PgStore, updated SQL template handling, enhanced query execution
src/common/meta/src/rpc/store.rs Added Default trait to response structs
src/meta-srv/src/bootstrap.rs Added explicit table name parameter to PostgreSQL pool creation
src/meta-srv/src/election/postgres.rs Updated SQL query parameter handling and string conversion methods

Poem

🐰 Postgres Whispers, Code Refined

Errors trimmed, templates aligned,
References dance, backends entwined,
With rabbit's touch, the system's designed,
A symphony of bytes, perfectly signed!

🔧✨


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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

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.

@WenyXu WenyXu marked this pull request as draft January 7, 2025 03:09
@github-actions github-actions bot added the docs-not-required This change does not impact docs. label Jan 7, 2025
WenyXu and others added 2 commits January 7, 2025 03:16
…atable (#4)

* fix: election use bytea as well

* fix: use Serializable to avoid read unrepeatable
@WenyXu WenyXu force-pushed the refactor/pg-store branch from a88bda6 to 14c8a60 Compare January 7, 2025 03:23
@WenyXu WenyXu marked this pull request as ready for review January 7, 2025 03:24
@WenyXu
Copy link
Member Author

WenyXu commented Jan 7, 2025

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Jan 7, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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 comments (1)
src/meta-srv/src/election/postgres.rs (1)

Line range hint 347-358: Improve Error Handling When Retrieving Database Values

In the get_value_with_lease method, using try_get().unwrap_or_default() may mask underlying errors by defaulting to an empty string if the retrieval fails:

let current_time_str = res[0].try_get(1).unwrap_or_default();

Consider explicitly handling potential errors returned by try_get() to ensure that unexpected issues are surfaced and handled appropriately, rather than silently defaulting and potentially leading to incorrect behavior.

🧹 Nitpick comments (1)
src/common/meta/src/kv_backend/postgres.rs (1)

173-202: Consider PostgreSQL Parameter Limit in Batch Operations

In the methods generate_batch_upsert_query, generate_batch_delete_query, and generate_batch_get_query, SQL queries are generated with a number of parameters proportional to the batch size. PostgreSQL has a limit on the number of parameters in a prepared statement (typically around 32767). For very large batches, this limit might be exceeded, causing runtime errors.

Please ensure that batch sizes are appropriately limited to avoid exceeding PostgreSQL's parameter limit. If necessary, implement logic to split large batches into smaller chunks that stay within the parameter limit.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 05f115e and 3a230f2.

📒 Files selected for processing (10)
  • src/cli/src/bench.rs (1 hunks)
  • src/common/meta/src/error.rs (3 hunks)
  • src/common/meta/src/kv_backend/etcd.rs (3 hunks)
  • src/common/meta/src/kv_backend/memory.rs (2 hunks)
  • src/common/meta/src/kv_backend/postgres.rs (14 hunks)
  • src/common/meta/src/kv_backend/test.rs (10 hunks)
  • src/common/meta/src/rpc/store.rs (4 hunks)
  • src/log-store/src/raft_engine/backend.rs (3 hunks)
  • src/meta-srv/src/bootstrap.rs (1 hunks)
  • src/meta-srv/src/election/postgres.rs (9 hunks)
🧰 Additional context used
🪛 GitHub Check: Check (ubuntu-20.04)
src/common/meta/src/error.rs

[failure] 817-817:
failed to resolve: use of undeclared crate or module tokio_postgres


[failure] 815-815:
no variant named PostgresTransaction found for enum error::Error

⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: Toml Check
  • GitHub Check: Check License Header
  • GitHub Check: Clippy
  • GitHub Check: Build GreptimeDB binaries (ubuntu-20.04)
  • GitHub Check: Build GreptimeDB binary (profile-CI) (ubuntu-20.04)
🔇 Additional comments (14)
src/common/meta/src/kv_backend/postgres.rs (1)

88-133: Enhancement: Introduction of SqlTemplateFactory Improves Modularity

The addition of SqlTemplateFactory and SqlTemplateSet encapsulates SQL template generation, enhancing code modularity and readability. This refactoring simplifies the creation and management of SQL statements and reduces redundancy.

src/meta-srv/src/election/postgres.rs (3)

333-337: Improved Function Signatures by Using &str Instead of &String

Changing the parameter types from &String to &str in methods like get_value_with_lease simplifies the code and aligns with Rust best practices by reducing unnecessary indirection.


Line range hint 65-70: Ensure Safe SQL Concatenation in CAS_WITH_EXPIRE_TIME

In the CAS_WITH_EXPIRE_TIME SQL statement, concatenation is performed using parameters:

v=convert_to($3 || $4 || TO_CHAR(CURRENT_TIMESTAMP + INTERVAL '1 second' * $5, 'YYYY-MM-DD HH24:MI:SS.MS'), 'UTF8')

Please ensure that the concatenation of parameters is secure against SQL injection and that all parameters ($3, $4, $5) are properly sanitized, especially if they include any untrusted input.

✅ Verification successful

SQL Concatenation in CAS_WITH_EXPIRE_TIME is Secure

The implementation is safe against SQL injection as it properly uses PostgreSQL's parameterized queries with type-safe parameter binding through Rust's ToSql trait. The string concatenation occurs at the database level after parameter binding, and the timestamp formatting uses a hardcoded format string.

🏁 Scripts executed

The following scripts were executed for the analysis:

Error: No shell scripts found


Script:

#!/bin/bash
# Find the complete implementation containing these SQL statements
ast-grep --pattern 'const CAS_WITH_EXPIRE_TIME: &str = r#"'

# Find where these constants are used
rg "CAS_WITH_EXPIRE_TIME" -A 5

Length of output: 982


54-57: Verify Safe Handling of SQL Parameter Concatenation

In the PUT_IF_NOT_EXISTS_WITH_EXPIRE_TIME SQL statement, values are concatenated using the || operator with parameters $2, $3, and a formatted date:

VALUES($1, convert_to($2 || $3 || TO_CHAR(CURRENT_TIMESTAMP + INTERVAL '1 second' * $4, 'YYYY-MM-DD HH24:MI:SS.MS'), 'UTF8'))

While parameterization is used, please verify that concatenating parameters in this manner does not introduce SQL injection vulnerabilities, especially if any of the parameters ($2, $3, $4) can contain untrusted data.

✅ Verification successful

SQL Parameter Concatenation is Secure

The implementation is safe from SQL injection as it:

  • Uses proper parameter binding via Postgres's client library
  • Performs string concatenation within Postgres after parameter binding, not through string interpolation
  • Has strongly typed parameters (bytes for key, fixed separator constant, and numeric TTL)
🏁 Scripts executed

The following scripts were executed for the analysis:

Error: No shell scripts found


Script:

#!/bin/bash
# Find the complete SQL statement and its usage
rg -A 10 -B 10 "PUT_IF_NOT_EXISTS_WITH_EXPIRE_TIME" src/

# Look for the function/method that uses this SQL statement
ast-grep --pattern 'const PUT_IF_NOT_EXISTS_WITH_EXPIRE_TIME: &str'

Length of output: 4267

src/cli/src/bench.rs (1)

81-83: Update to PgStore::with_url Reflects API Changes

The call to PgStore::with_url has been updated to include the table_name parameter:

PgStore::with_url(postgres_addr, "greptime_metakv", 128)

This change aligns with the updated method signature in PgStore. Please ensure that all usages of with_url are updated accordingly throughout the codebase to prevent compilation errors.

src/common/meta/src/kv_backend/memory.rs (1)

358-358: LGTM! Improved memory efficiency in tests.

The changes correctly modify the test functions to use references instead of taking ownership, which is more efficient and aligns with Rust's ownership model.

Also applies to: 379-379, 386-386, 391-397

src/common/meta/src/kv_backend/etcd.rs (1)

594-594: LGTM! Consistent test improvements.

The changes correctly:

  1. Use references in test functions for better memory efficiency
  2. Add proper cleanup after tests

Also applies to: 621-622, 631-632, 639-644

src/common/meta/src/kv_backend/test.rs (2)

64-75: LGTM! Improved error reporting.

The enhanced assertion message now includes the prefix in UTF-8 format, which will help with debugging test failures.


355-355: LGTM! Consistent use of references in test functions.

The changes consistently modify function signatures to use &impl KvBackend instead of taking ownership, which:

  1. Improves memory efficiency
  2. Follows Rust's ownership best practices
  3. Maintains consistency across the test suite

Also applies to: 359-359, 410-410, 414-414, 454-454, 479-479, 509-509, 538-538, 578-578, 618-618

src/log-store/src/raft_engine/backend.rs (1)

647-647: LGTM! Improved efficiency by passing backend by reference.

The changes consistently update test function calls to pass the backend by reference instead of by value, which is more efficient and aligns with Rust's ownership model.

Also applies to: 674-674, 683-683

src/common/meta/src/error.rs (2)

652-654: LGTM! Enhanced error context for Postgres execution failures.

Including the SQL statement in the error message improves debuggability by providing more context about the failing operation.


812-821: LGTM! Added helper method to identify Postgres serialization errors.

The is_serialization_error method provides a clean way to check for specific Postgres serialization failures, which is useful for handling transient errors in transaction retries.

🧰 Tools
🪛 GitHub Check: Check (ubuntu-20.04)

[failure] 817-817:
failed to resolve: use of undeclared crate or module tokio_postgres


[failure] 815-815:
no variant named PostgresTransaction found for enum error::Error

src/common/meta/src/rpc/store.rs (2)

269-269: LGTM! Added Default trait implementations.

Adding the Default trait to these response types improves ergonomics by allowing them to be created with default values.

Also applies to: 428-428, 512-512


757-768: LGTM! Enhanced DeleteRangeResponse API.

The new constructor and setter methods provide a more ergonomic way to create and modify DeleteRangeResponse instances.

src/common/meta/src/kv_backend/postgres.rs Show resolved Hide resolved
src/meta-srv/src/bootstrap.rs Show resolved Hide resolved
@fengjiachun
Copy link
Collaborator

Rest LGTM

Copy link
Collaborator

@CookiePieWw CookiePieWw left a comment

Choose a reason for hiding this comment

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

Super Weny!

@WenyXu WenyXu enabled auto-merge January 7, 2025 07:27
Copy link

codecov bot commented Jan 7, 2025

Codecov Report

Attention: Patch coverage is 85.63433% with 77 lines in your changes missing coverage. Please review.

Project coverage is 83.92%. Comparing base (05f115e) to head (6d6e6ff).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5309      +/-   ##
==========================================
- Coverage   84.15%   83.92%   -0.23%     
==========================================
  Files        1200     1200              
  Lines      225396   225386      -10     
==========================================
- Hits       189684   189162     -522     
- Misses      35712    36224     +512     

@WenyXu WenyXu added this pull request to the merge queue Jan 7, 2025
Merged via the queue into GreptimeTeam:main with commit 3d9df82 Jan 7, 2025
37 checks passed
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.

3 participants