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 bigkey fuse degration based on codis-proxy #2782

Closed
wants to merge 2 commits into from
Closed

Conversation

XiaoLiang2333
Copy link
Contributor

@XiaoLiang2333 XiaoLiang2333 commented Jul 5, 2024

BigKey检测逻辑

BigKey检测分两个阶段,

第一阶段检测Request,若Request被检测为大请求则直接被熔断掉

第二阶段检测Response,主要是记录一些bigkey

CheckerBkv中的CheckerRequest

首先会通过OpInfo的flag位调用CheckerRequest

若OpInfo中有特殊化检测(实现了CustomCheckFunc)则还会额外进行一次检测

CheckerCustumFunc

如果需要检测特定命令,则只需要在OpInfo中塞入对应结构体,并为其实现CustomCheckFunc接口即可

熔断

  1. 首先需要开启Checker,通过命令xconfig set checker_enabled 1

  2. 此时每个命令对应的session将会开启检测,当检测到bigkey,将会存入黑名单keyblacklist

  3. 随后通过命令xconfig set breaker_enabled 1开启熔断

  4. 通过xconfig set breaker_degradation_probability %d可以设置熔断概率,0-100

  5. 当命令进入到handleRequest()时,会首先进行isBigRequest检测,此处不会熔断,只是设置isBigRequest标识

  6. 除一些特殊命令以外,其他命令会进入到熔断检测IfDegradateService()

    func IfDegradateService(r *Request, isBigRequest bool, rd *rand.Rand) bool {
    	if !IsBreakerEnabled() {
    		return false
    	}
    	// Check CmdBlackList
    	if UseCmdBlackList() && IsCmdInBlackList(r) {
    			// If use cmdblacklist and exists, degraded
    			if ExecuteServiceDegradation(r, rd) { //Execute downgrade strategy
    				return true
    			}
    		}	
    	//Check KeyBlackList
    	if UseKeyBlackList() && IsKeyInBlackList(r) {
    			// If use keyblacklist and exists, degraded
    			if ExecuteServiceDegradation(r, rd) {
    				return true
    			}
    		}	
    	//Check BigRequest Flag
    	if isBigRequest {
    		if ExecuteServiceDegradation(r, rd) {
    			return true
    		}
    	}
    	// All pass
    	return false
    }
  7. ExecuteServiceDegradation(r, rd)负责执行熔断策略并返回熔断信息

Summary by CodeRabbit

  • New Features

    • Introduced request and response size and content checking capability to enhance monitoring and control.
    • Added custom check functions for specific Redis commands (e.g., SET, MGET, HSCAN).
    • Implemented a circuit breaker mechanism to degrade service probabilistically based on conditions.
    • Enabled management of command and key blacklists for better request handling.
  • Configuration Enhancements

    • Expanded configuration options for monitoring and breaker mechanisms, including enabling/disabling and setting various limits and blacklists.
  • Improved Handling

    • Enhanced proxy service with new logic for checking requests and responses, including custom checks and flag handling.
  • Testing Adjustments

    • Modified test functions to accommodate additional return values from the getOpInfo function.

@github-actions github-actions bot added the ✏️ Feature New feature or request label Jul 5, 2024
Copy link

coderabbitai bot commented Jul 5, 2024

Warning

Review failed

The pull request is closed.

Walkthrough

The changes introduce robust request-response checking mechanisms and a circuit breaker for the codis/pkg/proxy package, including functionality for detecting large requests, managing blacklists, and handling custom Redis commands. Additionally, new configuration options were added for monitoring and breaking functionalities.

Changes

File Path Change Summary
codis/pkg/proxy/CheckBkv.go Introduced constants, variables, and methods for checking large requests, keys, values, and result sets.
codis/pkg/proxy/CheckCustumFunc.go Added custom check functions for various Redis commands and their parameters.
codis/pkg/proxy/breaker.go Implemented circuit breaker logic with functions for state and probability handling and service degradation.
codis/pkg/proxy/breaker_black_list.go Introduced structures and functions for managing command and key blacklists.
codis/pkg/proxy/config.go Added new configuration parameters and logic for checker and breaker settings.
codis/pkg/proxy/mapper.go Introduced OpFlagChecker type, updated OpInfo, and added content checking methods and an interface.
codis/pkg/proxy/mapper_test.go Modified getOpInfo function to return additional values, impacting TestGetOpStr and TestGetOpStrCmd.
codis/pkg/proxy/monitorCustumFunc.go Introduced structs and methods to check requests and responses for specific Redis commands.
codis/pkg/proxy/proxy.go Expanded Proxy struct for new checker and breaker configurations and settings.
codis/pkg/proxy/request.go Reordered imports and added new fields OpFlagChecker and CustomCheckFunc to the Request struct.
codis/pkg/proxy/session.go Enhanced Session struct methods and added handleXConfig for managing new operations like XCONFIG.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Proxy
    participant Backend

    Client->>Proxy: Send Redis Command
    Proxy->>Proxy: Check Request Size
    alt Request is Large
        Proxy->>Proxy: Add to Blacklist
    end
    Proxy->>Backend: Forward Request
    Backend-->>Proxy: Send Response
    Proxy->>Proxy: Check Response Content
    alt Response Exceeds Threshold
        Proxy->>Proxy: Degrade Service
    end
    Proxy-->>Client: Forward Response
Loading

Poem

The code now guards with vigilant sight,
Breaking limits, it puts up a fight. 🔍
Requests and responses, all in line,
With custom checks, it's quite divine.
Blacklists ready, standing by,
Safety ensured as tasks fly high.
Codis evolves under the sky. 🌟


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.

@XiaoLiang2333
Copy link
Contributor Author

#1691
#2228

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✏️ Feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant