Skip to content

Commit 8ce0cb3

Browse files
cheniujhbuzhimingyonghu
andauthoredDec 16, 2024
feat: add log-net-activities configuration (OpenAtomFoundation#2964)
* add log-level to config if pika should log the activities of Net connctions --------- Co-authored-by: buzhimingyonghu <[email protected]>
1 parent 0906644 commit 8ce0cb3

11 files changed

+71
-23
lines changed
 

‎conf/pika.conf

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
###########################
44

55
# Pika port, the default value is 9221.
6-
# [NOTICE] Port Magic offsets of port+1000 / port+2000 are used by Pika at present.
7-
# Port 10221 is used for Rsync, and port 11221 is used for Replication, while the listening port is 9221.
6+
# [NOTICE] Port Magic offsets of port+1000 / port+10000 are used by Pika at present.
7+
# Port 9221+10000 is used for Rsync, and port 9221+1000 is used for incr Replication, while the listening port is 9221.
88
port : 9221
99

1010
db-instance-num : 3
@@ -74,6 +74,12 @@ log-path : ./log/
7474
# The unit of serverlogs is in [days] and the default value is 7(days).
7575
log-retention-time : 7
7676

77+
# log-net-activities can be config as yes or no, if an invalid value is given, normal will be auto set to no.
78+
# when log-net-activities is yes, connection activities will be logged.
79+
# Default log-net-activities value is no.
80+
# [NOTICE] you can use config set command to change log-net-activities dynamically.
81+
log-net-activities : no
82+
7783
# Directory to store the data of Pika.
7884
db-path : ./db/
7985

‎include/pika_conf.h

+11-8
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@ class PikaConf : public pstd::BaseConf {
8686
std::shared_lock l(rwlock_);
8787
return log_retention_time_;
8888
}
89-
std::string log_level() {
90-
std::shared_lock l(rwlock_);
91-
return log_level_;
89+
bool log_net_activities() {
90+
return log_net_activities_.load(std::memory_order::memory_order_relaxed);
9291
}
9392
std::string db_path() {
9493
std::shared_lock l(rwlock_);
@@ -831,10 +830,13 @@ class PikaConf : public pstd::BaseConf {
831830
max_compaction_bytes_ = value;
832831
}
833832

834-
void SetLogLevel(const std::string& value) {
835-
std::lock_guard l(rwlock_);
836-
TryPushDiffCommands("loglevel", value);
837-
log_level_ = value;
833+
void SetLogNetActivities(std::string& value) {
834+
TryPushDiffCommands("log-net-activities", value);
835+
if (value == "yes") {
836+
log_net_activities_.store(true);
837+
} else {
838+
log_net_activities_.store(false);
839+
}
838840
}
839841

840842
// Rsync Rate limiting configuration
@@ -958,7 +960,6 @@ class PikaConf : public pstd::BaseConf {
958960
std::string slaveof_;
959961
std::string log_path_;
960962
int log_retention_time_;
961-
std::string log_level_;
962963
std::string db_path_;
963964
int db_instance_num_ = 0;
964965
std::string db_sync_path_;
@@ -1099,6 +1100,8 @@ class PikaConf : public pstd::BaseConf {
10991100
std::atomic_int cache_maxmemory_policy_ = 1;
11001101
std::atomic_int cache_maxmemory_samples_ = 5;
11011102
std::atomic_int cache_lfu_decay_time_ = 1;
1103+
std::atomic<bool> log_net_activities_ = false;
1104+
11021105

11031106
// rocksdb blob
11041107
bool enable_blob_files_ = false;

‎include/pika_dispatch_thread.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class PikaDispatchThread {
1919

2020
bool ClientKill(const std::string& ip_port);
2121
void ClientKillAll();
22-
22+
void SetLogNetActivities(bool value);
2323
void SetQueueLimit(int queue_limit) { thread_rep_->SetQueueLimit(queue_limit); }
2424

2525
void UnAuthUserAndKillClient(const std::set<std::string> &users, const std::shared_ptr<User>& defaultUser);

‎include/pika_server.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ class PikaServer : public pstd::noncopyable {
492492
void CacheConfigInit(cache::CacheConfig &cache_cfg);
493493
void ProcessCronTask();
494494
double HitRatio();
495-
495+
void SetLogNetActivities(bool value);
496496
/*
497497
* disable compact
498498
*/

‎src/net/include/server_thread.h

+4
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ class ServerThread : public Thread {
128128

129129
int SetTcpNoDelay(int connfd);
130130

131+
void SetLogNetActivities(bool value);
132+
131133
/*
132134
* StartThread will return the error code as pthread_create
133135
* Return 0 if success
@@ -167,6 +169,8 @@ class ServerThread : public Thread {
167169
*/
168170
std::unique_ptr<NetMultiplexer> net_multiplexer_;
169171

172+
std::atomic<bool> log_net_activities_{false};
173+
170174
private:
171175
friend class HolyThread;
172176
friend class DispatchThread;

‎src/net/src/dispatch_thread.cc

+7-3
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,19 @@ void DispatchThread::HandleNewConn(const int connfd, const std::string& ip_port)
148148
// Slow workers may consume many fds.
149149
// We simply loop to find next legal worker.
150150
NetItem ti(connfd, ip_port);
151-
LOG(INFO) << "accept new conn " << ti.String();
151+
if (log_net_activities_.load(std::memory_order::memory_order_relaxed)) {
152+
LOG(INFO) << "accept new conn " << ti.String();
153+
}
152154
int next_thread = last_thread_;
153155
bool find = false;
154156
for (int cnt = 0; cnt < work_num_; cnt++) {
155157
std::unique_ptr<WorkerThread>& worker_thread = worker_thread_[next_thread];
156158
find = worker_thread->MoveConnIn(ti, false);
157159
if (find) {
158160
last_thread_ = (next_thread + 1) % work_num_;
159-
LOG(INFO) << "find worker(" << next_thread << "), refresh the last_thread_ to " << last_thread_;
161+
if (log_net_activities_.load(std::memory_order::memory_order_relaxed)) {
162+
LOG(INFO) << "find worker(" << next_thread << "), refresh the last_thread_ to " << last_thread_;
163+
}
160164
break;
161165
}
162166
next_thread = (next_thread + 1) % work_num_;
@@ -189,7 +193,7 @@ void DispatchThread::CleanWaitNodeOfUnBlockedBlrConn(std::shared_ptr<net::RedisC
189193
// removed all the waiting info of this conn/ doing cleaning work
190194
auto pair = blocked_conn_to_keys_.find(conn_unblocked->fd());
191195
if (pair == blocked_conn_to_keys_.end()) {
192-
LOG(WARNING) << "blocking info of blpop/brpop went wrong, blpop/brpop can't working correctly";
196+
LOG(ERROR) << "blocking info of blpop/brpop went wrong, blpop/brpop can't working correctly";
193197
return;
194198
}
195199
auto& blpop_keys_list = pair->second;

‎src/net/src/server_thread.cc

+4
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ void* ServerThread::ThreadMain() {
264264
return nullptr;
265265
}
266266

267+
void ServerThread::SetLogNetActivities(bool value) {
268+
log_net_activities_.store(value, std::memory_order::memory_order_relaxed);
269+
}
270+
267271
#ifdef __ENABLE_SSL
268272
static std::vector<std::unique_ptr<pstd::Mutex>> ssl_mutex_;
269273

‎src/pika_admin.cc

+19-7
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,13 @@ void ConfigCmd::ConfigGet(std::string& ret) {
15971597
EncodeNumber(&config_body, g_pika_conf->log_retention_time());
15981598
}
15991599

1600+
if (pstd::stringmatch(pattern.data(), "log-net-activities", 1) != 0) {
1601+
elements += 2;
1602+
EncodeString(&config_body, "log-net-activities");
1603+
auto output_str = g_pika_conf->log_net_activities() ? "yes" : "no";
1604+
EncodeString(&config_body, output_str);
1605+
}
1606+
16001607
if (pstd::stringmatch(pattern.data(), "thread-num", 1) != 0) {
16011608
elements += 2;
16021609
EncodeString(&config_body, "thread-num");
@@ -2167,12 +2174,6 @@ void ConfigCmd::ConfigGet(std::string& ret) {
21672174
EncodeString(&config_body, g_pika_conf->enable_blob_garbage_collection() ? "yes" : "no");
21682175
}
21692176

2170-
if (pstd::stringmatch(pattern.data(), "loglevel", 1) != 0) {
2171-
elements += 2;
2172-
EncodeString(&config_body, "loglevel");
2173-
EncodeString(&config_body, g_pika_conf->log_level());
2174-
}
2175-
21762177
if (pstd::stringmatch(pattern.data(), "min-blob-size", 1) != 0) {
21772178
elements += 2;
21782179
EncodeString(&config_body, "min-blob-size");
@@ -2492,6 +2493,15 @@ void ConfigCmd::ConfigSet(std::shared_ptr<DB> db) {
24922493
g_pika_conf->SetSlowlogMaxLen(static_cast<int>(ival));
24932494
g_pika_server->SlowlogTrim();
24942495
res_.AppendStringRaw("+OK\r\n");
2496+
} else if (set_item == "log-net-activities") {
2497+
if (value != "yes" && value != "no") {
2498+
res_.AppendStringRaw("-ERR Invalid argument \'" + value +
2499+
"\' for CONFIG SET 'log-net-activities', only yes or no is valid\r\n");
2500+
return;
2501+
}
2502+
g_pika_conf->SetLogNetActivities(value);
2503+
g_pika_server->SetLogNetActivities(value == "yes");
2504+
res_.AppendStringRaw("+OK\r\n");
24952505
} else if (set_item == "max-cache-statistic-keys") {
24962506
if ((pstd::string2int(value.data(), value.size(), &ival) == 0) || ival < 0) {
24972507
res_.AppendStringRaw("-ERR Invalid argument \'" + value + "\' for CONFIG SET 'max-cache-statistic-keys'\r\n");
@@ -3345,7 +3355,9 @@ void QuitCmd::DoInitial() {
33453355

33463356
void QuitCmd::Do() {
33473357
res_.SetRes(CmdRes::kOk);
3348-
LOG(INFO) << "QutCmd will close connection " << GetConn()->String();
3358+
if (g_pika_conf->log_net_activities()) {
3359+
LOG(INFO) << "QuitCmd will close connection " << GetConn()->String();
3360+
}
33493361
GetConn()->SetClose(true);
33503362
}
33513363

‎src/pika_conf.cc

+13-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,15 @@ int PikaConf::Load() {
132132
if(log_retention_time_ < 0){
133133
LOG(FATAL) << "log-retention-time invalid";
134134
}
135-
GetConfStr("loglevel", &log_level_);
135+
136+
std::string log_net_activities;
137+
GetConfStr("log-net-activities", &log_net_activities);
138+
if (log_net_activities == "yes") {
139+
log_net_activities_.store(true);
140+
} else {
141+
log_net_activities_.store(false);
142+
};
143+
136144
GetConfStr("db-path", &db_path_);
137145
GetConfInt("db-instance-num", &db_instance_num_);
138146
if (db_instance_num_ <= 0) {
@@ -830,6 +838,9 @@ int PikaConf::ConfigRewrite() {
830838
SetConfStr("slowlog-write-errorlog", slowlog_write_errorlog_.load() ? "yes" : "no");
831839
SetConfInt("slowlog-log-slower-than", slowlog_log_slower_than_.load());
832840
SetConfInt("slowlog-max-len", slowlog_max_len_);
841+
SetConfInt("log-retention-time", log_retention_time_);
842+
SetConfInt("slave-priority", slave_priority_);
843+
SetConfStr("log-net-activities", log_net_activities_ ? "yes" : "no");
833844
SetConfStr("write-binlog", write_binlog_ ? "yes" : "no");
834845
SetConfStr("run-id", run_id_);
835846
SetConfStr("replication-id", replication_id_);
@@ -889,6 +900,7 @@ int PikaConf::ConfigRewrite() {
889900
SetConfInt("max-cache-files", max_cache_files_);
890901
SetConfInt("max-background-compactions", max_background_compactions_);
891902
SetConfInt("max-background-jobs", max_background_jobs_);
903+
SetConfInt("max-subcompactions", max_subcompactions_);
892904
SetConfInt64("rate-limiter-bandwidth", rate_limiter_bandwidth_);
893905
SetConfInt64("delayed-write-rate", delayed_write_rate_);
894906
SetConfInt64("max-compaction-bytes", max_compaction_bytes_);

‎src/pika_dispatch_thread.cc

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ void PikaDispatchThread::UnAuthUserAndKillClient(const std::set<std::string>& us
6262
void PikaDispatchThread::StopThread() {
6363
thread_rep_->StopThread();
6464
}
65+
void PikaDispatchThread::SetLogNetActivities(bool value) { thread_rep_->SetLogNetActivities(value); }
6566

6667
bool PikaDispatchThread::Handles::AccessHandle(std::string& ip) const {
6768
if (ip == "127.0.0.1") {

‎src/pika_server.cc

+2
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ void PikaServer::Start() {
197197
<< (ret == net::kBindError ? ": bind port " + std::to_string(port_) + " conflict" : ": other error")
198198
<< ", Listen on this port to handle the connected redis client";
199199
}
200+
pika_dispatch_thread_->SetLogNetActivities(g_pika_conf->log_net_activities());
200201
ret = pika_pubsub_thread_->StartThread();
201202
if (ret != net::kSuccess) {
202203
dbs_.clear();
@@ -1919,3 +1920,4 @@ void PikaServer::CacheConfigInit(cache::CacheConfig& cache_cfg) {
19191920
cache_cfg.maxmemory_samples = g_pika_conf->cache_maxmemory_samples();
19201921
cache_cfg.lfu_decay_time = g_pika_conf->cache_lfu_decay_time();
19211922
}
1923+
void PikaServer::SetLogNetActivities(bool value) { pika_dispatch_thread_->SetLogNetActivities(value); }

0 commit comments

Comments
 (0)
Please sign in to comment.