Search in sources :

Example 1 with DataStream

use of org.apache.ratis.statemachine.StateMachine.DataStream 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

CompletableFuture (java.util.concurrent.CompletableFuture)1 RequestContext (org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestContext)1 RequestMetrics (org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestMetrics)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