Skip to content

Commit

Permalink
feat:update lastsave cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
hero-heng committed Nov 26, 2023
1 parent 76b0fe5 commit 0b2ffd0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
14 changes: 12 additions & 2 deletions include/pika_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,16 @@ class Cmd : public std::enable_shared_from_this<Cmd> {

virtual void DoBinlog(const std::shared_ptr<SyncMasterSlot>& slot);

// 获取最后一次成功保存到磁盘的时间戳
static __time_t GetLastSave() {
return lastsave_;
}

// 在 pika_slot.cc 中更新时间戳的值
static void UpdateLastSave() {
lastsave_ = time(nullptr);
}

protected:
// enable copy, used default copy
// Cmd(const Cmd&);
Expand All @@ -537,12 +547,12 @@ class Cmd : public std::enable_shared_from_this<Cmd> {
CmdStage stage_ = kNone;
uint64_t do_duration_ = 0;

struct timeval bgSaveTimeTv;

private:
virtual void DoInitial() = 0;
virtual void Clear(){};

static __time_t lastsave_;

Cmd& operator=(const Cmd&);
};

Expand Down
13 changes: 2 additions & 11 deletions src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,6 @@ void BgsaveCmd::Do(std::shared_ptr<Slot> slot) {
g_pika_server->DoSameThingSpecificDB(TaskType::kBgSave, bgsave_dbs_);
LogCommand();
res_.AppendContent("+Background saving started");
memset(&bgSaveTimeTv,0,sizeof(struct timeval));
if (gettimeofday(&bgSaveTimeTv, nullptr) != 0) {
res_.SetRes(CmdRes::kErrOther, strerror(errno));
}
}

void CompactCmd::DoInitial() {
Expand Down Expand Up @@ -2425,13 +2421,8 @@ void LastsaveCmd::DoInitial() {
}

void LastsaveCmd::Do(std::shared_ptr<Slot> slot) {
struct timeval tv;
if (gettimeofday(&tv, nullptr) == 0) {
__time_t relativeTime = tv.tv_sec - bgSaveTimeTv.tv_sec;
res_.AppendInteger(static_cast<int64_t>(relativeTime));
} else {
res_.SetRes(CmdRes::kErrOther, strerror(errno));
}
__time_t lastsave_time = Cmd::GetLastSave();
res_.AppendInteger(static_cast<int64_t>(lastsave_time));
}

void DelbackupCmd::DoInitial() {
Expand Down
2 changes: 2 additions & 0 deletions src/pika_command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ extern PikaServer* g_pika_server;
extern std::unique_ptr<PikaReplicaManager> g_pika_rm;
extern std::unique_ptr<PikaCmdTableManager> g_pika_cmd_table_manager;

__time_t Cmd::lastsave_ = 0;

void InitCmdTable(CmdTable* cmd_table) {
// Admin
////Slaveof
Expand Down
3 changes: 2 additions & 1 deletion src/pika_slot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "include/pika_rm.h"
#include "include/pika_server.h"
#include "include/pika_slot.h"
#include "include/pika_command.h"

#include "pstd/include/mutex_impl.h"
#include "pstd/include/pstd_hash.h"
Expand Down Expand Up @@ -378,7 +379,7 @@ bool Slot::RunBgsaveEngine() {
return false;
}
LOG(INFO) << slot_name_ << " create new backup finished.";

Cmd::UpdateLastSave();
return true;
}

Expand Down

0 comments on commit 0b2ffd0

Please sign in to comment.