From 047afe96bc1ad9d3ec38e676cdb618cfe333fb15 Mon Sep 17 00:00:00 2001 From: KernelMaker Date: Mon, 22 Aug 2016 15:37:21 +0800 Subject: [PATCH] bugfix: 1) master-slave error(rarely); 2) should exit trysync thread first in shutdown --- src/pika_binlog_sender_thread.cc | 1 + src/pika_server.cc | 9 +++++++-- src/pika_slaveping_thread.cc | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/pika_binlog_sender_thread.cc b/src/pika_binlog_sender_thread.cc index 4c309166d0..55dbe8ab1e 100644 --- a/src/pika_binlog_sender_thread.cc +++ b/src/pika_binlog_sender_thread.cc @@ -253,6 +253,7 @@ void* PikaBinlogSenderThread::ThreadMain() { while (!should_exit_) { + sleep(1); // 1. Connect to slave result = cli_->Connect(ip_, port_, g_pika_server->host()); LOG(INFO) << "BinlogSender Connect slave(" << ip_ << ":" << port_ << ") " << result.ToString(); diff --git a/src/pika_server.cc b/src/pika_server.cc index 700213b942..93f07d69a2 100644 --- a/src/pika_server.cc +++ b/src/pika_server.cc @@ -102,6 +102,7 @@ PikaServer::~PikaServer() { LOG(INFO) << "Delete slave success"; } } + delete pika_trysync_thread_; delete ping_thread_; delete pika_binlog_receiver_thread_; @@ -112,7 +113,6 @@ PikaServer::~PikaServer() { delete (*binlogbg_iter); binlogbg_iter++; } - delete pika_trysync_thread_; delete pika_heartbeat_thread_; delete monitor_thread_; @@ -450,7 +450,8 @@ void PikaServer::PlusMasterConnection() { bool PikaServer::ShouldAccessConnAsMaster(const std::string& ip) { slash::RWLock l(&state_protector_, false); DLOG(INFO) << "ShouldAccessConnAsMaster, repl_state_: " << repl_state_ << " ip: " << ip << " master_ip: " << master_ip_; - if (repl_state_ != PIKA_REPL_NO_CONNECT && repl_state_ != PIKA_REPL_WAIT_DBSYNC && ip == master_ip_) { +// if (repl_state_ != PIKA_REPL_NO_CONNECT && repl_state_ != PIKA_REPL_WAIT_DBSYNC && ip == master_ip_) { + if (repl_state_ == PIKA_REPL_CONNECTING && ip == master_ip_) { return true; } return false; @@ -494,6 +495,10 @@ void PikaServer::RemoveMaster() { delete ping_thread_; ping_thread_ = NULL; } + { + slash::RWLock l(&state_protector_, true); + master_connection_ = 0; + } LOG(INFO) << "close read-only mode"; g_pika_conf->SetReadonly(false); } diff --git a/src/pika_slaveping_thread.cc b/src/pika_slaveping_thread.cc index 0400a0dda6..f93417320f 100644 --- a/src/pika_slaveping_thread.cc +++ b/src/pika_slaveping_thread.cc @@ -81,6 +81,7 @@ void* PikaSlavepingThread::ThreadMain() { } sleep(1); } + sleep(2); g_pika_server->MinusMasterConnection(); } else if (!should_exit_) { LOG(WARNING) << "Slaveping, Connect timeout";