Search in sources :

Example 21 with LogEntryProto

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

the class RaftLog method append.

/**
 * Generate a log entry for the given term and message, and append the entry.
 * Used by the leader.
 * @return the index of the new log entry.
 */
public long append(long term, TransactionContext operation, ClientId clientId, long callId) throws StateMachineException {
    checkLogState();
    try (AutoCloseableLock writeLock = writeLock()) {
        final long nextIndex = getNextIndex();
        // the SM wants to attach a logic depending on ordered execution in the log commit order.
        try {
            operation = operation.preAppendTransaction();
        } catch (IOException e) {
            throw new StateMachineException(selfId, e);
        }
        // build the log entry after calling the StateMachine
        final LogEntryProto e = ProtoUtils.toLogEntryProto(operation.getSMLogEntry(), term, nextIndex, clientId, callId);
        int entrySize = e.getSerializedSize();
        if (entrySize > maxBufferSize) {
            throw new StateMachineException(selfId, new RaftLogIOException("Log entry size " + entrySize + " exceeds the max buffer limit of " + maxBufferSize));
        }
        appendEntry(e);
        operation.setLogEntry(e);
        return nextIndex;
    }
}
Also used : StateMachineException(org.apache.ratis.protocol.StateMachineException) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) IOException(java.io.IOException)

Example 22 with LogEntryProto

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

the class RaftLog method append.

/**
 * Generate a log entry for the given term and configurations,
 * and append the entry. Used by the leader.
 * @return the index of the new log entry.
 */
public long append(long term, RaftConfiguration newConf) {
    checkLogState();
    try (AutoCloseableLock writeLock = writeLock()) {
        final long nextIndex = getNextIndex();
        final LogEntryProto e = ServerProtoUtils.toLogEntryProto(newConf, term, nextIndex);
        appendEntry(e);
        return nextIndex;
    }
}
Also used : LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Example 23 with LogEntryProto

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

the class SegmentedRaftLog method append.

@Override
public List<CompletableFuture<Long>> append(LogEntryProto... entries) {
    checkLogState();
    if (entries == null || entries.length == 0) {
        return Collections.emptyList();
    }
    try (AutoCloseableLock writeLock = writeLock()) {
        Iterator<TermIndex> iter = cache.iterator(entries[0].getIndex());
        int index = 0;
        long truncateIndex = -1;
        for (; iter.hasNext() && index < entries.length; index++) {
            TermIndex storedEntry = iter.next();
            Preconditions.assertTrue(storedEntry.getIndex() == entries[index].getIndex(), "The stored entry's index %s is not consistent with" + " the received entries[%s]'s index %s", storedEntry.getIndex(), index, entries[index].getIndex());
            if (storedEntry.getTerm() != entries[index].getTerm()) {
                // we should truncate from the storedEntry's index
                truncateIndex = storedEntry.getIndex();
                if (LOG.isTraceEnabled()) {
                    LOG.trace("{}: truncate to {}, index={}, ti={}, storedEntry={}, entries={}", server.getId(), truncateIndex, index, ServerProtoUtils.toTermIndex(entries[index]), storedEntry, ServerProtoUtils.toString(entries));
                }
                while (true) {
                    try {
                        final LogEntryProto entry = get(storedEntry.getIndex());
                        server.failClientRequest(entry);
                    } catch (RaftLogIOException e) {
                        LOG.error("Failed to read log " + storedEntry, e);
                    }
                    if (iter.hasNext()) {
                        storedEntry = iter.next();
                    } else {
                        break;
                    }
                }
                break;
            }
        }
        final List<CompletableFuture<Long>> futures;
        if (truncateIndex != -1) {
            futures = new ArrayList<>(entries.length - index + 1);
            futures.add(truncate(truncateIndex));
        } else {
            futures = new ArrayList<>(entries.length - index);
        }
        for (int i = index; i < entries.length; i++) {
            futures.add(appendEntry(entries[i]));
        }
        return futures;
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 24 with LogEntryProto

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

the class RaftTestUtil method assertLogEntries.

static void assertLogEntries(RaftLog log, boolean async, long expectedTerm, SimpleMessage... expectedMessages) {
    final TermIndex[] termIndices = log.getEntries(1, Long.MAX_VALUE);
    final List<LogEntryProto> entries = new ArrayList<>(expectedMessages.length);
    for (TermIndex ti : termIndices) {
        final LogEntryProto e;
        try {
            e = log.get(ti.getIndex());
        } catch (IOException exception) {
            throw new AssertionError("Failed to get log at " + ti, exception);
        }
        if (e.getLogEntryBodyCase() == LogEntryProto.LogEntryBodyCase.SMLOGENTRY) {
            entries.add(e);
        } else if (e.getLogEntryBodyCase() == LogEntryProto.LogEntryBodyCase.NOOP) {
            LOG.info("Found " + LogEntryProto.LogEntryBodyCase.NOOP + " at " + ti + ", ignoring it.");
        } else {
            throw new AssertionError("Unexpected LogEntryBodyCase " + e.getLogEntryBodyCase() + " at " + ti);
        }
    }
    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();
        }
        if (!async) {
            Assert.assertTrue(e.getIndex() > logIndex);
        }
        logIndex = e.getIndex();
        Assert.assertArrayEquals(expectedMessages[i].getContent().toByteArray(), e.getSmLogEntry().getData().toByteArray());
    }
}
Also used : SMLogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) IOException(java.io.IOException) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 25 with LogEntryProto

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

the class TestCacheEviction method generateEntries.

private LogEntryProto[] generateEntries(List<SegmentRange> slist) {
    List<LogEntryProto> eList = new ArrayList<>();
    for (SegmentRange range : slist) {
        for (long index = range.start; index <= range.end; index++) {
            SimpleOperation m = new SimpleOperation(new String(new byte[1024]));
            eList.add(ProtoUtils.toLogEntryProto(m.getLogEntryContent(), range.term, index, clientId, callId));
        }
    }
    return eList.toArray(new LogEntryProto[eList.size()]);
}
Also used : SegmentRange(org.apache.ratis.server.storage.TestSegmentedRaftLog.SegmentRange) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) ArrayList(java.util.ArrayList) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation)

Aggregations

LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)36 SimpleOperation (org.apache.ratis.RaftTestUtil.SimpleOperation)16 Test (org.junit.Test)16 BaseTest (org.apache.ratis.BaseTest)14 File (java.io.File)12 ArrayList (java.util.ArrayList)8 SMLogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto)8 IOException (java.io.IOException)7 CompletableFuture (java.util.concurrent.CompletableFuture)6 RandomAccessFile (java.io.RandomAccessFile)5 RaftServerImpl (org.apache.ratis.server.impl.RaftServerImpl)5 List (java.util.List)4 RaftProperties (org.apache.ratis.conf.RaftProperties)4 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)4 TermIndex (org.apache.ratis.server.protocol.TermIndex)4 AutoCloseableLock (org.apache.ratis.util.AutoCloseableLock)4 Level (org.apache.log4j.Level)3 RaftServerConfigKeys (org.apache.ratis.server.RaftServerConfigKeys)3 RetryCache (org.apache.ratis.server.impl.RetryCache)3 RetryCacheTestUtil (org.apache.ratis.server.impl.RetryCacheTestUtil)3