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: Add bpop cmd #48

Open
wants to merge 1 commit into
base: unstable
Choose a base branch
from
Open

feat: Add bpop cmd #48

wants to merge 1 commit into from

Conversation

SamuelSze1
Copy link
Collaborator

@SamuelSze1 SamuelSze1 commented Sep 21, 2024

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced BLPop and BRPop commands for blocking list pop operations.
    • Added functionality to block clients while waiting for keys and to unblock them after operations.
    • Implemented a method to manage evicted blocked connections, enhancing connection management.
  • Bug Fixes

    • Improved handling of client state updates during rename and list operations.
  • Documentation

    • Updated command table to include new blocking operations and their functionalities.

These enhancements improve the efficiency and responsiveness of client interactions with the system.

Copy link

coderabbitai bot commented Sep 21, 2024

Walkthrough

The changes introduce new methods and classes to enhance the functionality of the BaseCmd and KiwiDB classes for managing blocked connections in a multi-database context. Notable additions include methods for blocking clients while waiting for data, unblocking connections, and handling expired connections. New command classes for blocking list pop operations are also defined, along with modifications to existing command handling functions to incorporate these features. Overall, the updates improve the system's ability to manage concurrent data access and manipulation.

Changes

Files Change Summary
src/base_cmd.cc, src/base_cmd.h Introduced methods BlockThisClientToWaitLRPush, ServeAndUnblockConns, and IsExpired in BaseCmd and BlockedConnNode classes. Added command names kCmdNameBLPop, kCmdNameBRPop, and structures BlockKey, BlockKeyHash for managing blocked connections.
src/cmd_admin.cc Added method calls to set the client's key and invoke ServeAndUnblockConns after successful data push operations.
src/cmd_keys.cc Updated RenameCmd::DoCmd to set the client's key and call ServeAndUnblockConns after a successful rename operation.
src/cmd_list.cc, src/cmd_list.h Modified DoCmd methods for LPushCmd, RPoplpushCmd, and RPushCmd to include calls to ServeAndUnblockConns. Introduced new classes BLPopCmd and BRPopCmd with relevant methods and member variables for blocking operations.
src/cmd_table_manager.cc Registered new commands BLPop and BRPop in the command table with a parameter count of -3.
src/kiwi.cc, src/kiwi.h Added method ScanEvictedBlockedConnsOfBlrpop for managing evicted blocked connections, and methods to access the blocked connections map and mutex. Introduced member variables for managing blocked connections.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant BaseCmd
    participant KiwiDB

    Client->>BaseCmd: Request to Block Client
    BaseCmd->>KiwiDB: BlockThisClientToWaitLRPush(keys, expire_time, client, type)
    KiwiDB-->>BaseCmd: Client Blocked
    BaseCmd->>Client: Confirmation of Blocking

    Client->>BaseCmd: Request to Unblock Client
    BaseCmd->>KiwiDB: ServeAndUnblockConns(client)
    KiwiDB-->>BaseCmd: Connections Unblocked
    BaseCmd->>Client: Confirmation of Unblocking
Loading

Poem

🐰 In the garden where bunnies play,
New commands hop in, brightening the day!
Block and unblock, a dance so sweet,
With each little change, our code's a treat!
Hooray for the features that help us thrive,
In the world of code, we joyfully dive! 🌼


Recent review details
Commits

Files that changed from the base of the PR and between ff31360 and d525c40.

Files selected for processing (9)
  • src/base_cmd.cc (2 hunks)
  • src/base_cmd.h (4 hunks)
  • src/cmd_admin.cc (1 hunks)
  • src/cmd_keys.cc (1 hunks)
  • src/cmd_list.cc (3 hunks)
  • src/cmd_list.h (1 hunks)
  • src/cmd_table_manager.cc (1 hunks)
  • src/kiwi.cc (3 hunks)
  • src/kiwi.h (2 hunks)

Note

Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

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.

@SamuelSze1
Copy link
Collaborator Author

增加blpop和brpop指令,步骤为下
1.首先尝试直接lpop或rpop,如果list中有数据就直接返回,否则进入下一步
2.调用BlockThisClientToWaitLRPush将client放入key_to_blocked_conns_中对应的key的阻塞队列中
3.在执行向list中添加元素的cmd后调用ServeAndUnblockConns,将list的元素根据key_to_blocked_conns_中对应的key的阻塞队列的顺序执行对应的lpop或rpop并把结果发回给client
4.注册了一个线程每隔250ms调用ScanEvictedBlockedConnsOfBlrpop,遍历所有key_to_blocked_conns_并删除已经超时的或被执行的链接并返回给client
5.因为blpop只能可以输入多个list作为参数,但最后只能返回一个list的元素,所以在BlockedConnNode中有一个std::shared_ptr<std::atomic>,在BlockThisClientToWaitLRPush中会给对应同一个bpop指令的BlockedConnNode一个指向同一个原子变量的指针,当一个BlockedConnNode被从队列中取出来的时候会把这个变量设为true,阻止其他BlockedConnNode被执行

@@ -106,6 +106,91 @@ BaseCmd* BaseCmdGroup::GetSubCmd(const std::string& cmdName) {
return subCmd->second.get();
}

void BaseCmd::BlockThisClientToWaitLRPush(std::vector<std::string>& keys, int64_t expire_time, PClient* client,
Copy link
Collaborator

Choose a reason for hiding this comment

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

BlockThisClientToWaitLRPush 这个方法没有看到哪里有调用

Copy link
Collaborator

Choose a reason for hiding this comment

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

看 pika 那边用到了这个方法, 可以补齐

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants