This is an automated email from the ASF dual-hosted git repository.
twice pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks.git
The following commit(s) were added to refs/heads/unstable by this push:
new 80f64d8f chore(server): improve code style of `Server::Get..Info()`
(#2774)
80f64d8f is described below
commit 80f64d8fdccf51d7a2ee887c7468aae2de54e028
Author: Twice <[email protected]>
AuthorDate: Fri Feb 7 09:47:28 2025 +0800
chore(server): improve code style of `Server::Get..Info()` (#2774)
---
src/commands/cmd_server.cc | 3 +-
src/server/server.cc | 81 +++++++++++++++++++++-------------------------
src/server/server.h | 24 +++++++-------
3 files changed, 49 insertions(+), 59 deletions(-)
diff --git a/src/commands/cmd_server.cc b/src/commands/cmd_server.cc
index 17815cbf..a91c1e13 100644
--- a/src/commands/cmd_server.cc
+++ b/src/commands/cmd_server.cc
@@ -239,8 +239,7 @@ class CommandInfo : public Commander {
for (size_t i = 1; i < args_.size(); ++i) {
sections.push_back(util::ToLower(args_[i]));
}
- std::string info;
- srv->GetInfo(conn->GetNamespace(), sections, &info);
+ auto info = srv->GetInfo(conn->GetNamespace(), sections);
*output = conn->VerbatimString("txt", info);
return Status::OK();
}
diff --git a/src/server/server.cc b/src/server/server.cc
index 07a66cf9..6a4c1b55 100644
--- a/src/server/server.cc
+++ b/src/server/server.cc
@@ -853,8 +853,8 @@ void Server::cron() {
}
}
-void Server::GetRocksDBInfo(std::string *info) {
- if (is_loading_) return;
+std::string Server::GetRocksDBInfo() {
+ if (is_loading_) return "";
std::ostringstream string_stream;
rocksdb::DB *db = storage->GetDB();
@@ -963,10 +963,10 @@ void Server::GetRocksDBInfo(std::string *info) {
string_stream << "is_compacting:" << (db_compacting_ ? "yes" : "no") <<
"\r\n";
db_job_mu_.unlock();
- *info = string_stream.str();
+ return string_stream.str();
}
-void Server::GetServerInfo(std::string *info) {
+std::string Server::GetServerInfo() {
static int call_uname = 1;
static utsname name;
if (call_uname) {
@@ -998,20 +998,20 @@ void Server::GetServerInfo(std::string *info) {
int64_t now_secs = util::GetTimeStamp<std::chrono::seconds>();
string_stream << "uptime_in_seconds:" << now_secs - start_time_secs_ <<
"\r\n";
string_stream << "uptime_in_days:" << (now_secs - start_time_secs_) / 86400
<< "\r\n";
- *info = string_stream.str();
+ return string_stream.str();
}
-void Server::GetClientsInfo(std::string *info) {
+std::string Server::GetClientsInfo() {
std::ostringstream string_stream;
string_stream << "# Clients\r\n";
string_stream << "maxclients:" << config_->maxclients << "\r\n";
string_stream << "connected_clients:" << connected_clients_ << "\r\n";
string_stream << "monitor_clients:" << monitor_clients_ << "\r\n";
string_stream << "blocked_clients:" << blocked_clients_ << "\r\n";
- *info = string_stream.str();
+ return string_stream.str();
}
-void Server::GetMemoryInfo(std::string *info) {
+std::string Server::GetMemoryInfo() {
int64_t rss = Stats::GetMemoryRSS();
int64_t memory_lua = 0;
for (auto &wt : worker_threads_) {
@@ -1027,11 +1027,11 @@ void Server::GetMemoryInfo(std::string *info) {
string_stream << "used_memory_lua:" << memory_lua << "\r\n";
string_stream << "used_memory_lua_human:" << used_memory_lua_human << "\r\n";
string_stream << "used_memory_startup:" <<
memory_startup_use_.load(std::memory_order_relaxed) << "\r\n";
- *info = string_stream.str();
+ return string_stream.str();
}
-void Server::GetReplicationInfo(std::string *info) {
- if (is_loading_) return;
+std::string Server::GetReplicationInfo() {
+ if (is_loading_) return "";
std::ostringstream string_stream;
string_stream << "# Replication\r\n";
@@ -1067,7 +1067,7 @@ void Server::GetReplicationInfo(std::string *info) {
string_stream << "master_repl_offset:" << latest_seq << "\r\n";
- *info = string_stream.str();
+ return string_stream.str();
}
void Server::GetRoleInfo(std::string *info) {
@@ -1139,7 +1139,7 @@ int64_t Server::GetLastBgsaveTime() {
return last_bgsave_timestamp_secs_ == -1 ? start_time_secs_ :
last_bgsave_timestamp_secs_;
}
-void Server::GetStatsInfo(std::string *info) {
+std::string Server::GetStatsInfo() {
std::ostringstream string_stream;
string_stream << "# Stats\r\n";
string_stream << "total_connections_received:" << total_clients_ << "\r\n";
@@ -1165,10 +1165,10 @@ void Server::GetStatsInfo(std::string *info) {
string_stream << "pubsub_patterns:" << pubsub_patterns_.size() << "\r\n";
}
- *info = string_stream.str();
+ return string_stream.str();
}
-void Server::GetCommandsStatsInfo(std::string *info) {
+std::string Server::GetCommandsStatsInfo() {
std::ostringstream string_stream;
string_stream << "# Commandstats\r\n";
@@ -1200,19 +1200,19 @@ void Server::GetCommandsStatsInfo(std::string *info) {
string_stream << "sum=" << sum << ",count=" << calls << "\r\n";
}
- *info = string_stream.str();
+ return string_stream.str();
}
-void Server::GetClusterInfo(std::string *info) {
+std::string Server::GetClusterInfo() {
std::ostringstream string_stream;
string_stream << "# Cluster\r\n";
string_stream << "cluster_enabled:" << config_->cluster_enabled << "\r\n";
- *info = string_stream.str();
+ return string_stream.str();
}
-void Server::GetPersistenceInfo(std::string *info) {
+std::string Server::GetPersistenceInfo() {
std::ostringstream string_stream;
string_stream << "# Persistence\r\n";
@@ -1225,10 +1225,10 @@ void Server::GetPersistenceInfo(std::string *info) {
string_stream << "last_bgsave_status:" << last_bgsave_status_ << "\r\n";
string_stream << "last_bgsave_time_sec:" << last_bgsave_duration_secs_ <<
"\r\n";
- *info = string_stream.str();
+ return string_stream.str();
}
-void Server::GetCpuInfo(std::string *info) { //
NOLINT(readability-convert-member-functions-to-static)
+std::string Server::GetCpuInfo() { //
NOLINT(readability-convert-member-functions-to-static)
std::ostringstream string_stream;
rusage self_ru;
@@ -1241,11 +1241,11 @@ void Server::GetCpuInfo(std::string *info) { //
NOLINT(readability-convert-memb
<< static_cast<float>(self_ru.ru_utime.tv_sec) +
static_cast<float>(self_ru.ru_utime.tv_usec / 1000000)
<< "\r\n";
- *info = string_stream.str();
+ return string_stream.str();
}
-void Server::GetKeyspaceInfo(const std::string &ns, std::string *info) {
- if (is_loading_) return;
+std::string Server::GetKeyspaceInfo(const std::string &ns) {
+ if (is_loading_) return "";
std::ostringstream string_stream;
@@ -1283,31 +1283,24 @@ void Server::GetKeyspaceInfo(const std::string &ns,
std::string *info) {
string_stream << "used_disk_percent: " << used_disk_percent << "%\r\n";
}
- *info = string_stream.str();
+ return string_stream.str();
}
// WARNING: we must not access DB(i.e. RocksDB) when server is loading since
// DB is closed and the pointer is invalid. Server may crash if we access DB
during loading.
// If you add new fields which access DB into INFO command output, make sure
// this section can't be shown when loading(i.e. !is_loading_).
-void Server::GetInfo(const std::string &ns, const std::vector<std::string>
§ions, std::string *info) {
- info->clear();
-
- std::vector<std::pair<std::string, std::function<void(Server *, std::string
*)>>> info_funcs = {
- {"server", &Server::GetServerInfo},
- {"clients", &Server::GetClientsInfo},
- {"memory", &Server::GetMemoryInfo},
- {"persistence", &Server::GetPersistenceInfo},
- {"stats", &Server::GetStatsInfo},
- {"replication", &Server::GetReplicationInfo},
- {"cpu", &Server::GetCpuInfo},
- {"commandstats", &Server::GetCommandsStatsInfo},
- {"cluster", &Server::GetClusterInfo},
- {"keyspace", [&ns](Server *srv, std::string *info) {
srv->GetKeyspaceInfo(ns, info); }},
+std::string Server::GetInfo(const std::string &ns, const
std::vector<std::string> §ions) {
+ std::vector<std::pair<std::string, std::function<std::string(Server *)>>>
info_funcs = {
+ {"server", &Server::GetServerInfo}, {"clients",
&Server::GetClientsInfo},
+ {"memory", &Server::GetMemoryInfo}, {"persistence",
&Server::GetPersistenceInfo},
+ {"stats", &Server::GetStatsInfo}, {"replication",
&Server::GetReplicationInfo},
+ {"cpu", &Server::GetCpuInfo}, {"commandstats",
&Server::GetCommandsStatsInfo},
+ {"cluster", &Server::GetClusterInfo}, {"keyspace", [&ns](Server *srv) {
return srv->GetKeyspaceInfo(ns); }},
{"rocksdb", &Server::GetRocksDBInfo},
};
- std::stringstream string_stream;
+ std::string info_str;
bool all = sections.empty() || std::find(sections.begin(), sections.end(),
"all") != sections.end();
@@ -1317,15 +1310,13 @@ void Server::GetInfo(const std::string &ns, const
std::vector<std::string> §
if (first)
first = false;
else
- string_stream << "\r\n";
+ info_str.append("\r\n");
- std::string out;
- fn(this, &out);
- string_stream << out;
+ info_str.append(fn(this));
}
}
- *info = string_stream.str();
+ return info_str;
}
std::string Server::GetRocksDBStatsJson() const {
diff --git a/src/server/server.h b/src/server/server.h
index c82c1019..e5365066 100644
--- a/src/server/server.h
+++ b/src/server/server.h
@@ -232,19 +232,19 @@ class Server {
static int64_t GetCachedUnixTime();
int64_t GetLastBgsaveTime();
- void GetStatsInfo(std::string *info);
- void GetServerInfo(std::string *info);
- void GetMemoryInfo(std::string *info);
- void GetRocksDBInfo(std::string *info);
- void GetClientsInfo(std::string *info);
- void GetReplicationInfo(std::string *info);
void GetRoleInfo(std::string *info);
- void GetCommandsStatsInfo(std::string *info);
- void GetClusterInfo(std::string *info);
- void GetPersistenceInfo(std::string *info);
- void GetCpuInfo(std::string *info);
- void GetKeyspaceInfo(const std::string &ns, std::string *info);
- void GetInfo(const std::string &ns, const std::vector<std::string>
§ions, std::string *info);
+ std::string GetStatsInfo();
+ std::string GetServerInfo();
+ std::string GetMemoryInfo();
+ std::string GetRocksDBInfo();
+ std::string GetClientsInfo();
+ std::string GetReplicationInfo();
+ std::string GetCommandsStatsInfo();
+ std::string GetClusterInfo();
+ std::string GetPersistenceInfo();
+ std::string GetCpuInfo();
+ std::string GetKeyspaceInfo(const std::string &ns);
+ std::string GetInfo(const std::string &ns, const std::vector<std::string>
§ions);
std::string GetRocksDBStatsJson() const;
ReplState GetReplicationState();