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;
}
}
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;
}
}
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;
}
}
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());
}
}
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()]);
}
Aggregations