-
Notifications
You must be signed in to change notification settings - Fork 0
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:Synchronize config content#117 #174
base: unstable
Are you sure you want to change the base?
Conversation
WalkthroughThe pull request introduces configuration and server management enhancements in the KiwiDB system. Changes focus on improving configuration flexibility by adding methods to dynamically update logging levels, Redis compatibility mode, and thread counts. A new Changes
Sequence DiagramsequenceDiagram
participant Config
participant KiwiDB
participant EventServer
Config->>KiwiDB: Set configuration
KiwiDB->>EventServer: UpdateOptions()
EventServer-->>KiwiDB: Update network settings
KiwiDB-->>Config: Configuration applied
Poem
Finishing Touches
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Nitpick comments (1)
src/kiwi.h (1)
79-80
: Consider returning a reference instead of a raw pointer.The
GetEventServer()
method returns a raw pointer to a resource managed byunique_ptr
. To prevent potential lifetime management issues, consider returning a reference instead:- net::EventServer<std::shared_ptr<kiwi::PClient>>* GetEventServer() { return event_server_.get(); } + net::EventServer<std::shared_ptr<kiwi::PClient>>& GetEventServer() { return *event_server_; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/config.cc
(2 hunks)src/kiwi.h
(1 hunks)src/net/event_server.h
(3 hunks)
🔇 Additional comments (1)
src/net/event_server.h (1)
109-109
: LGTM: Improved requires clause placement.The relocation of the requires clause improves readability while maintaining the same functionality.
Also applies to: 148-148
void UpdateOptions(const NetOptions &newOptions) { | ||
opt_ = newOptions; | ||
threadsManager_.reserve(opt_.GetThreadNum()); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add thread safety and validation to UpdateOptions.
The UpdateOptions
method modifies shared state without synchronization and lacks input validation. Consider:
- Adding thread safety mechanisms
- Validating the new options before applying them
- Ensuring thread capacity reservation is safe during runtime
- void UpdateOptions(const NetOptions &newOptions) {
- opt_ = newOptions;
- threadsManager_.reserve(opt_.GetThreadNum());
- }
+ void UpdateOptions(const NetOptions &newOptions) {
+ if (newOptions.GetThreadNum() <= 0) {
+ throw std::invalid_argument("Thread number must be greater than 0");
+ }
+ std::lock_guard<std::mutex> lock(mtx_);
+ opt_ = newOptions;
+ if (threadsManager_.capacity() < opt_.GetThreadNum()) {
+ threadsManager_.reserve(opt_.GetThreadNum());
+ }
+ }
Committable suggestion skipped: line range outside the PR's diff.
if (key == "log_level") g_kiwi->options_.SetLogLevel(value); | ||
if (key == "redis_compatible_mode") g_kiwi->options_.SetRedisCompatibleMode(stoi(value)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add error handling for log level and redis mode settings.
The configuration updates lack error handling. Consider adding try-catch blocks for potential exceptions:
- if (key == "log_level") g_kiwi->options_.SetLogLevel(value);
- if (key == "redis_compatible_mode") g_kiwi->options_.SetRedisCompatibleMode(stoi(value));
+ if (key == "log_level") {
+ g_kiwi->options_.SetLogLevel(value);
+ }
+ if (key == "redis_compatible_mode") {
+ try {
+ g_kiwi->options_.SetRedisCompatibleMode(stoi(value));
+ } catch (const std::exception& e) {
+ return Status::InvalidArgument("Invalid redis compatible mode value");
+ }
+ }
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
if (key == "log_level") g_kiwi->options_.SetLogLevel(value); | |
if (key == "redis_compatible_mode") g_kiwi->options_.SetRedisCompatibleMode(stoi(value)); | |
if (key == "log_level") { | |
g_kiwi->options_.SetLogLevel(value); | |
} | |
if (key == "redis_compatible_mode") { | |
try { | |
g_kiwi->options_.SetRedisCompatibleMode(stoi(value)); | |
} catch (const std::exception& e) { | |
return Status::InvalidArgument("Invalid redis compatible mode value"); | |
} | |
} |
if (key == "worker_threads_num") { | ||
g_kiwi->options_.SetThreadNum(stoi(value) + stoi((config_map_.find("salve_threads_num"))->second->Value())); | ||
g_kiwi->GetEventServer()->UpdateOptions(g_kiwi->options_); | ||
} | ||
if (key == "salve_threads_num") { | ||
g_kiwi->options_.SetThreadNum(stoi(value) + stoi((config_map_.find("worker_threads_num"))->second->Value())); | ||
g_kiwi->GetEventServer()->UpdateOptions(g_kiwi->options_); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add proper error handling and validation for thread number updates.
The thread number configuration has several issues:
- Unsafe use of
stoi()
without error handling - Map access without iterator validation
- Missing validation of thread number ranges
- Potential integer overflow when adding thread numbers
if (key == "worker_threads_num") {
- g_kiwi->options_.SetThreadNum(stoi(value) + stoi((config_map_.find("salve_threads_num"))->second->Value()));
- g_kiwi->GetEventServer()->UpdateOptions(g_kiwi->options_);
+ try {
+ auto slave_it = config_map_.find("salve_threads_num");
+ if (slave_it == config_map_.end()) {
+ return Status::InvalidArgument("Slave threads configuration not found");
+ }
+ int worker_threads = stoi(value);
+ int slave_threads = stoi(slave_it->second->Value());
+ if (worker_threads <= 0 || slave_threads <= 0) {
+ return Status::InvalidArgument("Thread numbers must be positive");
+ }
+ int64_t total_threads = static_cast<int64_t>(worker_threads) + static_cast<int64_t>(slave_threads);
+ if (total_threads > THREAD_MAX) {
+ return Status::InvalidArgument("Total thread count exceeds maximum limit");
+ }
+ g_kiwi->options_.SetThreadNum(static_cast<int>(total_threads));
+ g_kiwi->GetEventServer()->UpdateOptions(g_kiwi->options_);
+ } catch (const std::exception& e) {
+ return Status::InvalidArgument("Invalid thread number value");
+ }
}
Also, there appears to be a typo in the configuration key "salve_threads_num". Should it be "slave_threads_num"?
Committable suggestion skipped: line range outside the PR's diff.
format ci没过,看一下 |
ai的建议 看一下 |
AI’s suggestions take a look |
为什么用if判断这么多的key, 能说一下原因吗? |
Why use if to judge so many keys? Can you tell me the reason? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有时间看一下
if (key == "redis_compatible_mode") g_kiwi->options_.SetRedisCompatibleMode(stoi(value)); | ||
if (key == "worker_threads_num") { | ||
g_kiwi->options_.SetThreadNum(stoi(value) + stoi((config_map_.find("salve_threads_num"))->second->Value())); | ||
g_kiwi->GetEventServer()->UpdateOptions(g_kiwi->options_); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不能在g_kiwi的层面UpdateOptions吗,
like:
g_kiwi->UpdateOptions(g_kiwi->options_);
} | ||
if (key == "salve_threads_num") { | ||
g_kiwi->options_.SetThreadNum(stoi(value) + stoi((config_map_.find("worker_threads_num"))->second->Value())); | ||
g_kiwi->GetEventServer()->UpdateOptions(g_kiwi->options_); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好主意,可以的
@@ -72,6 +72,11 @@ class EventServer final { | |||
|
|||
void TCPConnect(const SocketAddr &addr, const std::function<void(std::string)> &cb); | |||
|
|||
void UpdateOptions(const NetOptions &newOptions) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
threadNum
现在应该没有能力动态调整吧, 这个要动态调整, 改的东西有点多
每次 set config后,先判断 option 内容是否变化,如果option 内容变了, 重新复制一个 option 传递都 net
Summary by CodeRabbit
New Features
Improvements
Technical Updates