diff --git a/include/pika_admin.h b/include/pika_admin.h index 1a722fbe65..a5cad43eef 100644 --- a/include/pika_admin.h +++ b/include/pika_admin.h @@ -237,6 +237,15 @@ class TimeCmd : public Cmd { virtual void DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info); }; +class DelbackupCmd : public Cmd { +public: + DelbackupCmd() { + } + virtual void Do(); +private: + virtual void DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info); +}; + #ifdef TCMALLOC_EXTENSION class TcmallocCmd : public Cmd { public: diff --git a/include/pika_command.h b/include/pika_command.h index 5100ddaf23..631fd043b5 100644 --- a/include/pika_command.h +++ b/include/pika_command.h @@ -35,6 +35,7 @@ const std::string kCmdNameConfig = "config"; const std::string kCmdNameMonitor = "monitor"; const std::string kCmdNameDbsize = "dbsize"; const std::string kCmdNameTime = "time"; +const std::string kCmdNameDelbackup = "delbackup"; #ifdef TCMALLOC_EXTENSION const std::string kCmdNameTcmalloc = "tcmalloc"; #endif diff --git a/include/pika_server.h b/include/pika_server.h index 1a09f067f3..03c147c635 100644 --- a/include/pika_server.h +++ b/include/pika_server.h @@ -254,7 +254,11 @@ class PikaServer { }; void DBSyncSendFile(const std::string& ip, int port); bool ChangeDb(const std::string& new_path); - + int CountSyncSlaves() { + slash::MutexLock ldb(&db_sync_protector_); + return db_sync_slaves_.size(); + } + slash::Mutex & GetSlavesMutex() { return db_sync_protector_; } //flushall bool FlushAll(); diff --git a/src/pika_admin.cc b/src/pika_admin.cc index 6eb704cfc2..d9080011a1 100644 --- a/src/pika_admin.cc +++ b/src/pika_admin.cc @@ -1212,6 +1212,49 @@ void TimeCmd::Do() { } } +void DelbackupCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) { + (void)ptr_info; + if (argv.size() != 1) { + res_.SetRes(CmdRes::kWrongNum, kCmdNameDelbackup); + return; + } +} + +void DelbackupCmd::Do() { + std::string db_sync_path = g_pika_conf->bgsave_path(); + std::vector dump_dir; + + // Dump file is not exist + if (!slash::FileExists(db_sync_path)) { + res_.AppendString(db_sync_path); + return; + } + // Directory traversal + if (slash::GetChildren(db_sync_path, dump_dir) != 0) { + res_.SetRes(CmdRes::kOk); + return; + } + + int len = dump_dir.size(); + for (size_t i = 0; i < dump_dir.size(); i++) { + if (g_pika_server->CountSyncSlaves() == 0) { + LOG(INFO) << "Delete dump file: " << db_sync_path + dump_dir[i]; + slash::DeleteDirIfExist(db_sync_path + dump_dir[i]); + len--; + } else if (g_pika_server->bgsave_info().path != dump_dir[i]){ + LOG(INFO) << "Delete dump file: " << db_sync_path + dump_dir[i]; + slash::DeleteDirIfExist(db_sync_path + dump_dir[i]); + len--; + } + } + if (len == 0) { + g_pika_server->bgsave_info().Clear(); + } + + res_.SetRes(CmdRes::kOk); + return; +} + #ifdef TCMALLOC_EXTENSION void TcmallocCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) { (void)ptr_info; diff --git a/src/pika_command.cc b/src/pika_command.cc index c8221a1ee7..4ace72e5ef 100644 --- a/src/pika_command.cc +++ b/src/pika_command.cc @@ -57,6 +57,8 @@ void InitCmdInfoTable() { cmd_infos.insert(std::pair(kCmdNameDbsize, dbsizeptr)); CmdInfo* timeptr = new CmdInfo(kCmdNameTime, 1, kCmdFlagsRead | kCmdFlagsAdmin); cmd_infos.insert(std::pair(kCmdNameTime, timeptr)); + CmdInfo* delbackupptr = new CmdInfo(kCmdNameDelbackup, 1, kCmdFlagsRead | kCmdFlagsAdmin); + cmd_infos.insert(std::pair(kCmdNameDelbackup, delbackupptr)); #ifdef TCMALLOC_EXTENSION CmdInfo* tcmallocptr = new CmdInfo(kCmdNameTcmalloc, -2, kCmdFlagsRead | kCmdFlagsAdmin); cmd_infos.insert(std::pair(kCmdNameTcmalloc, tcmallocptr)); @@ -469,6 +471,8 @@ void InitCmdTable(std::unordered_map *cmd_table) { cmd_table->insert(std::pair(kCmdNameDbsize, dbsizeptr)); Cmd* timeptr = new TimeCmd(); cmd_table->insert(std::pair(kCmdNameTime, timeptr)); + Cmd* delbackupptr = new DelbackupCmd(); + cmd_table->insert(std::pair(kCmdNameDelbackup, delbackupptr)); #ifdef TCMALLOC_EXTENSION Cmd* tcmallocptr = new TcmallocCmd(); cmd_table->insert(std::pair(kCmdNameTcmalloc, tcmallocptr)); diff --git a/src/pika_server.cc b/src/pika_server.cc index 2bcb4c76c5..107722c1d8 100644 --- a/src/pika_server.cc +++ b/src/pika_server.cc @@ -703,6 +703,8 @@ void PikaServer::DBSyncSendFile(const std::string& ip, int port) { } // We need specify the speed limit for every single file ret = slash::RsyncSendFile(*it, target_path, remote); + + if (0 != ret) { LOG(WARNING) << "rsync send file failed! From: " << *it << ", To: " << target_path @@ -1181,9 +1183,7 @@ void PikaServer::AutoDeleteExpiredDump() { if (slash::GetChildren(db_sync_path, dump_dir) != 0) { return; } - for (size_t i = 0; i < dump_dir.size(); i++) { - LOG(INFO) << dump_dir[i]; - } + // Handle dump directory for (size_t i = 0; i < dump_dir.size(); i++) { // Parse filename