Search in sources :

Example 1 with LogEntryBodyCase

use of org.apache.ratis.proto.RaftProtos.LogEntryProto.LogEntryBodyCase in project incubator-ratis by apache.

the class LogAppenderTests method runTest.

void runTest(CLUSTER cluster) throws Exception {
    final int numMsgs = 10;
    final int numClients = 5;
    final RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId();
    List<RaftClient> clients = new ArrayList<>();
    try {
        List<Sender> senders = new ArrayList<>();
        // start several clients and write concurrently
        final CountDownLatch latch = new CountDownLatch(1);
        for (int i = 0; i < numClients; i++) {
            RaftClient client = cluster.createClient(leaderId);
            clients.add(client);
            senders.add(new Sender(client, numMsgs, latch));
        }
        senders.forEach(Thread::start);
        latch.countDown();
        for (Sender s : senders) {
            s.join();
            final Exception e = s.exception.get();
            if (e != null) {
                throw e;
            }
            Assert.assertTrue(s.succeed.get());
        }
    } finally {
        for (int i = 0; i < clients.size(); i++) {
            try {
                clients.get(i).close();
            } catch (Exception ignored) {
                LOG.warn("{} is ignored", JavaUtils.getClassSimpleName(ignored.getClass()), ignored);
            }
        }
    }
    final RaftServer.Division leader = cluster.getLeader();
    final RaftLog leaderLog = cluster.getLeader().getRaftLog();
    final EnumMap<LogEntryBodyCase, AtomicLong> counts = RaftTestUtil.countEntries(leaderLog);
    LOG.info("counts = " + counts);
    Assert.assertEquals(6 * numMsgs * numClients, counts.get(LogEntryBodyCase.STATEMACHINELOGENTRY).get());
    final LogEntryProto last = RaftTestUtil.getLastEntry(LogEntryBodyCase.STATEMACHINELOGENTRY, leaderLog);
    LOG.info("last = {}", LogProtoUtils.toLogEntryString(last));
    Assert.assertNotNull(last);
    Assert.assertTrue(last.getIndex() <= leader.getInfo().getLastAppliedIndex());
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) LogEntryBodyCase(org.apache.ratis.proto.RaftProtos.LogEntryProto.LogEntryBodyCase) AtomicLong(java.util.concurrent.atomic.AtomicLong) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient) RaftLog(org.apache.ratis.server.raftlog.RaftLog)

Example 2 with LogEntryBodyCase

use of org.apache.ratis.proto.RaftProtos.LogEntryProto.LogEntryBodyCase in project incubator-ratis by apache.

the class RaftTestUtil method countEntries.

static EnumMap<LogEntryBodyCase, AtomicLong> countEntries(RaftLog raftLog) throws Exception {
    final EnumMap<LogEntryBodyCase, AtomicLong> counts = new EnumMap<>(LogEntryBodyCase.class);
    for (long i = 0; i < raftLog.getNextIndex(); i++) {
        final LogEntryProto e = raftLog.get(i);
        counts.computeIfAbsent(e.getLogEntryBodyCase(), c -> new AtomicLong()).incrementAndGet();
    }
    return counts;
}
Also used : Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) BooleanSupplier(java.util.function.BooleanSupplier) ProtoUtils(org.apache.ratis.util.ProtoUtils) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) JavaUtils(org.apache.ratis.util.JavaUtils) BlockRequestHandlingInjection(org.apache.ratis.server.impl.BlockRequestHandlingInjection) EnumMap(java.util.EnumMap) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) Predicate(java.util.function.Predicate) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) Objects(java.util.Objects) List(java.util.List) LogEntryBodyCase(org.apache.ratis.proto.RaftProtos.LogEntryProto.LogEntryBodyCase) ClientId(org.apache.ratis.protocol.ClientId) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) Preconditions(org.apache.ratis.thirdparty.com.google.common.base.Preconditions) Optional(java.util.Optional) DelayLocalExecutionInjection(org.apache.ratis.server.impl.DelayLocalExecutionInjection) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) LogProtoUtils(org.apache.ratis.server.raftlog.LogProtoUtils) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) RaftLog(org.apache.ratis.server.raftlog.RaftLog) CollectionUtils(org.apache.ratis.util.CollectionUtils) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) Message(org.apache.ratis.protocol.Message) StreamSupport(java.util.stream.StreamSupport) IntSupplier(java.util.function.IntSupplier) AssumptionViolatedException(org.junit.AssumptionViolatedException) Logger(org.slf4j.Logger) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) IOException(java.io.IOException) Field(java.lang.reflect.Field) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) RaftServer(org.apache.ratis.server.RaftServer) RaftLogBase(org.apache.ratis.server.raftlog.RaftLogBase) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) AtomicLong(java.util.concurrent.atomic.AtomicLong) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) EnumMap(java.util.EnumMap) LogEntryBodyCase(org.apache.ratis.proto.RaftProtos.LogEntryProto.LogEntryBodyCase)

Example 3 with LogEntryBodyCase

use of org.apache.ratis.proto.RaftProtos.LogEntryProto.LogEntryBodyCase in project incubator-ratis by apache.

the class OutputStreamBaseTest method assertRaftLog.

private RaftLog assertRaftLog(int expectedEntries, RaftServer.Division server) throws Exception {
    final RaftLog raftLog = server.getRaftLog();
    final EnumMap<LogEntryBodyCase, AtomicLong> counts = RaftTestUtil.countEntries(raftLog);
    Assert.assertEquals(expectedEntries, counts.get(LogEntryBodyCase.STATEMACHINELOGENTRY).get());
    final LogEntryProto last = RaftTestUtil.getLastEntry(LogEntryBodyCase.STATEMACHINELOGENTRY, raftLog);
    Assert.assertNotNull(last);
    Assert.assertTrue(raftLog.getLastCommittedIndex() >= last.getIndex());
    Assert.assertTrue(server.getInfo().getLastAppliedIndex() >= last.getIndex());
    return raftLog;
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftLog(org.apache.ratis.server.raftlog.RaftLog) LogEntryBodyCase(org.apache.ratis.proto.RaftProtos.LogEntryProto.LogEntryBodyCase)

Aggregations

AtomicLong (java.util.concurrent.atomic.AtomicLong)3 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)3 LogEntryBodyCase (org.apache.ratis.proto.RaftProtos.LogEntryProto.LogEntryBodyCase)3 RaftLog (org.apache.ratis.server.raftlog.RaftLog)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 RaftClient (org.apache.ratis.client.RaftClient)2 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)2 RaftServer (org.apache.ratis.server.RaftServer)2 Field (java.lang.reflect.Field)1 Arrays (java.util.Arrays)1 EnumMap (java.util.EnumMap)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1