org.apache.ratis.util.SizeInBytes Java Examples

The following examples show how to use org.apache.ratis.util.SizeInBytes. 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: TestSegmentedRaftLog.java    From ratis with Apache License 2.0 6 votes vote down vote up
/**
 * Keep appending entries, make sure the rolling is correct.
 */
@Test
public void testAppendAndRoll() throws Exception {
  RaftServerConfigKeys.Log.setPreallocatedSize(properties, SizeInBytes.valueOf("16KB"));
  RaftServerConfigKeys.Log.setSegmentSizeMax(properties, SizeInBytes.valueOf("128KB"));

  List<SegmentRange> ranges = prepareRanges(0, 1, 1024, 0);
  final byte[] content = new byte[1024];
  List<LogEntryProto> entries = prepareLogEntries(ranges,
      () -> new String(content));

  try (SegmentedRaftLog raftLog =
           new SegmentedRaftLog(peerId, null, storage, -1, properties)) {
    raftLog.open(RaftServerConstants.INVALID_LOG_INDEX, null);
    // append entries to the raftlog
    entries.stream().map(raftLog::appendEntry).forEach(CompletableFuture::join);
  }

  try (SegmentedRaftLog raftLog =
           new SegmentedRaftLog(peerId, null, storage, -1, properties)) {
    raftLog.open(RaftServerConstants.INVALID_LOG_INDEX, null);
    // check if the raft log is correct
    checkEntries(raftLog, entries, 0, entries.size());
    Assert.assertEquals(9, raftLog.getRaftLogCache().getNumOfSegments());
  }
}
 
Example #2
Source File: FileStoreAsyncBaseTest.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileStoreAsync() throws Exception {
  final CLUSTER cluster = newCluster(NUM_PEERS);
  cluster.start();
  RaftTestUtil.waitForLeader(cluster);

  final FileStoreClient client = new FileStoreClient(cluster.getGroup(), getProperties());
  final ExecutorService executor = Executors.newFixedThreadPool(20);

  testSingleFile("foo", SizeInBytes.valueOf("10M"), executor, client);
  testMultipleFiles("file", 100, SizeInBytes.valueOf("1M"), executor, client);

  executor.shutdown();
  client.close();
  cluster.shutdown();
}
 
Example #3
Source File: TestSegmentedRaftLog.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
/**
 * Keep appending entries, make sure the rolling is correct.
 */
@Test
public void testAppendAndRoll() throws Exception {
  RaftServerConfigKeys.Log.setPreallocatedSize(properties, SizeInBytes.valueOf("16KB"));
  RaftServerConfigKeys.Log.setSegmentSizeMax(properties, SizeInBytes.valueOf("128KB"));

  List<SegmentRange> ranges = prepareRanges(0, 1, 1024, 0);
  final byte[] content = new byte[1024];
  List<LogEntryProto> entries = prepareLogEntries(ranges,
      () -> new String(content));

  try (SegmentedRaftLog raftLog = newSegmentedRaftLog()) {
    raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
    // append entries to the raftlog
    entries.stream().map(raftLog::appendEntry).forEach(CompletableFuture::join);
  }

  try (SegmentedRaftLog raftLog = newSegmentedRaftLog()) {
    raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
    // check if the raft log is correct
    checkEntries(raftLog, entries, 0, entries.size());
    Assert.assertEquals(9, raftLog.getRaftLogCache().getNumOfSegments());
  }
}
 
Example #4
Source File: XceiverServerRatis.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
private int setRaftSegmentPreallocatedSize(RaftProperties properties) {
  final int raftSegmentPreallocatedSize = (int) conf.getStorageSize(
      OzoneConfigKeys.DFS_CONTAINER_RATIS_SEGMENT_PREALLOCATED_SIZE_KEY,
      OzoneConfigKeys.DFS_CONTAINER_RATIS_SEGMENT_PREALLOCATED_SIZE_DEFAULT,
      StorageUnit.BYTES);
  int logAppenderQueueNumElements = conf.getInt(
      OzoneConfigKeys.DFS_CONTAINER_RATIS_LOG_APPENDER_QUEUE_NUM_ELEMENTS,
      OzoneConfigKeys
          .DFS_CONTAINER_RATIS_LOG_APPENDER_QUEUE_NUM_ELEMENTS_DEFAULT);
  final int logAppenderQueueByteLimit = (int) conf.getStorageSize(
      OzoneConfigKeys.DFS_CONTAINER_RATIS_LOG_APPENDER_QUEUE_BYTE_LIMIT,
      OzoneConfigKeys
          .DFS_CONTAINER_RATIS_LOG_APPENDER_QUEUE_BYTE_LIMIT_DEFAULT,
      StorageUnit.BYTES);
  RaftServerConfigKeys.Log.Appender
      .setBufferElementLimit(properties, logAppenderQueueNumElements);
  RaftServerConfigKeys.Log.Appender.setBufferByteLimit(properties,
      SizeInBytes.valueOf(logAppenderQueueByteLimit));
  RaftServerConfigKeys.Log.setPreallocatedSize(properties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  return raftSegmentPreallocatedSize;
}
 
Example #5
Source File: FileStoreAsyncBaseTest.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileStoreAsync() throws Exception {
  final CLUSTER cluster = newCluster(NUM_PEERS);
  cluster.start();
  RaftTestUtil.waitForLeader(cluster);

  final FileStoreClient client = new FileStoreClient(cluster.getGroup(), getProperties());
  final ExecutorService executor = Executors.newFixedThreadPool(20);

  testSingleFile("foo", SizeInBytes.valueOf("2M"), executor, client);
  testMultipleFiles("file", 20, SizeInBytes.valueOf("1M"), executor, client);

  executor.shutdown();
  client.close();
  cluster.shutdown();
}
 
Example #6
Source File: ConfUtils.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@SafeVarargs
static void setSizeInBytes(
    BiConsumer<String, String> stringSetter, String key, SizeInBytes value,
    BiConsumer<String, Long>... assertions) {
  final long v = value.getSize();
  Arrays.asList(assertions).forEach(a -> a.accept(key, v));
  set(stringSetter, key, value.getInput());
}
 
Example #7
Source File: XceiverServerRatis.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private void setPendingRequestsLimits(RaftProperties properties) {

    final int pendingRequestsByteLimit = (int)conf.getStorageSize(
        OzoneConfigKeys.DFS_CONTAINER_RATIS_LEADER_PENDING_BYTES_LIMIT,
        OzoneConfigKeys.DFS_CONTAINER_RATIS_LEADER_PENDING_BYTES_LIMIT_DEFAULT,
        StorageUnit.BYTES);
    RaftServerConfigKeys.Write.setByteLimit(properties,
        SizeInBytes.valueOf(pendingRequestsByteLimit));
  }
 
Example #8
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 #9
Source File: FileStoreAsyncBaseTest.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private static void testSingleFile(
    String path, SizeInBytes fileLength, Executor executor, FileStoreClient client)
    throws Exception {
  LOG.info("runTestSingleFile with path={}, fileLength={}", path, fileLength);

  new Writer(path, fileLength, executor, () -> client)
      .writeAsync()
      .thenCompose(Writer::verifyAsync)
      .thenCompose(Writer::deleteAsync)
      .get();
}
 
Example #10
Source File: FileStoreBaseTest.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileStore() throws Exception {
  final CLUSTER cluster = newCluster(NUM_PEERS);
  cluster.start();
  RaftTestUtil.waitForLeader(cluster);

  final CheckedSupplier<FileStoreClient, IOException> newClient =
      () -> new FileStoreClient(cluster.getGroup(), getProperties());

  testSingleFile("foo", SizeInBytes.valueOf("2M"), newClient);
  testMultipleFiles("file", 20, SizeInBytes.valueOf("1M"), newClient);

  cluster.shutdown();
}
 
Example #11
Source File: FileStoreBaseTest.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private static void testSingleFile(
    String path, SizeInBytes fileLength, CheckedSupplier<FileStoreClient, IOException> newClient)
    throws Exception {
  LOG.info("runTestSingleFile with path={}, fileLength={}", path, fileLength);

  try (final Writer w = new Writer(path, fileLength, null, newClient)) {
    w.write().verify().delete();
  }
}
 
Example #12
Source File: FileStoreBaseTest.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
Writer(String fileName, SizeInBytes fileSize, Executor asyncExecutor,
    CheckedSupplier<FileStoreClient, IOException> clientSupplier)
    throws IOException {
  this.fileName = fileName;
  this.fileSize = fileSize;
  this.client = clientSupplier.get();
  this.asyncExecutor = asyncExecutor;
}
 
Example #13
Source File: Client.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Exception {
  int raftSegmentPreallocatedSize = 1024 * 1024 * 1024;
  RaftProperties raftProperties = new RaftProperties();
  RaftConfigKeys.Rpc.setType(raftProperties, SupportedRpcType.GRPC);
  GrpcConfigKeys.setMessageSizeMax(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.Appender.setBufferByteLimit(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setWriteBufferSize(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setPreallocatedSize(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setSegmentSizeMax(raftProperties,
      SizeInBytes.valueOf(1 * 1024 * 1024 * 1024));

  RaftServerConfigKeys.Log.setSegmentCacheNumMax(raftProperties, 2);

  RaftClientConfigKeys.Rpc.setRequestTimeout(raftProperties,
      TimeDuration.valueOf(50000, TimeUnit.MILLISECONDS));
  RaftClientConfigKeys.Async.setOutstandingRequestsMax(raftProperties, 1000);


  final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(getRaftGroupId())),
          getPeers());

  RaftClient.Builder builder =
      RaftClient.newBuilder().setProperties(raftProperties);
  builder.setRaftGroup(raftGroup);
  builder.setClientRpc(new GrpcFactory(new Parameters()).newRaftClientRpc(ClientId.randomId(), raftProperties));
  RaftClient client = builder.build();

  operation(client);
}
 
Example #14
Source File: TestRaftServerWithGrpc.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Test
public void testRaftServerMetrics() throws Exception {
  final RaftProperties p = getProperties();
  RaftServerConfigKeys.Write.setElementLimit(p, 10);
  RaftServerConfigKeys.Write.setByteLimit(p, SizeInBytes.valueOf(110));
  try {
    runWithNewCluster(3, this::testRequestMetrics);
  } finally {
    RaftServerConfigKeys.Write.setElementLimit(p, RaftServerConfigKeys.Write.ELEMENT_LIMIT_DEFAULT);
    RaftServerConfigKeys.Write.setByteLimit(p, RaftServerConfigKeys.Write.BYTE_LIMIT_DEFAULT);
  }
}
 
Example #15
Source File: TestClientProtoUtils.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
void runTestToRaftClientRequestProto(int n, SizeInBytes messageSize)
    throws Exception {
  final ClientId clientId = ClientId.randomId();
  final RaftPeerId leaderId = RaftPeerId.valueOf("s0");
  final RaftGroupId groupId = RaftGroupId.randomId();


  TimeDuration toProto = TimeDuration.ZERO;
  TimeDuration toRequest = TimeDuration.ZERO;

  for(int i = 0; i < n; i++) {
    final ByteString bytes = newByteString(messageSize.getSizeInt(), i);
    final RaftClientRequest request = new RaftClientRequest(clientId, leaderId, groupId,
        1, () -> bytes, RaftClientRequest.writeRequestType(), null);

    final Timestamp startTime = Timestamp.currentTime();
    final RaftClientRequestProto proto = ClientProtoUtils.toRaftClientRequestProto(request);
    final TimeDuration p = startTime.elapsedTime();
    final RaftClientRequest computed = ClientProtoUtils.toRaftClientRequest(proto);
    final TimeDuration r = startTime.elapsedTime().subtract(p);

    Assert.assertEquals(request.getMessage().getContent(), computed.getMessage().getContent());
    toProto = toProto.add(p);
    toRequest = toRequest.add(r);

  }

  System.out.printf("%nmessageSize=%s, n=%d%n", messageSize, n);
  print("toProto  ", toProto, n);
  print("toRequest", toRequest, n);
}
 
Example #16
Source File: ConfUtils.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@SafeVarargs
static SizeInBytes getSizeInBytes(
    BiFunction<String, SizeInBytes, SizeInBytes> getter,
    String key, SizeInBytes defaultValue, Consumer<String> logger, BiConsumer<String, SizeInBytes>... assertions) {
  final SizeInBytes value = get(getter, key, defaultValue, logger, assertions);
  requireMin(0L).accept(key, value.getSize());
  return value;
}
 
Example #17
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 #18
Source File: XceiverServerRatis.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private void setRaftSegmentAndWriteBufferSize(RaftProperties properties) {
  final int raftSegmentSize = (int)conf.getStorageSize(
      OzoneConfigKeys.DFS_CONTAINER_RATIS_SEGMENT_SIZE_KEY,
      OzoneConfigKeys.DFS_CONTAINER_RATIS_SEGMENT_SIZE_DEFAULT,
      StorageUnit.BYTES);
  RaftServerConfigKeys.Log.setSegmentSizeMax(properties,
      SizeInBytes.valueOf(raftSegmentSize));
  RaftServerConfigKeys.Log.setWriteBufferSize(properties,
          SizeInBytes.valueOf(raftSegmentSize));
}
 
Example #19
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 #20
Source File: StreamImpl.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<RaftClientReply> streamAsync(Message message, SizeInBytes subSize) {
  final int n = subSize.getSizeInt();
  final MessageOutputStream out = stream();
  final ByteString bytes = message.getContent();
  for(int i = 0; i < bytes.size(); ) {
    final int j = Math.min(i + n, bytes.size());
    final ByteString sub = bytes.substring(i, j);
    out.sendAsync(Message.valueOf(sub));
    i = j;
  }
  return out.closeAsync();
}
 
Example #21
Source File: GrpcClientProtocolClient.java    From ratis with Apache License 2.0 5 votes vote down vote up
public GrpcClientProtocolClient(ClientId id, RaftPeer target,
                                RaftProperties properties,
                                GrpcTlsConfig tlsConf) {
  this.name = JavaUtils.memoize(() -> id + "->" + target.getId());
  this.target = target;
  final SizeInBytes flowControlWindow = GrpcConfigKeys.flowControlWindow(properties, LOG::debug);
  final SizeInBytes maxMessageSize = GrpcConfigKeys.messageSizeMax(properties, LOG::debug);
  NettyChannelBuilder channelBuilder =
      NettyChannelBuilder.forTarget(target.getAddress());

  if (tlsConf!= null) {
    SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
    if (tlsConf.getTrustStore() != null) {
      sslContextBuilder.trustManager(tlsConf.getTrustStore());
    }
    if (tlsConf.getMtlsEnabled()) {
      sslContextBuilder.keyManager(tlsConf.getCertChain(),
          tlsConf.getPrivateKey());
    }
    try {
      channelBuilder.useTransportSecurity().sslContext(sslContextBuilder.build());
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  } else {
    channelBuilder.negotiationType(NegotiationType.PLAINTEXT);
  }
  channel = channelBuilder.flowControlWindow(flowControlWindow.getSizeInt())
      .maxInboundMessageSize(maxMessageSize.getSizeInt())
      .build();
  blockingStub = RaftClientProtocolServiceGrpc.newBlockingStub(channel);
  asyncStub = RaftClientProtocolServiceGrpc.newStub(channel);
  adminBlockingStub = AdminProtocolServiceGrpc.newBlockingStub(channel);
  this.requestTimeoutDuration = RaftClientConfigKeys.Rpc.requestTimeout(properties);
}
 
Example #22
Source File: PendingRequests.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
RequestMap(Object name, int elementLimit, SizeInBytes byteLimit, RaftServerMetrics raftServerMetrics) {
  this.name = name;
  this.resource = new RequestLimits(elementLimit, byteLimit);
  this.raftServerMetrics = raftServerMetrics;

  raftServerMetrics.addNumPendingRequestsGauge(resource::getElementCount);
  raftServerMetrics.addNumPendingRequestsByteSize(resource::getByteSize);
}
 
Example #23
Source File: ConfUtils.java    From ratis with Apache License 2.0 5 votes vote down vote up
@SafeVarargs
static void setSizeInBytes(
    BiConsumer<String, String> stringSetter, String key, SizeInBytes value,
    BiConsumer<String, Long>... assertions) {
  final long v = value.getSize();
  Arrays.asList(assertions).forEach(a -> a.accept(key, v));
  set(stringSetter, key, value.getInput());
}
 
Example #24
Source File: ConfUtils.java    From ratis with Apache License 2.0 5 votes vote down vote up
@SafeVarargs
static SizeInBytes getSizeInBytes(
    BiFunction<String, SizeInBytes, SizeInBytes> getter,
    String key, SizeInBytes defaultValue, Consumer<String> logger, BiConsumer<String, SizeInBytes>... assertions) {
  final SizeInBytes value = get(getter, key, defaultValue, logger, assertions);
  requireMin(0L).accept(key, value.getSize());
  return value;
}
 
Example #25
Source File: FileStoreBaseTest.java    From ratis with Apache License 2.0 5 votes vote down vote up
Writer(String fileName, SizeInBytes fileSize, Executor asyncExecutor,
    CheckedSupplier<FileStoreClient, IOException> clientSupplier)
    throws IOException {
  this.fileName = fileName;
  this.fileSize = fileSize;
  this.client = clientSupplier.get();
  this.asyncExecutor = asyncExecutor;
}
 
Example #26
Source File: FileStoreBaseTest.java    From ratis with Apache License 2.0 5 votes vote down vote up
private static void testSingleFile(
    String path, SizeInBytes fileLength, CheckedSupplier<FileStoreClient, IOException> newClient)
    throws Exception {
  LOG.info("runTestSingleFile with path={}, fileLength={}", path, fileLength);

  try (final Writer w = new Writer(path, fileLength, null, newClient)) {
    w.write().verify().delete();
  }
}
 
Example #27
Source File: FileStoreBaseTest.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Test
public void testFileStore() throws Exception {
  final CLUSTER cluster = newCluster(NUM_PEERS);
  cluster.start();
  RaftTestUtil.waitForLeader(cluster);

  final CheckedSupplier<FileStoreClient, IOException> newClient =
      () -> new FileStoreClient(cluster.getGroup(), getProperties());

  testSingleFile("foo", SizeInBytes.valueOf("10M"), newClient);
  testMultipleFiles("file", 100, SizeInBytes.valueOf("1M"), newClient);

  cluster.shutdown();
}
 
Example #28
Source File: FileStoreAsyncBaseTest.java    From ratis with Apache License 2.0 5 votes vote down vote up
private static void testSingleFile(
    String path, SizeInBytes fileLength, Executor executor, FileStoreClient client)
    throws Exception {
  LOG.info("runTestSingleFile with path={}, fileLength={}", path, fileLength);

  new Writer(path, fileLength, executor, () -> client)
      .writeAsync()
      .thenCompose(Writer::verifyAsync)
      .thenCompose(Writer::deleteAsync)
      .get();
}
 
Example #29
Source File: Client.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Override
public void run() throws Exception {
  int raftSegmentPreallocatedSize = 1024 * 1024 * 1024;
  RaftProperties raftProperties = new RaftProperties();
  RaftConfigKeys.Rpc.setType(raftProperties, SupportedRpcType.GRPC);
  GrpcConfigKeys.setMessageSizeMax(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.Appender.setBufferByteLimit(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setWriteBufferSize(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setPreallocatedSize(raftProperties,
      SizeInBytes.valueOf(raftSegmentPreallocatedSize));
  RaftServerConfigKeys.Log.setSegmentSizeMax(raftProperties,
      SizeInBytes.valueOf(1 * 1024 * 1024 * 1024));

  RaftServerConfigKeys.Log.setMaxCachedSegmentNum(raftProperties, 2);

  RaftClientConfigKeys.Rpc.setRequestTimeout(raftProperties,
      TimeDuration.valueOf(50000, TimeUnit.MILLISECONDS));
  RaftClientConfigKeys.Async.setSchedulerThreads(raftProperties, 10);
  RaftClientConfigKeys.Async.setMaxOutstandingRequests(raftProperties, 1000);


  final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(raftGroupId)),
      parsePeers(peers));

  RaftClient.Builder builder =
      RaftClient.newBuilder().setProperties(raftProperties);
  builder.setRaftGroup(raftGroup);
  builder.setClientRpc(new GrpcFactory(new Parameters()).newRaftClientRpc(ClientId.randomId(), raftProperties));
  RaftClient client = builder.build();

  operation(client);
}
 
Example #30
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());
}