use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestSegmentedRaftLogCache method prepareLogSegment.
private LogSegment prepareLogSegment(long start, long end, boolean isOpen) {
LogSegment s = LogSegment.newOpenSegment(null, start, null);
for (long i = start; i <= end; i++) {
SimpleOperation m = new SimpleOperation("m" + i);
LogEntryProto entry = LogProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i);
s.appendToOpenSegment(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
}
if (!isOpen) {
s.close();
}
return s;
}
use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestSegmentedRaftLogCache method testAppendEntry.
@Test
public void testAppendEntry() throws Exception {
LogSegment closedSegment = prepareLogSegment(0, 99, false);
cache.addSegment(closedSegment);
final SimpleOperation m = new SimpleOperation("m");
try {
LogEntryProto entry = LogProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, 0);
cache.appendEntry(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
Assert.fail("the open segment is null");
} catch (IllegalStateException ignored) {
}
LogSegment openSegment = prepareLogSegment(100, 100, true);
cache.addSegment(openSegment);
for (long index = 101; index < 200; index++) {
LogEntryProto entry = LogProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, index);
cache.appendEntry(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
}
Assert.assertNotNull(cache.getOpenSegment());
checkCache(0, 199, 100);
}
use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestLogSegment method prepareLog.
File prepareLog(boolean isOpen, long startIndex, int numEntries, long term, boolean isLastEntryPartiallyWritten) throws IOException {
if (!isOpen) {
Preconditions.assertTrue(!isLastEntryPartiallyWritten, "For closed log, the last entry cannot be partially written.");
}
RaftStorage storage = RaftStorageTestUtils.newRaftStorage(storageDir);
final File file = LogSegmentStartEnd.valueOf(startIndex, startIndex + numEntries - 1, isOpen).getFile(storage);
final LogEntryProto[] entries = new LogEntryProto[numEntries];
try (SegmentedRaftLogOutputStream out = new SegmentedRaftLogOutputStream(file, false, segmentMaxSize, preallocatedSize, ByteBuffer.allocateDirect(bufferSize))) {
for (int i = 0; i < entries.length; i++) {
SimpleOperation op = new SimpleOperation("m" + i);
entries[i] = LogProtoUtils.toLogEntryProto(op.getLogEntryContent(), term, i + startIndex);
out.write(entries[i]);
}
}
if (isLastEntryPartiallyWritten) {
final int entrySize = size(entries[entries.length - 1]);
final int truncatedEntrySize = ThreadLocalRandom.current().nextInt(entrySize - 1) + 1;
// 0 < truncatedEntrySize < entrySize
final long fileLength = file.length();
final long truncatedFileLength = fileLength - (entrySize - truncatedEntrySize);
LOG.info("truncate last entry: entry(size={}, truncated={}), file(length={}, truncated={})", entrySize, truncatedEntrySize, fileLength, truncatedFileLength);
FileUtils.truncateFile(file, truncatedFileLength);
}
storage.close();
return file;
}
use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestLogSegment method testAppendWithGap.
@Test
public void testAppendWithGap() throws Exception {
LogSegment segment = LogSegment.newOpenSegment(null, 1000, null);
SimpleOperation op = new SimpleOperation("m");
final StateMachineLogEntryProto m = op.getLogEntryContent();
try {
LogEntryProto entry = LogProtoUtils.toLogEntryProto(m, 0, 1001);
segment.appendToOpenSegment(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
Assert.fail("should fail since the entry's index needs to be 1000");
} catch (IllegalStateException e) {
// the exception is expected.
}
LogEntryProto entry = LogProtoUtils.toLogEntryProto(m, 0, 1000);
segment.appendToOpenSegment(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
try {
entry = LogProtoUtils.toLogEntryProto(m, 0, 1002);
segment.appendToOpenSegment(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
Assert.fail("should fail since the entry's index needs to be 1001");
} catch (IllegalStateException e) {
// the exception is expected.
}
}
use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestLogSegment method testAppendEntries.
@Test
public void testAppendEntries() throws Exception {
final long start = 1000;
LogSegment segment = LogSegment.newOpenSegment(null, start, null);
long size = SegmentedRaftLogFormat.getHeaderLength();
final long max = 8 * 1024 * 1024;
checkLogSegment(segment, start, start - 1, true, size, 0);
// append till full
long term = 0;
int i = 0;
while (size < max) {
SimpleOperation op = new SimpleOperation("m" + i);
LogEntryProto entry = LogProtoUtils.toLogEntryProto(op.getLogEntryContent(), term, i++ + start);
size += getEntrySize(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
segment.appendToOpenSegment(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
}
Assert.assertTrue(segment.getTotalFileSize() >= max);
checkLogSegment(segment, start, i - 1 + start, true, size, term);
}
Aggregations