Search in sources :

Example 1 with TimeoutNowResponse

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;
}
Also used : Status(com.alipay.sofa.jraft.Status) TimeoutNowResponse(com.alipay.sofa.jraft.rpc.RpcRequests.TimeoutNowResponse)

Aggregations

Status (com.alipay.sofa.jraft.Status)1 TimeoutNowResponse (com.alipay.sofa.jraft.rpc.RpcRequests.TimeoutNowResponse)1