Search in sources :

Example 1 with AppendEntriesResponse

use of com.alipay.sofa.jraft.rpc.RpcRequests.AppendEntriesResponse in project sofa-jraft by sofastack.

the class Replicator method sendEmptyEntries.

/**
 * Send probe or heartbeat request
 * @param isHeartbeat      if current entries is heartbeat
 * @param heartBeatClosure heartbeat callback
 */
@SuppressWarnings("NonAtomicOperationOnVolatileField")
private void sendEmptyEntries(final boolean isHeartbeat, final RpcResponseClosure<AppendEntriesResponse> heartBeatClosure) {
    final AppendEntriesRequest.Builder rb = AppendEntriesRequest.newBuilder();
    if (!fillCommonFields(rb, this.nextIndex - 1, isHeartbeat)) {
        // id is unlock in installSnapshot
        installSnapshot();
        if (isHeartbeat && heartBeatClosure != null) {
            RpcUtils.runClosureInThread(heartBeatClosure, new Status(RaftError.EAGAIN, "Fail to send heartbeat to peer %s", this.options.getPeerId()));
        }
        return;
    }
    try {
        final long monotonicSendTimeMs = Utils.monotonicMs();
        if (isHeartbeat) {
            final AppendEntriesRequest request = rb.build();
            // Sending a heartbeat request
            this.heartbeatCounter++;
            RpcResponseClosure<AppendEntriesResponse> heartbeatDone;
            // Prefer passed-in closure.
            if (heartBeatClosure != null) {
                heartbeatDone = heartBeatClosure;
            } else {
                heartbeatDone = new RpcResponseClosureAdapter<AppendEntriesResponse>() {

                    @Override
                    public void run(final Status status) {
                        onHeartbeatReturned(Replicator.this.id, status, request, getResponse(), monotonicSendTimeMs);
                    }
                };
            }
            this.heartbeatInFly = this.rpcService.appendEntries(this.options.getPeerId().getEndpoint(), request, this.options.getElectionTimeoutMs() / 2, heartbeatDone);
        } else {
            // No entries and has empty data means a probe request.
            // TODO(boyan) refactor, adds a new flag field?
            rb.setData(ByteString.EMPTY);
            final AppendEntriesRequest request = rb.build();
            // Sending a probe request.
            this.statInfo.runningState = RunningState.APPENDING_ENTRIES;
            this.statInfo.firstLogIndex = this.nextIndex;
            this.statInfo.lastLogIndex = this.nextIndex - 1;
            this.probeCounter++;
            setState(State.Probe);
            final int stateVersion = this.version;
            final int seq = getAndIncrementReqSeq();
            final Future<Message> rpcFuture = this.rpcService.appendEntries(this.options.getPeerId().getEndpoint(), request, -1, new RpcResponseClosureAdapter<AppendEntriesResponse>() {

                @Override
                public void run(final Status status) {
                    onRpcReturned(Replicator.this.id, RequestType.AppendEntries, status, request, getResponse(), seq, stateVersion, monotonicSendTimeMs);
                }
            });
            addInflight(RequestType.AppendEntries, this.nextIndex, 0, 0, seq, rpcFuture);
        }
        LOG.debug("Node {} send HeartbeatRequest to {} term {} lastCommittedIndex {}", this.options.getNode().getNodeId(), this.options.getPeerId(), this.options.getTerm(), rb.getCommittedIndex());
    } finally {
        unlockId();
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) AppendEntriesResponse(com.alipay.sofa.jraft.rpc.RpcRequests.AppendEntriesResponse) Message(com.google.protobuf.Message) AppendEntriesRequest(com.alipay.sofa.jraft.rpc.RpcRequests.AppendEntriesRequest)

Aggregations

Status (com.alipay.sofa.jraft.Status)1 AppendEntriesRequest (com.alipay.sofa.jraft.rpc.RpcRequests.AppendEntriesRequest)1 AppendEntriesResponse (com.alipay.sofa.jraft.rpc.RpcRequests.AppendEntriesResponse)1 Message (com.google.protobuf.Message)1