Skip to content

Commit

Permalink
replace read-only with slave-read-only (OpenAtomFoundation#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
Axlgrep authored Nov 17, 2018
1 parent 97823f6 commit 5913213
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 65 deletions.
2 changes: 1 addition & 1 deletion conf/pika.conf
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ slowlog-log-slower-than : 10000
# Slowlog-max-len
slowlog-max-len : 128
# slave-read-only(yes/no, 1/0)
slave-read-only : 0
slave-read-only : yes
# Pika db sync path
db-sync-path : ./dbsync/
# db sync speed(MB) max is set to 125MB, min is set to 0, and if below 0 or above 125, the value will be adjust to 125
Expand Down
10 changes: 0 additions & 10 deletions include/pika_admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,6 @@ class FlushdbCmd : public Cmd {
}
};

class ReadonlyCmd : public Cmd {
public:
ReadonlyCmd() : is_open_(false) {}
virtual void Do();

private:
bool is_open_;
virtual void DoInitial(PikaCmdArgsType &argvs, const CmdInfo* const ptr_info);
};

class ClientCmd : public Cmd {
public:
ClientCmd() {}
Expand Down
1 change: 0 additions & 1 deletion include/pika_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const std::string kCmdNamePing = "ping";
const std::string kCmdNameSelect = "select";
const std::string kCmdNameFlushall = "flushall";
const std::string kCmdNameFlushdb = "flushdb";
const std::string kCmdNameReadonly = "readonly";
const std::string kCmdNameClient = "client";
const std::string kCmdNameShutdown = "shutdown";
const std::string kCmdNameInfo = "info";
Expand Down
8 changes: 4 additions & 4 deletions include/pika_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class PikaConf : public slash::BaseConf {
int expire_logs_nums() { RWLock l(&rwlock_, false); return expire_logs_nums_; }
int expire_logs_days() { RWLock l(&rwlock_, false); return expire_logs_days_; }
std::string conf_path() { RWLock l(&rwlock_, false); return conf_path_; }
bool readonly() { RWLock l(&rwlock_, false); return readonly_; }
bool slave_read_only() { RWLock l(&rwlock_, false); return slave_read_only_; }
int maxclients() { RWLock l(&rwlock_, false); return maxclients_; }
int root_connection_num() { RWLock l(&rwlock_, false); return root_connection_num_; }
bool slowlog_write_errorlog() { RWLock l(&rwlock_, false); return slowlog_write_errorlog_;}
Expand Down Expand Up @@ -146,8 +146,8 @@ class PikaConf : public slash::BaseConf {
slash::StringToLower(item);
}
}
void SetReadonly(const bool value) {
RWLock l(&rwlock_, true); readonly_ = value;
void SetSlaveReadOnly(const bool value) {
RWLock l(&rwlock_, true); slave_read_only_ = value;
}
void SetExpireLogsNums(const int value){
RWLock l(&rwlock_, true);
Expand Down Expand Up @@ -234,7 +234,7 @@ class PikaConf : public slash::BaseConf {
int slowlog_max_len_;
int expire_logs_days_;
int expire_logs_nums_;
bool readonly_;
bool slave_read_only_;
std::string conf_path_;
int max_cache_statistic_keys_;
int small_compaction_threshold_;
Expand Down
10 changes: 10 additions & 0 deletions include/pika_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ class PikaServer {
slash::RWLock(&state_protector_, false);
return role_;
}

bool readonly() {
slash::RWLock(&state_protector_, false);
if ((role_ & PIKA_ROLE_SLAVE)
&& g_pika_conf->slave_read_only()) {
return true;
}
return false;
}

int repl_state() {
slash::RWLock(&state_protector_, false);
return repl_state_;
Expand Down
39 changes: 6 additions & 33 deletions src/pika_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ void SlaveofCmd::Do() {
if (g_pika_server->IsDoubleMaster(master_ip_, master_port_)) {
g_pika_server->PurgeLogs(0, true, true);
g_pika_server->SetForceFullSync(true);
g_pika_conf->SetReadonly(true);
} else {
res_.SetRes(CmdRes::kErrOther, "In double master mode, can not set other server as the peer-master");
return;
Expand Down Expand Up @@ -199,7 +198,7 @@ void TrysyncCmd::Do() {
res_.AppendString(kInnerReplWait);
} else {
LOG(WARNING) << "slave offset is larger than mine, slave ip: " << slave_ip_
<< "slave port:" << slave_port_
<< " slave port: " << slave_port_
<< " filenum: " << filenum_ << " pro_offset_: " << pro_offset_;
res_.SetRes(CmdRes::kErrOther, "InvalidOffset");
}
Expand Down Expand Up @@ -404,32 +403,6 @@ void FlushdbCmd::Do() {
g_pika_server->RWUnlock();
}

void ReadonlyCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
if (!ptr_info->CheckArg(argv.size())) {
res_.SetRes(CmdRes::kWrongNum, kCmdNameReadonly);
return;
}
std::string opt = slash::StringToLower(argv[1]);
if (opt == "on" || opt == "1") {
is_open_ = true;
} else if (opt == "off" || opt == "0") {
is_open_ = false;
} else {
res_.SetRes(CmdRes::kSyntaxErr, kCmdNameReadonly);
return;
}
}
void ReadonlyCmd::Do() {
g_pika_server->RWLockWriter();
if (is_open_) {
g_pika_conf->SetReadonly(true);
} else {
g_pika_conf->SetReadonly(false);
}
res_.SetRes(CmdRes::kOk);
g_pika_server->RWUnlock();
}

const std::string ClientCmd::CLIENT_LIST_S = "list";
const std::string ClientCmd::CLIENT_KILL_S = "kill";
void ClientCmd::DoInitial(PikaCmdArgsType &argv, const CmdInfo* const ptr_info) {
Expand Down Expand Up @@ -773,14 +746,14 @@ void InfoCmd::InfoReplication(std::string &info) {
tmp_stream << "master_port:" << g_pika_server->master_port() << "\r\n";
tmp_stream << "master_link_status:" << (g_pika_server->repl_state() == PIKA_REPL_CONNECTED ? "up" : "down") << "\r\n";
tmp_stream << "slave_priority:" << g_pika_conf->slave_priority() << "\r\n";
tmp_stream << "slave_read_only:" << g_pika_conf->readonly() << "\r\n";
tmp_stream << "slave_read_only:" << (g_pika_conf->slave_read_only() ? "yes" : "no") << "\r\n";
tmp_stream << "repl_state: " << (g_pika_server->repl_state_str()) << "\r\n";
break;
case PIKA_ROLE_MASTER | PIKA_ROLE_SLAVE :
tmp_stream << "master_host:" << g_pika_server->master_ip() << "\r\n";
tmp_stream << "master_port:" << g_pika_server->master_port() << "\r\n";
tmp_stream << "master_link_status:" << (g_pika_server->repl_state() == PIKA_REPL_CONNECTED ? "up" : "down") << "\r\n";
tmp_stream << "slave_read_only:" << g_pika_conf->readonly() << "\r\n";
tmp_stream << "slave_read_only:" << (g_pika_conf->slave_read_only() ? "yes" : "no") << "\r\n";
tmp_stream << "repl_state: " << (g_pika_server->repl_state_str()) << "\r\n";
case PIKA_ROLE_SINGLE :
case PIKA_ROLE_MASTER :
Expand Down Expand Up @@ -1181,7 +1154,7 @@ void ConfigCmd::ConfigGet(std::string &ret) {
} else if (get_item == "slave-read-only") {
ret = "*2\r\n";
EncodeString(&ret, "slave-read-only");
if (g_pika_conf->readonly()) {
if (g_pika_conf->slave_read_only()) {
EncodeString(&ret, "yes");
} else {
EncodeString(&ret, "no");
Expand Down Expand Up @@ -1281,7 +1254,7 @@ void ConfigCmd::ConfigGet(std::string &ret) {
EncodeString(&ret, "slowlog-max-len");
EncodeInt32(&ret, g_pika_conf->slowlog_max_len());
EncodeString(&ret, "slave-read-only");
EncodeInt32(&ret, g_pika_conf->readonly());
EncodeString(&ret, g_pika_conf->slave_read_only() ? "yes" : "no");
EncodeString(&ret, "write-binlog");
EncodeString(&ret, g_pika_conf->write_binlog() ? "yes" : "no");
EncodeString(&ret, "binlog-file-size");
Expand Down Expand Up @@ -1455,7 +1428,7 @@ void ConfigCmd::ConfigSet(std::string& ret) {
ret = "-ERR Invalid argument \'" + value + "\' for CONFIG SET 'slave-read-only'\r\n";
return;
}
g_pika_conf->SetReadonly(is_readonly);
g_pika_conf->SetSlaveReadOnly(is_readonly);
ret = "+OK\r\n";
} else if (set_item == "identify-binlog-type") {
int role = g_pika_server->role();
Expand Down
3 changes: 1 addition & 2 deletions src/pika_client_conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ std::string PikaClientConn::DoCmd(
if (g_pika_server->BinlogIoError()) {
return "-ERR Writing binlog failed, maybe no space left on device\r\n";
}
if (g_pika_conf->readonly()) {
if (g_pika_server->readonly()) {
return "-ERR Server in read-only\r\n";
}
if (argv.size() >= 2) {
Expand Down Expand Up @@ -187,7 +187,6 @@ std::string PikaClientConn::DoCmd(
if (!s.ok()) {
LOG(WARNING) << "Writing binlog failed, maybe no space left on device";
g_pika_server->SetBinlogIoError(true);
g_pika_conf->SetReadonly(true);
return "-ERR Writing binlog failed, maybe no space left on device\r\n";
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/pika_command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ void InitCmdInfoTable() {
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameFlushall, flushallptr));
CmdInfo* flushdbptr = new CmdInfo(kCmdNameFlushdb, 2, kCmdFlagsWrite | kCmdFlagsSuspend | kCmdFlagsAdmin);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameFlushdb, flushdbptr));
CmdInfo* readonlyptr = new CmdInfo(kCmdNameReadonly, 2, kCmdFlagsRead | kCmdFlagsSuspend | kCmdFlagsAdmin);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameReadonly, readonlyptr));
CmdInfo* clientptr = new CmdInfo(kCmdNameClient, -2, kCmdFlagsRead | kCmdFlagsAdmin);
cmd_infos.insert(std::pair<std::string, CmdInfo*>(kCmdNameClient, clientptr));
CmdInfo* shutdownptr = new CmdInfo(kCmdNameShutdown, 1, kCmdFlagsRead | kCmdFlagsLocal | kCmdFlagsAdmin);
Expand Down Expand Up @@ -486,8 +484,6 @@ void InitCmdTable(std::unordered_map<std::string, Cmd*> *cmd_table) {
cmd_table->insert(std::pair<std::string, Cmd*>(kCmdNameFlushall, flushallptr));
Cmd* flushdbptr = new FlushdbCmd();
cmd_table->insert(std::pair<std::string, Cmd*>(kCmdNameFlushdb, flushdbptr));
Cmd* readonlyptr = new ReadonlyCmd();
cmd_table->insert(std::pair<std::string, Cmd*>(kCmdNameReadonly, readonlyptr));
Cmd* clientptr = new ClientCmd();
cmd_table->insert(std::pair<std::string, Cmd*>(kCmdNameClient, clientptr));
Cmd* shutdownptr = new ShutdownCmd();
Expand Down
5 changes: 3 additions & 2 deletions src/pika_conf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ int PikaConf::Load()
expire_logs_days_ = 1;
}
GetConfStr("compression", &compression_);
GetConfBool("slave-read-only", &readonly_);
slave_read_only_ = true;
GetConfBool("slave-read-only", &slave_read_only_);
GetConfInt("slave-priority", &slave_priority_);

//
Expand Down Expand Up @@ -318,7 +319,7 @@ int PikaConf::ConfigRewrite() {
SetConfStr("slowlog-write-errorlog", slowlog_write_errorlog_ ? "yes" : "no");
SetConfInt("slowlog-log-slower-than", slowlog_log_slower_than_);
SetConfInt("slowlog-max-len", slowlog_max_len_);
SetConfBool("slave-read-only", readonly_);
SetConfBool("slave-read-only", slave_read_only_);
SetConfStr("compact-cron", compact_cron_);
SetConfStr("compact-interval", compact_interval_);
SetConfStr("network-interface", network_interface_);
Expand Down
2 changes: 1 addition & 1 deletion src/pika_master_conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ int PikaMasterConn::DealMessage(
g_pika_server->AddMonitorMessage(monitor_message);
}

bool is_readonly = g_pika_conf->readonly();
bool is_readonly = g_pika_server->readonly();

// Here, the binlog dispatch thread, instead of the binlog bgthread takes on the task to write binlog
// Only when the server is readonly
Expand Down
2 changes: 1 addition & 1 deletion src/pika_new_master_conn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ bool PikaNewMasterConn::ProcessBinlogData(const pink::RedisCmdArgsType& argv, co
g_pika_server->AddMonitorMessage(monitor_message);
}

bool is_readonly = g_pika_conf->readonly();
bool is_readonly = g_pika_server->readonly();

// Here, the binlog dispatch thread, instead of the binlog bgthread takes on the task to write binlog
// Only when the server is readonly
Expand Down
4 changes: 0 additions & 4 deletions src/pika_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,6 @@ bool PikaServer::SetMaster(std::string& master_ip, int master_port) {
if (!double_master_mode_) {
role_ |= PIKA_ROLE_SLAVE;
repl_state_ = PIKA_REPL_CONNECT;
LOG(INFO) << "Open read-only mode";
g_pika_conf->SetReadonly(true);
return true;
} else {
role_ |= PIKA_ROLE_DOUBLE_MASTER;
Expand Down Expand Up @@ -736,8 +734,6 @@ void PikaServer::RemoveMaster() {
slash::RWLock l(&state_protector_, true);
master_connection_ = 0;
}
LOG(INFO) << "Close read-only mode";
g_pika_conf->SetReadonly(false);
}

void PikaServer::TryDBSync(const std::string& ip, int port, int32_t top) {
Expand Down
2 changes: 0 additions & 2 deletions src/pika_trysync_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,6 @@ bool PikaTrysyncThread::TryUpdateMasterOffset() {
if (g_pika_server->DoubleMasterMode() && g_pika_server->IsDoubleMaster(master_ip, master_port)) {
g_pika_server->logger_->SetDoubleRecvInfo(filenum, offset);
LOG(INFO) << "Update receive infomation after rsync finished. filenum: " << filenum << " offset: " << offset;
// Close read-only mode
g_pika_conf->SetReadonly(false);
}
g_pika_server->WaitDBSyncFinish();
g_pika_server->SetForceFullSync(false);
Expand Down

0 comments on commit 5913213

Please sign in to comment.