use of org.apache.ratis.server.raftlog.LogEntryHeader in project incubator-ratis by apache.
the class LeaderStateImpl method updateCommit.
private void updateCommit(LogEntryHeader[] entriesToCommit) {
final long newCommitIndex = raftLog.getLastCommittedIndex();
logMetadata(newCommitIndex);
commitIndexChanged();
boolean hasConfiguration = false;
for (LogEntryHeader entry : entriesToCommit) {
if (entry.getIndex() > newCommitIndex) {
break;
}
hasConfiguration |= entry.getLogEntryBodyCase() == LogEntryBodyCase.CONFIGURATIONENTRY;
raftLog.getRaftLogMetrics().onLogEntryCommitted(entry);
}
if (hasConfiguration) {
checkAndUpdateConfiguration();
}
}
use of org.apache.ratis.server.raftlog.LogEntryHeader in project incubator-ratis by apache.
the class TestSegmentedRaftLog method checkEntries.
private void checkEntries(RaftLog raftLog, List<LogEntryProto> expected, int offset, int size) throws IOException {
if (size > 0) {
for (int i = offset; i < size + offset; i++) {
LogEntryProto entry = raftLog.get(expected.get(i).getIndex());
Assert.assertEquals(expected.get(i), entry);
}
final LogEntryHeader[] termIndices = raftLog.getEntries(expected.get(offset).getIndex(), expected.get(offset + size - 1).getIndex() + 1);
LogEntryProto[] entriesFromLog = Arrays.stream(termIndices).map(ti -> {
try {
return raftLog.get(ti.getIndex());
} catch (IOException e) {
throw new RuntimeException(e);
}
}).toArray(LogEntryProto[]::new);
LogEntryProto[] expectedArray = expected.subList(offset, offset + size).stream().toArray(LogEntryProto[]::new);
Assert.assertArrayEquals(expectedArray, entriesFromLog);
}
}
use of org.apache.ratis.server.raftlog.LogEntryHeader in project incubator-ratis by apache.
the class MemoryRaftLogTest method testEntryPerformTruncation.
@Test
public void testEntryPerformTruncation() throws Exception {
final RaftProperties prop = new RaftProperties();
prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class);
final RaftPeerId peerId = RaftPeerId.valueOf("s0");
final RaftGroupId groupId = RaftGroupId.randomId();
final RaftGroupMemberId memberId = RaftGroupMemberId.valueOf(peerId, groupId);
MemoryRaftLog raftLog = new MemoryRaftLog(memberId, () -> -1, prop);
raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
LogEntryProto[] entries1 = new LogEntryProto[2];
entries1[0] = LogEntryProto.newBuilder().setIndex(0).setTerm(0).build();
entries1[1] = LogEntryProto.newBuilder().setIndex(1).setTerm(0).build();
raftLog.append(entries1).forEach(CompletableFuture::join);
LogEntryProto[] entries2 = new LogEntryProto[1];
entries2[0] = LogEntryProto.newBuilder().setIndex(0).setTerm(2).build();
raftLog.append(entries2).forEach(CompletableFuture::join);
final LogEntryHeader[] termIndices = raftLog.getEntries(0, 10);
assertEquals(1, termIndices.length);
assertEquals(entries2[0].getIndex(), termIndices[0].getIndex());
assertEquals(entries2[0].getTerm(), termIndices[0].getTerm());
}
use of org.apache.ratis.server.raftlog.LogEntryHeader in project incubator-ratis by apache.
the class TestSegmentedRaftLog method testLoadLogSegments.
@Test
public void testLoadLogSegments() throws Exception {
// first generate log files
List<SegmentRange> ranges = prepareRanges(0, 5, 100, 0);
LogEntryProto[] entries = prepareLog(ranges);
// create RaftLog object and load log file
try (SegmentedRaftLog raftLog = newSegmentedRaftLog()) {
raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
// check if log entries are loaded correctly
for (LogEntryProto e : entries) {
LogEntryProto entry = raftLog.get(e.getIndex());
Assert.assertEquals(e, entry);
}
final LogEntryHeader[] termIndices = raftLog.getEntries(0, 500);
LogEntryProto[] entriesFromLog = Arrays.stream(termIndices).map(ti -> {
try {
return raftLog.get(ti.getIndex());
} catch (IOException e) {
throw new RuntimeException(e);
}
}).toArray(LogEntryProto[]::new);
Assert.assertArrayEquals(entries, entriesFromLog);
Assert.assertEquals(entries[entries.length - 1], getLastEntry(raftLog));
final RatisMetricRegistry metricRegistryForLogWorker = RaftLogMetricsBase.getLogWorkerMetricRegistry(memberId);
Timer raftLogSegmentLoadLatencyTimer = metricRegistryForLogWorker.timer("segmentLoadLatency");
assertTrue(raftLogSegmentLoadLatencyTimer.getMeanRate() > 0);
Timer raftLogReadLatencyTimer = metricRegistryForLogWorker.timer("readEntryLatency");
assertTrue(raftLogReadLatencyTimer.getMeanRate() > 0);
}
}
use of org.apache.ratis.server.raftlog.LogEntryHeader 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);
}
Aggregations