Search in sources :

Example 1 with InstallSnapshotReplyProto

use of org.apache.ratis.proto.RaftProtos.InstallSnapshotReplyProto 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 InstallSnapshotReplyProto

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

the class LogAppenderDefault method installSnapshot.

private InstallSnapshotReplyProto installSnapshot(SnapshotInfo snapshot) throws InterruptedIOException {
    String requestId = UUID.randomUUID().toString();
    InstallSnapshotReplyProto reply = null;
    try {
        for (InstallSnapshotRequestProto request : newInstallSnapshotRequests(requestId, snapshot)) {
            getFollower().updateLastRpcSendTime(false);
            reply = getServerRpc().installSnapshot(request);
            getFollower().updateLastRpcResponseTime();
            if (!reply.getServerReply().getSuccess()) {
                return reply;
            }
        }
    } catch (InterruptedIOException iioe) {
        throw iioe;
    } catch (Exception ioe) {
        LOG.warn("{}: Failed to installSnapshot {}: {}", this, snapshot, ioe);
        handleException(ioe);
        return null;
    }
    if (reply != null) {
        getFollower().setSnapshotIndex(snapshot.getTermIndex().getIndex());
        LOG.info("{}: installSnapshot {} successfully", this, snapshot);
        getServer().getRaftServerMetrics().onSnapshotInstalled();
    }
    return reply;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) InstallSnapshotRequestProto(org.apache.ratis.proto.RaftProtos.InstallSnapshotRequestProto) InstallSnapshotReplyProto(org.apache.ratis.proto.RaftProtos.InstallSnapshotReplyProto) IOException(java.io.IOException) RaftLogIOException(org.apache.ratis.server.raftlog.RaftLogIOException) InterruptedIOException(java.io.InterruptedIOException)

Aggregations

InstallSnapshotReplyProto (org.apache.ratis.proto.RaftProtos.InstallSnapshotReplyProto)2 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 AppendEntriesReplyProto (org.apache.ratis.proto.RaftProtos.AppendEntriesReplyProto)1 InstallSnapshotRequestProto (org.apache.ratis.proto.RaftProtos.InstallSnapshotRequestProto)1 RaftLogIOException (org.apache.ratis.server.raftlog.RaftLogIOException)1 SnapshotInfo (org.apache.ratis.statemachine.SnapshotInfo)1