Search in sources :

Example 1 with AbortedException

use of alluxio.exception.status.AbortedException 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)

Aggregations

AbortedException (alluxio.exception.status.AbortedException)1 AlluxioStatusException (alluxio.exception.status.AlluxioStatusException)1 NotFoundException (alluxio.exception.status.NotFoundException)1 Timer (com.codahale.metrics.Timer)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 CompletionException (java.util.concurrent.CompletionException)1 TermIndex (org.apache.ratis.server.protocol.TermIndex)1 FileInfo (org.apache.ratis.server.storage.FileInfo)1 SnapshotInfo (org.apache.ratis.statemachine.SnapshotInfo)1 SingleFileSnapshotInfo (org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo)1