Skip to content

Commit

Permalink
Add support for LASTSAVE command (#1702)
Browse files Browse the repository at this point in the history
Co-authored-by: xuqing <xuqing@cyou-inc.com>
Co-authored-by: Twice <twice@apache.org>
Co-authored-by: Binbin <binloveplay1314@qq.com>
  • Loading branch information
4 people authored Aug 28, 2023
1 parent f69982e commit a64cabf
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/commands/cmd_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,19 @@ static uint64_t GenerateConfigFlag(const std::vector<std::string> &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};
}

int64_t unix_sec = svr->GetLastBgsaveTime();
*output = redis::Integer(unix_sec);
return Status::OK();
}
};

class CommandRestore : public Commander {
public:
Status Parse(const std::vector<std::string> &args) override {
Expand Down Expand Up @@ -1080,6 +1093,7 @@ REDIS_REGISTER_COMMANDS(MakeCmdAttr<CommandAuth>("auth", 2, "read-only ok-loadin

MakeCmdAttr<CommandCompact>("compact", 1, "read-only no-script", 0, 0, 0),
MakeCmdAttr<CommandBGSave>("bgsave", 1, "read-only no-script", 0, 0, 0),
MakeCmdAttr<CommandLastSave>("lastsave", 1, "read-only no-script", 0, 0, 0),
MakeCmdAttr<CommandFlushBackup>("flushbackup", 1, "read-only no-script", 0, 0, 0),
MakeCmdAttr<CommandSlaveOf>("slaveof", 3, "read-only exclusive no-script", 0, 0, 0),
MakeCmdAttr<CommandStats>("stats", 1, "read-only", 0, 0, 0), )
Expand Down
7 changes: 6 additions & 1 deletion src/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,11 @@ int64_t Server::GetCachedUnixTime() {
return unix_time.load();
}

int64_t Server::GetLastBgsaveTime() {
std::lock_guard<std::mutex> 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";
Expand Down Expand Up @@ -1074,7 +1079,7 @@ void Server::GetInfo(const std::string &ns, const std::string &section, std::str

std::lock_guard<std::mutex> 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";
}
Expand Down
1 change: 1 addition & 0 deletions src/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class Server {
void SetLastRandomKeyCursor(const std::string &cursor);

static int64_t GetCachedUnixTime();
int64_t GetLastBgsaveTime();
void GetStatsInfo(std::string *info);
void GetServerInfo(std::string *info);
void GetMemoryInfo(std::string *info);
Expand Down
1 change: 0 additions & 1 deletion tests/gocase/unit/info/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down

0 comments on commit a64cabf

Please sign in to comment.