Search in sources :

Example 21 with LogEntryProto

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

the class SimpleStateMachine4Testing method loadSnapshot.

private synchronized long loadSnapshot(SingleFileSnapshotInfo snapshot) throws IOException {
    if (snapshot == null || !snapshot.getFile().getPath().toFile().exists()) {
        LOG.info("The snapshot file {} does not exist", snapshot == null ? null : snapshot.getFile());
        return RaftLog.INVALID_LOG_INDEX;
    } else {
        LOG.info("Loading snapshot {}", snapshot);
        final long endIndex = snapshot.getIndex();
        try (SegmentedRaftLogInputStream in = new SegmentedRaftLogInputStream(snapshot.getFile().getPath().toFile(), 0, endIndex, false)) {
            LogEntryProto entry;
            while ((entry = in.nextEntry()) != null) {
                put(entry);
                updateLastAppliedTermIndex(entry.getTerm(), entry.getIndex());
            }
        }
        // The end index is greater than last entry in indexMap as it also
        // includes the configuration and metadata entries
        Preconditions.assertTrue(!indexMap.isEmpty() && endIndex >= indexMap.lastKey(), "endIndex=%s, indexMap=%s", endIndex, indexMap);
        this.endIndexLastCkpt = endIndex;
        setLastAppliedTermIndex(snapshot.getTermIndex());
        this.storage.loadLatestSnapshot();
        return endIndex;
    }
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) SegmentedRaftLogInputStream(org.apache.ratis.server.raftlog.segmented.SegmentedRaftLogInputStream)

Example 22 with LogEntryProto

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

the class RaftBasicTests method runTestOldLeaderNotCommit.

void runTestOldLeaderNotCommit(CLUSTER cluster) throws Exception {
    final RaftPeerId leaderId = waitForLeader(cluster).getId();
    final List<RaftServer.Division> followers = cluster.getFollowers();
    final RaftServer.Division followerToCommit = followers.get(0);
    try {
        for (int i = 1; i < NUM_SERVERS - 1; i++) {
            cluster.killServer(followers.get(i).getId());
        }
    } catch (IndexOutOfBoundsException e) {
        throw new org.junit.AssumptionViolatedException("The assumption is follower.size() = NUM_SERVERS - 1, " + "actual NUM_SERVERS is " + NUM_SERVERS + ", and actual follower.size() is " + followers.size(), e);
    }
    SimpleMessage[] messages = SimpleMessage.create(1);
    RaftTestUtil.sendMessageInNewThread(cluster, leaderId, messages);
    Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) + 100);
    RaftTestUtil.logEntriesContains(followerToCommit.getRaftLog(), messages);
    cluster.killServer(leaderId);
    cluster.killServer(followerToCommit.getId());
    for (int i = 1; i < NUM_SERVERS - 1; i++) {
        cluster.restartServer(followers.get(i).getId(), false);
    }
    waitForLeader(cluster);
    Thread.sleep(cluster.getTimeoutMax().toLong(TimeUnit.MILLISECONDS) + 100);
    final Predicate<LogEntryProto> predicate = l -> l.getTerm() != 1;
    cluster.getServerAliveStream().map(RaftServer.Division::getRaftLog).forEach(log -> RaftTestUtil.checkLogEntries(log, messages, predicate));
}
Also used : RetryCacheTestUtil(org.apache.ratis.server.impl.RetryCacheTestUtil) Timer(java.util.Timer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Level(org.apache.log4j.Level) JavaUtils(org.apache.ratis.util.JavaUtils) TimerTask(java.util.TimerTask) BlockRequestHandlingInjection(org.apache.ratis.server.impl.BlockRequestHandlingInjection) RaftTestUtil.waitForLeader(org.apache.ratis.RaftTestUtil.waitForLeader) STATEMACHINE_APPLIED_INDEX_GAUGE(org.apache.ratis.server.impl.StateMachineMetrics.STATEMACHINE_APPLIED_INDEX_GAUGE) Timestamp(org.apache.ratis.util.Timestamp) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) Predicate(java.util.function.Predicate) Set(java.util.Set) RATIS_STATEMACHINE_METRICS(org.apache.ratis.server.impl.StateMachineMetrics.RATIS_STATEMACHINE_METRICS) Collectors(java.util.stream.Collectors) RATIS_STATEMACHINE_METRICS_DESC(org.apache.ratis.server.impl.StateMachineMetrics.RATIS_STATEMACHINE_METRICS_DESC) MetricRegistries(org.apache.ratis.metrics.MetricRegistries) MetricRegistryInfo(org.apache.ratis.metrics.MetricRegistryInfo) List(java.util.List) Stream(java.util.stream.Stream) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) Optional(java.util.Optional) ClientInvocationId(org.apache.ratis.protocol.ClientInvocationId) Gauge(com.codahale.metrics.Gauge) SortedMap(java.util.SortedMap) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) RaftGroupMemberId(org.apache.ratis.protocol.RaftGroupMemberId) RaftLog(org.apache.ratis.server.raftlog.RaftLog) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) RATIS_APPLICATION_NAME_METRICS(org.apache.ratis.metrics.RatisMetrics.RATIS_APPLICATION_NAME_METRICS) Log4jUtils(org.apache.ratis.util.Log4jUtils) RaftServerTestUtil(org.apache.ratis.server.impl.RaftServerTestUtil) RaftServerMetricsImpl(org.apache.ratis.server.metrics.RaftServerMetricsImpl) Logger(org.slf4j.Logger) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) RaftClientTestUtil(org.apache.ratis.client.impl.RaftClientTestUtil) STATEMACHINE_APPLY_COMPLETED_GAUGE(org.apache.ratis.server.impl.StateMachineMetrics.STATEMACHINE_APPLY_COMPLETED_GAUGE) Test(org.junit.Test) IOException(java.io.IOException) RatisMetricRegistry(org.apache.ratis.metrics.RatisMetricRegistry) TimeUnit(java.util.concurrent.TimeUnit) ExitUtils(org.apache.ratis.util.ExitUtils) RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftPeerId(org.apache.ratis.protocol.RaftPeerId)

Example 23 with LogEntryProto

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

the class RaftTestUtil method assertLogEntries.

static void assertLogEntries(List<LogEntryProto> entries, long expectedTerm, SimpleMessage... expectedMessages) {
    long logIndex = 0;
    Assert.assertEquals(expectedMessages.length, entries.size());
    for (int i = 0; i < expectedMessages.length; i++) {
        final LogEntryProto e = entries.get(i);
        Assert.assertTrue(e.getTerm() >= expectedTerm);
        if (e.getTerm() > expectedTerm) {
            expectedTerm = e.getTerm();
        }
        Assert.assertTrue(e.getIndex() > logIndex);
        logIndex = e.getIndex();
        Assert.assertArrayEquals(expectedMessages[i].getContent().toByteArray(), e.getStateMachineLogEntry().getLogData().toByteArray());
    }
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)

Example 24 with LogEntryProto

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

the class OutputStreamBaseTest method checkLog.

private void checkLog(RaftLog raftLog, long expectedCommittedIndex, Supplier<byte[]> s) throws IOException {
    long committedIndex = raftLog.getLastCommittedIndex();
    Assert.assertTrue(committedIndex >= expectedCommittedIndex);
    // check the log content
    final LogEntryHeader[] entries = raftLog.getEntries(0, Long.MAX_VALUE);
    int count = 0;
    for (LogEntryHeader entry : entries) {
        LogEntryProto log = raftLog.get(entry.getIndex());
        if (!log.hasStateMachineLogEntry()) {
            continue;
        }
        byte[] logData = log.getStateMachineLogEntry().getLogData().toByteArray();
        byte[] expected = s.get();
        final String message = "log " + entry + " " + log.getLogEntryBodyCase() + " " + StringUtils.bytes2HexString(logData) + ", expected=" + StringUtils.bytes2HexString(expected);
        Assert.assertArrayEquals(message, expected, logData);
        count++;
    }
    Assert.assertEquals(expectedCommittedIndex, count);
}
Also used : LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto)

Example 25 with LogEntryProto

use of org.apache.ratis.proto.RaftProtos.LogEntryProto 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)

Aggregations

LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)61 Test (org.junit.Test)22 BaseTest (org.apache.ratis.BaseTest)20 SimpleOperation (org.apache.ratis.RaftTestUtil.SimpleOperation)15 CompletableFuture (java.util.concurrent.CompletableFuture)14 File (java.io.File)13 IOException (java.io.IOException)13 StateMachineLogEntryProto (org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)12 RaftLog (org.apache.ratis.server.raftlog.RaftLog)12 RaftStorage (org.apache.ratis.server.storage.RaftStorage)11 ArrayList (java.util.ArrayList)10 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)10 TermIndex (org.apache.ratis.server.protocol.TermIndex)9 LogEntryHeader (org.apache.ratis.server.raftlog.LogEntryHeader)7 RaftClient (org.apache.ratis.client.RaftClient)6 RaftProperties (org.apache.ratis.conf.RaftProperties)6 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)6 RaftGroupMemberId (org.apache.ratis.protocol.RaftGroupMemberId)6 RaftServer (org.apache.ratis.server.RaftServer)6 RandomAccessFile (java.io.RandomAccessFile)5