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

fix:Synchronize config content#117 #174

Open
wants to merge 2 commits into
base: unstable
Choose a base branch
from

Conversation

wxnzb
Copy link
Collaborator

@wxnzb wxnzb commented Jan 17, 2025

每次 set config后,先判断 option 内容是否变化,如果option 内容变了, 重新复制一个 option 传递都 net

Summary by CodeRabbit

  • New Features

    • Added ability to dynamically configure log levels
    • Introduced method to access event server configuration
    • Enhanced configuration management for network thread settings
  • Improvements

    • Added support for updating server options at runtime
    • Improved flexibility in managing system parameters
  • Technical Updates

    • Updated method signatures for event server methods
    • Added new configuration handling for Redis-compatible mode and thread management

Copy link

coderabbitai bot commented Jan 17, 2025

Walkthrough

The 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 GetEventServer() method in the KiwiDB class provides direct access to the event server, enabling more granular control over network operations. The modifications allow runtime adjustments to server parameters without requiring a full system restart.

Changes

File Changes
src/config.cc Enhanced Config::Set method to handle configuration keys:
- Added logic for log_level using g_kiwi->options_.SetLogLevel()
- Implemented redis_compatible_mode configuration
- Added complex thread count management for worker_threads_num and salve_threads_num
src/kiwi.h Added GetEventServer() method to retrieve EventServer pointer
src/net/event_server.h Added UpdateOptions() method to modify server network options
- Updated method signatures for StartServer and StartClientServer

Sequence Diagram

sequenceDiagram
    participant Config
    participant KiwiDB
    participant EventServer
    
    Config->>KiwiDB: Set configuration
    KiwiDB->>EventServer: UpdateOptions()
    EventServer-->>KiwiDB: Update network settings
    KiwiDB-->>Config: Configuration applied
Loading

Poem

🐰 In config's realm, a rabbit's delight,
Threads dance and log levels take flight!
Redis mode shifts with a gentle tweak,
Server options no longer bleak.
CodeRabbit's magic, swift and bright! 🔧

Finishing Touches

  • 📝 Generate Docstrings (Beta)

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>, 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

Copy link

@coderabbitai coderabbitai bot left a 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 by unique_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

📥 Commits

Reviewing files that changed from the base of the PR and between c9ceb4a and 6a73ae1.

📒 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

Comment on lines +75 to +79
void UpdateOptions(const NetOptions &newOptions) {
opt_ = newOptions;
threadsManager_.reserve(opt_.GetThreadNum());
}

Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add thread safety and validation to UpdateOptions.

The UpdateOptions method modifies shared state without synchronization and lacks input validation. Consider:

  1. Adding thread safety mechanisms
  2. Validating the new options before applying them
  3. 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.

Comment on lines +226 to +227
if (key == "log_level") g_kiwi->options_.SetLogLevel(value);
if (key == "redis_compatible_mode") g_kiwi->options_.SetRedisCompatibleMode(stoi(value));
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

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.

Suggested change
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");
}
}

Comment on lines +228 to +235
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_);
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add proper error handling and validation for thread number updates.

The thread number configuration has several issues:

  1. Unsafe use of stoi() without error handling
  2. Map access without iterator validation
  3. Missing validation of thread number ranges
  4. 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.

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

Successfully merging this pull request may close these issues.

1 participant