use of com.alipay.sofa.jraft.rpc.RpcRequests.TimeoutNowResponse in project sofa-jraft by sofastack.
the class NodeImpl method handleTimeoutNowRequest.
@Override
public Message handleTimeoutNowRequest(final TimeoutNowRequest request, final RpcRequestClosure done) {
boolean doUnlock = true;
this.writeLock.lock();
try {
if (request.getTerm() != this.currTerm) {
final long savedCurrTerm = this.currTerm;
if (request.getTerm() > this.currTerm) {
stepDown(request.getTerm(), false, new Status(RaftError.EHIGHERTERMREQUEST, "Raft node receives higher term request"));
}
LOG.info("Node {} received TimeoutNowRequest from {} while currTerm={} didn't match requestTerm={}.", getNodeId(), request.getPeerId(), savedCurrTerm, request.getTerm());
return //
TimeoutNowResponse.newBuilder().setTerm(//
this.currTerm).setSuccess(//
false).build();
}
if (this.state != State.STATE_FOLLOWER) {
LOG.info("Node {} received TimeoutNowRequest from {}, while state={}, term={}.", getNodeId(), request.getServerId(), this.state, this.currTerm);
return //
TimeoutNowResponse.newBuilder().setTerm(//
this.currTerm).setSuccess(//
false).build();
}
final long savedTerm = this.currTerm;
final TimeoutNowResponse resp = //
TimeoutNowResponse.newBuilder().setTerm(//
this.currTerm + 1).setSuccess(//
true).build();
// Parallelize response and election
done.sendResponse(resp);
doUnlock = false;
electSelf();
LOG.info("Node {} received TimeoutNowRequest from {}, term={}.", getNodeId(), request.getServerId(), savedTerm);
} finally {
if (doUnlock) {
this.writeLock.unlock();
}
}
return null;
}
Aggregations