Search in sources :

Example 1 with AppendEntriesReplyProto

use of org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto in project incubator-ratis by apache.

the class LogAppenderDefault method run.

@Override
public void run() throws InterruptedException, IOException {
    while (isRunning()) {
        if (shouldSendAppendEntries()) {
            SnapshotInfo snapshot = shouldInstallSnapshot();
            if (snapshot != null) {
                LOG.info("{}: followerNextIndex = {} but logStartIndex = {}, send snapshot {} to follower", this, getFollower().getNextIndex(), getRaftLog().getStartIndex(), snapshot);
                final InstallSnapshotReplyProto r = installSnapshot(snapshot);
                if (r != null) {
                    switch(r.getResult()) {
                        case NOT_LEADER:
                            onFollowerTerm(r.getTerm());
                            break;
                        case SUCCESS:
                        case SNAPSHOT_UNAVAILABLE:
                        case ALREADY_INSTALLED:
                            getFollower().setAttemptedToInstallSnapshot();
                            break;
                        default:
                            break;
                    }
                }
            // otherwise if r is null, retry the snapshot installation
            } else {
                final AppendEntriesReplyProto r = sendAppendEntriesWithRetries();
                if (r != null) {
                    handleReply(r);
                }
            }
        }
        if (isRunning() && !hasAppendEntries()) {
            getEventAwaitForSignal().await(getHeartbeatWaitTimeMs(), TimeUnit.MILLISECONDS);
        }
        getLeaderState().checkHealth(getFollower());
    }
}
Also used : SnapshotInfo(org.apache.ratis.statemachine.SnapshotInfo) InstallSnapshotReplyProto(org.apache.ratis.proto.RaftProtos.InstallSnapshotReplyProto) AppendEntriesReplyProto(org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto)

Example 2 with AppendEntriesReplyProto

use of org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto in project incubator-ratis by apache.

the class LogAppenderDefault method sendAppendEntriesWithRetries.

/**
 * Send an appendEntries RPC; retry indefinitely.
 */
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE")
private AppendEntriesReplyProto sendAppendEntriesWithRetries() throws InterruptedException, InterruptedIOException, RaftLogIOException {
    int retry = 0;
    AppendEntriesRequestProto request = null;
    while (isRunning()) {
        // keep retrying for IOException
        try {
            if (request == null || request.getEntriesCount() == 0) {
                request = newAppendEntriesRequest(CallId.getAndIncrement(), false);
            }
            if (request == null) {
                LOG.trace("{} no entries to send now, wait ...", this);
                return null;
            } else if (!isRunning()) {
                LOG.info("{} is stopped. Skip appendEntries.", this);
                return null;
            }
            getFollower().updateLastRpcSendTime(request.getEntriesCount() == 0);
            final AppendEntriesReplyProto r = getServerRpc().appendEntries(request);
            getFollower().updateLastRpcResponseTime();
            getLeaderState().onFollowerCommitIndex(getFollower(), r.getFollowerCommit());
            return r;
        } catch (InterruptedIOException | RaftLogIOException e) {
            throw e;
        } catch (IOException ioe) {
            // TODO should have more detailed retry policy here.
            if (retry++ % 10 == 0) {
                // to reduce the number of messages
                LOG.warn("{}: Failed to appendEntries (retry={}): {}", this, retry++, ioe);
            }
            handleException(ioe);
        }
        if (isRunning()) {
            getServer().properties().rpcSleepTime().sleep();
        }
    }
    return null;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) RaftLogIOException(org.apache.ratis.server.raftlog.RaftLogIOException) IOException(java.io.IOException) RaftLogIOException(org.apache.ratis.server.raftlog.RaftLogIOException) InterruptedIOException(java.io.InterruptedIOException) AppendEntriesRequestProto(org.apache.ratis.proto.RaftProtos.AppendEntriesRequestProto) AppendEntriesReplyProto(org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

AppendEntriesReplyProto (org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 AppendEntriesRequestProto (org.apache.ratis.proto.RaftProtos.AppendEntriesRequestProto)1 InstallSnapshotReplyProto (org.apache.ratis.proto.RaftProtos.InstallSnapshotReplyProto)1 RaftLogIOException (org.apache.ratis.server.raftlog.RaftLogIOException)1 SnapshotInfo (org.apache.ratis.statemachine.SnapshotInfo)1