Search in sources :

Example 26 with SimpleOperation

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());
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) RaftStorage(org.apache.ratis.server.storage.RaftStorage) SizeInBytes(org.apache.ratis.util.SizeInBytes) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) File(java.io.File) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 27 with SimpleOperation

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());
    }
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) RaftStorage(org.apache.ratis.server.storage.RaftStorage) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) File(java.io.File) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 28 with SimpleOperation

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);
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) StateMachineLogEntryProto(org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 29 with SimpleOperation

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()]);
}
Also used : LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) ArrayList(java.util.ArrayList) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) File(java.io.File)

Aggregations

SimpleOperation (org.apache.ratis.RaftTestUtil.SimpleOperation)29 Test (org.junit.Test)18 BaseTest (org.apache.ratis.BaseTest)16 File (java.io.File)14 LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)14 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)13 ArrayList (java.util.ArrayList)8 RandomAccessFile (java.io.RandomAccessFile)6 StateMachineLogEntryProto (org.apache.ratis.proto.RaftProtos.StateMachineLogEntryProto)6 RaftStorage (org.apache.ratis.server.storage.RaftStorage)6 SMLogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto)6 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 SizeInBytes (org.apache.ratis.util.SizeInBytes)2 ChecksumException (org.apache.ratis.protocol.ChecksumException)1 ChecksumException (org.apache.ratis.protocol.exceptions.ChecksumException)1 SegmentRange (org.apache.ratis.server.raftlog.segmented.TestSegmentedRaftLog.SegmentRange)1 SegmentRange (org.apache.ratis.server.storage.TestSegmentedRaftLog.SegmentRange)1