Skip to content

Commit

Permalink
change conf item from log-level to logging-mode and add enum type for…
Browse files Browse the repository at this point in the history
… better clarity
  • Loading branch information
cheniujh committed Dec 12, 2024
1 parent c315da4 commit ec74fe3
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 49 deletions.
11 changes: 6 additions & 5 deletions conf/pika.conf
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,12 @@ log-path : ./log/
# The unit of serverlogs is in [days] and the default value is 7(days).
log-retention-time : 7

# log level can be config as 0 or 1.
# when log-level is 0: connection activities will only be summary.
# when log-level is 1: connection activities will be show in details.
# Default log-level is 0.
log-level : 0
# logging-mode can be config as normal or debug, if an invalid value is given, normal will be auto set.
# when logging-mode is normal: only important info will be logged.
# when logging-mode is debug: more info will be logged (eg. connection activities)
# Default logging-mode is normal.
# [NOTICE] you can use config set command to change logging-mode dynamically.
logging-mode : normal

# Directory to store the data of Pika.
db-path : ./db/
Expand Down
17 changes: 10 additions & 7 deletions include/pika_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class PikaConf : public pstd::BaseConf {
std::shared_lock l(rwlock_);
return log_retention_time_;
}
int32_t log_level() {
return log_level_;
net::LogMode logging_mode() {
return logging_mode_.load(std::memory_order::memory_order_relaxed);
}
std::string db_path() {
std::shared_lock l(rwlock_);
Expand Down Expand Up @@ -825,10 +825,13 @@ class PikaConf : public pstd::BaseConf {
max_compaction_bytes_ = value;
}

void SetLogLevel(int32_t value) {
std::lock_guard l(rwlock_);
TryPushDiffCommands("log-level", std::to_string(value));
log_level_ = value;
void SetLoggingMode(std::string& value) {
TryPushDiffCommands("logging-mode", value);
if (value == "debug") {
logging_mode_.store(net::LogMode::DEBUG, std::memory_order::memory_order_relaxed);
} else {
logging_mode_.store(net::LogMode::NORMAL, std::memory_order::memory_order_relaxed);
}
}

// Rsync Rate limiting configuration
Expand Down Expand Up @@ -1091,7 +1094,7 @@ class PikaConf : public pstd::BaseConf {
std::atomic_int cache_maxmemory_policy_ = 1;
std::atomic_int cache_maxmemory_samples_ = 5;
std::atomic_int cache_lfu_decay_time_ = 1;
std::atomic_int log_level_ = 0;
std::atomic<net::LogMode> logging_mode_ = net::LogMode::NORMAL;


// rocksdb blob
Expand Down
2 changes: 1 addition & 1 deletion include/pika_dispatch_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PikaDispatchThread {

bool ClientKill(const std::string& ip_port);
void ClientKillAll();
void SetLogLevel(int32_t value);
void SetLogLevel(net::LogMode new_mode);
void SetQueueLimit(int queue_limit) { thread_rep_->SetQueueLimit(queue_limit); }

void UnAuthUserAndKillClient(const std::set<std::string> &users, const std::shared_ptr<User>& defaultUser);
Expand Down
2 changes: 1 addition & 1 deletion include/pika_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ class PikaServer : public pstd::noncopyable {
void CacheConfigInit(cache::CacheConfig &cache_cfg);
void ProcessCronTask();
double HitRatio();
void SetLogLevel(int32_t value);
void SetLogLevel(net::LogMode new_mode);
/*
* disable compact
*/
Expand Down
5 changes: 5 additions & 0 deletions src/net/include/net_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ const int kProtoMaxMessage = 512 * 1024 * 1024; // 512MB

const int kCommandHeaderLength = 4;

enum LogMode {
DEBUG = 0,
NORMAL = 1
};

/*
* The socket block type
*/
Expand Down
4 changes: 2 additions & 2 deletions src/net/include/server_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class ServerThread : public Thread {

int SetTcpNoDelay(int connfd);

void SetLogLevel(int32_t value);
void SetLogLevel(net::LogMode new_mode);

/*
* StartThread will return the error code as pthread_create
Expand Down Expand Up @@ -169,7 +169,7 @@ class ServerThread : public Thread {
*/
std::unique_ptr<NetMultiplexer> net_multiplexer_;

std::atomic<int32_t> log_level_{0};
std::atomic<net::LogMode> logging_mode_{net::LogMode::NORMAL};

private:
friend class HolyThread;
Expand Down
4 changes: 2 additions & 2 deletions src/net/src/dispatch_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void DispatchThread::HandleNewConn(const int connfd, const std::string& ip_port)
// Slow workers may consume many fds.
// We simply loop to find next legal worker.
NetItem ti(connfd, ip_port);
if (log_level_ == 1) {
if (logging_mode_.load(std::memory_order::memory_order_relaxed) == net::LogMode::DEBUG) {
LOG(INFO) << "accept new conn " << ti.String();
}
int next_thread = last_thread_;
Expand All @@ -158,7 +158,7 @@ void DispatchThread::HandleNewConn(const int connfd, const std::string& ip_port)
find = worker_thread->MoveConnIn(ti, false);
if (find) {
last_thread_ = (next_thread + 1) % work_num_;
if (log_level_ == 1) {
if (logging_mode_.load(std::memory_order::memory_order_relaxed) == net::LogMode::DEBUG) {
LOG(INFO) << "find worker(" << next_thread << "), refresh the last_thread_ to " << last_thread_;
}
break;
Expand Down
7 changes: 2 additions & 5 deletions src/net/src/server_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,8 @@ void* ServerThread::ThreadMain() {
return nullptr;
}

void ServerThread::SetLogLevel(int32_t value) {
if (value != 0 && value != 1) {
return;
}
log_level_.store(value);
void ServerThread::SetLogLevel(net::LogMode new_mode) {
logging_mode_.store(new_mode, std::memory_order::memory_order_relaxed);
}

#ifdef __ENABLE_SSL
Expand Down
26 changes: 11 additions & 15 deletions src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1590,10 +1590,11 @@ void ConfigCmd::ConfigGet(std::string& ret) {
EncodeNumber(&config_body, g_pika_conf->port());
}

if (pstd::stringmatch(pattern.data(), "log-level", 1) != 0) {
if (pstd::stringmatch(pattern.data(), "logging-mode", 1) != 0) {
elements += 2;
EncodeString(&config_body, "log-level");
EncodeNumber(&config_body, g_pika_conf->log_level());
EncodeString(&config_body, "logging-mode");
auto output_str = g_pika_conf->logging_mode() == net::LogMode::DEBUG ? "debug" : "normal";
EncodeString(&config_body, output_str);
}

if (pstd::stringmatch(pattern.data(), "thread-num", 1) != 0) {
Expand Down Expand Up @@ -2160,12 +2161,6 @@ void ConfigCmd::ConfigGet(std::string& ret) {
EncodeString(&config_body, g_pika_conf->enable_blob_garbage_collection() ? "yes" : "no");
}

if (pstd::stringmatch(pattern.data(), "loglevel", 1) != 0) {
elements += 2;
EncodeString(&config_body, "loglevel");
EncodeString(&config_body, std::to_string(g_pika_conf->log_level()));
}

if (pstd::stringmatch(pattern.data(), "min-blob-size", 1) != 0) {
elements += 2;
EncodeString(&config_body, "min-blob-size");
Expand Down Expand Up @@ -2478,13 +2473,14 @@ void ConfigCmd::ConfigSet(std::shared_ptr<DB> db) {
g_pika_conf->SetSlowlogMaxLen(static_cast<int>(ival));
g_pika_server->SlowlogTrim();
res_.AppendStringRaw("+OK\r\n");
} else if (set_item == "log-level") {
if ((pstd::string2int(value.data(), value.size(), &ival) == 0) || (ival != 0 && ival != 1)) {
res_.AppendStringRaw("-ERR Invalid argument \'" + value + "\' for CONFIG SET 'log-level', only 0 or 1 is valid\r\n");
} else if (set_item == "logging-mode") {
if (value != "debug" && value != "normal") {
res_.AppendStringRaw("-ERR Invalid argument \'" + value +
"\' for CONFIG SET 'logging-mode', only debug or normal is valid\r\n");
return;
}
g_pika_conf->SetLogLevel(static_cast<int32_t>(ival));
g_pika_server->SetLogLevel(static_cast<int32_t>(ival));
g_pika_conf->SetLoggingMode(value);
g_pika_server->SetLogLevel(value == "debug" ? net::LogMode::DEBUG : net::LogMode::NORMAL);
res_.AppendStringRaw("+OK\r\n");
} else if (set_item == "max-cache-statistic-keys") {
if ((pstd::string2int(value.data(), value.size(), &ival) == 0) || ival < 0) {
Expand Down Expand Up @@ -3326,7 +3322,7 @@ void QuitCmd::DoInitial() {

void QuitCmd::Do() {
res_.SetRes(CmdRes::kOk);
if (g_pika_conf->log_level()) {
if (g_pika_conf->logging_mode() == net::LogMode::DEBUG) {
LOG(INFO) << "QutCmd will close connection " << GetConn()->String();
}
GetConn()->SetClose(true);
Expand Down
17 changes: 10 additions & 7 deletions src/pika_conf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,16 @@ int PikaConf::Load() {
LOG(FATAL) << "log-retention-time invalid";
}

int32_t log_level = 0;
GetConfInt("log-level", &log_level);
if (log_level != 0 && log_level != 1) {
LOG(ERROR) << "log-level loaded from pika.conf is invalid, auto change it to 0";
log_level = 0;
}
log_level_.store(log_level);
std::string logging_mode;
GetConfStr("logging-mode", &logging_mode);
if (logging_mode == "debug") {
logging_mode_.store(net::LogMode::DEBUG, std::memory_order::memory_order_relaxed);
} else if (logging_mode == "normal") {
logging_mode_.store(net::LogMode::NORMAL, std::memory_order::memory_order_relaxed);
} else {
LOG(ERROR) << "logging-mode loaded from pika.conf is invalid, auto change it to normal";
logging_mode_.store(net::LogMode::NORMAL, std::memory_order::memory_order_relaxed);
};

GetConfStr("db-path", &db_path_);
GetConfInt("db-instance-num", &db_instance_num_);
Expand Down
4 changes: 2 additions & 2 deletions src/pika_dispatch_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ void PikaDispatchThread::UnAuthUserAndKillClient(const std::set<std::string>& us
void PikaDispatchThread::StopThread() {
thread_rep_->StopThread();
}
void PikaDispatchThread::SetLogLevel(int32_t value) {
thread_rep_->SetLogLevel(value);
void PikaDispatchThread::SetLogLevel(net::LogMode new_mode) {
thread_rep_->SetLogLevel(new_mode);
}

bool PikaDispatchThread::Handles::AccessHandle(std::string& ip) const {
Expand Down
4 changes: 2 additions & 2 deletions src/pika_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void PikaServer::Start() {
<< (ret == net::kBindError ? ": bind port " + std::to_string(port_) + " conflict" : ": other error")
<< ", Listen on this port to handle the connected redis client";
}
pika_dispatch_thread_->SetLogLevel(g_pika_conf->log_level());
pika_dispatch_thread_->SetLogLevel(g_pika_conf->logging_mode());
ret = pika_pubsub_thread_->StartThread();
if (ret != net::kSuccess) {
dbs_.clear();
Expand Down Expand Up @@ -1920,4 +1920,4 @@ void PikaServer::CacheConfigInit(cache::CacheConfig& cache_cfg) {
cache_cfg.maxmemory_samples = g_pika_conf->cache_maxmemory_samples();
cache_cfg.lfu_decay_time = g_pika_conf->cache_lfu_decay_time();
}
void PikaServer::SetLogLevel(int32_t value) { pika_dispatch_thread_->SetLogLevel(value); }
void PikaServer::SetLogLevel(net::LogMode new_mode) { pika_dispatch_thread_->SetLogLevel(new_mode); }

0 comments on commit ec74fe3

Please sign in to comment.