Skip to content

Commit

Permalink
add bgsave mutex protect bgsave_info
Browse files Browse the repository at this point in the history
  • Loading branch information
CatKang committed Mar 24, 2016
1 parent 7028219 commit ea2d7b4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
17 changes: 12 additions & 5 deletions include/pika_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,36 @@ class PikaServer
* BGSave used
*/
struct BGSaveInfo {
bool bgsaving;
time_t start_time;
std::string s_start_time;
std::string path;
std::string tmp_path;
uint32_t filenum;
uint64_t offset;
BGSaveInfo() : filenum(0), offset(0){}
BGSaveInfo() : bgsaving(false), filenum(0), offset(0){}
void Clear() {
bgsaving = false;
path.clear();
tmp_path.clear();
filenum = 0;
offset = 0;
}
};
const BGSaveInfo& bgsave_info() const {
BGSaveInfo bgsave_info() {
slash::MutexLock l(&bgsave_protector_);
return bgsave_info_;
}
slash::Mutex* bgsave_protector() {
return &bgsave_protector_;
}

void Bgsave();
bool Bgsaveoff();
bool RunBgsaveEngine();
bool RunBgsaveEngine(const std::string path);
// need bgsave_protector protect
void ClearBgsave() {
bgsave_info_.Clear();
bgsaving_ = false;
}

/*
Expand Down Expand Up @@ -207,7 +214,7 @@ class PikaServer
/*
* Bgsave use
*/
std::atomic<bool> bgsaving_;
slash::Mutex bgsave_protector_;
pink::BGThread bgsave_thread_;
nemo::BackupEngine *bgsave_engine_;
BGSaveInfo bgsave_info_;
Expand Down
58 changes: 35 additions & 23 deletions src/pika_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ PikaServer::PikaServer() :
master_port_(0),
repl_state_(PIKA_REPL_NO_CONNECT),
role_(PIKA_ROLE_SINGLE),
bgsaving_(false),
purging_(false) {

pthread_rwlock_init(&rwlock_, NULL);
Expand Down Expand Up @@ -332,20 +331,21 @@ Status PikaServer::AddBinlogSender(SlaveItem &slave, uint32_t filenum, uint64_t
}
}

// Prepare engine, need bgsave_protector protect
bool PikaServer::InitBgsaveEnv(const std::string& bgsave_path) {
// Prepare for bgsave dir
bgsave_info_.start_time = time(NULL);
char s_time[32];
int len = strftime(s_time, sizeof(s_time), "%Y%m%d%H%M%S", localtime(&bgsave_info_.start_time));
bgsave_info_.s_start_time.assign(s_time, len);
bgsave_info_.path = bgsave_path + std::string(s_time, 8);
bgsave_info_.tmp_path = bgsave_path + "tmp";
if (!slash::DeleteDirIfExist(bgsave_info_.path)) {
LOG(ERROR) << "remove exist bgsave dir failed";
return false;
}
slash::CreateDir(bgsave_info_.path);
// Prepare for tmp dir
bgsave_info_.tmp_path = bgsave_path + "tmp";
if (!slash::DeleteDirIfExist(bgsave_info_.tmp_path)) {
LOG(ERROR) << "remove exist tmp bgsave dir failed";
return false;
Expand All @@ -358,6 +358,7 @@ bool PikaServer::InitBgsaveEnv(const std::string& bgsave_path) {
return true;
}

// Prepare bgsave env, need bgsave_protector protect
bool PikaServer::InitBgsaveEngine() {
if (bgsave_engine_ != NULL) {
delete bgsave_engine_;
Expand All @@ -382,14 +383,14 @@ bool PikaServer::InitBgsaveEngine() {
return true;
}

bool PikaServer::RunBgsaveEngine() {
bool PikaServer::RunBgsaveEngine(const std::string path) {
// Backup to tmp dir
nemo::Status nemo_s = bgsave_engine_->CreateNewBackup(db().get());
LOG(INFO) << "create new backup finished.";
// Restore to bgsave dir
if (nemo_s.ok()) {
nemo_s = bgsave_engine_->RestoreDBFromBackup(
bgsave_engine_->GetLatestBackupID() + 1, bgsave_info_.path);
bgsave_engine_->GetLatestBackupID() + 1, path);
}
LOG(INFO) << "backup finished.";

Expand All @@ -402,16 +403,19 @@ bool PikaServer::RunBgsaveEngine() {

void PikaServer::Bgsave() {
// Only one thread can go through
bool expect = false;
if (!bgsaving_.compare_exchange_strong(expect, true)) {
return;
}
{
slash::MutexLock l(&bgsave_protector_);
if (bgsave_info_.bgsaving) {
return;
}
bgsave_info_.bgsaving = true;

// Prepare for Bgsaving
if (!InitBgsaveEnv(g_pika_conf->bgsave_path())
|| !InitBgsaveEngine()) {
ClearBgsave();
return;
// Prepare for Bgsaving
if (!InitBgsaveEnv(g_pika_conf->bgsave_path())
|| !InitBgsaveEngine()) {
ClearBgsave();
return;
}
}

// Start new thread if needed
Expand All @@ -423,10 +427,10 @@ void PikaServer::Bgsave() {

void PikaServer::DoBgsave(void* arg) {
PikaServer* p = static_cast<PikaServer*>(arg);
const BGSaveInfo& info = p->bgsave_info();
BGSaveInfo info = p->bgsave_info();

// Do bgsave
bool ok = p->RunBgsaveEngine();
bool ok = p->RunBgsaveEngine(info.path);

// Delete tmp
if (!slash::DeleteDirIfExist(info.tmp_path)) {
Expand All @@ -447,13 +451,18 @@ void PikaServer::DoBgsave(void* arg) {
std::string fail_path = info.path + "_FAILED";
slash::RenameFile(info.path.c_str(), fail_path.c_str());
}

p->ClearBgsave();
{
slash::MutexLock l(p->bgsave_protector());
p->ClearBgsave();
}
}

bool PikaServer::Bgsaveoff() {
if (!bgsaving_) {
return false;
{
slash::MutexLock l(&bgsave_protector_);
if (!bgsave_info_.bgsaving) {
return false;
}
}
if (bgsave_engine_ != NULL) {
bgsave_engine_->StopBackup();
Expand Down Expand Up @@ -493,7 +502,7 @@ bool PikaServer::PurgeLogs(uint32_t to) {
void PikaServer::DoPurgeLogs(void* arg) {
PurgeArg *ppurge = static_cast<PurgeArg*>(arg);
PikaServer* ps = ppurge->p;

ps->PurgeFiles(ppurge->to);

ps->ClearPurge();
Expand Down Expand Up @@ -604,8 +613,11 @@ void PikaServer::AutoPurge() {
}

bool PikaServer::FlushAll() {
if (bgsaving_ /*|| bgscaning_*/) {
return false;
{
slash::MutexLock l(&bgsave_protector_);
if (bgsave_info_.bgsaving/*|| bgscaning_*/) {
return false;
}
}
std::string dbpath = g_pika_conf->db_path();
if (dbpath[dbpath.length() - 1] == '/') {
Expand Down

0 comments on commit ea2d7b4

Please sign in to comment.