use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class TestLogSegment method checkLogSegment.
static void checkLogSegment(LogSegment segment, long start, long end, boolean isOpen, long totalSize, long term) throws Exception {
Assert.assertEquals(start, segment.getStartIndex());
Assert.assertEquals(end, segment.getEndIndex());
Assert.assertEquals(isOpen, segment.isOpen());
Assert.assertEquals(totalSize, segment.getTotalFileSize());
long offset = SegmentedRaftLogFormat.getHeaderLength();
for (long i = start; i <= end; i++) {
LogSegment.LogRecord record = segment.getLogRecord(i);
final TermIndex ti = record.getTermIndex();
Assert.assertEquals(i, ti.getIndex());
Assert.assertEquals(term, ti.getTerm());
Assert.assertEquals(offset, record.getOffset());
LogEntryProto entry = segment.getEntryFromCache(ti);
if (entry == null) {
entry = segment.loadCache(record);
}
offset += getEntrySize(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
}
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class TestSegmentedRaftLog method testAppendEntryAfterPurge.
@Test
public void testAppendEntryAfterPurge() throws Exception {
List<SegmentRange> ranges = prepareRanges(0, 5, 200, 0);
List<LogEntryProto> entries = prepareLogEntries(ranges, null);
long desiredSnapshotIndex = entries.size() - 2;
final LongSupplier getSnapshotIndexFromStateMachine = new LongSupplier() {
private boolean firstCall = true;
@Override
public long getAsLong() {
long index = firstCall ? -1 : desiredSnapshotIndex;
firstCall = !firstCall;
return index;
}
};
try (SegmentedRaftLog raftLog = newSegmentedRaftLog(getSnapshotIndexFromStateMachine)) {
raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
entries.subList(0, entries.size() - 1).stream().map(raftLog::appendEntry).forEach(CompletableFuture::join);
raftLog.onSnapshotInstalled(desiredSnapshotIndex);
// Try appending last entry after snapshot + purge.
CompletableFuture<Long> appendEntryFuture = raftLog.appendEntry(entries.get(entries.size() - 1));
assertTrue(desiredSnapshotIndex + 1 == appendEntryFuture.get());
}
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto 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.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class TestSegmentedRaftLog method purgeAndVerify.
private void purgeAndVerify(int startTerm, int endTerm, int segmentSize, int purgeGap, long purgeIndex, long expectedIndex) throws Exception {
List<SegmentRange> ranges = prepareRanges(startTerm, endTerm, segmentSize, 0);
List<LogEntryProto> entries = prepareLogEntries(ranges, null);
final RaftProperties p = new RaftProperties();
RaftServerConfigKeys.Log.setPurgeGap(p, purgeGap);
try (SegmentedRaftLog raftLog = newSegmentedRaftLog(storage, p)) {
raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
entries.stream().map(raftLog::appendEntry).forEach(CompletableFuture::join);
final CompletableFuture<Long> f = raftLog.purge(purgeIndex);
final Long purged = f.get();
LOG.info("purgeIndex = {}, purged = {}", purgeIndex, purged);
Assert.assertEquals(expectedIndex, raftLog.getRaftLogCache().getStartIndex());
}
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto 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);
}
}
Aggregations