use of org.apache.ratis.RaftTestUtil.SimpleOperation 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.RaftTestUtil.SimpleOperation 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;
}
use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestCacheEviction method generateEntries.
private LogEntryProto[] generateEntries(List<SegmentRange> slist) {
List<LogEntryProto> eList = new ArrayList<>();
for (SegmentRange range : slist) {
for (long index = range.start; index <= range.end; index++) {
SimpleOperation m = new SimpleOperation(new String(new byte[1024]));
eList.add(LogProtoUtils.toLogEntryProto(m.getLogEntryContent(), range.term, index));
}
}
return eList.toArray(new LogEntryProto[eList.size()]);
}
use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestRaftLogReadWrite method testReadWithEntryCorruption.
/**
* Test the log reader to make sure it can detect the checksum mismatch.
*/
@Test
public void testReadWithEntryCorruption() throws IOException {
RaftStorage storage = RaftStorageTestUtils.newRaftStorage(storageDir);
final File openSegment = LogSegmentStartEnd.valueOf(0).getFile(storage);
try (SegmentedRaftLogOutputStream out = new SegmentedRaftLogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, ByteBuffer.allocateDirect(bufferSize))) {
for (int i = 0; i < 100; i++) {
LogEntryProto entry = LogProtoUtils.toLogEntryProto(new SimpleOperation("m" + i).getLogEntryContent(), 0, i);
out.write(entry);
}
} finally {
storage.close();
}
// corrupt the log file
try (RandomAccessFile raf = new RandomAccessFile(openSegment.getCanonicalFile(), "rw")) {
raf.seek(100);
int correctValue = raf.read();
raf.seek(100);
raf.write(correctValue + 1);
}
try {
readLog(openSegment, 0, RaftLog.INVALID_LOG_INDEX, true);
Assert.fail("The read of corrupted log file should fail");
} catch (ChecksumException e) {
LOG.info("Caught ChecksumException as expected", e);
}
}
use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestRaftLogReadWrite method testAppendLog.
@Test
public void testAppendLog() throws IOException {
final RaftStorage storage = RaftStorageTestUtils.newRaftStorage(storageDir);
final File openSegment = LogSegmentStartEnd.valueOf(0).getFile(storage);
LogEntryProto[] entries = new LogEntryProto[200];
try (SegmentedRaftLogOutputStream out = new SegmentedRaftLogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, ByteBuffer.allocateDirect(bufferSize))) {
for (int i = 0; i < 100; i++) {
SimpleOperation m = new SimpleOperation("m" + i);
entries[i] = LogProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i);
out.write(entries[i]);
}
}
try (SegmentedRaftLogOutputStream out = new SegmentedRaftLogOutputStream(openSegment, true, segmentMaxSize, preallocatedSize, ByteBuffer.allocateDirect(bufferSize))) {
for (int i = 100; i < 200; i++) {
SimpleOperation m = new SimpleOperation("m" + i);
entries[i] = LogProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i);
out.write(entries[i]);
}
}
final LogEntryProto[] readEntries = readLog(openSegment, 0, RaftLog.INVALID_LOG_INDEX, true);
Assert.assertArrayEquals(entries, readEntries);
storage.close();
}
Aggregations