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(ProtoUtils.toLogEntryProto(m.getLogEntryContent(), range.term, index, clientId, callId));
}
}
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 = new RaftStorage(storageDir, StartupOption.REGULAR);
File openSegment = storage.getStorageDir().getOpenLogFile(0);
try (LogOutputStream out = new LogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, bufferSize)) {
for (int i = 0; i < 100; i++) {
LogEntryProto entry = ProtoUtils.toLogEntryProto(new SimpleOperation("m" + i).getLogEntryContent(), 0, i, clientId, callId);
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, RaftServerConstants.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 writeMessages.
private long writeMessages(LogEntryProto[] entries, LogOutputStream out) throws IOException {
long size = 0;
for (int i = 0; i < entries.length; i++) {
SimpleOperation m = new SimpleOperation("m" + i);
entries[i] = ProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i, clientId, callId);
final int s = entries[i].getSerializedSize();
size += CodedOutputStream.computeUInt32SizeNoTag(s) + s + 4;
out.write(entries[i]);
}
return size;
}
use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestRaftLogReadWrite method testReadWithCorruptPadding.
/**
* corrupt the padding by inserting non-zero bytes. Make sure the reader
* throws exception.
*/
@Test
public void testReadWithCorruptPadding() throws IOException {
final RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR);
File openSegment = storage.getStorageDir().getOpenLogFile(0);
LogEntryProto[] entries = new LogEntryProto[10];
LogOutputStream out = new LogOutputStream(openSegment, false, 16 * 1024 * 1024, 4 * 1024 * 1024, bufferSize);
for (int i = 0; i < 10; i++) {
SimpleOperation m = new SimpleOperation("m" + i);
entries[i] = ProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i, clientId, callId);
out.write(entries[i]);
}
out.flush();
// make sure the file contains padding
Assert.assertEquals(4 * 1024 * 1024, openSegment.length());
try (FileOutputStream fout = new FileOutputStream(openSegment, true)) {
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[] { -1, 1 });
fout.getChannel().write(byteBuffer, 16 * 1024 * 1024 - 10);
}
List<LogEntryProto> list = new ArrayList<>();
try (LogInputStream in = new LogInputStream(openSegment, 0, RaftServerConstants.INVALID_LOG_INDEX, true)) {
LogEntryProto entry;
while ((entry = in.nextEntry()) != null) {
list.add(entry);
}
Assert.fail("should fail since we corrupt the padding");
} catch (IOException e) {
boolean findVerifyTerminator = false;
for (StackTraceElement s : e.getStackTrace()) {
if (s.getMethodName().equals("verifyTerminator")) {
findVerifyTerminator = true;
break;
}
}
Assert.assertTrue(findVerifyTerminator);
}
Assert.assertArrayEquals(entries, list.toArray(new LogEntryProto[list.size()]));
}
use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestRaftLogSegment method testAppendEntries.
@Test
public void testAppendEntries() throws Exception {
final long start = 1000;
LogSegment segment = LogSegment.newOpenSegment(null, start);
long size = SegmentedRaftLog.HEADER_BYTES.length;
final long max = 8 * 1024 * 1024;
checkLogSegment(segment, start, start - 1, true, size, 0);
// append till full
long term = 0;
int i = 0;
List<LogEntryProto> list = new ArrayList<>();
while (size < max) {
SimpleOperation op = new SimpleOperation("m" + i);
LogEntryProto entry = ProtoUtils.toLogEntryProto(op.getLogEntryContent(), term, i++ + start, clientId, callId);
size += getEntrySize(entry);
list.add(entry);
}
segment.appendToOpenSegment(list.toArray(new LogEntryProto[list.size()]));
Assert.assertTrue(segment.getTotalSize() >= max);
checkLogSegment(segment, start, i - 1 + start, true, size, term);
}
Aggregations