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());
}
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);
}
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();
}
}
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;
}
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());
}
}
Aggregations