Search in sources :

Example 11 with SnapshotInfo

use of org.apache.ratis.statemachine.SnapshotInfo in project alluxio by Alluxio.

the class SnapshotReplicationManager method installDownloadedSnapshot.

/**
 * Installs a downloaded snapshot in the journal snapshot directory.
 *
 * @return the index of the installed snapshot
 */
private long installDownloadedSnapshot() {
    if (!transitionState(DownloadState.DOWNLOADED, DownloadState.INSTALLING)) {
        return RaftLog.INVALID_LOG_INDEX;
    }
    File tempFile = null;
    try (Timer.Context ctx = MetricsSystem.timer(MetricKey.MASTER_EMBEDDED_JOURNAL_SNAPSHOT_INSTALL_TIMER.getName()).time()) {
        SnapshotInfo snapshot = mDownloadedSnapshot;
        if (snapshot == null) {
            throw new IllegalStateException("Snapshot is not completed");
        }
        FileInfo fileInfo = snapshot.getFiles().get(0);
        tempFile = fileInfo.getPath().toFile();
        if (!tempFile.exists()) {
            throw new FileNotFoundException(String.format("Snapshot file %s is not found", tempFile));
        }
        SnapshotInfo latestSnapshot = mStorage.getLatestSnapshot();
        TermIndex lastInstalled = latestSnapshot == null ? null : latestSnapshot.getTermIndex();
        TermIndex downloaded = snapshot.getTermIndex();
        if (lastInstalled != null && downloaded.compareTo(lastInstalled) < 0) {
            throw new AbortedException(String.format("Snapshot to be installed %s is older than current snapshot %s", downloaded, lastInstalled));
        }
        final File snapshotFile = mStorage.getSnapshotFile(downloaded.getTerm(), downloaded.getIndex());
        LOG.debug("Moving temp snapshot {} to file {}", tempFile, snapshotFile);
        MD5FileUtil.saveMD5File(snapshotFile, fileInfo.getFileDigest());
        if (!tempFile.renameTo(snapshotFile)) {
            throw new IOException(String.format("Failed to rename %s to %s", tempFile, snapshotFile));
        }
        mStorage.loadLatestSnapshot();
        LOG.info("Completed storing snapshot at {} to file {}", downloaded, snapshotFile);
        return downloaded.getIndex();
    } catch (Exception e) {
        LOG.error("Failed to install snapshot", e);
        if (tempFile != null) {
            tempFile.delete();
        }
        return RaftLog.INVALID_LOG_INDEX;
    } finally {
        transitionState(DownloadState.INSTALLING, DownloadState.IDLE);
    }
}
Also used : SnapshotInfo(org.apache.ratis.statemachine.SnapshotInfo) SingleFileSnapshotInfo(org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo) Timer(com.codahale.metrics.Timer) FileInfo(org.apache.ratis.server.storage.FileInfo) AbortedException(alluxio.exception.status.AbortedException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File) CompletionException(java.util.concurrent.CompletionException) FileNotFoundException(java.io.FileNotFoundException) AbortedException(alluxio.exception.status.AbortedException) AlluxioStatusException(alluxio.exception.status.AlluxioStatusException) IOException(java.io.IOException) NotFoundException(alluxio.exception.status.NotFoundException) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 12 with SnapshotInfo

use of org.apache.ratis.statemachine.SnapshotInfo in project alluxio by Alluxio.

the class SnapshotReplicationManager method sendSnapshotToLeader.

/**
 * Sends a snapshot to the leader.
 *
 * @throws IOException if error occurs while initializing the data stream
 */
public void sendSnapshotToLeader() throws IOException {
    if (mJournalSystem.isLeader()) {
        throw new IllegalStateException("Server is no longer a follower");
    }
    LOG.debug("Checking latest snapshot to send");
    SnapshotInfo snapshot = mStorage.getLatestSnapshot();
    if (snapshot == null) {
        throw new NotFoundException("No snapshot available");
    }
    StreamObserver<UploadSnapshotPResponse> responseObserver = SnapshotUploader.forFollower(mStorage, snapshot);
    try (RaftJournalServiceClient client = getJournalServiceClient()) {
        LOG.info("Sending stream request to {} for snapshot {}", client.getAddress(), snapshot.getTermIndex());
        StreamObserver<UploadSnapshotPRequest> requestObserver = client.uploadSnapshot(responseObserver);
        requestObserver.onNext(UploadSnapshotPRequest.newBuilder().setData(SnapshotData.newBuilder().setSnapshotTerm(snapshot.getTerm()).setSnapshotIndex(snapshot.getIndex()).setOffset(0)).build());
    }
}
Also used : SnapshotInfo(org.apache.ratis.statemachine.SnapshotInfo) SingleFileSnapshotInfo(org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(alluxio.exception.status.NotFoundException) UploadSnapshotPRequest(alluxio.grpc.UploadSnapshotPRequest) UploadSnapshotPResponse(alluxio.grpc.UploadSnapshotPResponse)

Example 13 with SnapshotInfo

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

the class LogAppender method getPrevious.

private TermIndex getPrevious() {
    TermIndex previous = raftLog.getTermIndex(follower.getNextIndex() - 1);
    if (previous == null) {
        // if previous is null, nextIndex must be equal to the log start
        // index (otherwise we will install snapshot).
        Preconditions.assertTrue(follower.getNextIndex() == raftLog.getStartIndex(), "follower's next index %s, local log start index %s", follower.getNextIndex(), raftLog.getStartIndex());
        SnapshotInfo snapshot = server.getState().getLatestSnapshot();
        previous = snapshot == null ? null : snapshot.getTermIndex();
    }
    return previous;
}
Also used : SnapshotInfo(org.apache.ratis.statemachine.SnapshotInfo) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 14 with SnapshotInfo

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

the class LogAppender method checkAndSendAppendEntries.

/**
 * Check and send appendEntries RPC
 */
private void checkAndSendAppendEntries() throws InterruptedException, InterruptedIOException, RaftLogIOException {
    while (isAppenderRunning()) {
        if (shouldSendRequest()) {
            SnapshotInfo snapshot = shouldInstallSnapshot();
            if (snapshot != null) {
                LOG.info("{}: follower {}'s next index is {}," + " log's start index is {}, need to install snapshot", server.getId(), follower.getPeer(), follower.getNextIndex(), raftLog.getStartIndex());
                final InstallSnapshotReplyProto r = installSnapshot(snapshot);
                if (r != null && r.getResult() == InstallSnapshotResult.NOT_LEADER) {
                    checkResponseTerm(r.getTerm());
                }
            // otherwise if r is null, retry the snapshot installation
            } else {
                final AppendEntriesReplyProto r = sendAppendEntriesWithRetries();
                if (r != null) {
                    handleReply(r);
                }
            }
        }
        if (isAppenderRunning() && !shouldAppendEntries(follower.getNextIndex() + buffer.getPendingEntryNum())) {
            final long waitTime = getHeartbeatRemainingTime(follower.getLastRpcTime());
            if (waitTime > 0) {
                synchronized (this) {
                    wait(waitTime);
                }
            }
        }
    }
}
Also used : SnapshotInfo(org.apache.ratis.statemachine.SnapshotInfo)

Example 15 with SnapshotInfo

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

the class StateMachineUpdater method reload.

private void reload() throws IOException {
    Preconditions.assertTrue(stateMachine.getLifeCycleState() == LifeCycle.State.PAUSED);
    stateMachine.reinitialize();
    final SnapshotInfo snapshot = stateMachine.getLatestSnapshot();
    Objects.requireNonNull(snapshot, "snapshot == null");
    final long i = snapshot.getIndex();
    snapshotIndex.setUnconditionally(i, infoIndexChange);
    appliedIndex.setUnconditionally(i, infoIndexChange);
    state = State.RUNNING;
}
Also used : SnapshotInfo(org.apache.ratis.statemachine.SnapshotInfo)

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