Search in sources :

Example 6 with ClientInvocationId

use of org.apache.ratis.protocol.ClientInvocationId in project incubator-ratis by apache.

the class NettyClientStreamRpc method getClientHandler.

private ChannelInboundHandler getClientHandler() {
    return new ChannelInboundHandlerAdapter() {

        private ClientInvocationId clientInvocationId;

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            if (!(msg instanceof DataStreamReply)) {
                LOG.error("{}: unexpected message {}", this, msg.getClass());
                return;
            }
            final DataStreamReply reply = (DataStreamReply) msg;
            LOG.debug("{}: read {}", this, reply);
            clientInvocationId = ClientInvocationId.valueOf(reply.getClientId(), reply.getStreamId());
            final ReplyQueue queue = reply.isSuccess() ? replies.get(clientInvocationId) : replies.remove(clientInvocationId);
            if (queue != null) {
                final CompletableFuture<DataStreamReply> f = queue.poll();
                if (f != null) {
                    f.complete(reply);
                    if (!reply.isSuccess() && queue.size() > 0) {
                        final IllegalStateException e = new IllegalStateException(this + ": an earlier request failed with " + reply);
                        queue.forEach(future -> future.completeExceptionally(e));
                    }
                    final Integer emptyId = queue.getEmptyId();
                    if (emptyId != null) {
                        timeoutScheduler.onTimeout(replyQueueGracePeriod, // remove the queue if the same queue has been empty for the entire grace period.
                        () -> replies.computeIfPresent(clientInvocationId, (key, q) -> q == queue && emptyId.equals(q.getEmptyId()) ? null : q), LOG, () -> "Timeout check failed, clientInvocationId=" + clientInvocationId);
                    }
                }
            }
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
            LOG.warn(name + ": exceptionCaught", cause);
            Optional.ofNullable(clientInvocationId).map(replies::remove).orElse(ReplyQueue.EMPTY).forEach(f -> f.completeExceptionally(cause));
            LOG.warn(name + ": exceptionCaught", cause);
            ctx.close();
        }
    };
}
Also used : DataStreamRequestByteBuffer(org.apache.ratis.datastream.impl.DataStreamRequestByteBuffer) NioSocketChannel(org.apache.ratis.thirdparty.io.netty.channel.socket.nio.NioSocketChannel) DataStreamClientRpc(org.apache.ratis.client.DataStreamClientRpc) NettyDataStreamUtils(org.apache.ratis.netty.NettyDataStreamUtils) LoggerFactory(org.slf4j.LoggerFactory) NioEventLoopGroup(org.apache.ratis.thirdparty.io.netty.channel.nio.NioEventLoopGroup) CompletableFuture(java.util.concurrent.CompletableFuture) NettyConfigKeys(org.apache.ratis.netty.NettyConfigKeys) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ConcurrentMap(java.util.concurrent.ConcurrentMap) NetUtils(org.apache.ratis.util.NetUtils) DataStreamReply(org.apache.ratis.protocol.DataStreamReply) org.apache.ratis.thirdparty.io.netty.channel(org.apache.ratis.thirdparty.io.netty.channel) JavaUtils(org.apache.ratis.util.JavaUtils) ByteBuf(org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) RaftPeer(org.apache.ratis.protocol.RaftPeer) Bootstrap(org.apache.ratis.thirdparty.io.netty.bootstrap.Bootstrap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SocketChannel(org.apache.ratis.thirdparty.io.netty.channel.socket.SocketChannel) DataStreamRequestFilePositionCount(org.apache.ratis.datastream.impl.DataStreamRequestFilePositionCount) MessageToMessageEncoder(org.apache.ratis.thirdparty.io.netty.handler.codec.MessageToMessageEncoder) List(java.util.List) DataStreamRequest(org.apache.ratis.protocol.DataStreamRequest) ByteToMessageDecoder(org.apache.ratis.thirdparty.io.netty.handler.codec.ByteToMessageDecoder) RaftProperties(org.apache.ratis.conf.RaftProperties) TimeoutScheduler(org.apache.ratis.util.TimeoutScheduler) Optional(java.util.Optional) ClientInvocationId(org.apache.ratis.protocol.ClientInvocationId) Queue(java.util.Queue) TimeDuration(org.apache.ratis.util.TimeDuration) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ClientInvocationId(org.apache.ratis.protocol.ClientInvocationId) DataStreamReply(org.apache.ratis.protocol.DataStreamReply)

Example 7 with ClientInvocationId

use of org.apache.ratis.protocol.ClientInvocationId 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)

Example 8 with ClientInvocationId

use of org.apache.ratis.protocol.ClientInvocationId in project incubator-ratis by apache.

the class TestRetryCacheMetrics method testRetryCacheEntryCount.

@Test
public void testRetryCacheEntryCount() {
    checkEntryCount(0);
    ClientId clientId = ClientId.randomId();
    final ClientInvocationId key = ClientInvocationId.valueOf(clientId, 1);
    final RetryCacheImpl.CacheEntry entry = new RetryCacheImpl.CacheEntry(key);
    retryCache.refreshEntry(entry);
    checkEntryCount(1);
}
Also used : ClientId(org.apache.ratis.protocol.ClientId) ClientInvocationId(org.apache.ratis.protocol.ClientInvocationId) Test(org.junit.Test)

Example 9 with ClientInvocationId

use of org.apache.ratis.protocol.ClientInvocationId in project incubator-ratis by apache.

the class TestRetryCacheMetrics method testRetryCacheHitMissCount.

@Test
public void testRetryCacheHitMissCount() {
    checkHit(0, 1.0);
    checkMiss(0, 0.0);
    final ClientInvocationId invocationId = ClientInvocationId.valueOf(ClientId.randomId(), 2);
    retryCache.getOrCreateEntry(invocationId);
    checkHit(0, 0.0);
    checkMiss(1, 1.0);
    retryCache.getOrCreateEntry(invocationId);
    checkHit(1, 0.5);
    checkMiss(1, 0.5);
}
Also used : ClientInvocationId(org.apache.ratis.protocol.ClientInvocationId) Test(org.junit.Test)

Example 10 with ClientInvocationId

use of org.apache.ratis.protocol.ClientInvocationId in project incubator-ratis by apache.

the class RetryCacheTestUtil method createEntry.

public static void createEntry(RetryCache cache, LogEntryProto logEntry) {
    if (logEntry.hasStateMachineLogEntry()) {
        final ClientInvocationId invocationId = ClientInvocationId.valueOf(logEntry.getStateMachineLogEntry());
        getOrCreateEntry(cache, invocationId);
    }
}
Also used : ClientInvocationId(org.apache.ratis.protocol.ClientInvocationId)

Aggregations

ClientInvocationId (org.apache.ratis.protocol.ClientInvocationId)11 CompletableFuture (java.util.concurrent.CompletableFuture)4 DataStreamReply (org.apache.ratis.protocol.DataStreamReply)3 List (java.util.List)2 Optional (java.util.Optional)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 RaftProperties (org.apache.ratis.conf.RaftProperties)2 RequestContext (org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestContext)2 RequestMetrics (org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestMetrics)2 MessageStreamRequestTypeProto (org.apache.ratis.proto.RaftProtos.MessageStreamRequestTypeProto)2 ClientId (org.apache.ratis.protocol.ClientId)2 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)2 RaftPeer (org.apache.ratis.protocol.RaftPeer)2 Test (org.junit.Test)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 Collection (java.util.Collection)1