Skip to content

Commit

Permalink
1, add option when pika start up\n2, add dump prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
CatKang committed Apr 15, 2016
1 parent 09dd9f4 commit e353b7e
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 18 deletions.
6 changes: 6 additions & 0 deletions include/pika_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class PikaConf : public slash::BaseConf {

std::string requirepass() { RWLock l(&rwlock_, false); return requirepass_; }
std::string bgsave_path() { RWLock l(&rwlock_, false); return bgsave_path_; }
std::string bgsave_prefix() { RWLock l(&rwlock_, false); return bgsave_prefix_; }
std::string userpass() { RWLock l(&rwlock_, false); return userpass_; }
const std::string suser_blacklist() {
RWLock l(&rwlock_, false);
Expand Down Expand Up @@ -66,6 +67,10 @@ class PikaConf : public slash::BaseConf {
bgsave_path_ += "/";
}
}
void SetBgsavePrefix(const std::string &value) {
RWLock l(&rwlock_, true);
bgsave_prefix_ = value;
}
void SetRequirePass(const std::string &value) {
RWLock l(&rwlock_, true);
requirepass_ = value;
Expand Down Expand Up @@ -124,6 +129,7 @@ class PikaConf : public slash::BaseConf {
std::string userpass_;
std::vector<std::string> user_blacklist_;
std::string bgsave_path_;
std::string bgsave_prefix_;
std::string pidfile_;

//char pidfile_[PIKA_WORD_SIZE];
Expand Down
2 changes: 1 addition & 1 deletion include/pika_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class PikaServer
BGSaveInfo bgsave_info_;

static void DoBgsave(void* arg);
bool InitBgsaveEnv(const std::string& bgsave_path);
bool InitBgsaveEnv();
bool InitBgsaveEngine();


Expand Down
44 changes: 43 additions & 1 deletion src/pika.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,52 @@ static void PikaSignalSetup() {
signal(SIGQUIT, &IntSigHandle);
}

static void usage()
{
fprintf(stderr,
"Pika module %s\n"
"usage: pika [-hv] [-c conf/file]\n"
"\t-h -- show this help\n"
"\t-c conf/file -- config file \n"
" example: ./output/bin/pika -c ./conf/pika.conf\n",
kPikaVersion.c_str()
);
}

int main(int argc, char *argv[]) {
if (argc < 2) {
usage();
exit(-1);
}

bool path_opt = false;
char c;
char path[1024];
while (-1 != (c = getopt(argc, argv, "c:hv"))) {
switch (c) {
case 'c':
snprintf(path, 1024, "%s", optarg);
path_opt = true;
break;
case 'h':
usage();
return 0;
case 'v':
version();
return 0;
default:
usage();
return 0;
}
}

if (path_opt == false) {
fprintf (stderr, "Please specify the conf file path\n" );
usage();
exit(-1);
}

PikaConfInit(argv[1]);
PikaConfInit(path);

// daemonize if needed
if (g_pika_conf->daemonize()) {
Expand Down
22 changes: 11 additions & 11 deletions src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -671,10 +671,10 @@ void ConfigCmd::ConfigGet(std::string &ret) {
ret = "*2\r\n";
EncodeString(&ret, "userblacklist");
EncodeString(&ret, (g_pika_conf->suser_blacklist()).c_str());
// } else if (get_item == "dump_prefix") {
// ret = "*2\r\n";
// EncodeString(&ret, "dump_prefix");
// EncodeString(&ret, g_pika_conf->dump_prefix());
} else if (get_item == "dump_prefix") {
ret = "*2\r\n";
EncodeString(&ret, "dump_prefix");
EncodeString(&ret, g_pika_conf->bgsave_prefix());
} else if (get_item == "daemonize") {
ret = "*2\r\n";
EncodeString(&ret, "daemonize");
Expand Down Expand Up @@ -728,7 +728,7 @@ void ConfigCmd::ConfigGet(std::string &ret) {
EncodeString(&ret, "no");
}
} else if (get_item == "*") {
ret = "*25\r\n";
ret = "*26\r\n";
EncodeString(&ret, "port");
EncodeString(&ret, "thread_num");
EncodeString(&ret, "log_path");
Expand All @@ -740,9 +740,9 @@ void ConfigCmd::ConfigGet(std::string &ret) {
EncodeString(&ret, "requirepass");
EncodeString(&ret, "userpass");
EncodeString(&ret, "userblacklist");
//EncodeString(&ret, "dump_prefix");
EncodeString(&ret, "daemonize");
EncodeString(&ret, "dump_path");
EncodeString(&ret, "dump_prefix");
EncodeString(&ret, "pidfile");
EncodeString(&ret, "maxconnection");
EncodeString(&ret, "target_file_size_base");
Expand All @@ -763,13 +763,13 @@ void ConfigCmd::ConfigGet(std::string &ret) {
void ConfigCmd::ConfigSet(std::string& ret) {
std::string set_item = config_args_v_[1];
if (set_item == "*") {
ret = "*12\r\n";
ret = "*13\r\n";
EncodeString(&ret, "log_level");
EncodeString(&ret, "timeout");
EncodeString(&ret, "requirepass");
EncodeString(&ret, "userpass");
EncodeString(&ret, "userblacklist");
//EncodeString(&ret, "dump_prefix");
EncodeString(&ret, "dump_prefix");
EncodeString(&ret, "maxconnection");
EncodeString(&ret, "expire_logs_days");
EncodeString(&ret, "expire_logs_nums");
Expand Down Expand Up @@ -805,9 +805,9 @@ void ConfigCmd::ConfigSet(std::string& ret) {
} else if (set_item == "userblacklist") {
g_pika_conf->SetUserBlackList(value);
ret = "+OK\r\n";
// } else if (set_item == "dump_prefix") {
// g_pika_conf->SetDumpPrefix(value);
// ret = "+OK\r\n";
} else if (set_item == "dump_prefix") {
g_pika_conf->SetBgsavePrefix(value);
ret = "+OK\r\n";
} else if (set_item == "maxconnection") {
if (!slash::string2l(value.data(), value.size(), &ival)) {
ret = "-ERR Invalid argument " + value + " for CONFIG SET 'maxconnection'\r\n";
Expand Down
3 changes: 3 additions & 0 deletions src/pika_conf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ int PikaConf::Load()
if (bgsave_path_[bgsave_path_.length() - 1] != '/') {
bgsave_path_ += "/";
}
GetConfStr("dump_prefix", &bgsave_prefix_);

GetConfInt("expire_logs_nums", &expire_logs_nums_);
if (expire_logs_nums_ <= 10 ) {
expire_logs_nums_ = 10;
Expand Down Expand Up @@ -120,6 +122,7 @@ int PikaConf::ConfigRewrite() {
SetConfStr("userpass", userpass_);
SetConfStr("userblacklist", suser_blacklist());
SetConfStr("dump_path", bgsave_path_);
SetConfStr("dump_prefix", bgsave_prefix_);
SetConfInt("maxconnection", maxconnection_);
SetConfInt("root_connection_num", root_connection_num_);
SetConfInt("slowlog_log_slower_than", slowlog_log_slower_than_);
Expand Down
9 changes: 5 additions & 4 deletions src/pika_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,14 @@ Status PikaServer::AddBinlogSender(SlaveItem &slave, uint32_t filenum, uint64_t
}

// Prepare engine, need bgsave_protector protect
bool PikaServer::InitBgsaveEnv(const std::string& bgsave_path) {
bool PikaServer::InitBgsaveEnv() {
// 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);
std::string bgsave_path(g_pika_conf->bgsave_path());
bgsave_info_.path = bgsave_path + g_pika_conf->bgsave_prefix() + 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";
Expand Down Expand Up @@ -604,9 +605,9 @@ void PikaServer::Bgsave() {
return;
}
bgsave_info_.bgsaving = true;

// Prepare for Bgsaving
if (!InitBgsaveEnv(g_pika_conf->bgsave_path())
if (!InitBgsaveEnv()
|| !InitBgsaveEngine()) {
ClearBgsave();
return;
Expand Down
2 changes: 1 addition & 1 deletion third/slash
Submodule slash updated from b29fb8 to d24fe9

0 comments on commit e353b7e

Please sign in to comment.