From fc95d413692471ee6899794edf7f786dc66c9019 Mon Sep 17 00:00:00 2001 From: xuqing Date: Fri, 25 Aug 2023 16:59:09 +0800 Subject: [PATCH 1/5] Add LASTSAVE command --- src/commands/cmd_server.cc | 14 ++++++++++++++ src/server/server.cc | 7 ++++++- src/server/server.h | 1 + 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/commands/cmd_server.cc b/src/commands/cmd_server.cc index 95dd0086b23..083c0bb2f80 100644 --- a/src/commands/cmd_server.cc +++ b/src/commands/cmd_server.cc @@ -978,6 +978,19 @@ static uint64_t GenerateConfigFlag(const std::vector &args) { return 0; } +class CommandLastSave : public Commander { + public: + Status Execute(Server *svr, Connection *conn, std::string *output) override { + if (!conn->IsAdmin()) { + return {Status::RedisExecErr, errAdminPermissionRequired}; + } + + int unix_sec = svr->GetLastBgsaveTime(); + *output = redis::Integer(unix_sec); + return Status::OK(); + } +}; + REDIS_REGISTER_COMMANDS(MakeCmdAttr("auth", 2, "read-only ok-loading", 0, 0, 0), MakeCmdAttr("ping", -1, "read-only", 0, 0, 0), MakeCmdAttr("select", 2, "read-only", 0, 0, 0), @@ -1007,6 +1020,7 @@ REDIS_REGISTER_COMMANDS(MakeCmdAttr("auth", 2, "read-only ok-loadin MakeCmdAttr("compact", 1, "read-only no-script", 0, 0, 0), MakeCmdAttr("bgsave", 1, "read-only no-script", 0, 0, 0), + MakeCmdAttr("lastsave", 1, "read-only no-script", 0, 0, 0), MakeCmdAttr("flushbackup", 1, "read-only no-script", 0, 0, 0), MakeCmdAttr("slaveof", 3, "read-only exclusive no-script", 0, 0, 0), MakeCmdAttr("stats", 1, "read-only", 0, 0, 0), ) diff --git a/src/server/server.cc b/src/server/server.cc index b07821e706e..8250cf65711 100644 --- a/src/server/server.cc +++ b/src/server/server.cc @@ -990,6 +990,11 @@ int Server::GetCachedUnixTime() { return unix_time.load(); } +int Server::GetLastBgsaveTime() { + std::lock_guard lg(db_job_mu_); + return last_bgsave_time_ == -1 ? start_time_ : last_bgsave_time_; +} + void Server::GetStatsInfo(std::string *info) { std::ostringstream string_stream; string_stream << "# Stats\r\n"; @@ -1078,7 +1083,7 @@ void Server::GetInfo(const std::string &ns, const std::string §ion, std::str std::lock_guard lg(db_job_mu_); string_stream << "bgsave_in_progress:" << (is_bgsave_in_progress_ ? 1 : 0) << "\r\n"; - string_stream << "last_bgsave_time:" << last_bgsave_time_ << "\r\n"; + string_stream << "last_bgsave_time:" << (last_bgsave_time_ == -1 ? start_time_ : last_bgsave_time_) << "\r\n"; string_stream << "last_bgsave_status:" << last_bgsave_status_ << "\r\n"; string_stream << "last_bgsave_time_sec:" << last_bgsave_time_sec_ << "\r\n"; } diff --git a/src/server/server.h b/src/server/server.h index 7ac6128a0cc..a219b737595 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -211,6 +211,7 @@ class Server { void SetLastRandomKeyCursor(const std::string &cursor); static int GetCachedUnixTime(); + int GetLastBgsaveTime(); void GetStatsInfo(std::string *info); void GetServerInfo(std::string *info); void GetMemoryInfo(std::string *info); From 801102c697b672e6e75127120f111de72a3986d4 Mon Sep 17 00:00:00 2001 From: xq2010 <122602529+xq2010@users.noreply.github.com> Date: Sat, 26 Aug 2023 18:23:25 +0800 Subject: [PATCH 2/5] Update src/server/server.h Co-authored-by: Twice --- src/server/server.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server/server.h b/src/server/server.h index a219b737595..a9ef72ad524 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -211,7 +211,7 @@ class Server { void SetLastRandomKeyCursor(const std::string &cursor); static int GetCachedUnixTime(); - int GetLastBgsaveTime(); + std::int64_t GetLastBgsaveTime(); void GetStatsInfo(std::string *info); void GetServerInfo(std::string *info); void GetMemoryInfo(std::string *info); From 5604857b7533ac807cb0fe7b079f7f2bbc01fe65 Mon Sep 17 00:00:00 2001 From: Binbin Date: Sun, 27 Aug 2023 16:14:52 +0800 Subject: [PATCH 3/5] change int to std::int64_t --- src/commands/cmd_server.cc | 2 +- src/server/server.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/cmd_server.cc b/src/commands/cmd_server.cc index 083c0bb2f80..8ea501b7ed1 100644 --- a/src/commands/cmd_server.cc +++ b/src/commands/cmd_server.cc @@ -985,7 +985,7 @@ class CommandLastSave : public Commander { return {Status::RedisExecErr, errAdminPermissionRequired}; } - int unix_sec = svr->GetLastBgsaveTime(); + std::int64_t unix_sec = svr->GetLastBgsaveTime(); *output = redis::Integer(unix_sec); return Status::OK(); } diff --git a/src/server/server.cc b/src/server/server.cc index 8250cf65711..22d7f97b6b2 100644 --- a/src/server/server.cc +++ b/src/server/server.cc @@ -990,7 +990,7 @@ int Server::GetCachedUnixTime() { return unix_time.load(); } -int Server::GetLastBgsaveTime() { +std::int64_t Server::GetLastBgsaveTime() { std::lock_guard lg(db_job_mu_); return last_bgsave_time_ == -1 ? start_time_ : last_bgsave_time_; } From c609e564ea3a5fdbf111e1d9e8168a2ab4802a52 Mon Sep 17 00:00:00 2001 From: xq2010 Date: Mon, 28 Aug 2023 11:51:52 +0800 Subject: [PATCH 4/5] Fix TestInfo error: remove the last_bgsave_time check because last_bgsave_time is changed to start_time_ at server startup. --- tests/gocase/unit/info/info_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/gocase/unit/info/info_test.go b/tests/gocase/unit/info/info_test.go index b0078a59bed..136be197bc3 100644 --- a/tests/gocase/unit/info/info_test.go +++ b/tests/gocase/unit/info/info_test.go @@ -72,7 +72,6 @@ func TestInfo(t *testing.T) { t.Run("get bgsave information by INFO", func(t *testing.T) { require.Equal(t, "0", util.FindInfoEntry(rdb, "bgsave_in_progress", "persistence")) - require.Equal(t, "-1", util.FindInfoEntry(rdb, "last_bgsave_time", "persistence")) require.Equal(t, "ok", util.FindInfoEntry(rdb, "last_bgsave_status", "persistence")) require.Equal(t, "-1", util.FindInfoEntry(rdb, "last_bgsave_time_sec", "persistence")) From ad953ee4cc0fc08e2623a758cabb7e3d0f948eea Mon Sep 17 00:00:00 2001 From: xq2010 Date: Mon, 28 Aug 2023 16:41:55 +0800 Subject: [PATCH 5/5] Remove the prefix 'std::' from int64_t --- src/commands/cmd_server.cc | 3 ++- src/server/server.cc | 2 +- src/server/server.h | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/commands/cmd_server.cc b/src/commands/cmd_server.cc index b69e0a6051c..d9b972de17c 100644 --- a/src/commands/cmd_server.cc +++ b/src/commands/cmd_server.cc @@ -987,11 +987,12 @@ class CommandLastSave : public Commander { return {Status::RedisExecErr, errAdminPermissionRequired}; } - std::int64_t unix_sec = svr->GetLastBgsaveTime(); + int64_t unix_sec = svr->GetLastBgsaveTime(); *output = redis::Integer(unix_sec); return Status::OK(); } }; + class CommandRestore : public Commander { public: Status Parse(const std::vector &args) override { diff --git a/src/server/server.cc b/src/server/server.cc index 25044b0f540..3b583cbed1d 100644 --- a/src/server/server.cc +++ b/src/server/server.cc @@ -986,7 +986,7 @@ int64_t Server::GetCachedUnixTime() { return unix_time.load(); } -std::int64_t Server::GetLastBgsaveTime() { +int64_t Server::GetLastBgsaveTime() { std::lock_guard lg(db_job_mu_); return last_bgsave_time_ == -1 ? start_time_ : last_bgsave_time_; } diff --git a/src/server/server.h b/src/server/server.h index 13ed83b78e2..953cbbbcd78 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -211,7 +211,7 @@ class Server { void SetLastRandomKeyCursor(const std::string &cursor); static int64_t GetCachedUnixTime(); - std::int64_t GetLastBgsaveTime(); + int64_t GetLastBgsaveTime(); void GetStatsInfo(std::string *info); void GetServerInfo(std::string *info); void GetMemoryInfo(std::string *info);