Search in sources :

Example 1 with RequestContext

use of org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestContext in project incubator-ratis by apache.

the class DataStreamManagement method startTransaction.

private CompletableFuture<RaftClientReply> startTransaction(StreamInfo info, DataStreamRequestByteBuf request, long bytesWritten, ChannelHandlerContext ctx) {
    final RequestMetrics metrics = getMetrics().newRequestMetrics(RequestType.START_TRANSACTION);
    final RequestContext context = metrics.start();
    try {
        AsyncRpcApi asyncRpcApi = (AsyncRpcApi) (server.getDivision(info.getRequest().getRaftGroupId()).getRaftClient().async());
        return asyncRpcApi.sendForward(info.request).whenCompleteAsync((reply, e) -> {
            metrics.stop(context, e == null);
            if (e != null) {
                replyDataStreamException(server, e, info.getRequest(), request, ctx);
            } else {
                ctx.writeAndFlush(newDataStreamReplyByteBuffer(request, reply, bytesWritten, info.getCommitInfos()));
            }
        }, requestExecutor);
    } catch (IOException e) {
        throw new CompletionException(e);
    }
}
Also used : AsyncRpcApi(org.apache.ratis.client.AsyncRpcApi) CompletionException(java.util.concurrent.CompletionException) RequestMetrics(org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestMetrics) RequestContext(org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestContext) IOException(java.io.IOException)

Example 2 with RequestContext

use of org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestContext in project incubator-ratis by apache.

the class DataStreamManagement method computeDataStreamIfAbsent.

private CompletableFuture<DataStream> computeDataStreamIfAbsent(RaftClientRequest request) throws IOException {
    final Division division = server.getDivision(request.getRaftGroupId());
    final ClientInvocationId invocationId = ClientInvocationId.valueOf(request);
    final MemoizedSupplier<CompletableFuture<DataStream>> supplier = JavaUtils.memoize(() -> {
        final RequestMetrics metrics = getMetrics().newRequestMetrics(RequestType.STATE_MACHINE_STREAM);
        final RequestContext context = metrics.start();
        return division.getStateMachine().data().stream(request).whenComplete((r, e) -> metrics.stop(context, e == null));
    });
    final CompletableFuture<DataStream> f = division.getDataStreamMap().computeIfAbsent(invocationId, key -> supplier.get());
    if (!supplier.isInitialized()) {
        throw new AlreadyExistsException("A DataStream already exists for " + invocationId);
    }
    return f;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) AlreadyExistsException(org.apache.ratis.protocol.exceptions.AlreadyExistsException) DataStream(org.apache.ratis.statemachine.StateMachine.DataStream) RequestMetrics(org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestMetrics) Division(org.apache.ratis.server.RaftServer.Division) RequestContext(org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestContext) ClientInvocationId(org.apache.ratis.protocol.ClientInvocationId)

Aggregations

RequestContext (org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestContext)2 RequestMetrics (org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestMetrics)2 IOException (java.io.IOException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionException (java.util.concurrent.CompletionException)1 AsyncRpcApi (org.apache.ratis.client.AsyncRpcApi)1 ClientInvocationId (org.apache.ratis.protocol.ClientInvocationId)1 AlreadyExistsException (org.apache.ratis.protocol.exceptions.AlreadyExistsException)1 Division (org.apache.ratis.server.RaftServer.Division)1 DataStream (org.apache.ratis.statemachine.StateMachine.DataStream)1