Search in sources :

Example 1 with SizeInBytes

use of org.apache.ratis.util.SizeInBytes in project incubator-ratis by apache.

the class ConfUtils method getSizeInBytes.

@SafeVarargs
static SizeInBytes getSizeInBytes(BiFunction<String, SizeInBytes, SizeInBytes> getter, String key, SizeInBytes defaultValue, BiConsumer<String, SizeInBytes>... assertions) {
    final SizeInBytes value = get(getter, key, defaultValue, assertions);
    requireMin(0L).accept(key, value.getSize());
    return value;
}
Also used : SizeInBytes(org.apache.ratis.util.SizeInBytes)

Example 2 with SizeInBytes

use of org.apache.ratis.util.SizeInBytes in project incubator-ratis by apache.

the class TestRaftLogSegment 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 = new RaftStorage(storageDir, StartupOption.REGULAR);
    final File file = storage.getStorageDir().getOpenLogFile(0);
    final byte[] content = new byte[1024];
    Arrays.fill(content, (byte) 1);
    SimpleOperation op = new SimpleOperation(new String(content));
    LogEntryProto entry = ProtoUtils.toLogEntryProto(op.getLogEntryContent(), 0, 0, clientId, callId);
    final long entrySize = LogSegment.getEntrySize(entry);
    long totalSize = SegmentedRaftLog.HEADER_BYTES.length;
    long preallocated = 16 * 1024;
    try (LogOutputStream out = new LogOutputStream(file, false, max.getSize(), 16 * 1024, 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 : SMLogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) 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)

Aggregations

SizeInBytes (org.apache.ratis.util.SizeInBytes)2 File (java.io.File)1 BaseTest (org.apache.ratis.BaseTest)1 SimpleOperation (org.apache.ratis.RaftTestUtil.SimpleOperation)1 LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)1 SMLogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto)1 Test (org.junit.Test)1