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

fix: ensure keep alive is completed in time #4349

Merged
merged 9 commits into from
Jul 17, 2024

Conversation

WenyXu
Copy link
Member

@WenyXu WenyXu commented Jul 11, 2024

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?

ensure keep alive is completed in time

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

    • Improved lease management and leader handling in the election process with enhanced timeout error handling.
  • Bug Fixes

    • Adjusted default replica values for Greptimedb and Etcd clusters from 3 to 1, improving resource efficiency.
  • Refactor

    • Switched function call from common_runtime::spawn_bg to common_runtime::spawn_write for better asynchronous operation handling.
  • Performance Improvements

    • Extended lease duration for the metasrv leader from 3 to 5 seconds, enhancing stability.

Copy link
Contributor

coderabbitai bot commented Jul 11, 2024

Walkthrough

Recent updates introduced new imports and logic changes to the keep_alive method in the EtcdElection implementation. These changes improve the handling of leadership changes and lease management. Default values for meta-replicas and etcd-replicas in GitHub action configurations were adjusted, and the constant META_LEASE_SECS was updated. Additionally, the Metasrv implementation now uses spawn_write instead of spawn_bg for asynchronous operations.

Changes

File Path Summary of Changes
src/meta-srv/src/election/etcd.rs Introduced new imports (LeaderKey, LeaseKeepAliveStream, LeaseKeeper, PutOptions, timeout) and modified the keep_alive method to improve error handling and lease management.
.github/actions/setup-greptimedb-cluster/action.yml Changed default value for meta-replicas from 3 to 1.
.github/actions/setup-etcd-cluster/action.yml Changed default value for etcd-replicas from 3 to 1.
src/common/meta/src/distributed_time_constants.rs Updated META_LEASE_SECS constant value from 3 to 5, impacting lease duration.
src/meta-srv/src/metasrv.rs Changed function call from common_runtime::spawn_bg to common_runtime::spawn_write for asynchronous operations in Metasrv implementation.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant EtcdElection
    participant EtcdCluster
    participant EtcdLeader

    Client->>EtcdElection: Request to keep alive
    activate EtcdElection
    EtcdElection->>EtcdCluster: Request lease keep alive
    activate EtcdCluster
    EtcdCluster-->>EtcdElection: Lease keep alive response
    deactivate EtcdCluster

    EtcdElection->>EtcdLeader: Check leader status
    activate EtcdLeader
    EtcdLeader-->>EtcdElection: Leader status response
    deactivate EtcdLeader

    EtcdElection-->>Client: Respond with keep alive status
    deactivate EtcdElection
Loading

Poem

In the land of code, where changes thrive,
The leader's lease now stays alive.
From three to one, replicas stand,
With spawn_write now, the tasks are planned.
Constants shift, new imports gleam,
Our code evolves, like in a dream. 🌟


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>.
    • 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 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 Jul 11, 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: 0

Outside diff range, codebase verification and nitpick comments (2)
src/meta-srv/src/election/etcd.rs (2)

240-264: Ensure the campaign function is tested.

No direct tests for the campaign function were found in the codebase. It is crucial to add tests to verify the functionality and error handling of this function.

  • Add unit tests or integration tests covering the campaign function to ensure its reliability.
Analysis chain

Keep-alive logic in campaign looks good.

The modifications ensure the keep-alive operation is completed within the specified interval, and appropriate error handling is in place.

However, ensure that this function and its new logic are adequately tested.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify if the `campaign` function is tested.

# Test: Search for tests related to the `campaign` function. Expect: Results indicating tests.
rg --type rust -A 5 $'campaign'

Length of output: 2114


Script:

#!/bin/bash
# Description: Verify if the `campaign` function is tested.

# Test: Search for test functions related to the `campaign` function, particularly in test directories or files.
rg --type rust -A 5 'campaign' --glob '*test*'

Length of output: 46


Script:

#!/bin/bash
# Description: Broaden the search to verify if the `campaign` function is tested.

# Test: Search for all references to the `campaign` function in the entire codebase.
rg --type rust -A 5 'campaign'

Length of output: 2114


300-342: No tests found for the keep_alive function.

The search did not yield any results indicating that the keep_alive function is directly tested in the identified test files. It is recommended to add explicit tests to ensure the function's behavior is thoroughly validated.

  • Review and add tests for the keep_alive function to ensure it is adequately covered.
Analysis chain

The keep_alive function looks good.

The function correctly handles the keep-alive logic and includes appropriate error handling and logging.

However, ensure that this function is adequately tested.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify if the `keep_alive` function is tested.

# Test: Search for tests related to the `keep_alive` function. Expect: Results indicating tests.
rg --type rust -A 5 $'keep_alive'

Length of output: 11998


Script:

#!/bin/bash
# Description: Search for test files and functions related to the `keep_alive` function.

# Find all Rust test files in the codebase
fd --type f --extension rs --exec grep -l '#[cfg(test)]'

# Search for test functions that call the `keep_alive` function
rg --type rust -A 5 'fn test_.*keep_alive'

Length of output: 1376


Script:

#!/bin/bash
# Description: Search for the `keep_alive` function in the identified test files.

test_files=(
  "./tests-integration/tests/sql.rs"
  "./tests-integration/tests/grpc.rs"
  "./tests-integration/src/prom_store.rs"
  "./tests-integration/src/tests/instance_test.rs"
  "./src/sql/src/statements/show.rs"
  "./src/sql/src/statements/set_variables.rs"
  "./src/sql/src/statements/drop.rs"
  "./src/sql/src/statements/create.rs"
  "./src/sql/src/statements/describe.rs"
  "./src/sql/src/statements/alter.rs"
  "./src/sql/src/statements/truncate.rs"
  "./src/servers/src/prom_store.rs"
  "./src/query/src/promql/planner.rs"
  "./src/query/src/optimizer/count_wildcard.rs"
  "./src/servers/tests/http/prom_store_test.rs"
  "./src/promql/src/functions/changes.rs"
  "./src/puffin/src/file_metadata.rs"
  "./src/script/src/python/ffi_types/utils.rs"
  "./src/promql/src/extension_plan/scalar_calculate.rs"
  "./src/promql/src/extension_plan/empty_metric.rs"
  "./src/log-store/src/kafka/log_store.rs"
  "./src/mito2/src/request.rs"
  "./src/flow/src/transform/expr.rs"
  "./src/flow/src/transform/aggr.rs"
  "./src/flow/src/transform/literal.rs"
  "./src/flow/src/expr/scalar.rs"
  "./src/datatypes/src/value.rs"
  "./src/common/time/src/range.rs"
  "./src/common/mem-prof/src/jemalloc.rs"
  "./src/common/macro/src/stack_trace_debug.rs"
  "./src/common/macro/src/range_fn.rs"
  "./src/common/macro/src/admin_fn.rs"
  "./src/common/query/src/lib.rs"
  "./src/common/base/src/secrets.rs"
  "./src/cmd/src/cli/export.rs"
)

# Search for the `keep_alive` function in the identified test files
for file in "${test_files[@]}"; do
  rg 'keep_alive' "$file"
done

Length of output: 4345

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between da0c840 and 976a814.

Files selected for processing (1)
  • src/meta-srv/src/election/etcd.rs (3 hunks)
Additional comments not posted (1)
src/meta-srv/src/election/etcd.rs (1)

21-21: Imports look good.

The new imports are necessary for the keep-alive functionality and are correctly added.

Also applies to: 26-26

@WenyXu WenyXu enabled auto-merge July 11, 2024 11:41
Copy link

codecov bot commented Jul 11, 2024

Codecov Report

Attention: Patch coverage is 0% with 59 lines in your changes missing coverage. Please review.

Project coverage is 84.89%. Comparing base (da0c840) to head (b956d8d).
Report is 30 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4349      +/-   ##
==========================================
- Coverage   85.18%   84.89%   -0.30%     
==========================================
  Files        1060     1061       +1     
  Lines      189069   189503     +434     
==========================================
- Hits       161057   160870     -187     
- Misses      28012    28633     +621     

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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 976a814 and ec33d8c.

Files selected for processing (1)
  • src/meta-srv/src/election/etcd.rs (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/meta-srv/src/election/etcd.rs

@WenyXu WenyXu requested a review from a team as a code owner July 12, 2024 03:39
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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ec33d8c and aafca73.

Files selected for processing (1)
  • .github/actions/setup-greptimedb-cluster/action.yml (1 hunks)
Files skipped from review due to trivial changes (1)
  • .github/actions/setup-greptimedb-cluster/action.yml

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: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between aafca73 and 7dda31c.

Files selected for processing (4)
  • .github/actions/setup-etcd-cluster/action.yml (1 hunks)
  • src/common/meta/src/distributed_time_constants.rs (1 hunks)
  • src/meta-srv/src/election/etcd.rs (3 hunks)
  • src/meta-srv/src/metasrv.rs (1 hunks)
Files skipped from review due to trivial changes (1)
  • .github/actions/setup-etcd-cluster/action.yml
Additional comments not posted (4)
src/common/meta/src/distributed_time_constants.rs (1)

36-36: Increase in META_LEASE_SECS duration

The META_LEASE_SECS constant has been increased from 3 to 5 seconds. This change affects the lease duration for the metasrv leader. Ensure that this adjustment aligns with the desired lease duration for the system.

src/meta-srv/src/election/etcd.rs (2)

21-21: Approved: New imports for lease management and timeout handling

The new imports LeaderKey, LeaseKeepAliveStream, LeaseKeeper, and timeout are essential for the new keep_alive method and enhanced error handling in the campaign method.

Also applies to: 26-26


238-253: Approved: Enhanced error handling with timeout in campaign method

The usage of timeout for the keep-alive operation in the campaign method ensures that the operation completes within the specified interval, which is crucial for maintaining leadership. The error handling is also enhanced.

Verify that the META_KEEP_ALIVE_INTERVAL_SECS value is appropriate for the desired keep-alive interval.

src/meta-srv/src/metasrv.rs (1)

453-453: Approved: Change to common_runtime::spawn_write

The change from common_runtime::spawn_bg to common_runtime::spawn_write in the try_start method impacts asynchronous operation handling. This adjustment is likely aimed at ensuring that the write operations are handled appropriately in the background.

Verify that this change aligns with the desired behavior for asynchronous write operations.

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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7dda31c and 640e462.

Files selected for processing (1)
  • src/meta-srv/src/election/etcd.rs (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/meta-srv/src/election/etcd.rs

Copy link
Collaborator

@fengjiachun fengjiachun left a comment

Choose a reason for hiding this comment

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

LGTM

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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 640e462 and 941ef2d.

Files selected for processing (1)
  • src/meta-srv/src/election/etcd.rs (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/meta-srv/src/election/etcd.rs

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: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 941ef2d and b956d8d.

Files selected for processing (1)
  • src/meta-srv/src/election/etcd.rs (3 hunks)
Additional comments not posted (2)
src/meta-srv/src/election/etcd.rs (2)

21-22: Approved: New imports are necessary and used appropriately.

The new imports (LeaderKey, LeaseKeepAliveStream, LeaseKeeper, and timeout) are essential for the keep_alive method.

Also applies to: 26-26


235-271: Ensure proper error handling for timeout.

The timeout for the keep_alive operation ensures it completes within META_KEEP_ALIVE_INTERVAL_SECS. The error handling seems appropriate, but consider logging additional context for debugging purposes.

src/meta-srv/src/election/etcd.rs Show resolved Hide resolved
@WenyXu WenyXu added this pull request to the merge queue Jul 17, 2024
Merged via the queue into GreptimeTeam:main with commit e39f49f Jul 17, 2024
55 checks passed
@WenyXu WenyXu deleted the fix/keep-alive-timeout branch July 17, 2024 06:37
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.

None yet

3 participants