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

fail-fast on connection fail #586

Merged
merged 2 commits into from
May 12, 2021
Merged
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 @@ -21,6 +21,7 @@
import java.util.concurrent.Executor;
import java.util.concurrent.Future;

import com.alipay.sofa.jraft.Closure;
import com.alipay.sofa.jraft.ReplicatorGroup;
import com.alipay.sofa.jraft.Status;
import com.alipay.sofa.jraft.error.RaftError;
Expand Down Expand Up @@ -93,12 +94,20 @@ public synchronized boolean init(final RpcOptions rpcOptions) {
@Override
public Future<Message> preVote(final Endpoint endpoint, final RequestVoteRequest request,
final RpcResponseClosure<RequestVoteResponse> done) {
if (!checkConnection(endpoint, true)) {
return onConnectionFail(endpoint, request, done, this.rpcExecutor);
}

return invokeWithDone(endpoint, request, done, this.nodeOptions.getElectionTimeoutMs());
}

@Override
public Future<Message> requestVote(final Endpoint endpoint, final RequestVoteRequest request,
final RpcResponseClosure<RequestVoteResponse> done) {
if (!checkConnection(endpoint, true)) {
return onConnectionFail(endpoint, request, done, this.rpcExecutor);
}

return invokeWithDone(endpoint, request, done, this.nodeOptions.getElectionTimeoutMs());
}

Expand All @@ -107,26 +116,11 @@ public Future<Message> appendEntries(final Endpoint endpoint, final AppendEntrie
final int timeoutMs, final RpcResponseClosure<AppendEntriesResponse> done) {
final Executor executor = this.appendEntriesExecutorMap.computeIfAbsent(endpoint, k -> APPEND_ENTRIES_EXECUTORS.next());

if (checkConnection(endpoint, true)) {
return invokeWithDone(endpoint, request, done, timeoutMs, executor);
if (!checkConnection(endpoint, true)) {
return onConnectionFail(endpoint, request, done, executor);
}

// fail-fast when no connection
final FutureImpl<Message> future = new FutureImpl<>();
executor.execute(() -> {
if (done != null) {
try {
done.run(new Status(RaftError.EINTERNAL, "Check connection[%s] fail and try to create new one", endpoint));
} catch (final Throwable t) {
LOG.error("Fail to run RpcResponseClosure, the request is {}.", request, t);
}
}
if (!future.isDone()) {
future.failure(new RemotingException("Check connection[" +
endpoint.toString() + "] fail and try to create new one"));
}
});
return future;
return invokeWithDone(endpoint, request, done, timeoutMs, executor);
}

@Override
Expand Down Expand Up @@ -155,4 +149,23 @@ public Future<Message> readIndex(final Endpoint endpoint, final ReadIndexRequest
final RpcResponseClosure<ReadIndexResponse> done) {
return invokeWithDone(endpoint, request, done, timeoutMs);
}

// fail-fast when no connection
private Future<Message> onConnectionFail(final Endpoint endpoint, final Message request, Closure done, final Executor executor) {
final FutureImpl<Message> future = new FutureImpl<>();
executor.execute(() -> {
final String fmt = "Check connection[%s] fail and try to create new one";
if (done != null) {
try {
done.run(new Status(RaftError.EINTERNAL, fmt, endpoint));
} catch (final Throwable t) {
LOG.error("Fail to run RpcResponseClosure, the request is {}.", request, t);
}
}
if (!future.isDone()) {
future.failure(new RemotingException(String.format(fmt, endpoint)));
}
});
return future;
}
}