Search in sources :

Example 16 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 17 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 18 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 19 with RaftPeerId

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

the class RaftBasicTests method runTestBasicAppendEntries.

static void runTestBasicAppendEntries(boolean async, int numMessages, MiniRaftCluster cluster, Logger LOG) throws Exception {
    LOG.info("runTestBasicAppendEntries: async? " + async + ", numMessages=" + numMessages);
    RaftServerImpl leader = waitForLeader(cluster);
    final long term = leader.getState().getCurrentTerm();
    final RaftPeerId killed = cluster.getFollowers().get(0).getId();
    cluster.killServer(killed);
    LOG.info(cluster.printServers());
    final SimpleMessage[] messages = SimpleMessage.create(numMessages);
    try (final RaftClient client = cluster.createClient()) {
        final AtomicInteger asyncReplyCount = new AtomicInteger();
        final CompletableFuture<Void> f = new CompletableFuture<>();
        for (SimpleMessage message : messages) {
            if (async) {
                client.sendAsync(message).thenAcceptAsync(reply -> {
                    if (!reply.isSuccess()) {
                        f.completeExceptionally(new AssertionError("Failed with reply " + reply));
                    } else if (asyncReplyCount.incrementAndGet() == messages.length) {
                        f.complete(null);
                    }
                });
            } else {
                client.send(message);
            }
        }
        if (async) {
            f.join();
            Assert.assertEquals(messages.length, asyncReplyCount.get());
        }
    }
    Thread.sleep(cluster.getMaxTimeout() + 100);
    LOG.info(cluster.printAllLogs());
    cluster.getServerAliveStream().map(s -> s.getState().getLog()).forEach(log -> RaftTestUtil.assertLogEntries(log, async, term, messages));
}
Also used : RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RetryCacheTestUtil(org.apache.ratis.server.impl.RetryCacheTestUtil) Timer(java.util.Timer) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Level(org.apache.log4j.Level) After(org.junit.After) JavaUtils(org.apache.ratis.util.JavaUtils) TimerTask(java.util.TimerTask) BlockRequestHandlingInjection(org.apache.ratis.server.impl.BlockRequestHandlingInjection) Before(org.junit.Before) LogUtils(org.apache.ratis.util.LogUtils) Logger(org.slf4j.Logger) RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) Predicate(java.util.function.Predicate) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) RaftClientTestUtil(org.apache.ratis.client.impl.RaftClientTestUtil) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) RaftLog(org.apache.ratis.server.storage.RaftLog) RaftTestUtil(org.apache.ratis.RaftTestUtil) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Stream(java.util.stream.Stream) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) TimeDuration(org.apache.ratis.util.TimeDuration) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient)

Example 20 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)29 Test (org.junit.Test)16 RaftClient (org.apache.ratis.client.RaftClient)15 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)14 IOException (java.io.IOException)13 RaftProperties (org.apache.ratis.conf.RaftProperties)9 Collectors (java.util.stream.Collectors)8 RaftPeer (org.apache.ratis.protocol.RaftPeer)8 List (java.util.List)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 BaseTest (org.apache.ratis.BaseTest)7 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)6 RaftServerConfigKeys (org.apache.ratis.server.RaftServerConfigKeys)6 Assert (org.junit.Assert)6 Logger (org.slf4j.Logger)6 TimeUnit (java.util.concurrent.TimeUnit)5 Level (org.apache.log4j.Level)5 RaftServerImpl (org.apache.ratis.server.impl.RaftServerImpl)5 LogUtils (org.apache.ratis.util.LogUtils)5 TimeDuration (org.apache.ratis.util.TimeDuration)5