Skip to content

Commit 680e1e2

Browse files
authored
chore: refactor ServerFamily::Info (#4671)
Also, allow printing Metrics in tests. Signed-off-by: Roman Gershman <roman@dragonflydb.io>
1 parent 4da1678 commit 680e1e2

File tree

5 files changed

+54
-26
lines changed

5 files changed

+54
-26
lines changed

src/server/server_family.cc

+35-23
Original file line numberDiff line numberDiff line change
@@ -2230,17 +2230,8 @@ Metrics ServerFamily::GetMetrics(Namespace* ns) const {
22302230
return result;
22312231
}
22322232

2233-
void ServerFamily::Info(CmdArgList args, const CommandContext& cmd_cntx) {
2234-
if (args.size() > 1) {
2235-
return cmd_cntx.rb->SendError(kSyntaxErr);
2236-
}
2237-
2238-
string section;
2239-
2240-
if (args.size() == 1) {
2241-
section = absl::AsciiStrToUpper(ArgS(args, 0));
2242-
}
2243-
2233+
string ServerFamily::FormatInfoMetrics(const Metrics& m, std::string_view section,
2234+
bool priveleged) const {
22442235
string info;
22452236

22462237
auto should_enter = [&](string_view name, bool hidden = false) {
@@ -2256,13 +2247,13 @@ void ServerFamily::Info(CmdArgList args, const CommandContext& cmd_cntx) {
22562247
absl::StrAppend(&info, a1, ":", a2, "\r\n");
22572248
};
22582249

2259-
ServerState* ss = ServerState::tlocal();
2260-
2261-
bool show_managed_info =
2262-
!absl::GetFlag(FLAGS_managed_service_info) || cmd_cntx.conn_cntx->conn()->IsPrivileged();
2250+
bool show_managed_info = priveleged || !absl::GetFlag(FLAGS_managed_service_info);
22632251

22642252
if (should_enter("SERVER")) {
2265-
auto kind = ProactorBase::me()->GetKind();
2253+
ProactorBase* proactor = ProactorBase::me();
2254+
2255+
// proactor might be null in tests.
2256+
auto kind = proactor ? ProactorBase::me()->GetKind() : ProactorBase::EPOLL;
22662257
const char* multiplex_api = (kind == ProactorBase::IOURING) ? "iouring" : "epoll";
22672258

22682259
append("redis_version", kRedisVersion);
@@ -2282,12 +2273,6 @@ void ServerFamily::Info(CmdArgList args, const CommandContext& cmd_cntx) {
22822273
append("uptime_in_days", uptime / (3600 * 24));
22832274
}
22842275

2285-
Metrics m;
2286-
// Save time by not calculating metrics if we don't need them.
2287-
if (!(section == "SERVER" || section == "REPLICATION")) {
2288-
m = GetMetrics(cmd_cntx.conn_cntx->ns);
2289-
}
2290-
22912276
DbStats total;
22922277
for (const auto& db_stats : m.db_stats)
22932278
total += db_stats;
@@ -2486,7 +2471,10 @@ void ServerFamily::Info(CmdArgList args, const CommandContext& cmd_cntx) {
24862471
append("last_saved_file", save_info.file_name);
24872472
append("last_success_save_duration_sec", save_info.success_duration_sec);
24882473

2489-
unsigned is_loading = (ss->gstate() == GlobalState::LOADING);
2474+
ServerState* ss = ServerState::tlocal();
2475+
2476+
// ss can be null in tests.
2477+
unsigned is_loading = ss && (ss->gstate() == GlobalState::LOADING);
24902478
append("loading", is_loading);
24912479
append("saving", is_saving);
24922480
append("current_save_duration_sec", curent_durration_sec);
@@ -2653,6 +2641,30 @@ void ServerFamily::Info(CmdArgList args, const CommandContext& cmd_cntx) {
26532641
append("cluster_enabled", IsClusterEnabledOrEmulated());
26542642
append("migration_errors_total", service_.cluster_family().MigrationsErrorsCount());
26552643
}
2644+
2645+
return info;
2646+
}
2647+
2648+
void ServerFamily::Info(CmdArgList args, const CommandContext& cmd_cntx) {
2649+
if (args.size() > 1) {
2650+
return cmd_cntx.rb->SendError(kSyntaxErr);
2651+
}
2652+
2653+
string section;
2654+
2655+
if (args.size() == 1) {
2656+
section = absl::AsciiStrToUpper(ArgS(args, 0));
2657+
}
2658+
2659+
Metrics metrics;
2660+
2661+
// Save time by not calculating metrics if we don't need them.
2662+
if (!(section == "SERVER" || section == "REPLICATION")) {
2663+
metrics = GetMetrics(cmd_cntx.conn_cntx->ns);
2664+
}
2665+
2666+
string info = FormatInfoMetrics(metrics, section, cmd_cntx.conn_cntx->conn()->IsPrivileged());
2667+
26562668
auto* rb = static_cast<RedisReplyBuilder*>(cmd_cntx.rb);
26572669
rb->SendVerbatimString(info);
26582670
}

src/server/server_family.h

+3
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ class ServerFamily {
180180

181181
Metrics GetMetrics(Namespace* ns) const;
182182

183+
std::string FormatInfoMetrics(const Metrics& metrics, std::string_view section,
184+
bool priveleged) const;
185+
183186
ScriptMgr* script_mgr() {
184187
return script_mgr_.get();
185188
}

src/server/test_utils.cc

+4
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,10 @@ void BaseFamilyTest::ClearMetrics() {
368368
});
369369
}
370370

371+
string BaseFamilyTest::FormatMetrics(const Metrics& metrics) const {
372+
return service_->server_family().FormatInfoMetrics(metrics, "ALL", true);
373+
}
374+
371375
void BaseFamilyTest::WaitUntilLocked(DbIndex db_index, string_view key, double timeout) {
372376
auto step = 50us;
373377
auto timeout_micro = chrono::duration_cast<chrono::microseconds>(1000ms * timeout);

src/server/test_utils.h

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class BaseFamilyTest : public ::testing::Test {
120120
}
121121

122122
void ClearMetrics();
123+
std::string FormatMetrics(const Metrics& metrics) const;
123124

124125
void AdvanceTime(int64_t ms) {
125126
TEST_current_time_ms += ms;

src/server/tiered_storage_test.cc

+11-3
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,22 @@ TEST_F(TieredStorageTest, FlushAll) {
273273
}
274274
});
275275

276-
ExpectConditionWithinTimeout([&] { return GetMetrics().events.hits > 2; });
276+
Metrics metrics;
277+
ExpectConditionWithinTimeout([&] {
278+
metrics = GetMetrics();
279+
return metrics.events.hits > 2;
280+
});
281+
LOG(INFO) << FormatMetrics(metrics);
282+
277283
Run({"FLUSHALL"});
278284

279285
done = true;
280-
util::ThisFiber::SleepFor(50ms);
286+
util::ThisFiber::SleepFor(100ms);
281287
reader.Join();
282288

283-
auto metrics = GetMetrics();
289+
metrics = GetMetrics();
290+
LOG(INFO) << FormatMetrics(metrics);
291+
284292
EXPECT_EQ(metrics.db_stats.front().tiered_entries, 0u);
285293
EXPECT_GT(metrics.tiered_stats.total_fetches, 2u);
286294
}

0 commit comments

Comments
 (0)