Search in sources :

Example 6 with DataStreamReply

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

the class DataStream method streamWrite.

private Map<String, CompletableFuture<List<CompletableFuture<DataStreamReply>>>> streamWrite(List<String> paths, List<FileStoreClient> clients, RoutingTable routingTable, ExecutorService executor) {
    Map<String, CompletableFuture<List<CompletableFuture<DataStreamReply>>>> fileMap = new HashMap<>();
    int clientIndex = 0;
    for (String path : paths) {
        final CompletableFuture<List<CompletableFuture<DataStreamReply>>> future = new CompletableFuture<>();
        final FileStoreClient client = clients.get(clientIndex % clients.size());
        clientIndex++;
        CompletableFuture.supplyAsync(() -> {
            File file = new File(path);
            final long fileLength = file.length();
            Preconditions.assertTrue(fileLength == getFileSizeInBytes(), "Unexpected file size: expected size is " + getFileSizeInBytes() + " but actual size is " + fileLength);
            final Type type = Optional.ofNullable(Type.valueOfIgnoreCase(dataStreamType)).orElseThrow(IllegalStateException::new);
            final TransferType writer = type.getConstructor().apply(path, this);
            try {
                future.complete(writer.transfer(client, routingTable));
            } catch (IOException e) {
                future.completeExceptionally(e);
            }
            return future;
        }, executor);
        fileMap.put(path, future);
    }
    return fileMap;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) DataStreamReply(org.apache.ratis.protocol.DataStreamReply) FileStoreClient(org.apache.ratis.examples.filestore.FileStoreClient) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Example 7 with DataStreamReply

use of org.apache.ratis.protocol.DataStreamReply 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 8 with DataStreamReply

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

the class DataStreamTestUtils method writeAndAssertReplies.

static int writeAndAssertReplies(DataStreamOutputImpl out, int bufferSize, int bufferNum) {
    final List<CompletableFuture<DataStreamReply>> futures = new ArrayList<>();
    final List<Integer> sizes = new ArrayList<>();
    // send data
    final int halfBufferSize = bufferSize / 2;
    int dataSize = 0;
    for (int i = 0; i < bufferNum; i++) {
        final int size = halfBufferSize + ThreadLocalRandom.current().nextInt(halfBufferSize);
        sizes.add(size);
        final ByteBuffer bf = initBuffer(dataSize, size);
        futures.add(i == bufferNum - 1 ? out.writeAsync(bf, StandardWriteOption.SYNC) : out.writeAsync(bf));
        dataSize += size;
    }
    {
        // check header
        final DataStreamReply reply = out.getHeaderFuture().join();
        assertSuccessReply(Type.STREAM_HEADER, 0, reply);
    }
    // check writeAsync requests
    for (int i = 0; i < futures.size(); i++) {
        final DataStreamReply reply = futures.get(i).join();
        final Type expectedType = Type.STREAM_DATA;
        assertSuccessReply(expectedType, sizes.get(i).longValue(), reply);
    }
    return dataSize;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Type(org.apache.ratis.proto.RaftProtos.DataStreamPacketHeaderProto.Type) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) DataStreamReplyByteBuffer(org.apache.ratis.datastream.impl.DataStreamReplyByteBuffer) DataStreamRequestByteBuffer(org.apache.ratis.datastream.impl.DataStreamRequestByteBuffer) DataStreamReply(org.apache.ratis.protocol.DataStreamReply)

Example 9 with DataStreamReply

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

the class DataStreamBaseTest method runTestMockCluster.

void runTestMockCluster(ClientId clientId, int bufferSize, int bufferNum, Exception expectedException, Exception headerException) throws IOException {
    try (final RaftClient client = newRaftClientForDataStream(clientId)) {
        final DataStreamOutputImpl out = (DataStreamOutputImpl) client.getDataStreamApi().stream(null, DataStreamTestUtils.getRoutingTableChainTopology(peers, getPrimaryServer().getPeer()));
        if (headerException != null) {
            final DataStreamReply headerReply = out.getHeaderFuture().join();
            Assert.assertFalse(headerReply.isSuccess());
            final RaftClientReply clientReply = ClientProtoUtils.toRaftClientReply(((DataStreamReplyByteBuffer) headerReply).slice());
            Assert.assertTrue(clientReply.getException().getMessage().contains(headerException.getMessage()));
            return;
        }
        final RaftClientReply clientReply = DataStreamTestUtils.writeAndCloseAndAssertReplies(CollectionUtils.as(servers, Server::getRaftServer), null, out, bufferSize, bufferNum, getPrimaryClientId(), client.getId(), false).join();
        if (expectedException != null) {
            Assert.assertFalse(clientReply.isSuccess());
            Assert.assertTrue(clientReply.getException().getMessage().contains(expectedException.getMessage()));
        } else {
            Assert.assertTrue(clientReply.isSuccess());
        }
    }
}
Also used : DataStreamOutputImpl(org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) DataStreamServer(org.apache.ratis.server.DataStreamServer) RaftServer(org.apache.ratis.server.RaftServer) DataStreamReply(org.apache.ratis.protocol.DataStreamReply) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

DataStreamReply (org.apache.ratis.protocol.DataStreamReply)9 CompletableFuture (java.util.concurrent.CompletableFuture)6 ByteBuffer (java.nio.ByteBuffer)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ClientInvocationId (org.apache.ratis.protocol.ClientInvocationId)3 IOException (java.io.IOException)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 DataStreamOutputImpl (org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl)2 RaftProperties (org.apache.ratis.conf.RaftProperties)2 DataStreamReplyByteBuffer (org.apache.ratis.datastream.impl.DataStreamReplyByteBuffer)2 DataStreamRequestByteBuffer (org.apache.ratis.datastream.impl.DataStreamRequestByteBuffer)2 Type (org.apache.ratis.proto.RaftProtos.DataStreamPacketHeaderProto.Type)2 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 File (java.io.File)1 Collection (java.util.Collection)1