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);
}
}
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;
}
Aggregations