use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestLogSegment method testPreallocationAndAppend.
/**
* Keep appending and check if pre-allocation is correct
*/
@Test
public void testPreallocationAndAppend() throws Exception {
final SizeInBytes max = SizeInBytes.valueOf(2, TraditionalBinaryPrefix.MEGA);
RaftStorage storage = RaftStorageTestUtils.newRaftStorage(storageDir);
final File file = LogSegmentStartEnd.valueOf(0).getFile(storage);
final byte[] content = new byte[1024];
Arrays.fill(content, (byte) 1);
SimpleOperation op = new SimpleOperation(new String(content));
LogEntryProto entry = LogProtoUtils.toLogEntryProto(op.getLogEntryContent(), 0, 0);
final long entrySize = LogSegment.getEntrySize(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
long totalSize = SegmentedRaftLogFormat.getHeaderLength();
long preallocated = 16 * 1024;
try (SegmentedRaftLogOutputStream out = new SegmentedRaftLogOutputStream(file, false, max.getSize(), 16 * 1024, ByteBuffer.allocateDirect(10 * 1024))) {
Assert.assertEquals(preallocated, file.length());
while (totalSize + entrySize < max.getSize()) {
totalSize += entrySize;
out.write(entry);
if (totalSize > preallocated) {
Assert.assertEquals("totalSize==" + totalSize, preallocated + 16 * 1024, file.length());
preallocated += 16 * 1024;
}
}
}
Assert.assertEquals(totalSize, file.length());
}
use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestLogSegment method testPreallocateSegment.
@Test
public void testPreallocateSegment() throws Exception {
RaftStorage storage = RaftStorageTestUtils.newRaftStorage(storageDir);
final File file = LogSegmentStartEnd.valueOf(0).getFile(storage);
final int[] maxSizes = new int[] { 1024, 1025, 1024 * 1024 - 1, 1024 * 1024, 1024 * 1024 + 1, 2 * 1024 * 1024 - 1, 2 * 1024 * 1024, 2 * 1024 * 1024 + 1, 8 * 1024 * 1024 };
final int[] preallocated = new int[] { 512, 1024, 1025, 1024 * 1024, 1024 * 1024 + 1, 2 * 1024 * 1024 };
// make sure preallocation is correct with different max/pre-allocated size
for (int max : maxSizes) {
for (int a : preallocated) {
try (SegmentedRaftLogOutputStream ignored = new SegmentedRaftLogOutputStream(file, false, max, a, ByteBuffer.allocateDirect(bufferSize))) {
Assert.assertEquals("max=" + max + ", a=" + a, file.length(), Math.min(max, a));
}
try (SegmentedRaftLogInputStream in = new SegmentedRaftLogInputStream(file, 0, INVALID_LOG_INDEX, true)) {
LogEntryProto entry = in.nextEntry();
Assert.assertNull(entry);
}
}
}
// test the scenario where an entry's size is larger than the max size
final byte[] content = new byte[1024 * 2];
Arrays.fill(content, (byte) 1);
final long size;
try (SegmentedRaftLogOutputStream out = new SegmentedRaftLogOutputStream(file, false, 1024, 1024, ByteBuffer.allocateDirect(bufferSize))) {
SimpleOperation op = new SimpleOperation(new String(content));
LogEntryProto entry = LogProtoUtils.toLogEntryProto(op.getLogEntryContent(), 0, 0);
size = LogSegment.getEntrySize(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
out.write(entry);
}
Assert.assertEquals(file.length(), size + SegmentedRaftLogFormat.getHeaderLength());
try (SegmentedRaftLogInputStream in = new SegmentedRaftLogInputStream(file, 0, INVALID_LOG_INDEX, true)) {
LogEntryProto entry = in.nextEntry();
Assert.assertArrayEquals(content, entry.getStateMachineLogEntry().getLogData().toByteArray());
Assert.assertNull(in.nextEntry());
}
}
use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestLogSegment method testTruncate.
@Test
public void testTruncate() throws Exception {
final long term = 1;
final long start = 1000;
LogSegment segment = LogSegment.newOpenSegment(null, start, null);
for (int i = 0; i < 100; i++) {
LogEntryProto entry = LogProtoUtils.toLogEntryProto(new SimpleOperation("m" + i).getLogEntryContent(), term, i + start);
segment.appendToOpenSegment(entry, LogSegment.Op.WRITE_CACHE_WITHOUT_STATE_MACHINE_CACHE);
}
// 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, SegmentedRaftLogFormat.getHeaderLength(), term);
}
use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestSegmentedRaftLog method prepareLog.
private LogEntryProto[] prepareLog(List<SegmentRange> list) throws IOException {
List<LogEntryProto> entryList = new ArrayList<>();
for (SegmentRange range : list) {
final File file = range.getFile(storage);
final int size = (int) (range.end - range.start + 1);
LogEntryProto[] entries = new LogEntryProto[size];
try (SegmentedRaftLogOutputStream out = new SegmentedRaftLogOutputStream(file, false, segmentMaxSize, preallocatedSize, ByteBuffer.allocateDirect(bufferSize))) {
for (int i = 0; i < size; i++) {
SimpleOperation m = new SimpleOperation("m" + (i + range.start));
entries[i] = LogProtoUtils.toLogEntryProto(m.getLogEntryContent(), range.term, i + range.start);
out.write(entries[i]);
}
}
Collections.addAll(entryList, entries);
}
return entryList.toArray(new LogEntryProto[entryList.size()]);
}
Aggregations