Source:
public CompletableFuture<TermIndex> installSnapshotFromLeader() { if (mJournalSystem.isLeader()) { return RaftJournalUtils.completeExceptionally( new IllegalStateException("Abort snapshot installation after becoming a leader")); } if (!transitionState(DownloadState.IDLE, DownloadState.STREAM_DATA)) { return RaftJournalUtils.completeExceptionally( new IllegalStateException("Illegal state while installing a snapshot")); } try { RaftJournalServiceClient client = getJournalServiceClient(); SnapshotDownloader<DownloadSnapshotPRequest, DownloadSnapshotPResponse> observer = SnapshotDownloader.forFollower(mStorage, String.valueOf(client.getAddress())); client.downloadSnapshot(observer); return observer.getFuture().thenApplyAsync((termIndex) -> { mDownloadedSnapshot = observer.getSnapshotToInstall(); transitionState(DownloadState.STREAM_DATA, DownloadState.DOWNLOADED); long index = installDownloadedSnapshot(); if (index == RaftLog.INVALID_LOG_INDEX) { throw new CompletionException( new RuntimeException("Failed to install downloaded snapshot")); } if (index != termIndex.getIndex()) { throw new CompletionException(new IllegalStateException( String.format("Mismatched snapshot installed - downloaded %d, installed %d", termIndex.getIndex(), index))); } return termIndex; }).whenComplete((termIndex, throwable) -> { if (throwable != null) { LOG.error("Unexpected exception downloading snapshot from leader.", throwable); transitionState(DownloadState.STREAM_DATA, DownloadState.IDLE); } }); } catch (Exception e) { transitionState(DownloadState.STREAM_DATA, DownloadState.IDLE); return RaftJournalUtils.completeExceptionally(e); } }

Target:
public CompletableFuture<TermIndex> installSnapshotFromLeader() { if (mJournalSystem.isLeader()) { return RaftJournalUtils.completeExceptionally( new IllegalStateException("Abort snapshot installation after becoming a leader")); } if (!transitionState(DownloadState.IDLE, DownloadState.STREAM_DATA)) { return RaftJournalUtils.completeExceptionally( new IllegalStateException("State is not IDLE when starting a snapshot installation")); } try { RaftJournalServiceClient client = getJournalServiceClient(); String address = String.valueOf(client.getAddress()); SnapshotDownloader<DownloadSnapshotPRequest, DownloadSnapshotPResponse> observer = SnapshotDownloader.forFollower(mStorage, address); client.downloadSnapshot(observer); return observer.getFuture().thenApplyAsync((termIndex) -> { mDownloadedSnapshot = observer.getSnapshotToInstall(); transitionState(DownloadState.STREAM_DATA, DownloadState.DOWNLOADED); long index = installDownloadedSnapshot(); if (index == RaftLog.INVALID_LOG_INDEX) { throw new CompletionException(new RuntimeException( String.format("Failed to install the downloaded snapshot %s", termIndex))); } if (index != termIndex.getIndex()) { throw new CompletionException(new IllegalStateException( String.format("Mismatched snapshot installed - downloaded %d, installed %d", termIndex.getIndex(), index))); } return termIndex; }).whenComplete((termIndex, throwable) -> { if (throwable != null) { LOG.error("Unexpected exception downloading snapshot from leader {}.", address, throwable); transitionState(DownloadState.STREAM_DATA, DownloadState.IDLE); } }); } catch (Exception e) { transitionState(DownloadState.STREAM_DATA, DownloadState.IDLE); return RaftJournalUtils.completeExceptionally(e); } }