use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class TestRaftLogCache method checkCache.
private void checkCache(long start, long end, int segmentSize) throws IOException {
Assert.assertEquals(start, cache.getStartIndex());
Assert.assertEquals(end, cache.getEndIndex());
for (long index = start; index <= end; index++) {
LogEntryProto entry = cache.getSegment(index).getEntryWithoutLoading(index).getEntry();
Assert.assertEquals(index, entry.getIndex());
}
long[] offsets = new long[] { start, start + 1, start + (end - start) / 2, end - 1, end };
for (long offset : offsets) {
checkCacheEntries(offset, (int) (end - offset + 1), end);
checkCacheEntries(offset, 1, end);
checkCacheEntries(offset, 20, end);
checkCacheEntries(offset, segmentSize, end);
checkCacheEntries(offset, segmentSize - 1, end);
}
}
use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class TestRaftLogReadWrite method testReadWithPadding.
/**
* Simulate the scenario that the peer is shutdown without truncating
* log segment file padding. Make sure the reader can correctly handle this.
*/
@Test
public void testReadWithPadding() throws IOException {
final RaftStorage storage = new RaftStorage(storageDir, StartupOption.REGULAR);
File openSegment = storage.getStorageDir().getOpenLogFile(0);
long size = SegmentedRaftLog.HEADER_BYTES.length;
LogEntryProto[] entries = new LogEntryProto[100];
LogOutputStream out = new LogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, bufferSize);
size += writeMessages(entries, out);
out.flush();
// make sure the file contains padding
Assert.assertEquals(RaftServerConfigKeys.Log.PREALLOCATED_SIZE_DEFAULT.getSize(), openSegment.length());
// check if the reader can correctly read the log file
LogEntryProto[] readEntries = readLog(openSegment, 0, RaftServerConstants.INVALID_LOG_INDEX, true);
Assert.assertArrayEquals(entries, readEntries);
out.close();
Assert.assertEquals(size, openSegment.length());
}
use of org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto 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.shaded.proto.RaftProtos.LogEntryProto 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.shaded.proto.RaftProtos.LogEntryProto 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