pika icon indicating copy to clipboard operation
pika copied to clipboard

feat: Add Keyspace hits metrics for set

Open chenbt-hz opened this issue 1 year ago • 10 comments

修改内容 1.新增keyspace_hits、keyspace_misses指标 2.修改了set相关命令的返回结果,使得keyspace_hits、keyspace_misses可以正确统计 3.新增了go测试

需要审查的点

  • [ ] 当前修改新增了kNoExists是否合理?
  • [ ] 当前返回是否符合预期,尤其是异常列表中的情况能否接受?

说明

这次改动主要为了在pika支持keyspace hit相关参数。 关联的issue: https://github.com/OpenAtomFoundation/pika/issues/2423 参考redis的实现: redis在执行操作前会统一在内存查找key是否存在并统计 但在pika这一层,需要在各个命令执行阶段实现统计。

  • 本次的改动默认s_ = db_->storage()返回的状态对象能够符合预期的返回s_.IsNotFound()==true。 即,当key不存在时s_.IsNotFound()==true。
  • 管理命令、有部分的write命令无关key是否存在,因此这些命令将不会纳入keyspace_hit相关的统计。
  • 对于执行报错的命令,不会纳入keyspace_hit相关的统计。
  • 对于read相关命令以及关于某个key的统计命令,应根据实际含义添加keyspace_hit相关统计。

当前只修改set,后续还需要将其他的数据类型命令全部修改。

异常返回统计表

当前版本由于storage层部分s_.IsNotFound()接口返回结果不适配,导致部分命令的返回结果会超出预期,这里是相关列表。


异常描述 命令列表
执行不存在的key命令返回keyspace_hits SInter,SInterstore,SDiffstore
执行存在的key命令返回keyspace_misses  
对存在的key,操作不存在的元素命令返回keyspace_misses Sismember,SMoveCmd
对存在的key,操作存在的元素命令返回keyspace_misses  
操作多个key时,有key存在且有key不存在时,返回keyspace_misses  
操作多个key时,所有key不存在时,返回keyspace_hits Sdiff,SUnionstore,SUnion
   

Summary by CodeRabbit

  • New Features

    • Introduced a new command response type for non-existent keys.
    • Added new methods to track server keyspace hits and misses.
    • Enhanced reporting of keyspace metrics in server statistics.
  • Bug Fixes

    • Improved command response logic to consistently handle non-existent keys.
  • Tests

    • Added integration tests focusing on tracking keyspace hits and misses.
    • Enhanced testing script to accommodate longer test durations and integrate new testing frameworks.

chenbt-hz avatar Apr 03 '24 03:04 chenbt-hz

基本的验证

$redis-cli -p 9221 info stats |grep key keyspace_hits:19 keyspace_misses:0 is_scaning_keyspace:No

集成测试 image

image

chenbt-hz avatar Apr 03 '24 03:04 chenbt-hz

@chenbt-hz please fix the confliction

AlexStocks avatar May 31 '24 12:05 AlexStocks

@chenbt-hz please fix the confliction

done

chenbt-hz avatar Jun 03 '24 03:06 chenbt-hz

@CodiumAI-Agent /review

AlexStocks avatar Jun 05 '24 07:06 AlexStocks

PR Review 🔍

⏱️ Estimated effort to review [1-5]

4, because the PR involves a significant amount of changes across multiple files, including core functionality and tests. The changes impact the command execution flow and statistics calculation, which are critical areas in the application. Understanding and verifying the correctness of these changes requires a deep understanding of the existing architecture and the intended new functionality.

🧪 Relevant tests

Yes

⚡ Possible issues

Possible Bug: The handling of kNoExists in CmdRes::ToString() might not return a proper error message. The current implementation just returns message_, which might not be informative if not set elsewhere.

Performance Concern: The frequent atomic operations for incrementing keyspace hits and misses might impact performance, especially under high load. This should be benchmarked to ensure that the performance impact is minimal.

🔒 Security concerns

No

Code feedback:
relevant filesrc/pika_command.cc
suggestion      

Consider adding more specific error handling or messages for kNoExists in CmdRes::ToString(). This will improve debugging and logging capabilities. [important]

relevant linereturn message_;

relevant filesrc/pika_set.cc
suggestion      

Ensure consistency in response format for commands when s_.IsNotFound() is true. Currently, some commands append array lengths or other data which might not be expected in an error scenario. [important]

relevant lineres_.AppendArrayLenUint64(members.size());

relevant filesrc/pika_server.cc
suggestion      

Implement a mechanism to limit the rate of increment operations on keyspace hits and misses to mitigate potential performance impacts. This could involve batching increments or only updating the counters every few operations. [medium]

relevant linevoid PikaServer::incr_server_keyspace_hits() { ++(statistic_.server_stat.keyspace_hits); }

relevant filetests/integration/server_test.go
suggestion      

Add more comprehensive tests for edge cases and error conditions in keyspace hit/miss statistics to ensure robustness. [medium]

relevant lineExpect(client.SMembers(ctx, "keyspace_hits").Err()).NotTo(HaveOccurred());

QodoAI-Agent avatar Jun 05 '24 07:06 QodoAI-Agent

@CodiumAI-Agent /improve

AlexStocks avatar Jun 05 '24 07:06 AlexStocks

PR Code Suggestions ✨

CategorySuggestion                                                                                                                                    Score
Thread safety
Ensure thread safety by using atomic increment operations

Use atomic increment operations for thread safety when updating keyspace hits and misses.

src/pika_server.cc [957-958]

-void PikaServer::incr_server_keyspace_hits() { ++(statistic_.server_stat.keyspace_hits); }
-void PikaServer::incr_server_keyspace_misses() { ++(statistic_.server_stat.keyspace_misses); }
+void PikaServer::incr_server_keyspace_hits() { statistic_.server_stat.keyspace_hits.fetch_add(1, std::memory_order_relaxed); }
+void PikaServer::incr_server_keyspace_misses() { statistic_.server_stat.keyspace_misses.fetch_add(1, std::memory_order_relaxed); }
 
Suggestion importance[1-10]: 10

Why: This suggestion is crucial for ensuring thread safety when updating keyspace hits and misses, which is essential in a multi-threaded environment.

10
Possible bug
Separate the checks for 'noexist' and 'ok' to ensure accurate keyspace hit/miss tracking

Consider checking the result of res().noexist() and res().ok() separately to avoid
potential logical errors where a command might be marked as a miss incorrectly if
res().ok() returns false.

src/pika_command.cc [916-920]

-if (!IsAdmin() && res().ok()) {
+if (!IsAdmin()) {
   if (res().noexist()) {
     g_pika_server->incr_server_keyspace_misses();
-  } else {
+  } else if (res().ok()) {
     g_pika_server->incr_server_keyspace_hits();
   }
 }
 
Suggestion importance[1-10]: 9

Why: This suggestion addresses a potential logical error by ensuring that keyspace hits are only incremented when the result is explicitly OK, improving the accuracy of the keyspace hit/miss tracking.

9
Best practice
Initialize atomic variables to prevent undefined behavior

Consider initializing the newly added atomic variables keyspace_hits and keyspace_misses
to ensure they start with a defined state. This can prevent any undefined behavior or
incorrect statistics due to uninitialized values.

include/pika_statistic.h [40-41]

-std::atomic<long long> keyspace_hits;
-std::atomic<long long> keyspace_misses;
+std::atomic<long long> keyspace_hits{0};
+std::atomic<long long> keyspace_misses{0};
 
Suggestion importance[1-10]: 9

Why: Initializing atomic variables is a best practice to ensure they start with a defined state, preventing any potential undefined behavior or incorrect statistics due to uninitialized values.

9
Refactor the 'ok' method to strictly check for 'kOk' status

Refactor the ok() method to explicitly check for kOk status to avoid unintended behavior
where kNoExists is treated as a successful status.

include/pika_command.h [342]

-bool ok() const { return ret_ == kOk || ret_ == kNone || ret_ == kNoExists; }
+bool ok() const { return ret_ == kOk; }
 
Suggestion importance[1-10]: 8

Why: This suggestion improves the clarity and correctness of the ok() method by ensuring it only returns true for the kOk status, avoiding unintended behavior.

8
Enhancement
Add a space after the colon for consistent formatting in the output

Ensure consistent formatting by adding a space after the colon in the output for better
readability.

src/pika_admin.cc [972-973]

-tmp_stream << "keyspace_hits:" << g_pika_server->ServerKeyspaceHits() << "\r\n";
-tmp_stream << "keyspace_misses:" << g_pika_server->ServerKeyspaceMisses() << "\r\n";
+tmp_stream << "keyspace_hits: " << g_pika_server->ServerKeyspaceHits() << "\r\n";
+tmp_stream << "keyspace_misses: " << g_pika_server->ServerKeyspaceMisses() << "\r\n";
 
Suggestion importance[1-10]: 5

Why: This suggestion improves the readability of the output by ensuring consistent formatting, but it is a minor enhancement.

5

QodoAI-Agent avatar Jun 05 '24 07:06 QodoAI-Agent

两位大佬 @luky116 @chejinge 的意见我都回复了,麻烦帮忙再看下,感谢~

chenbt-hz avatar Jul 19 '24 08:07 chenbt-hz

Bot detected the issue body's language is not English, translate it automatically.


I have replied to the opinions of the two big guys @luky116 @chejinge. Please help me and check again. Thank you~

Issues-translate-bot avatar Jul 19 '24 08:07 Issues-translate-bot

Walkthrough

This update enhances the server's command handling and performance tracking, introducing new metrics for keyspace hits and misses. The functionality has been expanded to include better response handling for non-existent keys, improved admin status checks, and refined testing capabilities with Ginkgo. Overall, these changes aim to optimize server performance monitoring and improve the clarity and robustness of command responses.

Changes

Files Change Summary
include/pika_command.h Added kNoExists to CmdRes enum, updated ok() method logic, new noexist() method, and renamed IsAdminRequire to IsAdmin.
include/pika_server.h Introduced new methods for retrieving and incrementing keyspace hits and misses.
include/pika_statistic.h Added std::atomic<long long> variables for tracking keyspace hits and misses.
src/pika_admin.cc Enhanced InfoStats method to report keyspace hits and misses.
src/pika_command.cc Modified command handling to increment keyspace hits/misses based on command execution results, renamed method for clarity.
src/pika_server.cc Added methods for managing keyspace statistics to improve server monitoring capabilities.
src/pika_set.cc Improved response handling for non-existent keys across multiple command methods.
tests/integration/integrate_test.sh Enhanced testing script with Ginkgo, added debug output, and increased test timeout.
tests/integration/server_test.go Added test for verifying accurate keyspace hits and misses tracking.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server
    participant Cmd

    Client->>Cmd: Execute Command
    Cmd->>Server: Check Key Existence
    alt Key Exists
        Server-->>Cmd: Key Found
        Cmd->>Client: Return Success Response
    else Key Does Not Exist
        Server-->>Cmd: Key Not Found
        Cmd->>Server: Increment Keyspace Misses
        Cmd->>Client: Return No Exists Response
    end

🐰 In the meadow, where bunnies hop,
New features bloom, we won't stop!
Keyspace hits, misses, oh what a delight,
With Ginkgo's help, our tests take flight!
In code we trust, through changes we bound,
Happy updates make our world go round! 🌼


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 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.

coderabbitai[bot] avatar Aug 02 '24 01:08 coderabbitai[bot]