Skip to content
Open
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public void run() {
}

private Socket connectToLeader() throws IOException, X509Exception, InterruptedException {
Socket sock = createSocket();
Socket sock = null;

// leader connection timeout defaults to tickTime * initLimit
int connectTimeout = self.tickTime * self.initLimit;
Expand All @@ -410,8 +410,10 @@ private Socket connectToLeader() throws IOException, X509Exception, InterruptedE
int remainingTimeout;
long startNanoTime = nanoTime();

for (int tries = 0; tries < 5 && socket.get() == null; tries++) {
for (int tries = 0; tries < 5; tries++) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

connectToLeader could be called concurrently for different addresses, so it is good to break eagerly when there is a success connection.

try {
sock = createSocket();

// recalculate the init limit time because retries sleep for 1000 milliseconds
remainingTimeout = connectTimeout - (int) ((nanoTime() - startNanoTime) / 1_000_000);
if (remainingTimeout <= 0) {
Expand All @@ -423,35 +425,29 @@ private Socket connectToLeader() throws IOException, X509Exception, InterruptedE
if (self.isSslQuorum()) {
((SSLSocket) sock).startHandshake();
}
sock.setTcpNoDelay(nodelay);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@functioner I believe this shouldn't be deleted, along with break statement next line

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I feel same here.

break;
} catch (IOException e) {
// close failed socket before retrying
if (sock != null) {
try {
sock.close();
} catch (IOException closeException) {
LOG.debug("Failed to close socket after connection failure", closeException);
}
sock = null;
}
remainingTimeout = connectTimeout - (int) ((nanoTime() - startNanoTime) / 1_000_000);

if (remainingTimeout <= leaderConnectDelayDuringRetryMs) {
LOG.error(
"Unexpected exception, connectToLeader exceeded. tries={}, remaining init limit={}, connecting to {}",
tries,
remainingTimeout,
address,
e);
LOG.error("Unexpected exception, connectToLeader exceeded. tries={}, remaining init limit={}, connecting to {}",
tries, remainingTimeout, address, e);
throw e;
} else if (tries >= 4) {
LOG.error(
"Unexpected exception, retries exceeded. tries={}, remaining init limit={}, connecting to {}",
tries,
remainingTimeout,
address,
e);
LOG.error("Unexpected exception, retries exceeded. tries={}, remaining init limit={}, connecting to {}",
tries, remainingTimeout, address, e);
throw e;
} else {
LOG.warn(
"Unexpected exception, tries={}, remaining init limit={}, connecting to {}",
tries,
remainingTimeout,
address,
e);
sock = createSocket();
LOG.warn("Unexpected exception, tries={}, remaining init limit={}, connecting to {}",
tries, remainingTimeout, address, e);
}
}
Thread.sleep(leaderConnectDelayDuringRetryMs);
Expand Down
Loading