org.apache.ratis.util.function.CheckedFunction Java Examples

The following examples show how to use org.apache.ratis.util.function.CheckedFunction. 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: 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 #2
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 #3
Source File: FileInfo.java    From ratis with Apache License 2.0 6 votes vote down vote up
ByteString read(CheckedFunction<Path, Path, IOException> resolver, long offset, long length)
    throws IOException {
  flush();
  if (offset + length > getSize()) {
    throw new IOException("Failed to read: offset (=" + offset
        + " + length (=" + length + ") > size = " + getSize()
        + ", path=" + getRelativePath());
  }

  try(final SeekableByteChannel in = Files.newByteChannel(
      resolver.apply(getRelativePath()), StandardOpenOption.READ)) {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(FileStoreCommon.getChunkSize(length));
    in.position(offset).read(buffer);
    buffer.flip();
    return ByteString.copyFrom(buffer);
  }
}
 
Example #4
Source File: FileInfo.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
ByteString read(CheckedFunction<Path, Path, IOException> resolver, long offset, long length)
    throws IOException {
  flush();
  if (offset + length > getSize()) {
    throw new IOException("Failed to read: offset (=" + offset
        + " + length (=" + length + ") > size = " + getSize()
        + ", path=" + getRelativePath());
  }

  try(SeekableByteChannel in = Files.newByteChannel(
      resolver.apply(getRelativePath()), StandardOpenOption.READ)) {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(FileStoreCommon.getChunkSize(length));
    in.position(offset).read(buffer);
    buffer.flip();
    return ByteString.copyFrom(buffer);
  }
}
 
Example #5
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 #6
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 #7
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 #8
Source File: FileInfo.java    From ratis with Apache License 2.0 5 votes vote down vote up
CompletableFuture<Integer> submitCreate(
    CheckedFunction<Path, Path, IOException> resolver, ByteString data, boolean close,
    ExecutorService executor, RaftPeerId id, long index) {
  final Supplier<String> name = () -> "create(" + getRelativePath() + ", "
      + close + ") @" + id + ":" + index;
  final CheckedSupplier<Integer, IOException> task = LogUtils.newCheckedSupplier(LOG, () -> {
    if (out == null) {
      out = new FileOut(resolver.apply(getRelativePath()));
    }
    return write(0L, data, close);
  }, name);
  return submitWrite(task, executor, id, index);
}
 
Example #9
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 #10
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 #11
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 #12
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 #13
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 #14
Source File: FileInfo.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
CompletableFuture<Integer> submitCreate(
    CheckedFunction<Path, Path, IOException> resolver, ByteString data, boolean close,
    ExecutorService executor, RaftPeerId id, long index) {
  final Supplier<String> name = () -> "create(" + getRelativePath() + ", "
      + close + ") @" + id + ":" + index;
  final CheckedSupplier<Integer, IOException> task = LogUtils.newCheckedSupplier(LOG, () -> {
    if (out == null) {
      out = new FileOut(resolver.apply(getRelativePath()));
    }
    return write(0L, data, close);
  }, name);
  return submitWrite(task, executor, id, index);
}
 
Example #15
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 #16
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 #17
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 #18
Source File: RaftServerProxy.java    From ratis with Apache License 2.0 4 votes vote down vote up
private <REPLY> CompletableFuture<REPLY> submitRequest(RaftGroupId groupId,
    CheckedFunction<RaftServerImpl, CompletableFuture<REPLY>, IOException> submitFunction) {
  return getImplFuture(groupId).thenCompose(
      impl -> JavaUtils.callAsUnchecked(() -> submitFunction.apply(impl), CompletionException::new));
}
 
Example #19
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;
}
 
Example #20
Source File: PeerProxyMap.java    From ratis with Apache License 2.0 4 votes vote down vote up
public PeerProxyMap(String name, CheckedFunction<RaftPeer, PROXY, IOException> createProxy) {
  this.name = name;
  this.createProxy = createProxy;
}
 
Example #21
Source File: RaftServerProxy.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
private <REPLY> CompletableFuture<REPLY> submitRequest(RaftGroupId groupId,
    CheckedFunction<RaftServerImpl, CompletableFuture<REPLY>, IOException> submitFunction) {
  return getImplFuture(groupId).thenCompose(
      impl -> JavaUtils.callAsUnchecked(() -> submitFunction.apply(impl), CompletionException::new));
}
 
Example #22
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 #23
Source File: PeerProxyMap.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public PeerProxyMap(String name, CheckedFunction<RaftPeer, PROXY, IOException> createProxy) {
  this.name = name;
  this.createProxy = createProxy;
}