use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestRaftLogCache method prepareLogSegment.
private LogSegment prepareLogSegment(long start, long end, boolean isOpen) {
LogSegment s = LogSegment.newOpenSegment(null, start);
for (long i = start; i <= end; i++) {
SimpleOperation m = new SimpleOperation("m" + i);
LogEntryProto entry = ProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i, clientId, callId);
s.appendToOpenSegment(entry);
}
if (!isOpen) {
s.close();
}
return s;
}
use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestRaftLogCache 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 = ProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, 0, clientId, callId);
cache.appendEntry(entry);
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 = ProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, index, clientId, callId);
cache.appendEntry(entry);
}
Assert.assertNotNull(cache.getOpenSegment());
checkCache(0, 199, 100);
}
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 = new RaftStorage(storageDir, StartupOption.REGULAR);
File openSegment = storage.getStorageDir().getOpenLogFile(0);
LogEntryProto[] entries = new LogEntryProto[200];
try (LogOutputStream out = new LogOutputStream(openSegment, false, segmentMaxSize, preallocatedSize, bufferSize)) {
for (int i = 0; i < 100; i++) {
SimpleOperation m = new SimpleOperation("m" + i);
entries[i] = ProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i, clientId, callId);
out.write(entries[i]);
}
}
try (LogOutputStream out = new LogOutputStream(openSegment, true, segmentMaxSize, preallocatedSize, bufferSize)) {
for (int i = 100; i < 200; i++) {
SimpleOperation m = new SimpleOperation("m" + i);
entries[i] = ProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i, clientId, callId);
out.write(entries[i]);
}
}
LogEntryProto[] readEntries = readLog(openSegment, 0, RaftServerConstants.INVALID_LOG_INDEX, true);
Assert.assertArrayEquals(entries, readEntries);
storage.close();
}
use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestRaftLogSegment method testTruncate.
@Test
public void testTruncate() throws Exception {
final long term = 1;
final long start = 1000;
LogSegment segment = LogSegment.newOpenSegment(null, start);
for (int i = 0; i < 100; i++) {
LogEntryProto entry = ProtoUtils.toLogEntryProto(new SimpleOperation("m" + i).getLogEntryContent(), term, i + start, clientId, callId);
segment.appendToOpenSegment(entry);
}
// 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, SegmentedRaftLog.HEADER_BYTES.length, term);
}
use of org.apache.ratis.RaftTestUtil.SimpleOperation in project incubator-ratis by apache.
the class TestRaftLogSegment method testAppendWithGap.
@Test
public void testAppendWithGap() throws Exception {
LogSegment segment = LogSegment.newOpenSegment(null, 1000);
SimpleOperation op = new SimpleOperation("m");
final SMLogEntryProto m = op.getLogEntryContent();
try {
LogEntryProto entry = ProtoUtils.toLogEntryProto(m, 0, 1001, clientId, callId);
segment.appendToOpenSegment(entry);
Assert.fail("should fail since the entry's index needs to be 1000");
} catch (IllegalStateException e) {
// the exception is expected.
}
LogEntryProto entry = ProtoUtils.toLogEntryProto(m, 0, 1000, clientId, callId);
segment.appendToOpenSegment(entry);
try {
entry = ProtoUtils.toLogEntryProto(m, 0, 1002, clientId, callId);
segment.appendToOpenSegment(entry);
Assert.fail("should fail since the entry's index needs to be 1001");
} catch (IllegalStateException e) {
// the exception is expected.
}
LogEntryProto[] entries = new LogEntryProto[2];
for (int i = 0; i < 2; i++) {
entries[i] = ProtoUtils.toLogEntryProto(m, 0, 1001 + i * 2, clientId, callId);
}
try {
segment.appendToOpenSegment(entries);
Assert.fail("should fail since there is gap between entries");
} catch (IllegalStateException e) {
// the exception is expected.
}
}
Aggregations