Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LASTSAVE command #1702

Merged
merged 9 commits into from
Aug 28, 2023
14 changes: 14 additions & 0 deletions src/commands/cmd_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,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};
}

int unix_sec = svr->GetLastBgsaveTime();
enjoy-binbin marked this conversation as resolved.
Show resolved Hide resolved
*output = redis::Integer(unix_sec);
return Status::OK();
}
};

REDIS_REGISTER_COMMANDS(MakeCmdAttr<CommandAuth>("auth", 2, "read-only ok-loading", 0, 0, 0),
MakeCmdAttr<CommandPing>("ping", -1, "read-only", 0, 0, 0),
MakeCmdAttr<CommandSelect>("select", 2, "read-only", 0, 0, 0),
Expand Down Expand Up @@ -1007,6 +1020,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 @@ -990,6 +990,11 @@ int Server::GetCachedUnixTime() {
return unix_time.load();
}

int Server::GetLastBgsaveTime() {
enjoy-binbin marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -1078,7 +1083,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 int GetCachedUnixTime();
int GetLastBgsaveTime();
xq2010 marked this conversation as resolved.
Show resolved Hide resolved
void GetStatsInfo(std::string *info);
void GetServerInfo(std::string *info);
void GetMemoryInfo(std::string *info);
Expand Down