Search in sources :

Example 6 with SnapshotInfo

use of org.apache.ratis.statemachine.SnapshotInfo in project incubator-ratis by apache.

the class ServerState method getSnapshotIndex.

/**
 * The last index included in either the latestSnapshot or the latestInstalledSnapshot
 * @return the last snapshot index
 */
long getSnapshotIndex() {
    final SnapshotInfo s = getLatestSnapshot();
    final long latestSnapshotIndex = s != null ? s.getIndex() : RaftLog.INVALID_LOG_INDEX;
    return Math.max(latestSnapshotIndex, getLatestInstalledSnapshotIndex());
}
Also used : SnapshotInfo(org.apache.ratis.statemachine.SnapshotInfo)

Example 7 with SnapshotInfo

use of org.apache.ratis.statemachine.SnapshotInfo in project incubator-ratis by apache.

the class GrpcLogAppender method run.

@Override
public void run() throws IOException {
    boolean installSnapshotRequired;
    for (; isRunning(); mayWait()) {
        installSnapshotRequired = false;
        // HB period is expired OR we have messages OR follower is behind with commit index
        if (shouldSendAppendEntries() || isFollowerCommitBehindLastCommitIndex()) {
            if (installSnapshotEnabled) {
                SnapshotInfo snapshot = shouldInstallSnapshot();
                if (snapshot != null) {
                    installSnapshot(snapshot);
                    installSnapshotRequired = true;
                }
            } else {
                TermIndex installSnapshotNotificationTermIndex = shouldNotifyToInstallSnapshot();
                if (installSnapshotNotificationTermIndex != null) {
                    installSnapshot(installSnapshotNotificationTermIndex);
                    installSnapshotRequired = true;
                }
            }
            appendLog(installSnapshotRequired || haveTooManyPendingRequests());
        }
        getLeaderState().checkHealth(getFollower());
    }
    Optional.ofNullable(appendLogRequestObserver).ifPresent(StreamObserver::onCompleted);
}
Also used : StreamObserver(org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver) SnapshotInfo(org.apache.ratis.statemachine.SnapshotInfo) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 8 with SnapshotInfo

use of org.apache.ratis.statemachine.SnapshotInfo in project incubator-ratis by apache.

the class InstallSnapshotNotificationTests method testInstallSnapshotDuringBootstrap.

private void testInstallSnapshotDuringBootstrap(CLUSTER cluster) throws Exception {
    leaderSnapshotInfoRef.set(null);
    numSnapshotRequests.set(0);
    int i = 0;
    try {
        RaftTestUtil.waitForLeader(cluster);
        final RaftPeerId leaderId = cluster.getLeader().getId();
        try (final RaftClient client = cluster.createClient(leaderId)) {
            for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
                RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
                Assert.assertTrue(reply.isSuccess());
            }
        }
        // wait for the snapshot to be done
        final RaftServer.Division leader = cluster.getLeader();
        final long nextIndex = leader.getRaftLog().getNextIndex();
        LOG.info("nextIndex = {}", nextIndex);
        final List<File> snapshotFiles = RaftSnapshotBaseTest.getSnapshotFiles(cluster, nextIndex - SNAPSHOT_TRIGGER_THRESHOLD, nextIndex);
        JavaUtils.attemptRepeatedly(() -> {
            Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
            return null;
        }, 10, ONE_SECOND, "snapshotFile.exist", LOG);
        RaftSnapshotBaseTest.assertLeaderContent(cluster);
        final SnapshotInfo leaderSnapshotInfo = cluster.getLeader().getStateMachine().getLatestSnapshot();
        final boolean set = leaderSnapshotInfoRef.compareAndSet(null, leaderSnapshotInfo);
        Assert.assertTrue(set);
        // add two more peers
        final MiniRaftCluster.PeerChanges change = cluster.addNewPeers(2, true, true);
        // trigger setConfiguration
        cluster.setConfiguration(change.allPeersInNewConf);
        RaftServerTestUtil.waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
        // leader snapshot.
        for (RaftServer.Division follower : cluster.getFollowers()) {
            Assert.assertEquals(leaderSnapshotInfo.getIndex(), RaftServerTestUtil.getLatestInstalledSnapshotIndex(follower));
        }
        // Make sure each new peer got one snapshot notification.
        Assert.assertEquals(2, numSnapshotRequests.get());
    } finally {
        cluster.shutdown();
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SnapshotInfo(org.apache.ratis.statemachine.SnapshotInfo) SingleFileSnapshotInfo(org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) File(java.io.File) RaftClient(org.apache.ratis.client.RaftClient)

Example 9 with SnapshotInfo

use of org.apache.ratis.statemachine.SnapshotInfo in project incubator-ratis by apache.

the class LogAppenderBase method getPrevious.

private TermIndex getPrevious(long nextIndex) {
    if (nextIndex == RaftLog.LEAST_VALID_LOG_INDEX) {
        return null;
    }
    final long previousIndex = nextIndex - 1;
    final TermIndex previous = getRaftLog().getTermIndex(previousIndex);
    if (previous != null) {
        return previous;
    }
    final SnapshotInfo snapshot = server.getStateMachine().getLatestSnapshot();
    if (snapshot != null) {
        final TermIndex snapshotTermIndex = snapshot.getTermIndex();
        if (snapshotTermIndex.getIndex() == previousIndex) {
            return snapshotTermIndex;
        }
    }
    return null;
}
Also used : SnapshotInfo(org.apache.ratis.statemachine.SnapshotInfo) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 10 with SnapshotInfo

use of org.apache.ratis.statemachine.SnapshotInfo 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)

Aggregations

SnapshotInfo (org.apache.ratis.statemachine.SnapshotInfo)21 SingleFileSnapshotInfo (org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo)7 TermIndex (org.apache.ratis.server.protocol.TermIndex)6 File (java.io.File)5 RaftClient (org.apache.ratis.client.RaftClient)3 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)3 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)3 RaftServer (org.apache.ratis.server.RaftServer)3 MiniRaftCluster (org.apache.ratis.server.impl.MiniRaftCluster)3 NotFoundException (alluxio.exception.status.NotFoundException)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 AbortedException (alluxio.exception.status.AbortedException)1 AlluxioStatusException (alluxio.exception.status.AlluxioStatusException)1 DownloadSnapshotPRequest (alluxio.grpc.DownloadSnapshotPRequest)1 DownloadSnapshotPResponse (alluxio.grpc.DownloadSnapshotPResponse)1 JournalQueryResponse (alluxio.grpc.JournalQueryResponse)1 UploadSnapshotPRequest (alluxio.grpc.UploadSnapshotPRequest)1 UploadSnapshotPResponse (alluxio.grpc.UploadSnapshotPResponse)1 Timer (com.codahale.metrics.Timer)1