Skip to content

Commit

Permalink
add info command(2)
Browse files Browse the repository at this point in the history
  • Loading branch information
JacketWoo committed Mar 24, 2016
2 parents 90e2dd6 + 40144a9 commit bff2aa5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 31 deletions.
19 changes: 13 additions & 6 deletions include/pika_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,32 +138,39 @@ 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_;
}
bool bgsaving() {
return bgsaving_;
slash::MutexLock l(&bgsave_protector_);
return bgsave_info_.bgsaving;
}
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 @@ -259,7 +266,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
4 changes: 2 additions & 2 deletions src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,8 @@ void InfoCmd::InfoServer(std::string &info) {
tmp_stream << "log_size: " << (slash::Du(g_pika_conf->log_path()) >> 20) << "M\r\n";
tmp_stream << "compression: " << g_pika_conf->compression() << "\r\n";
tmp_stream << "safety purge: " << (g_pika_server->GetPurgeWindow(purge_max) ? static_cast<int32_t>(purge_max) : -1) << "\r\n";
tmp_stream << "expire_logs_days: " << "\r\n";
tmp_stream << "expire_logs_num: " << "\r\n"; //TODO
tmp_stream << "expire_logs_days: " << g_pika_conf->expire_logs_days() << "\r\n";
tmp_stream << "expire_logs_nums: " << g_pika_conf->expire_logs_nums() << "\r\n";

info.append(tmp_stream.str());
}
Expand Down
64 changes: 41 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),
accumulative_connections_(0) {

Expand Down Expand Up @@ -349,20 +348,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 @@ -375,6 +375,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 @@ -399,14 +400,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 @@ -419,16 +420,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 @@ -440,10 +444,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 @@ -464,13 +468,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 @@ -510,7 +519,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 @@ -621,8 +630,17 @@ void PikaServer::AutoPurge() {
}

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

0 comments on commit bff2aa5

Please sign in to comment.