From 089047117e2e20797fd1b1980c463ed8334d1b8b Mon Sep 17 00:00:00 2001 From: shenmengju Date: Wed, 1 May 2024 20:58:40 +0800 Subject: [PATCH 01/15] add infoCmd:server stats cpu commandstats --- src/client.cc | 9 +- src/client.h | 33 +++++ src/cmd_admin.cc | 242 ++++++++++++++++++++++++++++++---- src/cmd_admin.h | 40 +++++- src/cmd_table_manager.cc | 1 + src/cmd_thread_pool_worker.cc | 16 +++ src/pikiwidb.cc | 3 + src/pikiwidb.h | 4 + 8 files changed, 318 insertions(+), 30 deletions(-) diff --git a/src/client.cc b/src/client.cc index 890ec6164..a69d41053 100644 --- a/src/client.cc +++ b/src/client.cc @@ -17,7 +17,11 @@ #include "base_cmd.h" #include "config.h" +#include "env.h" #include "pikiwidb.h" +#include "pstd_string.h" +#include "slow_log.h" +#include "store.h" namespace pikiwidb { @@ -353,7 +357,7 @@ int PClient::handlePacket(const char* start, int bytes) { // executeCommand(); // return static_cast(ptr - start); // } - + time_stat_->enqueue_ts_ = pstd::NowMicros(); g_pikiwidb->SubmitFast(std::make_shared(shared_from_this())); // check transaction @@ -424,6 +428,7 @@ PClient* PClient::Current() { return s_current; } PClient::PClient() : parser_(params_) { auth_ = false; reset(); + time_stat_.reset(new TimeStat()); } void PClient::OnConnect() { @@ -665,4 +670,6 @@ void PClient::SetKey(std::vector& names) { keys_ = std::move(names); // use std::move clear copy expense } +std::unordered_map* PClient::GetCommandStatMap() { return &cmdstat_map_; } + } // namespace pikiwidb diff --git a/src/client.h b/src/client.h index 6cbcb7c8d..48c91bfe2 100644 --- a/src/client.h +++ b/src/client.h @@ -21,6 +21,30 @@ namespace pikiwidb { +struct CommandStatistics { + CommandStatistics() = default; + CommandStatistics(const CommandStatistics& other) { + cmd_time_consuming.store(other.cmd_time_consuming.load()); + cmd_count.store(other.cmd_count.load()); + } + std::atomic cmd_count = 0; + std::atomic cmd_time_consuming = 0; +}; + +struct TimeStat { + TimeStat() = default; + void Reset() { + enqueue_ts_ = dequeue_ts_ = 0; + process_done_ts_ = 0; + } + + uint64_t total_time() const { return process_done_ts_ > enqueue_ts_ ? process_done_ts_ - enqueue_ts_ : 0; } + + uint64_t enqueue_ts_; + uint64_t dequeue_ts_; + uint64_t process_done_ts_; +}; + class CmdRes { public: enum CmdRet { @@ -236,6 +260,10 @@ class PClient : public std::enable_shared_from_this, public CmdRes { // e.g:["set","key","value"] std::span argv_; + // Info Commandstats used + std::shared_ptr time_stat_; + std::unordered_map* GetCommandStatMap(); + // std::shared_ptr getTcpConnection() const { return tcp_connection_.lock(); } int handlePacket(const char*, int); @@ -291,5 +319,10 @@ class PClient : public std::enable_shared_from_this, public CmdRes { net::SocketAddr addr_; static thread_local PClient* s_current; + + /* + * Info Commandstats used + */ + std::unordered_map cmdstat_map_; }; } // namespace pikiwidb diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 0964bd821..9c5f02af4 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -5,6 +5,11 @@ * of patent rights can be found in the PATENTS file in the same directory. */ +#include +#include +#include +#include + #include "cmd_admin.h" #include #include @@ -21,6 +26,8 @@ #include "praft/praft.h" #include "pstd/env.h" +#include "cmd_table_manager.h" +#include "slow_log.h" #include "store.h" namespace pikiwidb { @@ -144,24 +151,122 @@ bool PingCmd::DoInitial(PClient* client) { return true; } void PingCmd::DoCmd(PClient* client) { client->SetRes(CmdRes::kPong, "PONG"); } +const std::string InfoCmd::kInfoSection = "info"; +const std::string InfoCmd::kAllSection = "all"; +const std::string InfoCmd::kServerSection = "server"; +const std::string InfoCmd::kClientsSection = "clients"; +const std::string InfoCmd::kStatsSection = "stats"; +const std::string InfoCmd::kCPUSection = "cpu"; +const std::string InfoCmd::kReplicationSection = "replication"; +const std::string InfoCmd::kKeyspaceSection = "keyspace"; +const std::string InfoCmd::kDataSection = "data"; +const std::string InfoCmd::kRocksDBSection = "rocksdb"; +const std::string InfoCmd::kDebugSection = "debug"; +const std::string InfoCmd::kCommandStatsSection = "commandstats"; +const std::string InfoCmd::kCacheSection = "cache"; +const std::string InfoCmd::kRaftSection = "RAFT"; + +const std::string InfoCmd::kInfoSection = "info"; +const std::string InfoCmd::kAllSection = "all"; +const std::string InfoCmd::kServerSection = "server"; +const std::string InfoCmd::kClientsSection = "clients"; +const std::string InfoCmd::kStatsSection = "stats"; +const std::string InfoCmd::kCPUSection = "cpu"; +const std::string InfoCmd::kReplicationSection = "replication"; +const std::string InfoCmd::kKeyspaceSection = "keyspace"; +const std::string InfoCmd::kDataSection = "data"; +const std::string InfoCmd::kRocksDBSection = "rocksdb"; +const std::string InfoCmd::kDebugSection = "debug"; +const std::string InfoCmd::kCommandStatsSection = "commandstats"; +const std::string InfoCmd::kCacheSection = "cache"; +const std::string InfoCmd::kRaftSection = "RAFT"; + InfoCmd::InfoCmd(const std::string& name, int16_t arity) : BaseCmd(name, arity, kCmdFlagsAdmin, kAclCategoryAdmin) {} -bool InfoCmd::DoInitial(PClient* client) { return true; } +bool InfoCmd::DoInitial(PClient* client) { + size_t argc = client->argv_.size(); + if (argc > 4) { + client->SetRes(CmdRes::kSyntaxErr); + return false; + } + if (argc == 1) { + info_section_ = kInfo; + return true; + } + + const auto& argv_ = client->argv_; + if (strcasecmp(argv_[1].data(), kAllSection.data()) == 0) { + info_section_ = kInfoAll; + } else if (strcasecmp(argv_[1].data(), kServerSection.data()) == 0) { + info_section_ = kInfoServer; + } else if (strcasecmp(argv_[1].data(), kStatsSection.data()) == 0) { + info_section_ = kInfoStats; + } else if (strcasecmp(argv_[1].data(), kCPUSection.data()) == 0) { + info_section_ = kInfoCPU; + } else if (strcasecmp(argv_[1].data(), kDataSection.data()) == 0) { + info_section_ = kInfoData; + } else if (strcasecmp(argv_[1].data(), kRaftSection.data()) == 0) { + info_section_ = kInfoRaft; + } else if (strcasecmp(argv_[1].data(), kCommandStatsSection.data()) == 0) { + info_section_ = kInfoCommandStats; + } + + if (argc != 2) { + client->SetRes(CmdRes::kSyntaxErr); + return false; + } + return true; +} // @todo The info raft command is only supported for the time being void InfoCmd::DoCmd(PClient* client) { - if (client->argv_.size() <= 1) { - return client->SetRes(CmdRes::kWrongNum, client->CmdName()); + std::string info; + switch (info_section_) { + case kInfo: + InfoServer(info); + info.append("\r\n"); + InfoData(info); + info.append("\r\n"); + InfoStats(info); + info.append("\r\n"); + InfoCPU(info); + info.append("\r\n"); + break; + case kInfoAll: + InfoServer(info); + info.append("\r\n"); + InfoData(info); + info.append("\r\n"); + InfoStats(info); + info.append("\r\n"); + InfoCommandStats(client, info); + info.append("\r\n"); + InfoCPU(info); + info.append("\r\n"); + break; + case kInfoServer: + InfoServer(info); + break; + case kInfoStats: + InfoStats(info); + break; + case kInfoCPU: + InfoCPU(info); + break; + case kInfoData: + InfoData(info); + break; + case kInfoCommandStats: + InfoCommandStats(client, info); + break; + case kInfoRaft: + InfoRaft(info); + break; + default: + break; } - auto cmd = client->argv_[1]; - if (!strcasecmp(cmd.c_str(), "RAFT")) { - InfoRaft(client); - } else if (!strcasecmp(cmd.c_str(), "data")) { - InfoData(client); - } else { - client->SetRes(CmdRes::kErrOther, "the cmd is not supported"); - } + client->AppendString(info); } /* @@ -178,21 +283,18 @@ void InfoCmd::DoCmd(PClient* client) { raft_num_voting_nodes:2 raft_node1:id=1733428433,state=connected,voting=yes,addr=localhost,port=5001,last_conn_secs=5,conn_errors=0,conn_oks=1 */ -void InfoCmd::InfoRaft(PClient* client) { - if (client->argv_.size() != 2) { - return client->SetRes(CmdRes::kWrongNum, client->CmdName()); - } - +void InfoCmd::InfoRaft(std::string& message) { if (!PRAFT.IsInitialized()) { - return client->SetRes(CmdRes::kErrOther, "Don't already cluster member"); + message += "-ERR:Don't already cluster member \r\n"; + return; } auto node_status = PRAFT.GetNodeStatus(); if (node_status.state == braft::State::STATE_END) { - return client->SetRes(CmdRes::kErrOther, "Node is not initialized"); + message += "-ERR:Node is not initialized \r\n"; + return; } - std::string message; message += "raft_group_id:" + PRAFT.GetGroupID() + "\r\n"; message += "raft_node_id:" + PRAFT.GetNodeID() + "\r\n"; message += "raft_peer_id:" + PRAFT.GetPeerID() + "\r\n"; @@ -209,7 +311,8 @@ void InfoCmd::InfoRaft(PClient* client) { std::vector peers; auto status = PRAFT.GetListPeers(&peers); if (!status.ok()) { - return client->SetRes(CmdRes::kErrOther, status.error_str()); + message += "-ERR:" + status.error_str(); + return; } for (int i = 0; i < peers.size(); i++) { @@ -217,21 +320,106 @@ void InfoCmd::InfoRaft(PClient* client) { ",port=" + std::to_string(peers[i].addr.port) + "\r\n"; } } - - client->AppendString(message); } -void InfoCmd::InfoData(PClient* client) { - if (client->argv_.size() != 2) { - return client->SetRes(CmdRes::kWrongNum, client->CmdName()); +void InfoCmd::InfoServer(std::string& info) { + static struct utsname host_info; + static bool host_info_valid = false; + if (!host_info_valid) { + uname(&host_info); + host_info_valid = true; } - std::string message; + time_t current_time_s = time(nullptr); + std::stringstream tmp_stream; + char version[32]; + snprintf(version, sizeof(version), "%s", KPIKIWIDB_VERSION); + + tmp_stream << "# Server\r\n"; + tmp_stream << "PikiwiDB_version:" << version << "\r\n"; + tmp_stream << "PikiwiDB_build_git_sha:" << KPIKIWIDB_GIT_COMMIT_ID << "\r\n"; + tmp_stream << "Pikiwidb_build_compile_date: " << KPIKIWIDB_BUILD_DATE << "\r\n"; + tmp_stream << "os:" << host_info.sysname << " " << host_info.release << " " << host_info.machine << "\r\n"; + tmp_stream << "arch_bits:" << (reinterpret_cast(&host_info.machine) + strlen(host_info.machine) - 2) << "\r\n"; + tmp_stream << "process_id:" << getpid() << "\r\n"; + tmp_stream << "run_id:" << static_cast(g_config.run_id) << "\r\n"; + tmp_stream << "tcp_port:" << g_config.port << "\r\n"; + tmp_stream << "uptime_in_seconds:" << (current_time_s - g_pikiwidb->Start_time_s()) << "\r\n"; + tmp_stream << "uptime_in_days:" << (current_time_s / (24 * 3600) - g_pikiwidb->Start_time_s() / (24 * 3600) + 1) + << "\r\n"; + tmp_stream << "config_file:" << g_pikiwidb->GetConfigName() << "\r\n"; + + info.append(tmp_stream.str()); +} + +void InfoCmd::InfoStats(std::string& info) { + std::stringstream tmp_stream; + tmp_stream << "# Stats" + << "\r\n"; + + tmp_stream << "is_bgsaving:" << (PREPL.IsBgsaving() ? "Yes" : "No") << "\r\n"; + tmp_stream << "slow_logs_count:" << PSlowLog::Instance().GetLogsCount() << "\r\n"; + info.append(tmp_stream.str()); +} + +void InfoCmd::InfoCPU(std::string& info) { + struct rusage self_ru; + struct rusage c_ru; + getrusage(RUSAGE_SELF, &self_ru); + getrusage(RUSAGE_CHILDREN, &c_ru); + std::stringstream tmp_stream; + tmp_stream << "# CPU" + << "\r\n"; + tmp_stream << "used_cpu_sys:" << std::setiosflags(std::ios::fixed) << std::setprecision(2) + << static_cast(self_ru.ru_stime.tv_sec) + static_cast(self_ru.ru_stime.tv_usec) / 1000000 + << "\r\n"; + tmp_stream << "used_cpu_user:" << std::setiosflags(std::ios::fixed) << std::setprecision(2) + << static_cast(self_ru.ru_utime.tv_sec) + static_cast(self_ru.ru_utime.tv_usec) / 1000000 + << "\r\n"; + tmp_stream << "used_cpu_sys_children:" << std::setiosflags(std::ios::fixed) << std::setprecision(2) + << static_cast(c_ru.ru_stime.tv_sec) + static_cast(c_ru.ru_stime.tv_usec) / 1000000 + << "\r\n"; + tmp_stream << "used_cpu_user_children:" << std::setiosflags(std::ios::fixed) << std::setprecision(2) + << static_cast(c_ru.ru_utime.tv_sec) + static_cast(c_ru.ru_utime.tv_usec) / 1000000 + << "\r\n"; + info.append(tmp_stream.str()); +} + +void InfoCmd::InfoData(std::string& message) { + message += "# DATA \r\n "; message += DATABASES_NUM + std::string(":") + std::to_string(pikiwidb::g_config.databases) + "\r\n"; message += ROCKSDB_NUM + std::string(":") + std::to_string(pikiwidb::g_config.db_instance_num) + "\r\n"; message += ROCKSDB_VERSION + std::string(":") + ROCKSDB_NAMESPACE::GetRocksVersionAsString() + "\r\n"; +} + +double InfoCmd::MethodofTotalTimeCalculation(const uint64_t time_consuming) { + return static_cast(time_consuming) / 1000.0; +} + +double InfoCmd::MethodofCommandStatistics(const uint64_t time_consuming, const uint64_t frequency) { + return (static_cast(time_consuming) / 1000.0) / static_cast(frequency); +} - client->AppendString(message); +void InfoCmd::InfoCommandStats(PClient* client, std::string& info) { + std::stringstream tmp_stream; + tmp_stream.precision(2); + tmp_stream.setf(std::ios::fixed); + tmp_stream << "# Commandstats" + << "\r\n"; + auto cmdstat_map = client->GetCommandStatMap(); + for (auto iter : *cmdstat_map) { + if (iter.second.cmd_count != 0) { + tmp_stream << iter.first << ":" + << "calls=" << iter.second.cmd_count + << ", usec=" << MethodofTotalTimeCalculation(iter.second.cmd_time_consuming) << ", usec_per_call="; + if (!iter.second.cmd_time_consuming) { + tmp_stream << 0 << "\r\n"; + } else { + tmp_stream << MethodofCommandStatistics(iter.second.cmd_time_consuming, iter.second.cmd_count) << "\r\n"; + } + } + } + info.append(tmp_stream.str()); } CmdDebug::CmdDebug(const std::string& name, int arity) : BaseCmdGroup(name, kCmdFlagsAdmin, kAclCategoryAdmin) {} diff --git a/src/cmd_admin.h b/src/cmd_admin.h index 3c846d19e..0efa6c1ba 100644 --- a/src/cmd_admin.h +++ b/src/cmd_admin.h @@ -124,8 +124,44 @@ class InfoCmd : public BaseCmd { private: void DoCmd(PClient* client) override; - void InfoRaft(PClient* client); - void InfoData(PClient* client); + enum InfoSection { + kInfoErr = 0x0, + kInfoServer, + kInfoStats, + kInfoCPU, + kInfoData, + kInfo, + kInfoAll, + kInfoCommandStats, + kInfoRaft + }; + + InfoSection info_section_; + const static std::string kInfoSection; + const static std::string kAllSection; + const static std::string kServerSection; + const static std::string kClientsSection; + const static std::string kStatsSection; + const static std::string kExecCountSection; + const static std::string kCPUSection; + const static std::string kReplicationSection; + const static std::string kKeyspaceSection; + const static std::string kDataSection; + const static std::string kRocksDBSection; + const static std::string kDebugSection; + const static std::string kCommandStatsSection; + const static std::string kCacheSection; + const static std::string kRaftSection; + + void InfoServer(std::string& info); + void InfoStats(std::string& info); + void InfoCPU(std::string& info); + void InfoRaft(std::string& info); + void InfoData(std::string& info); + void InfoCommandStats(PClient* client, std::string& info); + + double MethodofTotalTimeCalculation(const uint64_t time_consuming); + double MethodofCommandStatistics(const uint64_t time_consuming, const uint64_t frequency); }; class CmdDebug : public BaseCmdGroup { diff --git a/src/cmd_table_manager.cc b/src/cmd_table_manager.cc index b9f398319..72edb7d43 100644 --- a/src/cmd_table_manager.cc +++ b/src/cmd_table_manager.cc @@ -209,4 +209,5 @@ bool CmdTableManager::CmdExist(const std::string& cmd) const { } uint32_t CmdTableManager::GetCmdId() { return ++cmdId_; } + } // namespace pikiwidb diff --git a/src/cmd_thread_pool_worker.cc b/src/cmd_thread_pool_worker.cc index 7de20a520..2f8eefb4c 100644 --- a/src/cmd_thread_pool_worker.cc +++ b/src/cmd_thread_pool_worker.cc @@ -6,6 +6,8 @@ */ #include "cmd_thread_pool_worker.h" +#include "client.h" +#include "env.h" #include "log.h" #include "pikiwidb.h" @@ -37,7 +39,21 @@ void CmdWorkThreadPoolWorker::Work() { g_pikiwidb->PushWriteTask(task->Client()); continue; } + + auto cmdstat_map = task->Client()->GetCommandStatMap(); + CommandStatistics statistics; + if (cmdstat_map->find(task->CmdName()) == cmdstat_map->end()) { + cmdstat_map->emplace(task->CmdName(), statistics); + } + task->Client()->time_stat_->dequeue_ts_ = pstd::NowMicros(); task->Run(cmdPtr); + + // Info Commandstats used + + task->Client()->time_stat_->process_done_ts_ = pstd::NowMicros(); + (*cmdstat_map)[task->CmdName()].cmd_count.fetch_add(1); + (*cmdstat_map)[task->CmdName()].cmd_time_consuming.fetch_add(task->Client()->time_stat_->total_time()); + g_pikiwidb->PushWriteTask(task->Client()); } self_task_.clear(); diff --git a/src/pikiwidb.cc b/src/pikiwidb.cc index 0880549ad..a3f21e289 100644 --- a/src/pikiwidb.cc +++ b/src/pikiwidb.cc @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -174,6 +175,8 @@ bool PikiwiDB::Init() { timerTask->SetCallback([]() { PREPL.Cron(); }); event_server_->AddTimerTask(timerTask); + time(&start_time_s_); + return true; } diff --git a/src/pikiwidb.h b/src/pikiwidb.h index a9112ba21..4bba614dc 100644 --- a/src/pikiwidb.h +++ b/src/pikiwidb.h @@ -63,6 +63,8 @@ class PikiwiDB final { const std::function&, const net::SocketAddr&)>& onConnect, const std::function& cb); + time_t Start_time_s() { return start_time_s_; } + public: PString cfg_file_; uint16_t port_{0}; @@ -78,6 +80,8 @@ class PikiwiDB final { std::unique_ptr>> event_server_; uint32_t cmd_id_ = 0; + + time_t start_time_s_ = 0; }; extern std::unique_ptr g_pikiwidb; From 290e080bc51b30b90d183381d3b6f751ebf0e0f4 Mon Sep 17 00:00:00 2001 From: shenmengju Date: Sun, 2 Jun 2024 21:42:13 +0800 Subject: [PATCH 02/15] fix details --- src/client.cc | 2 ++ src/client.h | 16 ++++++++++------ src/cmd_admin.cc | 16 +++++----------- src/cmd_admin.h | 7 ------- src/cmd_thread_pool_worker.cc | 8 ++++---- 5 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/client.cc b/src/client.cc index a69d41053..e7a1921cf 100644 --- a/src/client.cc +++ b/src/client.cc @@ -672,4 +672,6 @@ void PClient::SetKey(std::vector& names) { std::unordered_map* PClient::GetCommandStatMap() { return &cmdstat_map_; } +std::shared_ptr PClient::GetTimeStat() { return time_stat_; } + } // namespace pikiwidb diff --git a/src/client.h b/src/client.h index 48c91bfe2..0e6b6dc41 100644 --- a/src/client.h +++ b/src/client.h @@ -24,11 +24,11 @@ namespace pikiwidb { struct CommandStatistics { CommandStatistics() = default; CommandStatistics(const CommandStatistics& other) { - cmd_time_consuming.store(other.cmd_time_consuming.load()); - cmd_count.store(other.cmd_count.load()); + cmd_time_consuming_.store(other.cmd_time_consuming_.load()); + cmd_count_.store(other.cmd_count_.load()); } - std::atomic cmd_count = 0; - std::atomic cmd_time_consuming = 0; + std::atomic cmd_count_ = 0; + std::atomic cmd_time_consuming_ = 0; }; struct TimeStat { @@ -38,7 +38,10 @@ struct TimeStat { process_done_ts_ = 0; } - uint64_t total_time() const { return process_done_ts_ > enqueue_ts_ ? process_done_ts_ - enqueue_ts_ : 0; } + uint64_t GetTotalTime() const { return process_done_ts_ > enqueue_ts_ ? process_done_ts_ - enqueue_ts_ : 0; } + void SetEnqueueTs(uint64_t now_time) { enqueue_ts_ = now_time; } + void SetDequeueTs(uint64_t now_time) { dequeue_ts_ = now_time; } + void SetProcessDoneTs(uint64_t now_time) { process_done_ts_ = now_time; } uint64_t enqueue_ts_; uint64_t dequeue_ts_; @@ -261,8 +264,8 @@ class PClient : public std::enable_shared_from_this, public CmdRes { std::span argv_; // Info Commandstats used - std::shared_ptr time_stat_; std::unordered_map* GetCommandStatMap(); + std::shared_ptr GetTimeStat(); // std::shared_ptr getTcpConnection() const { return tcp_connection_.lock(); } int handlePacket(const char*, int); @@ -324,5 +327,6 @@ class PClient : public std::enable_shared_from_this, public CmdRes { * Info Commandstats used */ std::unordered_map cmdstat_map_; + std::shared_ptr time_stat_; }; } // namespace pikiwidb diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 9c5f02af4..5fbfd823d 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -154,16 +154,10 @@ void PingCmd::DoCmd(PClient* client) { client->SetRes(CmdRes::kPong, "PONG"); } const std::string InfoCmd::kInfoSection = "info"; const std::string InfoCmd::kAllSection = "all"; const std::string InfoCmd::kServerSection = "server"; -const std::string InfoCmd::kClientsSection = "clients"; const std::string InfoCmd::kStatsSection = "stats"; const std::string InfoCmd::kCPUSection = "cpu"; -const std::string InfoCmd::kReplicationSection = "replication"; -const std::string InfoCmd::kKeyspaceSection = "keyspace"; const std::string InfoCmd::kDataSection = "data"; -const std::string InfoCmd::kRocksDBSection = "rocksdb"; -const std::string InfoCmd::kDebugSection = "debug"; const std::string InfoCmd::kCommandStatsSection = "commandstats"; -const std::string InfoCmd::kCacheSection = "cache"; const std::string InfoCmd::kRaftSection = "RAFT"; const std::string InfoCmd::kInfoSection = "info"; @@ -408,14 +402,14 @@ void InfoCmd::InfoCommandStats(PClient* client, std::string& info) { << "\r\n"; auto cmdstat_map = client->GetCommandStatMap(); for (auto iter : *cmdstat_map) { - if (iter.second.cmd_count != 0) { + if (iter.second.cmd_count_ != 0) { tmp_stream << iter.first << ":" - << "calls=" << iter.second.cmd_count - << ", usec=" << MethodofTotalTimeCalculation(iter.second.cmd_time_consuming) << ", usec_per_call="; - if (!iter.second.cmd_time_consuming) { + << "calls=" << iter.second.cmd_count_ + << ", usec=" << MethodofTotalTimeCalculation(iter.second.cmd_time_consuming_) << ", usec_per_call="; + if (!iter.second.cmd_time_consuming_) { tmp_stream << 0 << "\r\n"; } else { - tmp_stream << MethodofCommandStatistics(iter.second.cmd_time_consuming, iter.second.cmd_count) << "\r\n"; + tmp_stream << MethodofCommandStatistics(iter.second.cmd_time_consuming_, iter.second.cmd_count_) << "\r\n"; } } } diff --git a/src/cmd_admin.h b/src/cmd_admin.h index 0efa6c1ba..0aaef6cab 100644 --- a/src/cmd_admin.h +++ b/src/cmd_admin.h @@ -140,17 +140,10 @@ class InfoCmd : public BaseCmd { const static std::string kInfoSection; const static std::string kAllSection; const static std::string kServerSection; - const static std::string kClientsSection; const static std::string kStatsSection; - const static std::string kExecCountSection; const static std::string kCPUSection; - const static std::string kReplicationSection; - const static std::string kKeyspaceSection; const static std::string kDataSection; - const static std::string kRocksDBSection; - const static std::string kDebugSection; const static std::string kCommandStatsSection; - const static std::string kCacheSection; const static std::string kRaftSection; void InfoServer(std::string& info); diff --git a/src/cmd_thread_pool_worker.cc b/src/cmd_thread_pool_worker.cc index 2f8eefb4c..96f9f51c1 100644 --- a/src/cmd_thread_pool_worker.cc +++ b/src/cmd_thread_pool_worker.cc @@ -45,14 +45,14 @@ void CmdWorkThreadPoolWorker::Work() { if (cmdstat_map->find(task->CmdName()) == cmdstat_map->end()) { cmdstat_map->emplace(task->CmdName(), statistics); } - task->Client()->time_stat_->dequeue_ts_ = pstd::NowMicros(); + task->Client()->GetTimeStat()->SetDequeueTs(pstd::NowMicros()); task->Run(cmdPtr); // Info Commandstats used - task->Client()->time_stat_->process_done_ts_ = pstd::NowMicros(); - (*cmdstat_map)[task->CmdName()].cmd_count.fetch_add(1); - (*cmdstat_map)[task->CmdName()].cmd_time_consuming.fetch_add(task->Client()->time_stat_->total_time()); + task->Client()->GetTimeStat()->SetProcessDoneTs(pstd::NowMicros()); + (*cmdstat_map)[task->CmdName()].cmd_count_.fetch_add(1); + (*cmdstat_map)[task->CmdName()].cmd_time_consuming_.fetch_add(task->Client()->GetTimeStat()->GetTotalTime()); g_pikiwidb->PushWriteTask(task->Client()); } From 23f2b3dc39ffa754f50c0742bee5b74ce22d88c1 Mon Sep 17 00:00:00 2001 From: shenmengju Date: Sun, 2 Jun 2024 22:42:50 +0800 Subject: [PATCH 03/15] fix go test error --- src/cmd_admin.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 5fbfd823d..5a71d4f8a 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -380,7 +380,6 @@ void InfoCmd::InfoCPU(std::string& info) { } void InfoCmd::InfoData(std::string& message) { - message += "# DATA \r\n "; message += DATABASES_NUM + std::string(":") + std::to_string(pikiwidb::g_config.databases) + "\r\n"; message += ROCKSDB_NUM + std::string(":") + std::to_string(pikiwidb::g_config.db_instance_num) + "\r\n"; message += ROCKSDB_VERSION + std::string(":") + ROCKSDB_NAMESPACE::GetRocksVersionAsString() + "\r\n"; From 4bff0720203c435adac0653462605cac16c32473 Mon Sep 17 00:00:00 2001 From: shenmengju Date: Mon, 10 Jun 2024 20:09:31 +0800 Subject: [PATCH 04/15] fix the way of strings connect --- src/cmd_admin.cc | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 5a71d4f8a..fce51d411 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -289,31 +289,35 @@ void InfoCmd::InfoRaft(std::string& message) { return; } - message += "raft_group_id:" + PRAFT.GetGroupID() + "\r\n"; - message += "raft_node_id:" + PRAFT.GetNodeID() + "\r\n"; - message += "raft_peer_id:" + PRAFT.GetPeerID() + "\r\n"; + std::stringstream tmp_stream; + + tmp_stream << "raft_group_id:" << PRAFT.GetGroupID() << "\r\n"; + tmp_stream << "raft_node_id:" << PRAFT.GetNodeID() << "\r\n"; + tmp_stream << "raft_peer_id:" << PRAFT.GetPeerID() << "\r\n"; if (braft::is_active_state(node_status.state)) { - message += "raft_state:up\r\n"; + tmp_stream << "raft_state:up\r\n"; } else { - message += "raft_state:down\r\n"; + tmp_stream << "raft_state:down\r\n"; } - message += "raft_role:" + std::string(braft::state2str(node_status.state)) + "\r\n"; - message += "raft_leader_id:" + node_status.leader_id.to_string() + "\r\n"; - message += "raft_current_term:" + std::to_string(node_status.term) + "\r\n"; + tmp_stream << "raft_role:" << std::string(braft::state2str(node_status.state)) << "\r\n"; + tmp_stream << "raft_leader_id:" << node_status.leader_id.to_string() << "\r\n"; + tmp_stream << "raft_current_term:" << std::to_string(node_status.term) << "\r\n"; if (PRAFT.IsLeader()) { std::vector peers; auto status = PRAFT.GetListPeers(&peers); if (!status.ok()) { - message += "-ERR:" + status.error_str(); + tmp_stream << "-ERR:" << status.error_str(); return; } for (int i = 0; i < peers.size(); i++) { - message += "raft_node" + std::to_string(i) + ":addr=" + butil::ip2str(peers[i].addr.ip).c_str() + - ",port=" + std::to_string(peers[i].addr.port) + "\r\n"; + tmp_stream << "raft_node" << std::to_string(i) << ":addr=" << butil::ip2str(peers[i].addr.ip).c_str() + << ",port=" << std::to_string(peers[i].addr.port) << "\r\n"; } } + + message.append(tmp_stream.str()); } void InfoCmd::InfoServer(std::string& info) { From 60b6af5a010a1ceb8aafcddcf72a95d7b7c022c4 Mon Sep 17 00:00:00 2001 From: shenmengju Date: Fri, 14 Jun 2024 21:33:28 +0800 Subject: [PATCH 05/15] fix timestat and string match --- src/client.cc | 3 ++- src/client.h | 28 +++++++++++++++++++--------- src/cmd_admin.cc | 17 +++-------------- src/cmd_admin.h | 9 +++++++++ src/cmd_thread_pool_worker.cc | 7 ++++--- 5 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/client.cc b/src/client.cc index e7a1921cf..3a3883f68 100644 --- a/src/client.cc +++ b/src/client.cc @@ -357,7 +357,8 @@ int PClient::handlePacket(const char* start, int bytes) { // executeCommand(); // return static_cast(ptr - start); // } - time_stat_->enqueue_ts_ = pstd::NowMicros(); + auto now = std::chrono::steady_clock::now(); + time_stat_->SetEnqueueTs(now); g_pikiwidb->SubmitFast(std::make_shared(shared_from_this())); // check transaction diff --git a/src/client.h b/src/client.h index 0e6b6dc41..4cbcbbbc5 100644 --- a/src/client.h +++ b/src/client.h @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include @@ -32,20 +33,29 @@ struct CommandStatistics { }; struct TimeStat { + using TimePoint = std::chrono::time_point; + TimeStat() = default; + void Reset() { - enqueue_ts_ = dequeue_ts_ = 0; - process_done_ts_ = 0; + enqueue_ts_ = TimePoint::min(); + dequeue_ts_ = TimePoint::min(); + process_done_ts_ = TimePoint::min(); + } + + uint64_t GetTotalTime() const { + return (process_done_ts_ > enqueue_ts_) + ? std::chrono::duration_cast(process_done_ts_ - enqueue_ts_).count() + : 0; } - uint64_t GetTotalTime() const { return process_done_ts_ > enqueue_ts_ ? process_done_ts_ - enqueue_ts_ : 0; } - void SetEnqueueTs(uint64_t now_time) { enqueue_ts_ = now_time; } - void SetDequeueTs(uint64_t now_time) { dequeue_ts_ = now_time; } - void SetProcessDoneTs(uint64_t now_time) { process_done_ts_ = now_time; } + void SetEnqueueTs(TimePoint now_time) { enqueue_ts_ = now_time; } + void SetDequeueTs(TimePoint now_time) { dequeue_ts_ = now_time; } + void SetProcessDoneTs(TimePoint now_time) { process_done_ts_ = now_time; } - uint64_t enqueue_ts_; - uint64_t dequeue_ts_; - uint64_t process_done_ts_; + TimePoint enqueue_ts_; + TimePoint dequeue_ts_; + TimePoint process_done_ts_; }; class CmdRes { diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index fce51d411..5b59e3c7e 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -189,20 +189,9 @@ bool InfoCmd::DoInitial(PClient* client) { } const auto& argv_ = client->argv_; - if (strcasecmp(argv_[1].data(), kAllSection.data()) == 0) { - info_section_ = kInfoAll; - } else if (strcasecmp(argv_[1].data(), kServerSection.data()) == 0) { - info_section_ = kInfoServer; - } else if (strcasecmp(argv_[1].data(), kStatsSection.data()) == 0) { - info_section_ = kInfoStats; - } else if (strcasecmp(argv_[1].data(), kCPUSection.data()) == 0) { - info_section_ = kInfoCPU; - } else if (strcasecmp(argv_[1].data(), kDataSection.data()) == 0) { - info_section_ = kInfoData; - } else if (strcasecmp(argv_[1].data(), kRaftSection.data()) == 0) { - info_section_ = kInfoRaft; - } else if (strcasecmp(argv_[1].data(), kCommandStatsSection.data()) == 0) { - info_section_ = kInfoCommandStats; + auto it = sectionMap.find(argv_[1].data()); + if (it != sectionMap.end()) { + info_section_ = it->second; } if (argc != 2) { diff --git a/src/cmd_admin.h b/src/cmd_admin.h index 0aaef6cab..52d94b7e1 100644 --- a/src/cmd_admin.h +++ b/src/cmd_admin.h @@ -146,6 +146,15 @@ class InfoCmd : public BaseCmd { const static std::string kCommandStatsSection; const static std::string kRaftSection; + const std::unordered_map sectionMap = {{kAllSection, kInfoAll}, + {kServerSection, kInfoServer}, + {kStatsSection, kInfoStats}, + {kCPUSection, kInfoCPU}, + {kDataSection, kInfoData}, + {kRaftSection, kInfoRaft}, + {kCommandStatsSection, kInfoCommandStats}, + {kRaftSection, kInfoRaft}}; + void InfoServer(std::string& info); void InfoStats(std::string& info); void InfoCPU(std::string& info); diff --git a/src/cmd_thread_pool_worker.cc b/src/cmd_thread_pool_worker.cc index 96f9f51c1..487c7fe54 100644 --- a/src/cmd_thread_pool_worker.cc +++ b/src/cmd_thread_pool_worker.cc @@ -45,12 +45,13 @@ void CmdWorkThreadPoolWorker::Work() { if (cmdstat_map->find(task->CmdName()) == cmdstat_map->end()) { cmdstat_map->emplace(task->CmdName(), statistics); } - task->Client()->GetTimeStat()->SetDequeueTs(pstd::NowMicros()); + auto now = std::chrono::steady_clock::now(); + task->Client()->GetTimeStat()->SetDequeueTs(now); task->Run(cmdPtr); // Info Commandstats used - - task->Client()->GetTimeStat()->SetProcessDoneTs(pstd::NowMicros()); + now = std::chrono::steady_clock::now(); + task->Client()->GetTimeStat()->SetProcessDoneTs(now); (*cmdstat_map)[task->CmdName()].cmd_count_.fetch_add(1); (*cmdstat_map)[task->CmdName()].cmd_time_consuming_.fetch_add(task->Client()->GetTimeStat()->GetTotalTime()); From e2e2e68e6608815229a9b081b0c1e48962a9d41a Mon Sep 17 00:00:00 2001 From: shenmengju Date: Sat, 15 Jun 2024 11:56:59 +0800 Subject: [PATCH 06/15] fix errorOther output --- src/cmd_admin.cc | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 5b59e3c7e..a68e97700 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -179,10 +179,6 @@ InfoCmd::InfoCmd(const std::string& name, int16_t arity) : BaseCmd(name, arity, bool InfoCmd::DoInitial(PClient* client) { size_t argc = client->argv_.size(); - if (argc > 4) { - client->SetRes(CmdRes::kSyntaxErr); - return false; - } if (argc == 1) { info_section_ = kInfo; return true; @@ -268,13 +264,13 @@ void InfoCmd::DoCmd(PClient* client) { */ void InfoCmd::InfoRaft(std::string& message) { if (!PRAFT.IsInitialized()) { - message += "-ERR:Don't already cluster member \r\n"; + message += "-ERR Don't already cluster member\r\n"; return; } auto node_status = PRAFT.GetNodeStatus(); if (node_status.state == braft::State::STATE_END) { - message += "-ERR:Node is not initialized \r\n"; + message += "-ERR Node is not initialized\r\n"; return; } @@ -296,7 +292,7 @@ void InfoCmd::InfoRaft(std::string& message) { std::vector peers; auto status = PRAFT.GetListPeers(&peers); if (!status.ok()) { - tmp_stream << "-ERR:" << status.error_str(); + tmp_stream << "-ERR " << status.error_str()<<"\r\n"; return; } From 9b1f39068b766cbb1c54448be13c6ff1accc517f Mon Sep 17 00:00:00 2001 From: shenmengju Date: Sat, 15 Jun 2024 18:31:52 +0800 Subject: [PATCH 07/15] error when some section not supportd --- src/cmd_admin.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index a68e97700..77dcc8a9d 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -188,6 +188,9 @@ bool InfoCmd::DoInitial(PClient* client) { auto it = sectionMap.find(argv_[1].data()); if (it != sectionMap.end()) { info_section_ = it->second; + } else { + client->SetRes(CmdRes::kErrOther, "the cmd is not supported"); + return false; } if (argc != 2) { @@ -292,7 +295,8 @@ void InfoCmd::InfoRaft(std::string& message) { std::vector peers; auto status = PRAFT.GetListPeers(&peers); if (!status.ok()) { - tmp_stream << "-ERR " << status.error_str()<<"\r\n"; + tmp_stream.str("-ERR "); + tmp_stream << status.error_str() << "\r\n"; return; } From 23125efb3ee49700ce71362eeee8c32995750030 Mon Sep 17 00:00:00 2001 From: shenmengju Date: Fri, 21 Jun 2024 18:22:41 +0800 Subject: [PATCH 08/15] add lowercase change --- src/cmd_admin.cc | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 77dcc8a9d..e57d511e9 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include "cmd_admin.h" #include @@ -158,7 +160,7 @@ const std::string InfoCmd::kStatsSection = "stats"; const std::string InfoCmd::kCPUSection = "cpu"; const std::string InfoCmd::kDataSection = "data"; const std::string InfoCmd::kCommandStatsSection = "commandstats"; -const std::string InfoCmd::kRaftSection = "RAFT"; +const std::string InfoCmd::kRaftSection = "raft"; const std::string InfoCmd::kInfoSection = "info"; const std::string InfoCmd::kAllSection = "all"; @@ -184,19 +186,23 @@ bool InfoCmd::DoInitial(PClient* client) { return true; } - const auto& argv_ = client->argv_; - auto it = sectionMap.find(argv_[1].data()); - if (it != sectionMap.end()) { - info_section_ = it->second; - } else { - client->SetRes(CmdRes::kErrOther, "the cmd is not supported"); - return false; - } + std::string argv_ = client->argv_[1].data(); + //统一转换成为小写后进行比较 + std::transform(argv_.begin(), argv_.end(), argv_.begin(), [](unsigned char c) { return std::tolower(c); }); + if (argc == 2) { + auto it = sectionMap.find(argv_); + if (it != sectionMap.end()) { + info_section_ = it->second; + } else { + client->SetRes(CmdRes::kErrOther, "the cmd is not supported"); + return false; + } - if (argc != 2) { + } else { client->SetRes(CmdRes::kSyntaxErr); return false; } + return true; } From f86f5a7cbdb6121914cc89c9545485468fcc4398 Mon Sep 17 00:00:00 2001 From: shenmengju Date: Sat, 6 Jul 2024 17:01:13 +0800 Subject: [PATCH 09/15] modify according to comment --- src/cmd_admin.cc | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index e57d511e9..909948270 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -187,7 +187,7 @@ bool InfoCmd::DoInitial(PClient* client) { } std::string argv_ = client->argv_[1].data(); - //统一转换成为小写后进行比较 + // convert section to lowercase std::transform(argv_.begin(), argv_.end(), argv_.begin(), [](unsigned char c) { return std::tolower(c); }); if (argc == 2) { auto it = sectionMap.find(argv_); @@ -197,16 +197,13 @@ bool InfoCmd::DoInitial(PClient* client) { client->SetRes(CmdRes::kErrOther, "the cmd is not supported"); return false; } - } else { client->SetRes(CmdRes::kSyntaxErr); return false; } - return true; } -// @todo The info raft command is only supported for the time being void InfoCmd::DoCmd(PClient* client) { std::string info; switch (info_section_) { From e50a18992daf7703a41bbb20203bcd67db5f2024 Mon Sep 17 00:00:00 2001 From: shenmengju Date: Wed, 10 Jul 2024 23:19:55 +0800 Subject: [PATCH 10/15] fix details --- src/cmd_admin.cc | 27 +++++++++++++++++---------- src/cmd_admin.h | 2 +- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 909948270..20a8341f3 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -270,13 +270,13 @@ void InfoCmd::DoCmd(PClient* client) { */ void InfoCmd::InfoRaft(std::string& message) { if (!PRAFT.IsInitialized()) { - message += "-ERR Don't already cluster member\r\n"; + message += "-ERR Not a cluster member.\r\n"; return; } auto node_status = PRAFT.GetNodeStatus(); if (node_status.state == braft::State::STATE_END) { - message += "-ERR Node is not initialized\r\n"; + message += "-ERR Node is not initialized.\r\n"; return; } @@ -398,19 +398,26 @@ void InfoCmd::InfoCommandStats(PClient* client, std::string& info) { auto cmdstat_map = client->GetCommandStatMap(); for (auto iter : *cmdstat_map) { if (iter.second.cmd_count_ != 0) { - tmp_stream << iter.first << ":" - << "calls=" << iter.second.cmd_count_ - << ", usec=" << MethodofTotalTimeCalculation(iter.second.cmd_time_consuming_) << ", usec_per_call="; - if (!iter.second.cmd_time_consuming_) { - tmp_stream << 0 << "\r\n"; - } else { - tmp_stream << MethodofCommandStatistics(iter.second.cmd_time_consuming_, iter.second.cmd_count_) << "\r\n"; - } + tmp_stream << iter.first << ":" << FormatCommandStatLine(iter.second); } } info.append(tmp_stream.str()); } +std::string InfoCmd::FormatCommandStatLine(const CommandStatistics& stats) { + std::stringstream stream; + stream.precision(2); + stream.setf(std::ios::fixed); + stream << "calls=" << stats.cmd_count_ << ", usec=" << MethodofTotalTimeCalculation(stats.cmd_time_consuming_) + << ", usec_per_call="; + if (!stats.cmd_time_consuming_) { + stream << 0 << "\r\n"; + } else { + stream << MethodofCommandStatistics(stats.cmd_time_consuming_, stats.cmd_count_) << "\r\n"; + } + return stream.str(); +} + CmdDebug::CmdDebug(const std::string& name, int arity) : BaseCmdGroup(name, kCmdFlagsAdmin, kAclCategoryAdmin) {} bool CmdDebug::HasSubCommand() const { return true; } diff --git a/src/cmd_admin.h b/src/cmd_admin.h index 52d94b7e1..a4af46eb9 100644 --- a/src/cmd_admin.h +++ b/src/cmd_admin.h @@ -161,7 +161,7 @@ class InfoCmd : public BaseCmd { void InfoRaft(std::string& info); void InfoData(std::string& info); void InfoCommandStats(PClient* client, std::string& info); - + std::string FormatCommandStatLine(const CommandStatistics& stats); double MethodofTotalTimeCalculation(const uint64_t time_consuming); double MethodofCommandStatistics(const uint64_t time_consuming, const uint64_t frequency); }; From 158e39704a8d5cd351ad11eb737cd000c7391d5a Mon Sep 17 00:00:00 2001 From: shenmengju Date: Thu, 11 Jul 2024 13:22:47 +0800 Subject: [PATCH 11/15] fix error --- src/cmd_admin.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/cmd_admin.h b/src/cmd_admin.h index a4af46eb9..8dd195b40 100644 --- a/src/cmd_admin.h +++ b/src/cmd_admin.h @@ -152,8 +152,7 @@ class InfoCmd : public BaseCmd { {kCPUSection, kInfoCPU}, {kDataSection, kInfoData}, {kRaftSection, kInfoRaft}, - {kCommandStatsSection, kInfoCommandStats}, - {kRaftSection, kInfoRaft}}; + {kCommandStatsSection, kInfoCommandStats}}; void InfoServer(std::string& info); void InfoStats(std::string& info); From 44f20ebd717d87ff623b222bca6df0cceb8eeabc Mon Sep 17 00:00:00 2001 From: shenmengju Date: Mon, 5 Aug 2024 15:13:30 +0800 Subject: [PATCH 12/15] format code --- src/cmd_admin.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 20a8341f3..119000cd8 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -12,12 +12,12 @@ #include #include -#include "cmd_admin.h" #include #include #include #include #include +#include "cmd_admin.h" #include "db.h" #include "braft/raft.h" From b2e6cdfe115052d1722498c197f7ba2e4ac3e2fe Mon Sep 17 00:00:00 2001 From: shenmengju Date: Sat, 17 Aug 2024 12:00:40 +0800 Subject: [PATCH 13/15] comment fix --- src/client.h | 3 ++- src/cmd_admin.cc | 15 --------------- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/src/client.h b/src/client.h index 4cbcbbbc5..524bb0f87 100644 --- a/src/client.h +++ b/src/client.h @@ -24,7 +24,8 @@ namespace pikiwidb { struct CommandStatistics { CommandStatistics() = default; - CommandStatistics(const CommandStatistics& other) { + CommandStatistics(const CommandStatistics& other) + :cmd_count_(other.cmd_count_.load()), cmd_time_consuming_(other.cmd_time_consuming_.load()) { cmd_time_consuming_.store(other.cmd_time_consuming_.load()); cmd_count_.store(other.cmd_count_.load()); } diff --git a/src/cmd_admin.cc b/src/cmd_admin.cc index 119000cd8..d933c580f 100644 --- a/src/cmd_admin.cc +++ b/src/cmd_admin.cc @@ -162,21 +162,6 @@ const std::string InfoCmd::kDataSection = "data"; const std::string InfoCmd::kCommandStatsSection = "commandstats"; const std::string InfoCmd::kRaftSection = "raft"; -const std::string InfoCmd::kInfoSection = "info"; -const std::string InfoCmd::kAllSection = "all"; -const std::string InfoCmd::kServerSection = "server"; -const std::string InfoCmd::kClientsSection = "clients"; -const std::string InfoCmd::kStatsSection = "stats"; -const std::string InfoCmd::kCPUSection = "cpu"; -const std::string InfoCmd::kReplicationSection = "replication"; -const std::string InfoCmd::kKeyspaceSection = "keyspace"; -const std::string InfoCmd::kDataSection = "data"; -const std::string InfoCmd::kRocksDBSection = "rocksdb"; -const std::string InfoCmd::kDebugSection = "debug"; -const std::string InfoCmd::kCommandStatsSection = "commandstats"; -const std::string InfoCmd::kCacheSection = "cache"; -const std::string InfoCmd::kRaftSection = "RAFT"; - InfoCmd::InfoCmd(const std::string& name, int16_t arity) : BaseCmd(name, arity, kCmdFlagsAdmin, kAclCategoryAdmin) {} bool InfoCmd::DoInitial(PClient* client) { From 471f662a8c5ebc9a0db611bd4879a425b5810644 Mon Sep 17 00:00:00 2001 From: shenmengju Date: Sat, 17 Aug 2024 14:58:21 +0800 Subject: [PATCH 14/15] format code --- src/client.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/client.h b/src/client.h index 524bb0f87..4ebd6427d 100644 --- a/src/client.h +++ b/src/client.h @@ -25,10 +25,8 @@ namespace pikiwidb { struct CommandStatistics { CommandStatistics() = default; CommandStatistics(const CommandStatistics& other) - :cmd_count_(other.cmd_count_.load()), cmd_time_consuming_(other.cmd_time_consuming_.load()) { - cmd_time_consuming_.store(other.cmd_time_consuming_.load()); - cmd_count_.store(other.cmd_count_.load()); - } + : cmd_count_(other.cmd_count_.load()), cmd_time_consuming_(other.cmd_time_consuming_.load()) {} + std::atomic cmd_count_ = 0; std::atomic cmd_time_consuming_ = 0; }; From 2a3ab183ecc1ad12a268cd495d2f0cd3a22f7b58 Mon Sep 17 00:00:00 2001 From: "Xin.Zh" Date: Mon, 26 Aug 2024 11:50:55 +0800 Subject: [PATCH 15/15] Update client.h --- src/client.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/client.h b/src/client.h index 4ebd6427d..73f97a760 100644 --- a/src/client.h +++ b/src/client.h @@ -52,9 +52,9 @@ struct TimeStat { void SetDequeueTs(TimePoint now_time) { dequeue_ts_ = now_time; } void SetProcessDoneTs(TimePoint now_time) { process_done_ts_ = now_time; } - TimePoint enqueue_ts_; - TimePoint dequeue_ts_; - TimePoint process_done_ts_; + TimePoint enqueue_ts_ = TimePoint::min(); + TimePoint dequeue_ts_ = TimePoint::min(); + TimePoint process_done_ts_ = TimePoint::min(); }; class CmdRes {