@Test public void copySnapshotToFollower() throws Exception { createSnapshotFile(mLeaderStore); Assert.assertNull(mFollowerStore.getLatestSnapshot()); mFollowerSnapshotManager.installSnapshotFromLeader(); CommonUtils.waitFor("follower snapshot to complete", () -> mFollowerStore.getLatestSnapshot() != null, mWaitOptions); validateSnapshotFile(mFollowerStore); }
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); } }
SnapshotReplicationManager { 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); } } }
SnapshotReplicationManager { 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); } } SnapshotReplicationManager(RaftJournalSystem journalSystem,
      SimpleStateMachineStorage storage); @VisibleForTesting  SnapshotReplicationManager(RaftJournalSystem journalSystem,
      SimpleStateMachineStorage storage, RaftJournalServiceClient client); }
SnapshotReplicationManager { 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); } } SnapshotReplicationManager(RaftJournalSystem journalSystem,
      SimpleStateMachineStorage storage); @VisibleForTesting  SnapshotReplicationManager(RaftJournalSystem journalSystem,
      SimpleStateMachineStorage storage, RaftJournalServiceClient client); CompletableFuture<TermIndex> installSnapshotFromLeader(); void sendSnapshotToLeader(); long maybeCopySnapshotFromFollower(); StreamObserver<UploadSnapshotPRequest> receiveSnapshotFromFollower(
      StreamObserver<UploadSnapshotPResponse> responseStreamObserver); Message handleRequest(JournalQueryRequest queryRequest); StreamObserver<DownloadSnapshotPRequest> sendSnapshotToFollower(
      StreamObserver<DownloadSnapshotPResponse> responseObserver); synchronized void close(); }
SnapshotReplicationManager { 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); } } SnapshotReplicationManager(RaftJournalSystem journalSystem,
      SimpleStateMachineStorage storage); @VisibleForTesting  SnapshotReplicationManager(RaftJournalSystem journalSystem,
      SimpleStateMachineStorage storage, RaftJournalServiceClient client); CompletableFuture<TermIndex> installSnapshotFromLeader(); void sendSnapshotToLeader(); long maybeCopySnapshotFromFollower(); StreamObserver<UploadSnapshotPRequest> receiveSnapshotFromFollower(
      StreamObserver<UploadSnapshotPResponse> responseStreamObserver); Message handleRequest(JournalQueryRequest queryRequest); StreamObserver<DownloadSnapshotPRequest> sendSnapshotToFollower(
      StreamObserver<DownloadSnapshotPResponse> responseObserver); synchronized void close();  }