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

Support delbackup command #133

Merged
merged 3 commits into from
Jul 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/pika_admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions include/pika_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion include/pika_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
43 changes: 43 additions & 0 deletions src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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;
Expand Down
4 changes: 4 additions & 0 deletions src/pika_command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ void InitCmdInfoTable() {
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameDbsize, dbsizeptr));
CmdInfo* timeptr = new CmdInfo(kCmdNameTime, 1, kCmdFlagsRead | kCmdFlagsAdmin);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameTime, timeptr));
CmdInfo* delbackupptr = new CmdInfo(kCmdNameDelbackup, 1, kCmdFlagsRead | kCmdFlagsAdmin);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameDelbackup, delbackupptr));
#ifdef TCMALLOC_EXTENSION
CmdInfo* tcmallocptr = new CmdInfo(kCmdNameTcmalloc, -2, kCmdFlagsRead | kCmdFlagsAdmin);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameTcmalloc, tcmallocptr));
Expand Down Expand Up @@ -469,6 +471,8 @@ void InitCmdTable(std::unordered_map<std::string, Cmd*> *cmd_table) {
cmd_table->insert(std::pair<std::string, Cmd*>(kCmdNameDbsize, dbsizeptr));
Cmd* timeptr = new TimeCmd();
cmd_table->insert(std::pair<std::string, Cmd*>(kCmdNameTime, timeptr));
Cmd* delbackupptr = new DelbackupCmd();
cmd_table->insert(std::pair<std::string, Cmd*>(kCmdNameDelbackup, delbackupptr));
#ifdef TCMALLOC_EXTENSION
Cmd* tcmallocptr = new TcmallocCmd();
cmd_table->insert(std::pair<std::string, Cmd*>(kCmdNameTcmalloc, tcmallocptr));
Expand Down
6 changes: 3 additions & 3 deletions src/pika_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down