Search in sources :

Example 51 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project alluxio by Alluxio.

the class SnapshotReplicationManager method requestInfo.

private void requestInfo() {
    Preconditions.checkState(mDownloadState.get() == DownloadState.REQUEST_INFO);
    try {
        SingleFileSnapshotInfo latestSnapshot = mStorage.getLatestSnapshot();
        SnapshotMetadata snapshotMetadata = latestSnapshot == null ? null : SnapshotMetadata.newBuilder().setSnapshotTerm(latestSnapshot.getTerm()).setSnapshotIndex(latestSnapshot.getIndex()).build();
        // build SnapshotInfoRequests
        Map<RaftPeerId, CompletableFuture<RaftClientReply>> jobs = mJournalSystem.getQuorumServerInfoList().stream().filter(server -> server.getServerState() == QuorumServerState.AVAILABLE).map(server -> RaftJournalUtils.getPeerId(server.getServerAddress().getHost(), server.getServerAddress().getRpcPort())).filter(peerId -> !peerId.equals(mJournalSystem.getLocalPeerId())).collect(Collectors.toMap(Function.identity(), peerId -> mJournalSystem.sendMessageAsync(peerId, toMessage(JournalQueryRequest.newBuilder().setSnapshotInfoRequest(GetSnapshotInfoRequest.getDefaultInstance()).build()))));
        // query all secondary masters for information about their latest snapshot
        for (Map.Entry<RaftPeerId, CompletableFuture<RaftClientReply>> job : jobs.entrySet()) {
            RaftPeerId peerId = job.getKey();
            try {
                RaftClientReply reply = job.getValue().get();
                if (reply.getException() != null) {
                    throw reply.getException();
                }
                JournalQueryResponse response = JournalQueryResponse.parseFrom(reply.getMessage().getContent().asReadOnlyByteBuffer());
                if (!response.hasSnapshotInfoResponse()) {
                    throw new IOException("Invalid response for GetSnapshotInfoRequest " + response);
                }
                LOG.debug("Received snapshot info from follower {} - {}", peerId, response);
                SnapshotMetadata latest = response.getSnapshotInfoResponse().getLatest();
                if (snapshotMetadata == null || (latest.getSnapshotTerm() >= snapshotMetadata.getSnapshotTerm()) && latest.getSnapshotIndex() > snapshotMetadata.getSnapshotIndex()) {
                    mSnapshotCandidates.add(new Pair<>(latest, peerId));
                }
            } catch (Exception e) {
                LOG.warn("Error while requesting snapshot info from {}: {}", peerId, e.toString());
            }
        }
    } catch (Exception e) {
        LogUtils.warnWithException(LOG, "Failed to request snapshot info from followers", e);
    }
}
Also used : 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) JournalQueryResponse(alluxio.grpc.JournalQueryResponse) IOException(java.io.IOException) 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) SingleFileSnapshotInfo(org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo) CompletableFuture(java.util.concurrent.CompletableFuture) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SnapshotMetadata(alluxio.grpc.SnapshotMetadata) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) Map(java.util.Map)

Example 52 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project alluxio by Alluxio.

the class SnapshotReplicationManager method requestData.

private boolean requestData() {
    Preconditions.checkState(mDownloadState.get() == DownloadState.REQUEST_DATA);
    // request snapshots from the most recent to least recent
    while (!mSnapshotCandidates.isEmpty()) {
        Pair<SnapshotMetadata, RaftPeerId> candidate = mSnapshotCandidates.poll();
        SnapshotMetadata metadata = candidate.getFirst();
        RaftPeerId peerId = candidate.getSecond();
        LOG.info("Request data from follower {} for snapshot (t: {}, i: {})", peerId, metadata.getSnapshotTerm(), metadata.getSnapshotIndex());
        try {
            RaftClientReply reply = mJournalSystem.sendMessageAsync(peerId, toMessage(JournalQueryRequest.newBuilder().setSnapshotRequest(GetSnapshotRequest.getDefaultInstance()).build())).get();
            if (reply.getException() != null) {
                throw reply.getException();
            }
            return true;
        } catch (Exception e) {
            LOG.warn("Failed to request snapshot data from {}: {}", peerId, e);
        }
    }
    // return failure if there are no more candidates to ask snapshot from
    return false;
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SnapshotMetadata(alluxio.grpc.SnapshotMetadata) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) 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 53 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class NettyRpcService method sendRaftNettyServerRequestProto.

private RaftNettyServerReplyProto sendRaftNettyServerRequestProto(RaftRpcRequestProto request, RaftNettyServerRequestProto proto) throws IOException {
    final RaftPeerId id = RaftPeerId.valueOf(request.getReplyId());
    final NettyRpcProxy p = getProxies().getProxy(id);
    try {
        return p.send(request, proto);
    } catch (ClosedChannelException cce) {
        getProxies().resetProxy(id);
        throw cce;
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) NettyRpcProxy(org.apache.ratis.netty.NettyRpcProxy) RaftPeerId(org.apache.ratis.protocol.RaftPeerId)

Example 54 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class LeaderElection method waitForResults.

private ResultAndTerm waitForResults(final long electionTerm, final int submitted) throws InterruptedException {
    final Timestamp timeout = new Timestamp().addTimeMs(server.getRandomTimeoutMs());
    final List<RequestVoteReplyProto> responses = new ArrayList<>();
    final List<Exception> exceptions = new ArrayList<>();
    int waitForNum = submitted;
    Collection<RaftPeerId> votedPeers = new ArrayList<>();
    while (waitForNum > 0 && running && server.isCandidate()) {
        final long waitTime = -timeout.elapsedTimeMs();
        if (waitTime <= 0) {
            return logAndReturn(Result.TIMEOUT, responses, exceptions, -1);
        }
        try {
            final Future<RequestVoteReplyProto> future = service.poll(waitTime, TimeUnit.MILLISECONDS);
            if (future == null) {
                // poll timeout, continue to return Result.TIMEOUT
                continue;
            }
            final RequestVoteReplyProto r = future.get();
            responses.add(r);
            if (r.getShouldShutdown()) {
                return logAndReturn(Result.SHUTDOWN, responses, exceptions, -1);
            }
            if (r.getTerm() > electionTerm) {
                return logAndReturn(Result.DISCOVERED_A_NEW_TERM, responses, exceptions, r.getTerm());
            }
            if (r.getServerReply().getSuccess()) {
                votedPeers.add(RaftPeerId.valueOf(r.getServerReply().getReplyId()));
                if (conf.hasMajority(votedPeers, server.getId())) {
                    return logAndReturn(Result.PASSED, responses, exceptions, -1);
                }
            }
        } catch (ExecutionException e) {
            LOG.info("{} got exception when requesting votes: {}", server.getId(), e);
            LOG.trace("TRACE", e);
            exceptions.add(e);
        }
        waitForNum--;
    }
    // received all the responses
    return logAndReturn(Result.REJECTED, responses, exceptions, -1);
}
Also used : ArrayList(java.util.ArrayList) RequestVoteReplyProto(org.apache.ratis.shaded.proto.RaftProtos.RequestVoteReplyProto) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) Timestamp(org.apache.ratis.util.Timestamp) IOException(java.io.IOException)

Example 55 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class RaftTestUtil method changeLeader.

static RaftPeerId changeLeader(MiniRaftCluster cluster, RaftPeerId oldLeader) throws InterruptedException {
    cluster.setBlockRequestsFrom(oldLeader.toString(), true);
    try {
        return JavaUtils.attempt(() -> {
            final RaftPeerId newLeader = waitForLeader(cluster).getId();
            Preconditions.assertTrue(!newLeader.equals(oldLeader), () -> "Failed to change leader: newLeader=" + newLeader + " equals oldLeader=" + oldLeader);
            LOG.info("Changed leader from " + oldLeader + " to " + newLeader);
            return newLeader;
        }, 10, 100L, "changeLeader", LOG);
    } finally {
        cluster.setBlockRequestsFrom(oldLeader.toString(), false);
    }
}
Also used : RaftPeerId(org.apache.ratis.protocol.RaftPeerId)

Aggregations

RaftPeerId (org.apache.ratis.protocol.RaftPeerId)101 RaftClient (org.apache.ratis.client.RaftClient)58 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)45 IOException (java.io.IOException)36 Test (org.junit.Test)32 RaftPeer (org.apache.ratis.protocol.RaftPeer)31 RaftServer (org.apache.ratis.server.RaftServer)31 BaseTest (org.apache.ratis.BaseTest)21 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)20 CompletableFuture (java.util.concurrent.CompletableFuture)18 RaftProperties (org.apache.ratis.conf.RaftProperties)18 File (java.io.File)17 ArrayList (java.util.ArrayList)15 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)15 RaftGroup (org.apache.ratis.protocol.RaftGroup)14 TimeDuration (org.apache.ratis.util.TimeDuration)14 List (java.util.List)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 Collectors (java.util.stream.Collectors)11 RaftLog (org.apache.ratis.server.raftlog.RaftLog)11