Skip to content

Commit

Permalink
add a new configuration item control whether to write binlog (OpenAto…
Browse files Browse the repository at this point in the history
  • Loading branch information
Axlgrep authored Aug 22, 2018
1 parent c63aefb commit eadc743
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 30 deletions.
12 changes: 7 additions & 5 deletions conf/pika.conf
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ db-sync-path : ./dbsync/
db-sync-speed : -1
# The slave priority
slave-priority : 100
# When it becomes slave, the type of binlog it receives from the master
# if this option is set to 'new', that means I will be a slave to Pika who's version 3.0
# if this opsion is set to 'old', that means I will be a slave to Pika who's version 2.3.3 ~ 2.3.5
# identify-binlog-type [new | old]
identify-binlog-type : new
# network interface
#network-interface : eth1
# replication
Expand All @@ -85,8 +80,15 @@ double-master-server-id :
###################
## Critical Settings
###################
# write_binlog [yes | no]
write-binlog : yes
# binlog file size: default is 100M, limited in [1K, 2G]
binlog-file-size : 104857600
# When it becomes slave, the type of binlog it receives from the master
# if this option is set to 'new', that means I will be a slave to Pika who's version 3.0
# if this opsion is set to 'old', that means I will be a slave to Pika who's version 2.3.3 ~ 2.3.5
# identify-binlog-type [new | old]
identify-binlog-type : new
# Compression
compression : snappy
# max-background-flushes: default is 1, limited in [1, 4]
Expand Down
6 changes: 6 additions & 0 deletions include/pika_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class PikaConf : public slash::BaseConf {
std::string double_master_sid() { RWLock l(&rwlock_, false); return double_master_sid_; }
std::string slaveof() {RWLock l(&rwlock_, false); return slaveof_;}
int slave_priority() {RWLock l(&rwlock_, false); return slave_priority_;}
bool write_binlog() {RWLock l(&rwlock_, false); return write_binlog_;}
std::string identify_binlog_type() {RWLock l(&rwlock_, false); return identify_binlog_type_;}
int thread_num() { RWLock l(&rwlock_, false); return thread_num_; }
int sync_thread_num() { RWLock l(&rwlock_, false); return sync_thread_num_; }
Expand Down Expand Up @@ -89,6 +90,10 @@ class PikaConf : public slash::BaseConf {
slaveof_ = value;
}
void SetSlavePriority(const int value) { RWLock l(&rwlock_, true); slave_priority_ = value; }
void SetWriteBinlog(const std::string& value) {
RWLock l(&rwlock_, true);
write_binlog_ = (value == "yes") ? true : false;
}
void SetIdentifyBinlogType(const std::string& value) {
RWLock l(&rwlock_, true);
identify_binlog_type_ = value;
Expand Down Expand Up @@ -223,6 +228,7 @@ class PikaConf : public slash::BaseConf {
//
// Critical configure items
//
bool write_binlog_;
int target_file_size_base_;
int binlog_file_size_;

Expand Down
61 changes: 40 additions & 21 deletions src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1051,10 +1051,18 @@ void ConfigCmd::ConfigGet(std::string &ret) {
ret = "*2\r\n";
EncodeString(&ret, "slowlog-log-slower-than");
EncodeInt32(&ret, g_pika_conf->slowlog_slower_than());
} else if (get_item == "write-binlog") {
ret = "*2\r\n";
EncodeString(&ret, "write-binlog");
EncodeString(&ret, g_pika_conf->write_binlog() ? "yes" : "no");
} else if (get_item == "binlog-file-size") {
ret = "*2\r\n";
EncodeString(&ret, "binlog-file-size");
EncodeInt32(&ret, g_pika_conf->binlog_file_size());
} else if (get_item == "identify-binlog-type") {
ret = "*2\r\n";
EncodeString(&ret, "identify-binlog-type");
EncodeString(&ret, g_pika_conf->identify_binlog_type());
} else if (get_item == "compression") {
ret = "*2\r\n";
EncodeString(&ret, "compression");
Expand All @@ -1075,12 +1083,8 @@ void ConfigCmd::ConfigGet(std::string &ret) {
ret = "*2\r\n";
EncodeString(&ret, "slave-priority");
EncodeInt32(&ret, g_pika_conf->slave_priority());
} else if (get_item == "identify-binlog-type") {
ret = "*2\r\n";
EncodeString(&ret, "identify-binlog-type");
EncodeString(&ret, g_pika_conf->identify_binlog_type());
} else if (get_item == "*") {
ret = "*86\r\n";
ret = "*88\r\n";
EncodeString(&ret, "port");
EncodeInt32(&ret, g_pika_conf->port());
EncodeString(&ret, "double-master-ip");
Expand Down Expand Up @@ -1147,8 +1151,12 @@ void ConfigCmd::ConfigGet(std::string &ret) {
EncodeInt32(&ret, g_pika_conf->slowlog_slower_than());
EncodeString(&ret, "slave-read-only");
EncodeInt32(&ret, g_pika_conf->readonly());
EncodeString(&ret, "write-binlog");
EncodeString(&ret, g_pika_conf->write_binlog() ? "yes" : "no");
EncodeString(&ret, "binlog-file-size");
EncodeInt32(&ret, g_pika_conf->binlog_file_size());
EncodeString(&ret, "identify-binlog-type");
EncodeString(&ret, g_pika_conf->identify_binlog_type());
EncodeString(&ret, "compression");
EncodeString(&ret, g_pika_conf->compression());
EncodeString(&ret, "db-sync-path");
Expand All @@ -1165,8 +1173,6 @@ void ConfigCmd::ConfigGet(std::string &ret) {
EncodeString(&ret, g_pika_conf->slaveof());
EncodeString(&ret, "slave-priority");
EncodeInt32(&ret, g_pika_conf->slave_priority());
EncodeString(&ret, "identify-binlog-type");
EncodeString(&ret, g_pika_conf->identify_binlog_type());
} else {
ret = "*0\r\n";
}
Expand All @@ -1175,7 +1181,7 @@ void ConfigCmd::ConfigGet(std::string &ret) {
void ConfigCmd::ConfigSet(std::string& ret) {
std::string set_item = config_args_v_[1];
if (set_item == "*") {
ret = "*19\r\n";
ret = "*20\r\n";
EncodeString(&ret, "loglevel");
EncodeString(&ret, "timeout");
EncodeString(&ret, "requirepass");
Expand All @@ -1190,11 +1196,12 @@ void ConfigCmd::ConfigSet(std::string& ret) {
EncodeString(&ret, "root-connection-num");
EncodeString(&ret, "slowlog-log-slower-than");
EncodeString(&ret, "slave-read-only");
EncodeString(&ret, "write-binlog");
EncodeString(&ret, "identify-binlog-type");
EncodeString(&ret, "db-sync-speed");
EncodeString(&ret, "compact-cron");
EncodeString(&ret, "compact-interval");
EncodeString(&ret, "slave-priority");
EncodeString(&ret, "identify-binlog-type");
return;
}
std::string value = config_args_v_[2];
Expand Down Expand Up @@ -1297,6 +1304,30 @@ void ConfigCmd::ConfigSet(std::string& ret) {
}
g_pika_conf->SetReadonly(is_readonly);
ret = "+OK\r\n";
} else if (set_item == "identify-binlog-type") {
int role = g_pika_server->role();
if (role == PIKA_ROLE_SLAVE || role == PIKA_ROLE_DOUBLE_MASTER) {
ret = "-ERR need to close master-slave or double-master mode first\r\n";
return;
} else if (value != "new" && value != "old") {
ret = "-ERR invalid identify-binlog-type (new or old)\r\n";
return;
} else {
g_pika_conf->SetIdentifyBinlogType(value);
ret = "+OK\r\n";
}
} else if (set_item == "write-binlog") {
int role = g_pika_server->role();
if (role == PIKA_ROLE_SLAVE || role == PIKA_ROLE_DOUBLE_MASTER) {
ret = "-ERR need to close master-slave or double-master mode first\r\n";
return;
} else if (value != "yes" && value != "no") {
ret = "-ERR invalid write-binlog (yes or no)\r\n";
return;
} else {
g_pika_conf->SetWriteBinlog(value);
ret = "+OK\r\n";
}
} else if (set_item == "db-sync-speed") {
if (!slash::string2l(value.data(), value.size(), &ival)) {
ret = "-ERR Invalid argument " + value + " for CONFIG SET 'db-sync-speed(MB)'\r\n";
Expand Down Expand Up @@ -1355,18 +1386,6 @@ void ConfigCmd::ConfigSet(std::string& ret) {
g_pika_conf->SetCompactInterval(value);
ret = "+OK\r\n";
}
} else if (set_item == "identify-binlog-type") {
int role = g_pika_server->role();
if (role == PIKA_ROLE_SLAVE || role == PIKA_ROLE_DOUBLE_MASTER) {
ret = "-ERR need to close master-slave or double-master mode first\r\n";
return;
} else if (value != "old" && value != "new") {
ret = "-ERR invalid identify-binlog-type\r\n";
return;
} else {
g_pika_conf->SetIdentifyBinlogType(value);
ret = "+OK\r\n";
}
} else {
ret = "-ERR No such configure item\r\n";
}
Expand Down
3 changes: 2 additions & 1 deletion src/pika_client_conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ std::string PikaClientConn::DoCmd(
uint32_t exec_time = time(nullptr);
c_ptr->Do();

if (cinfo_ptr->is_write()) {
if (g_pika_conf->write_binlog()
&& cinfo_ptr->is_write()) {
if (c_ptr->res().ok()) {
g_pika_server->logger_->Lock();
uint32_t filenum = 0;
Expand Down
13 changes: 10 additions & 3 deletions src/pika_conf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ int PikaConf::Load()
GetConfStr("compression", &compression_);
GetConfBool("slave-read-only", &readonly_);
GetConfInt("slave-priority", &slave_priority_);
GetConfStr("identify-binlog-type", &identify_binlog_type_);

//
// Immutable Sections
Expand Down Expand Up @@ -199,8 +198,15 @@ int PikaConf::Load()
std::string dmz;
GetConfStr("daemonize", &dmz);
daemonize_ = (dmz == "yes") ? true : false;

// binlog
std::string wb;
GetConfStr("write-binlog", &wb);
write_binlog_ = (wb == "yes") ? true : false;
GetConfStr("identify-binlog-type", &identify_binlog_type_);
GetConfInt("binlog-file-size", &binlog_file_size_);
if (binlog_file_size_ < 1024 || static_cast<int64_t>(binlog_file_size_) > (1024LL * 1024 * 1024)) {
if (binlog_file_size_ < 1024
|| static_cast<int64_t>(binlog_file_size_) > (1024LL * 1024 * 1024)) {
binlog_file_size_ = 100 * 1024 * 1024; // 100M
}
GetConfStr("pidfile", &pidfile_);
Expand Down Expand Up @@ -266,9 +272,10 @@ int PikaConf::ConfigRewrite() {
SetConfStr("network-interface", network_interface_);
SetConfStr("slaveof", slaveof_);
SetConfInt("slave-priority", slave_priority_);
SetConfStr("identify-binlog-type", identify_binlog_type_);

SetConfStr("write-binlog", write_binlog_ ? "yes" : "no");
SetConfInt("binlog-file-size", binlog_file_size_);
SetConfStr("identify-binlog-type", identify_binlog_type_);
SetConfStr("compression", compression_);
SetConfInt("max-background-flushes", max_background_flushes_);
SetConfInt("max-background-compactions", max_background_compactions_);
Expand Down
2 changes: 2 additions & 0 deletions src/pika_trysync_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ void* PikaTrysyncThread::ThreadMain() {
}

if (Send(lip) && RecvProc()) {
LOG(INFO) << "Open write-binlog mode";
g_pika_conf->SetWriteBinlog("yes");
g_pika_server->ConnectMasterDone();
// Stop rsync, binlog sync with master is begin
slash::StopRsync(dbsync_path);
Expand Down

0 comments on commit eadc743

Please sign in to comment.