Search in sources :

Example 1 with DownloadSnapshotPRequest

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);
    }
}
Also used : DownloadSnapshotPRequest(alluxio.grpc.DownloadSnapshotPRequest) TermIndex(org.apache.ratis.server.protocol.TermIndex) PriorityQueue(java.util.PriorityQueue) LoggerFactory(org.slf4j.LoggerFactory) LogUtils(alluxio.util.LogUtils) GetSnapshotInfoResponse(alluxio.grpc.GetSnapshotInfoResponse) StreamObserver(io.grpc.stub.StreamObserver) JournalQueryRequest(alluxio.grpc.JournalQueryRequest) MetricKey(alluxio.metrics.MetricKey) Map(java.util.Map) Status(io.grpc.Status) ClientContext(alluxio.ClientContext) SnapshotInfo(org.apache.ratis.statemachine.SnapshotInfo) SingleFileSnapshotInfo(org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo) UnsafeByteOperations(org.apache.ratis.thirdparty.com.google.protobuf.UnsafeByteOperations) ServerConfiguration(alluxio.conf.ServerConfiguration) JournalQueryResponse(alluxio.grpc.JournalQueryResponse) CompletionException(java.util.concurrent.CompletionException) SimpleStateMachineStorage(org.apache.ratis.statemachine.impl.SimpleStateMachineStorage) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) Timer(com.codahale.metrics.Timer) FileInfo(org.apache.ratis.server.storage.FileInfo) SnapshotData(alluxio.grpc.SnapshotData) MD5FileUtil(org.apache.ratis.util.MD5FileUtil) GetSnapshotInfoRequest(alluxio.grpc.GetSnapshotInfoRequest) UploadSnapshotPResponse(alluxio.grpc.UploadSnapshotPResponse) RaftLog(org.apache.ratis.server.raftlog.RaftLog) DownloadSnapshotPRequest(alluxio.grpc.DownloadSnapshotPRequest) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Message(org.apache.ratis.protocol.Message) GetSnapshotRequest(alluxio.grpc.GetSnapshotRequest) QuorumServerState(alluxio.grpc.QuorumServerState) SnapshotMetadata(alluxio.grpc.SnapshotMetadata) ClientIpAddressInjector(alluxio.security.authentication.ClientIpAddressInjector) AbortedException(alluxio.exception.status.AbortedException) MetricsSystem(alluxio.metrics.MetricsSystem) AlluxioStatusException(alluxio.exception.status.AlluxioStatusException) DownloadSnapshotPResponse(alluxio.grpc.DownloadSnapshotPResponse) Logger(org.slf4j.Logger) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) IOException(java.io.IOException) Pair(alluxio.collections.Pair) NotFoundException(alluxio.exception.status.NotFoundException) File(java.io.File) MessageLite(com.google.protobuf.MessageLite) UploadSnapshotPRequest(alluxio.grpc.UploadSnapshotPRequest) MasterClientContext(alluxio.master.MasterClientContext) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Timer(com.codahale.metrics.Timer) CompletionException(java.util.concurrent.CompletionException) DownloadSnapshotPResponse(alluxio.grpc.DownloadSnapshotPResponse) 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)

Example 2 with DownloadSnapshotPRequest

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;
}
Also used : DownloadSnapshotPRequest(alluxio.grpc.DownloadSnapshotPRequest) SnapshotInfo(org.apache.ratis.statemachine.SnapshotInfo) SingleFileSnapshotInfo(org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo) DownloadSnapshotPResponse(alluxio.grpc.DownloadSnapshotPResponse)

Aggregations

DownloadSnapshotPRequest (alluxio.grpc.DownloadSnapshotPRequest)2 DownloadSnapshotPResponse (alluxio.grpc.DownloadSnapshotPResponse)2 ClientContext (alluxio.ClientContext)1 Pair (alluxio.collections.Pair)1 ServerConfiguration (alluxio.conf.ServerConfiguration)1 AbortedException (alluxio.exception.status.AbortedException)1 AlluxioStatusException (alluxio.exception.status.AlluxioStatusException)1 NotFoundException (alluxio.exception.status.NotFoundException)1 GetSnapshotInfoRequest (alluxio.grpc.GetSnapshotInfoRequest)1 GetSnapshotInfoResponse (alluxio.grpc.GetSnapshotInfoResponse)1 GetSnapshotRequest (alluxio.grpc.GetSnapshotRequest)1 JournalQueryRequest (alluxio.grpc.JournalQueryRequest)1 JournalQueryResponse (alluxio.grpc.JournalQueryResponse)1 QuorumServerState (alluxio.grpc.QuorumServerState)1 SnapshotData (alluxio.grpc.SnapshotData)1 SnapshotMetadata (alluxio.grpc.SnapshotMetadata)1 UploadSnapshotPRequest (alluxio.grpc.UploadSnapshotPRequest)1 UploadSnapshotPResponse (alluxio.grpc.UploadSnapshotPResponse)1 MasterClientContext (alluxio.master.MasterClientContext)1 MetricKey (alluxio.metrics.MetricKey)1