Java Code Examples for org.apache.ratis.util.function.CheckedFunction#apply()

The following examples show how to use org.apache.ratis.util.function.CheckedFunction#apply() . 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: FileStoreClient.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
private static <OUTPUT, THROWABLE extends Throwable> OUTPUT writeImpl(
    CheckedFunction<ByteString, OUTPUT, THROWABLE> sendFunction,
    String path, long offset, boolean close, ByteBuffer data)
    throws THROWABLE {
  final WriteRequestHeaderProto.Builder header = WriteRequestHeaderProto.newBuilder()
      .setPath(ProtoUtils.toByteString(path))
      .setOffset(offset)
      .setLength(data.position())
      .setClose(close);

  final WriteRequestProto.Builder write = WriteRequestProto.newBuilder()
      .setHeader(header)
      .setData(ByteString.copyFrom(data));

  final FileStoreRequestProto request = FileStoreRequestProto.newBuilder().setWrite(write).build();
  return sendFunction.apply(request.toByteString());
}
 
Example 2
Source File: CombinedClientProtocolClientSideTranslatorPB.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
static <REQUEST extends RaftClientRequest, REPLY extends RaftClientReply,
    PROTO_REQ, PROTO_REP> REPLY handleRequest(
    REQUEST request,
    Function<REQUEST, PROTO_REQ> reqToProto,
    Function<PROTO_REP, REPLY> repToProto,
    CheckedFunction<PROTO_REQ, PROTO_REP, ServiceException> handler)
    throws IOException {
  final PROTO_REQ proto = reqToProto.apply(request);
  try {
    final PROTO_REP reply = handler.apply(proto);
    return repToProto.apply(reply);
  } catch (ServiceException se) {
    LOG.trace("Failed to handle " + request, se);
    throw ProtoUtils.toIOException(se);
  }
}
 
Example 3
Source File: FileStoreClient.java    From ratis with Apache License 2.0 6 votes vote down vote up
private static <OUTPUT, THROWABLE extends Throwable> OUTPUT writeImpl(
    CheckedFunction<ByteString, OUTPUT, THROWABLE> sendFunction,
    String path, long offset, boolean close, ByteBuffer data)
    throws THROWABLE {
  final WriteRequestHeaderProto.Builder header = WriteRequestHeaderProto.newBuilder()
      .setPath(ProtoUtils.toByteString(path))
      .setOffset(offset)
      .setLength(data.position())
      .setClose(close);

  final WriteRequestProto.Builder write = WriteRequestProto.newBuilder()
      .setHeader(header)
      .setData(ByteString.copyFrom(data));

  final FileStoreRequestProto request = FileStoreRequestProto.newBuilder().setWrite(write).build();
  return sendFunction.apply(request.toByteString());
}
 
Example 4
Source File: CombinedClientProtocolClientSideTranslatorPB.java    From ratis with Apache License 2.0 6 votes vote down vote up
static <REQUEST extends RaftClientRequest, REPLY extends RaftClientReply,
    PROTO_REQ, PROTO_REP> REPLY handleRequest(
    REQUEST request,
    Function<REQUEST, PROTO_REQ> reqToProto,
    Function<PROTO_REP, REPLY> repToProto,
    CheckedFunction<PROTO_REQ, PROTO_REP, ServiceException> handler)
    throws IOException {
  final PROTO_REQ proto = reqToProto.apply(request);
  try {
    final PROTO_REP reply = handler.apply(proto);
    return repToProto.apply(reply);
  } catch (ServiceException se) {
    LOG.trace("Failed to handle " + request, se);
    throw ProtoUtils.toIOException(se);
  }
}
 
Example 5
Source File: OzoneClientKeyValidator.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
private <T> T readKey(String keyName,
    CheckedFunction<InputStream, T, IOException> reader)
    throws IOException {
  try (InputStream in = rpcClient.getObjectStore().getVolume(volumeName)
      .getBucket(bucketName).readKey(keyName)) {
    return reader.apply(in);
  }
}
 
Example 6
Source File: FileStoreClient.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
static ByteString send(
    ByteString request, CheckedFunction<Message, RaftClientReply, IOException> sendFunction)
    throws IOException {
  final RaftClientReply reply = sendFunction.apply(Message.valueOf(request));
  final StateMachineException sme = reply.getStateMachineException();
  if (sme != null) {
    throw new IOException("Failed to send request " + request, sme);
  }
  Preconditions.assertTrue(reply.isSuccess(), () -> "Failed " + request + ", reply=" + reply);
  return reply.getMessage().getContent();
}
 
Example 7
Source File: FileStoreClient.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private static <OUTPUT, THROWABLE extends Throwable> OUTPUT readImpl(
    CheckedFunction<ByteString, OUTPUT, THROWABLE> sendReadOnlyFunction,
    String path, long offset, long length) throws THROWABLE {
  final ReadRequestProto read = ReadRequestProto.newBuilder()
      .setPath(ProtoUtils.toByteString(path))
      .setOffset(offset)
      .setLength(length)
      .build();

  return sendReadOnlyFunction.apply(read.toByteString());
}
 
Example 8
Source File: FileStoreClient.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private static <OUTPUT, THROWABLE extends Throwable> OUTPUT deleteImpl(
    CheckedFunction<ByteString, OUTPUT, THROWABLE> sendFunction, String path)
    throws THROWABLE {
  final DeleteRequestProto.Builder delete = DeleteRequestProto.newBuilder()
      .setPath(ProtoUtils.toByteString(path));
  final FileStoreRequestProto request = FileStoreRequestProto.newBuilder().setDelete(delete).build();
  return sendFunction.apply(request.toByteString());
}
 
Example 9
Source File: HadoopRpcService.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private <REQUEST, REPLY> REPLY processRequest(
    REQUEST request, ByteString replyId,
    CheckedFunction<RaftServerProtocolPB, REPLY, ServiceException> f)
    throws IOException {
  CodeInjectionForTesting.execute(SEND_SERVER_REQUEST, getId(), null, request);

  final RaftServerProtocolPB proxy = getProxies().getProxy(
      RaftPeerId.valueOf(replyId)).getProtocol();
  try {
    return f.apply(proxy);
  } catch (ServiceException se) {
    throw ProtoUtils.toIOException(se);
  }
}
 
Example 10
Source File: FileStoreClient.java    From ratis with Apache License 2.0 5 votes vote down vote up
static ByteString send(
    ByteString request, CheckedFunction<Message, RaftClientReply, IOException> sendFunction)
    throws IOException {
  final RaftClientReply reply = sendFunction.apply(Message.valueOf(request));
  final StateMachineException sme = reply.getStateMachineException();
  if (sme != null) {
    throw new IOException("Failed to send request " + request, sme);
  }
  Preconditions.assertTrue(reply.isSuccess(), () -> "Failed " + request + ", reply=" + reply);
  return reply.getMessage().getContent();
}
 
Example 11
Source File: FileStoreClient.java    From ratis with Apache License 2.0 5 votes vote down vote up
private static <OUTPUT, THROWABLE extends Throwable> OUTPUT readImpl(
    CheckedFunction<ByteString, OUTPUT, THROWABLE> sendReadOnlyFunction,
    String path, long offset, long length) throws THROWABLE {
  final ReadRequestProto read = ReadRequestProto.newBuilder()
      .setPath(ProtoUtils.toByteString(path))
      .setOffset(offset)
      .setLength(length)
      .build();

  return sendReadOnlyFunction.apply(read.toByteString());
}
 
Example 12
Source File: FileStoreClient.java    From ratis with Apache License 2.0 5 votes vote down vote up
private static <OUTPUT, THROWABLE extends Throwable> OUTPUT deleteImpl(
    CheckedFunction<ByteString, OUTPUT, THROWABLE> sendFunction, String path)
    throws THROWABLE {
  final DeleteRequestProto.Builder delete = DeleteRequestProto.newBuilder()
      .setPath(ProtoUtils.toByteString(path));
  final FileStoreRequestProto request = FileStoreRequestProto.newBuilder().setDelete(delete).build();
  return sendFunction.apply(request.toByteString());
}
 
Example 13
Source File: HadoopRpcService.java    From ratis with Apache License 2.0 5 votes vote down vote up
private <REQUEST, REPLY> REPLY processRequest(
    REQUEST request, ByteString replyId,
    CheckedFunction<RaftServerProtocolPB, REPLY, ServiceException> f)
    throws IOException {
  CodeInjectionForTesting.execute(SEND_SERVER_REQUEST, getId(), null, request);

  final RaftServerProtocolPB proxy = getProxies().getProxy(
      RaftPeerId.valueOf(replyId)).getProtocol();
  try {
    return f.apply(proxy);
  } catch (ServiceException se) {
    throw ProtoUtils.toIOException(se);
  }
}
 
Example 14
Source File: SegmentedRaftLogFormat.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
static <T> T applyHeaderTo(CheckedFunction<byte[], T, IOException> function) throws IOException {
  final T t = function.apply(Internal.HEADER_BYTES);
  Internal.assertHeader(); // assert that the header is unmodified by the function.
  return t;
}
 
Example 15
Source File: SegmentedRaftLogFormat.java    From ratis with Apache License 2.0 4 votes vote down vote up
static <T> T applyHeaderTo(CheckedFunction<byte[], T, IOException> function) throws IOException {
  final T t = function.apply(Internal.HEADER_BYTES);
  Internal.assertHeader(); // assert that the header is unmodified by the function.
  return t;
}