Skip to content

Conversation

@YuCai18
Copy link
Collaborator

@YuCai18 YuCai18 commented Jun 25, 2025

新增功能 #3079:pika3.5新增大key检测及其日志写入功能

功能展示

1. info命令展示

image

image

2.大key日志写入结果展示

image

go测试结果

image

@github-actions github-actions bot added the ✏️ Feature New feature or request label Jun 25, 2025
@coderabbitai
Copy link

coderabbitai bot commented Jun 25, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

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.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this 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.

@YuCai18 YuCai18 force-pushed the codereview3.5/bigkey branch 28 times, most recently from 8212d18 to fb563a7 Compare June 30, 2025 12:31
@YuCai18 YuCai18 force-pushed the codereview3.5/bigkey branch 16 times, most recently from c465971 to 2a28e2b Compare July 8, 2025 05:05
@YuCai18 YuCai18 force-pushed the codereview3.5/bigkey branch from 2a28e2b to 71e92ec Compare July 8, 2025 10:11
# Interval time for scanning expired big key threads.
# Default: 1
# When set to 0, big key logging will be disabled.
bigkeys_log_interval : 1
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里默认改成0吧,默认不打印

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done


Redis::~Redis() {
std::vector<rocksdb::ColumnFamilyHandle*> tmp_handles = handles_;
for (auto handle : handles_) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里为什么要改动

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

之前两个后台进程的改动遗留下来的,要重写析构函数,现在不需要了恢复到原本的代码。

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

return;
}

std::lock_guard<std::mutex> lock(big_keys_mutex_);
Copy link
Collaborator

Choose a reason for hiding this comment

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

这把锁是用来保护big_keys_info_map_还是bigkeys,如果是big_keys_info_map_,这里不需要用互斥锁?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

这把互斥锁是用来保护 big_keys_info_map_ 的,这里不是已经加了吗?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

// Data CF
column_families.emplace_back("data_cf", data_cf_ops);
return rocksdb::DB::Open(db_ops, db_path, column_families, &handles_, &db_);
s = rocksdb::DB::Open(db_ops, db_path, column_families, &handles_, &db_);
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里为啥要改动

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

之前以为这里也需要调用Check函数,后面就忘记删了

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

}
s = db_->Write(default_write_options_, &batch);
UpdateSpecificKeyStatistics(key.ToString(), statistic);
if (s.ok()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里你缺少了对已经存在的Hash类型的Key的插入统计的判断,这里的逻辑你写的是对一个新的Key的插入做大 Key 检测的判断,我理解这里是不需要的,你可以直接再 Write 之前就能判断出这个 Key 是否是大 Key 了,不需要一次额外的 Get 操作

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

return s;
}
s = db_->Write(default_write_options_, &batch);
UpdateSpecificKeyStatistics(key.ToString(), statistic);
Copy link
Collaborator

Choose a reason for hiding this comment

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

同理 HIncrbyfloat

}
s = db_->Write(default_write_options_, &batch);
UpdateSpecificKeyStatistics(key.ToString(), statistic);
if (s.ok()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

同理这里不需要插入之后再 Get 统计是不是大 Key, 你再调用 Write 之前就应该知道这个是不是大 Key, 你可以直接查看const Slice& key, const std::vector& fvs这两个参数

return s;
}
return db_->Write(default_write_options_, &batch);
s = db_->Write(default_write_options_, &batch);
Copy link
Collaborator

Choose a reason for hiding this comment

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

同理

@YuCai18 YuCai18 force-pushed the codereview3.5/bigkey branch from fac4718 to c97fff0 Compare July 15, 2025 01:36
@YuCai18 YuCai18 force-pushed the codereview3.5/bigkey branch from d877a6c to b42bb46 Compare July 15, 2025 04:50
rescan_ = false;
off_ = false;
keyspace_scan_dbs_.clear();
info_section_ = kInfoErr;
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里在 Clear() 情况下为什么要置为 kInfoErr 状态,什么场景下这个命令会调用 Clear()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

我用 kInfoErr 状态代表一个错误或者未初始化的状态。确保命令对象在被重用之前,其内部状态恢复到初始状态,防止数据污染。当一个命令执行完成后,Clear() 方法会被调用,清理内部状态,释放资源,为下次复用做准备。

void PikaServer::UpdateDBBigKeysConfig() {
std::shared_lock l(dbs_rw_);
for (const auto& db_item : dbs_) {
db_item.second->DBLockShared();
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里应该需要上 DBLock() 吧,写锁,因为你后面修改了 Storage 里面的值

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

}

void PikaServer::UpdateDBBigKeysConfig() {
std::shared_lock l(dbs_rw_);
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里同理也是上写锁

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

这里外层保持 std::shared_lock l(dbs_rw_) 是合适的,不需要改成写锁。dbs_rw_ 保护的是 dbs_ 这个 map 容器本身的结构,在 UpdateDBBigKeysConfig() 里,我们只是遍历 dbs_,不会对容器做插入、删除或指针重排,因此只需要读锁。

Copy link
Collaborator

@Mixficsol Mixficsol Jul 23, 2025

Choose a reason for hiding this comment

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

db_item.second->UpdateStorageBigKeysConfig你这里不是对db_item做了更新操作吗

YuCai18: done

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

thread_local uint64_t last_output_time = 0;
uint64_t current_time = pstd::NowMicros();

uint64_t interval_us = static_cast<uint64_t>(interval_minutes) * 60 * 1000000;
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里为什么不能把 g_pika_conf->bigkeys_log_interval() 设置成 uint64_t 类型,这里还需要强转

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

small_compaction_threshold_(5000),
small_compaction_duration_threshold_(10000) {
small_compaction_duration_threshold_(10000),
db_(nullptr),
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里为什么db_初始化是nullptr

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

因为在构造函数里把 db_ 初始化为 nullptr,代表此时还未打开数据库。在 rocksdb::DB::Open(ops, db_path, &db_) 中,这里才会真正 new 出 rocksdb::DB,并把指针写回 db_。之后若任何成员函数若在未调用 Open() 之前误用 db_,通过空指针检查就能及时发现问题。

// Member CF
column_families.emplace_back("member_cf", member_cf_ops);
return rocksdb::DB::Open(db_ops, db_path, column_families, &handles_, &db_);
s = rocksdb::DB::Open(db_ops, db_path, column_families, &handles_, &db_);
Copy link
Collaborator

Choose a reason for hiding this comment

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

同理

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

// Data CF
column_families.emplace_back("data_cf", data_cf_ops);
return rocksdb::DB::Open(db_ops, db_path, column_families, &handles_, &db_);
s = rocksdb::DB::Open(db_ops, db_path, column_families, &handles_, &db_);
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个改动有什么作用吗,都是 return s

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

ops.table_factory.reset(rocksdb::NewBlockBasedTableFactory(table_ops));

return rocksdb::DB::Open(ops, db_path, &db_);
Status s = rocksdb::DB::Open(ops, db_path, &db_);
Copy link
Collaborator

Choose a reason for hiding this comment

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

同理

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

column_families.emplace_back("data_cf", data_cf_ops);
column_families.emplace_back("score_cf", score_cf_ops);
return rocksdb::DB::Open(db_ops, db_path, column_families, &handles_, &db_);
s = rocksdb::DB::Open(db_ops, db_path, column_families, &handles_, &db_);
Copy link
Collaborator

Choose a reason for hiding this comment

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

同理

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

return s;
}


Copy link
Collaborator

Choose a reason for hiding this comment

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

同理

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

}

void PikaServer::UpdateDBBigKeysConfig() {
std::shared_lock l(dbs_rw_);
Copy link
Collaborator

@Mixficsol Mixficsol Jul 23, 2025

Choose a reason for hiding this comment

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

db_item.second->UpdateStorageBigKeysConfig你这里不是对db_item做了更新操作吗

YuCai18: done

return info, nil
}

info, err := c.Info()
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里为什么要单独抽出来一个c.InfoBigKeys()的逻辑,这里可以直接写在c.Info()或者c.InfoAll()或者c.InfoNoneCommandList()里面

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

去掉了

return redis.String(c.conn.Do("INFO", command))
}

func (c *client) InfoBigKeys() (string, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个不需要

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

return version, extracts, nil
}

func parseInfoBigkey(s string) string {
Copy link
Collaborator

Choose a reason for hiding this comment

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

这里解析不需要单独抽出来一个函数写,你修改一下你的正则表达式即可

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@YuCai18 YuCai18 force-pushed the codereview3.5/bigkey branch from f8cf12b to 526db36 Compare July 29, 2025 06:48
@YuCai18 YuCai18 force-pushed the codereview3.5/bigkey branch from 526db36 to d920126 Compare July 29, 2025 09:20
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.

2 participants