Java Code Examples for org.apache.ratis.util.SizeInBytes#valueOf()

The following examples show how to use org.apache.ratis.util.SizeInBytes#valueOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestClientProtoUtils.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Test
public void testToRaftClientRequestProto() throws Exception {
  for(int i = 1; i < 32; i <<= 2) {
    final SizeInBytes messageSize = SizeInBytes.valueOf(i + "MB");
    runTestToRaftClientRequestProto(100, messageSize);
  }
}
 
Example 2
Source File: TestLogSegment.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
/**
 * 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 = ServerProtoUtils.toLogEntryProto(op.getLogEntryContent(), 0, 0);
  final long entrySize = LogSegment.getEntrySize(entry);

  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());
}
 
Example 3
Source File: LogServer.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
void setRaftProperties(RaftProperties properties) {
  super.setRaftProperties(properties);

  // Increase the client timeout
  long rpcTimeout = getConfig().getLong(Constants.LOG_SERVICE_RPC_TIMEOUT_KEY,
    Constants.DEFAULT_RPC_TIMEOUT);
  RaftClientConfigKeys.Rpc.setRequestTimeout(properties,
    TimeDuration.valueOf(rpcTimeout, TimeUnit.MILLISECONDS));

  // Increase the segment size to avoid rolling so quickly
  long segmentSize = getConfig().getLong(Constants.RATIS_RAFT_SEGMENT_SIZE_KEY,
    Constants.DEFAULT_RATIS_RAFT_SEGMENT_SIZE);
  SizeInBytes segmentSizeBytes = SizeInBytes.valueOf(segmentSize);
  String archiveLocation = getConfig().get(Constants.LOG_SERVICE_ARCHIVAL_LOCATION_KEY);
  if (archiveLocation != null) {
    properties.set(Constants.LOG_SERVICE_ARCHIVAL_LOCATION_KEY, archiveLocation);
  }
  heartbeatInterval = getConfig().getLong(Constants.LOG_SERVICE_HEARTBEAT_INTERVAL_KEY,
    Constants.DEFAULT_HEARTBEAT_INTERVAL);
  if(heartbeatInterval <= 0) {
      LOG.warn("Heartbeat interval configuration is invalid." +
              " Setting default value "+ Constants.DEFAULT_HEARTBEAT_INTERVAL);
      heartbeatInterval = Constants.DEFAULT_HEARTBEAT_INTERVAL;
  }
  RaftServerConfigKeys.Log.setSegmentSizeMax(properties, segmentSizeBytes);
  RaftServerConfigKeys.Log.setPreallocatedSize(properties, segmentSizeBytes);

  // TODO this seems to cause errors, not sure if pushing Ratis too hard?
  // SizeInBytes writeBufferSize = SizeInBytes.valueOf("128KB");
  // RaftServerConfigKeys.Log.setWriteBufferSize(properties, writeBufferSize);
}
 
Example 4
Source File: TestRaftLogSegment.java    From ratis with Apache License 2.0 5 votes vote down vote up
/**
 * 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 = ServerProtoUtils.toLogEntryProto(op.getLogEntryContent(), 0, 0);
  final long entrySize = LogSegment.getEntrySize(entry);

  long totalSize = SegmentedRaftLogFormat.getHeaderLength();
  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());
}
 
Example 5
Source File: RaftProperties.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
/** @return property value; if it is not set, return the default value. */
public SizeInBytes getSizeInBytes(String name, SizeInBytes defaultValue) {
  final String valueString = getTrimmed(name);
  return valueString == null? defaultValue: SizeInBytes.valueOf(valueString);
}
 
Example 6
Source File: OutputStreamBaseTest.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public OutputStream newOutputStream(CLUSTER cluster, int bufferSize) {
  return new RaftOutputStream(cluster::createClient, SizeInBytes.valueOf(bufferSize));
}
 
Example 7
Source File: RaftProperties.java    From ratis with Apache License 2.0 4 votes vote down vote up
/** @return property value; if it is not set, return the default value. */
public SizeInBytes getSizeInBytes(String name, SizeInBytes defaultValue) {
  final String valueString = getTrimmed(name);
  return valueString == null? defaultValue: SizeInBytes.valueOf(valueString);
}