use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class TestRaftLogSegment method testTruncate.
@Test
public void testTruncate() throws Exception {
final long term = 1;
final long start = 1000;
LogSegment segment = LogSegment.newOpenSegment(null, start);
for (int i = 0; i < 100; i++) {
LogEntryProto entry = ProtoUtils.toLogEntryProto(new SimpleOperation("m" + i).getLogEntryContent(), term, i + start, clientId, callId);
segment.appendToOpenSegment(entry);
}
// truncate an open segment (remove 1080~1099)
long newSize = segment.getLogRecord(start + 80).getOffset();
segment.truncate(start + 80);
Assert.assertEquals(80, segment.numOfEntries());
checkLogSegment(segment, start, start + 79, false, newSize, term);
// truncate a closed segment (remove 1050~1079)
newSize = segment.getLogRecord(start + 50).getOffset();
segment.truncate(start + 50);
Assert.assertEquals(50, segment.numOfEntries());
checkLogSegment(segment, start, start + 49, false, newSize, term);
// truncate all the remaining entries
segment.truncate(start);
Assert.assertEquals(0, segment.numOfEntries());
checkLogSegment(segment, start, start - 1, false, SegmentedRaftLog.HEADER_BYTES.length, term);
}
use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class TestRaftLogSegment method checkLogSegment.
private 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.getTotalSize());
long offset = SegmentedRaftLog.HEADER_BYTES.length;
for (long i = start; i <= end; i++) {
LogSegment.LogRecord record = segment.getLogRecord(i);
LogRecordWithEntry lre = segment.getEntryWithoutLoading(i);
Assert.assertEquals(i, lre.getRecord().getTermIndex().getIndex());
Assert.assertEquals(term, lre.getRecord().getTermIndex().getTerm());
Assert.assertEquals(offset, record.getOffset());
LogEntryProto entry = lre.hasEntry() ? lre.getEntry() : segment.loadCache(lre.getRecord());
offset += getEntrySize(entry);
}
}
use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class TestRaftLogSegment method testAppendWithGap.
@Test
public void testAppendWithGap() throws Exception {
LogSegment segment = LogSegment.newOpenSegment(null, 1000);
SimpleOperation op = new SimpleOperation("m");
final SMLogEntryProto m = op.getLogEntryContent();
try {
LogEntryProto entry = ProtoUtils.toLogEntryProto(m, 0, 1001, clientId, callId);
segment.appendToOpenSegment(entry);
Assert.fail("should fail since the entry's index needs to be 1000");
} catch (IllegalStateException e) {
// the exception is expected.
}
LogEntryProto entry = ProtoUtils.toLogEntryProto(m, 0, 1000, clientId, callId);
segment.appendToOpenSegment(entry);
try {
entry = ProtoUtils.toLogEntryProto(m, 0, 1002, clientId, callId);
segment.appendToOpenSegment(entry);
Assert.fail("should fail since the entry's index needs to be 1001");
} catch (IllegalStateException e) {
// the exception is expected.
}
LogEntryProto[] entries = new LogEntryProto[2];
for (int i = 0; i < 2; i++) {
entries[i] = ProtoUtils.toLogEntryProto(m, 0, 1001 + i * 2, clientId, callId);
}
try {
segment.appendToOpenSegment(entries);
Assert.fail("should fail since there is gap between entries");
} catch (IllegalStateException e) {
// the exception is expected.
}
}
use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class TestRaftLogSegment method prepareLog.
private File prepareLog(boolean isOpen, long start, int size, long term) throws IOException {
RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR);
File file = isOpen ? storage.getStorageDir().getOpenLogFile(start) : storage.getStorageDir().getClosedLogFile(start, start + size - 1);
LogEntryProto[] entries = new LogEntryProto[size];
try (LogOutputStream out = new LogOutputStream(file, false, segmentMaxSize, preallocatedSize, bufferSize)) {
for (int i = 0; i < size; i++) {
SimpleOperation op = new SimpleOperation("m" + i);
entries[i] = ProtoUtils.toLogEntryProto(op.getLogEntryContent(), term, i + start, clientId, callId);
out.write(entries[i]);
}
}
storage.close();
return file;
}
use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class TestSegmentedRaftLog method prepareLogEntries.
List<LogEntryProto> prepareLogEntries(List<SegmentRange> slist, Supplier<String> stringSupplier) {
List<LogEntryProto> eList = new ArrayList<>();
for (SegmentRange range : slist) {
for (long index = range.start; index <= range.end; index++) {
SimpleOperation m = stringSupplier == null ? new SimpleOperation("m" + index) : new SimpleOperation(stringSupplier.get());
eList.add(ProtoUtils.toLogEntryProto(m.getLogEntryContent(), range.term, index, clientId, index));
}
}
return eList;
}
Aggregations