Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout when replica connect master #1172

Merged
merged 8 commits into from
Dec 12, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/cluster/replication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "status.h"
#include "storage/batch_debugger.h"
#include "thread_util.h"
#include "time_util.h"

Status FeedSlaveThread::Start() {
try {
Expand Down Expand Up @@ -236,18 +237,23 @@ void ReplicationThread::CallbacksStateMachine::Start() {
handlers_.emplace_front(CallbacksStateMachine::WRITE, "auth write", authWriteCB);
}

uint64_t last_connect_timestamp = 0, connect_timeout_ms = 3100;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If connect_timeout_ms is not dynamically updated, it would be better to use constants?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had thought about using a constant before, but found that it wasn't used elsewhere, so I wrote it as a local variable to make it more intuitive.


while (!repl_->stop_flag_ && bev == nullptr) {
Status s = Util::SockConnect(repl_->host_, repl_->port_, &cfd);
if (Util::GetTimeStampMS() - last_connect_timestamp < 1000) {
// prevent frequent re-connect when the master is down with the connection refused error
sleep(1);
}
last_connect_timestamp = Util::GetTimeStampMS();
Status s = Util::SockConnect(repl_->host_, repl_->port_, &cfd, connect_timeout_ms);
if (!s.IsOK()) {
LOG(ERROR) << "[replication] Failed to connect the master, err: " << s.Msg();
sleep(1);
continue;
}
bev = bufferevent_socket_new(repl_->base_, cfd, BEV_OPT_CLOSE_ON_FREE);
if (bev == nullptr) {
close(cfd);
LOG(ERROR) << "[replication] Failed to create the event socket";
sleep(1);
continue;
}
}
Expand Down