use of alluxio.grpc.DownloadSnapshotPRequest in project alluxio by Alluxio.
the class SnapshotReplicationManager method installSnapshotFromLeader.
/**
* Downloads and installs a snapshot from the leader.
*
* @return a future with the term index of the installed snapshot
*/
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);
Timer.Context ctx = MetricsSystem.timer(MetricKey.MASTER_EMBEDDED_JOURNAL_SNAPSHOT_DOWNLOAD_TIMER.getName()).time();
client.downloadSnapshot(observer);
return observer.getFuture().thenApplyAsync((termIndex) -> {
ctx.close();
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);
}
}
use of alluxio.grpc.DownloadSnapshotPRequest in project alluxio by Alluxio.
the class SnapshotReplicationManager method sendSnapshotToFollower.
/**
* Sends a snapshot to a follower.
*
* @param responseObserver the response stream observer
* @return the request stream observer
*/
public StreamObserver<DownloadSnapshotPRequest> sendSnapshotToFollower(StreamObserver<DownloadSnapshotPResponse> responseObserver) {
SnapshotInfo snapshot = mStorage.getLatestSnapshot();
LOG.debug("Received snapshot download request from {}", ClientIpAddressInjector.getIpAddress());
SnapshotUploader<DownloadSnapshotPResponse, DownloadSnapshotPRequest> requestStreamObserver = SnapshotUploader.forLeader(mStorage, snapshot, responseObserver);
if (snapshot == null) {
responseObserver.onError(Status.NOT_FOUND.withDescription("Cannot find a valid snapshot to download.").asException());
return requestStreamObserver;
}
responseObserver.onNext(DownloadSnapshotPResponse.newBuilder().setData(SnapshotData.newBuilder().setSnapshotTerm(snapshot.getTerm()).setSnapshotIndex(snapshot.getIndex()).setOffset(0)).build());
return requestStreamObserver;
}
Aggregations