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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <vector>

#include "config.h"
#include "kiwi.h"
#include "std/std_string.h"
#include "store.h"

Expand Down Expand Up @@ -222,6 +223,16 @@ Status Config::Set(std::string key, const std::string& value, bool init_stage) {
if (iter == config_map_.end()) {
return Status::NotFound("Non-existent configuration items.");
}
if (key == "log_level") g_kiwi->options_.SetLogLevel(value);
if (key == "redis_compatible_mode") g_kiwi->options_.SetRedisCompatibleMode(stoi(value));
Comment on lines +226 to +227
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");
}
}

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_);
Copy link
Collaborator

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_);
Copy link
Collaborator

Choose a reason for hiding this comment

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

同上

}
Comment on lines +228 to +235
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.

return iter->second->Set(value, init_stage);
}

Expand Down
2 changes: 2 additions & 0 deletions src/kiwi.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class KiwiDB final {

time_t GetStartTime() { return start_time_s_; }

net::EventServer<std::shared_ptr<kiwi::PClient>>* GetEventServer() { return event_server_.get(); }

public:
uint16_t port_{0};

Expand Down
11 changes: 7 additions & 4 deletions src/net/event_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class EventServer final {

void TCPConnect(const SocketAddr &addr, const std::function<void(std::string)> &cb);

void UpdateOptions(const NetOptions &newOptions) {
opt_ = newOptions;
threadsManager_.reserve(opt_.GetThreadNum());
}

Comment on lines +75 to +79
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.

private:
int StartThreadManager(bool serverMode);

Expand Down Expand Up @@ -101,8 +106,7 @@ class EventServer final {
};

template <typename T>
requires HasSetFdFunction<T>
std::pair<bool, std::string> EventServer<T>::StartServer(int64_t interval) {
requires HasSetFdFunction<T> std::pair<bool, std::string> EventServer<T>::StartServer(int64_t interval) {
if (opt_.GetThreadNum() <= 0) {
return std::pair(false, "thread num must be greater than 0");
}
Expand Down Expand Up @@ -141,8 +145,7 @@ std::pair<bool, std::string> EventServer<T>::StartServer(int64_t interval) {
}

template <typename T>
requires HasSetFdFunction<T>
std::pair<bool, std::string> EventServer<T>::StartClientServer() {
requires HasSetFdFunction<T> std::pair<bool, std::string> EventServer<T>::StartClientServer() {
if (opt_.GetThreadNum() <= 0) {
return std::pair(false, "thread num must be greater than 0");
}
Expand Down
Loading