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;
}
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());
}
Aggregations