From 0b2ffd098a40fb0d1b5c5392de6bf1fb0377b018 Mon Sep 17 00:00:00 2001 From: hero-heng <2374114527@qq.com> Date: Sun, 26 Nov 2023 13:11:05 +0800 Subject: [PATCH] feat:update lastsave cmd --- include/pika_command.h | 14 ++++++++++++-- src/pika_admin.cc | 13 ++----------- src/pika_command.cc | 2 ++ src/pika_slot.cc | 3 ++- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/include/pika_command.h b/include/pika_command.h index c6eddc5929..fff85226d1 100644 --- a/include/pika_command.h +++ b/include/pika_command.h @@ -512,6 +512,16 @@ class Cmd : public std::enable_shared_from_this { virtual void DoBinlog(const std::shared_ptr& 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&); @@ -537,12 +547,12 @@ class Cmd : public std::enable_shared_from_this { 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&); }; diff --git a/src/pika_admin.cc b/src/pika_admin.cc index ca0e73a6f9..113a136ccf 100644 --- a/src/pika_admin.cc +++ b/src/pika_admin.cc @@ -308,10 +308,6 @@ void BgsaveCmd::Do(std::shared_ptr 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() { @@ -2425,13 +2421,8 @@ void LastsaveCmd::DoInitial() { } void LastsaveCmd::Do(std::shared_ptr slot) { - struct timeval tv; - if (gettimeofday(&tv, nullptr) == 0) { - __time_t relativeTime = tv.tv_sec - bgSaveTimeTv.tv_sec; - res_.AppendInteger(static_cast(relativeTime)); - } else { - res_.SetRes(CmdRes::kErrOther, strerror(errno)); - } + __time_t lastsave_time = Cmd::GetLastSave(); + res_.AppendInteger(static_cast(lastsave_time)); } void DelbackupCmd::DoInitial() { diff --git a/src/pika_command.cc b/src/pika_command.cc index 0fb7e07f8c..3b6f0bb943 100644 --- a/src/pika_command.cc +++ b/src/pika_command.cc @@ -30,6 +30,8 @@ extern PikaServer* g_pika_server; extern std::unique_ptr g_pika_rm; extern std::unique_ptr g_pika_cmd_table_manager; +__time_t Cmd::lastsave_ = 0; + void InitCmdTable(CmdTable* cmd_table) { // Admin ////Slaveof diff --git a/src/pika_slot.cc b/src/pika_slot.cc index a9af0980bd..3c09d1c50b 100644 --- a/src/pika_slot.cc +++ b/src/pika_slot.cc @@ -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" @@ -378,7 +379,7 @@ bool Slot::RunBgsaveEngine() { return false; } LOG(INFO) << slot_name_ << " create new backup finished."; - + Cmd::UpdateLastSave(); return true; }