Search in sources :

Example 1 with DataStreamReply

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

the class NettyClientStreamRpc method streamAsync.

@Override
public CompletableFuture<DataStreamReply> streamAsync(DataStreamRequest request) {
    final CompletableFuture<DataStreamReply> f = new CompletableFuture<>();
    ClientInvocationId clientInvocationId = ClientInvocationId.valueOf(request.getClientId(), request.getStreamId());
    final ReplyQueue q = replies.computeIfAbsent(clientInvocationId, key -> new ReplyQueue());
    if (!q.offer(f)) {
        f.completeExceptionally(new IllegalStateException(this + ": Failed to offer a future for " + request));
        return f;
    }
    LOG.debug("{}: write {}", this, request);
    getChannel().writeAndFlush(request);
    return f;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ClientInvocationId(org.apache.ratis.protocol.ClientInvocationId) DataStreamReply(org.apache.ratis.protocol.DataStreamReply)

Example 2 with DataStreamReply

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

the class DataStreamManagement method readImpl.

private void readImpl(DataStreamRequestByteBuf request, ChannelHandlerContext ctx, ByteBuf buf, CheckedBiFunction<RaftClientRequest, Set<RaftPeer>, Set<DataStreamOutputRpc>, IOException> getStreams) {
    boolean close = WriteOption.containsOption(request.getWriteOptions(), StandardWriteOption.CLOSE);
    ClientInvocationId key = ClientInvocationId.valueOf(request.getClientId(), request.getStreamId());
    final StreamInfo info;
    if (request.getType() == Type.STREAM_HEADER) {
        final MemoizedSupplier<StreamInfo> supplier = JavaUtils.memoize(() -> newStreamInfo(buf, getStreams));
        info = streams.computeIfAbsent(key, id -> supplier.get());
        if (!supplier.isInitialized()) {
            streams.remove(key);
            throw new IllegalStateException("Failed to create a new stream for " + request + " since a stream already exists Key: " + key + " StreamInfo:" + info);
        }
        getMetrics().onRequestCreate(RequestType.HEADER);
    } else if (close) {
        info = Optional.ofNullable(streams.remove(key)).orElseThrow(() -> new IllegalStateException("Failed to remove StreamInfo for " + request));
    } else {
        info = Optional.ofNullable(streams.get(key)).orElseThrow(() -> {
            streams.remove(key);
            return new IllegalStateException("Failed to get StreamInfo for " + request);
        });
    }
    final CompletableFuture<Long> localWrite;
    final List<CompletableFuture<DataStreamReply>> remoteWrites;
    if (request.getType() == Type.STREAM_HEADER) {
        localWrite = CompletableFuture.completedFuture(0L);
        remoteWrites = Collections.emptyList();
    } else if (request.getType() == Type.STREAM_DATA) {
        localWrite = info.getLocal().write(buf, request.getWriteOptions(), writeExecutor);
        remoteWrites = info.applyToRemotes(out -> out.write(request, requestExecutor));
    } else {
        throw new IllegalStateException(this + ": Unexpected type " + request.getType() + ", request=" + request);
    }
    composeAsync(info.getPrevious(), requestExecutor, n -> JavaUtils.allOf(remoteWrites).thenCombineAsync(localWrite, (v, bytesWritten) -> {
        if (request.getType() == Type.STREAM_HEADER || (request.getType() == Type.STREAM_DATA && !close)) {
            sendReply(remoteWrites, request, bytesWritten, info.getCommitInfos(), ctx);
        } else if (close) {
            if (info.isPrimary()) {
                // after all server close stream, primary server start transaction
                startTransaction(info, request, bytesWritten, ctx);
            } else {
                sendReply(remoteWrites, request, bytesWritten, info.getCommitInfos(), ctx);
            }
        } else {
            throw new IllegalStateException(this + ": Unexpected type " + request.getType() + ", request=" + request);
        }
        return null;
    }, requestExecutor)).whenComplete((v, exception) -> {
        try {
            if (exception != null) {
                streams.remove(key);
                replyDataStreamException(server, exception, info.getRequest(), request, ctx);
            }
        } finally {
            buf.release();
        }
    });
}
Also used : RoutingTable(org.apache.ratis.protocol.RoutingTable) RequestContext(org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestContext) LoggerFactory(org.slf4j.LoggerFactory) ByteBuffer(java.nio.ByteBuffer) RaftConfiguration(org.apache.ratis.server.RaftConfiguration) DataStreamReplyByteBuffer(org.apache.ratis.datastream.impl.DataStreamReplyByteBuffer) WriteOption(org.apache.ratis.io.WriteOption) JavaUtils(org.apache.ratis.util.JavaUtils) MemoizedSupplier(org.apache.ratis.util.MemoizedSupplier) RaftPeer(org.apache.ratis.protocol.RaftPeer) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) AlreadyExistsException(org.apache.ratis.protocol.exceptions.AlreadyExistsException) DataChannel(org.apache.ratis.statemachine.StateMachine.DataChannel) ClientProtoUtils(org.apache.ratis.client.impl.ClientProtoUtils) List(java.util.List) ClientId(org.apache.ratis.protocol.ClientId) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftProperties(org.apache.ratis.conf.RaftProperties) NettyServerStreamRpcMetrics(org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics) Optional(java.util.Optional) ClientInvocationId(org.apache.ratis.protocol.ClientInvocationId) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) DataStreamOutputRpc(org.apache.ratis.client.DataStreamOutputRpc) Preconditions(org.apache.ratis.util.Preconditions) ChannelHandlerContext(org.apache.ratis.thirdparty.io.netty.channel.ChannelHandlerContext) StandardWriteOption(org.apache.ratis.io.StandardWriteOption) RequestMetrics(org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestMetrics) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) DataStreamException(org.apache.ratis.protocol.exceptions.DataStreamException) ConcurrentMap(java.util.concurrent.ConcurrentMap) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) RaftClientRequestProto(org.apache.ratis.proto.RaftProtos.RaftClientRequestProto) DataStreamReply(org.apache.ratis.protocol.DataStreamReply) AsyncRpcApi(org.apache.ratis.client.AsyncRpcApi) ConcurrentUtils(org.apache.ratis.util.ConcurrentUtils) ByteBuf(org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf) DataStream(org.apache.ratis.statemachine.StateMachine.DataStream) Logger(org.slf4j.Logger) CheckedBiFunction(org.apache.ratis.util.function.CheckedBiFunction) Executor(java.util.concurrent.Executor) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) IOException(java.io.IOException) Type(org.apache.ratis.proto.RaftProtos.DataStreamPacketHeaderProto.Type) Division(org.apache.ratis.server.RaftServer.Division) CommitInfoProto(org.apache.ratis.proto.RaftProtos.CommitInfoProto) RequestType(org.apache.ratis.netty.metrics.NettyServerStreamRpcMetrics.RequestType) RaftServer(org.apache.ratis.server.RaftServer) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) ClientInvocationId(org.apache.ratis.protocol.ClientInvocationId)

Example 3 with DataStreamReply

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

the class DataStreamManagement method checkSuccessRemoteWrite.

static boolean checkSuccessRemoteWrite(List<CompletableFuture<DataStreamReply>> replyFutures, long bytesWritten, final DataStreamRequestByteBuf request) {
    for (CompletableFuture<DataStreamReply> replyFuture : replyFutures) {
        final DataStreamReply reply = replyFuture.join();
        assertReplyCorrespondingToRequest(request, reply);
        if (!reply.isSuccess()) {
            LOG.warn("reply is not success, request: {}", request);
            return false;
        }
        if (reply.getBytesWritten() != bytesWritten) {
            LOG.warn("reply written bytes not match, local size: {} remote size: {} request: {}", bytesWritten, reply.getBytesWritten(), request);
            return false;
        }
    }
    return true;
}
Also used : DataStreamReply(org.apache.ratis.protocol.DataStreamReply)

Example 4 with DataStreamReply

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

the class FileStoreWriter method streamWriteAndVerify.

public FileStoreWriter streamWriteAndVerify(RoutingTable routingTable) {
    final int size = fileSize.getSizeInt();
    final DataStreamOutput dataStreamOutput = client.getStreamOutput(fileName, size, routingTable);
    final List<CompletableFuture<DataStreamReply>> futures = new ArrayList<>();
    final List<Integer> sizes = new ArrayList<>();
    for (int offset = 0; offset < size; ) {
        final int remaining = size - offset;
        final int length = Math.min(remaining, bufferSize);
        final boolean close = length == remaining;
        LOG.trace("write {}, offset={}, length={}, close? {}", fileName, offset, length, close);
        final ByteBuffer bf = DataStreamTestUtils.initBuffer(0, length);
        futures.add(close ? dataStreamOutput.writeAsync(bf, StandardWriteOption.CLOSE) : dataStreamOutput.writeAsync(bf));
        sizes.add(length);
        offset += length;
    }
    DataStreamReply reply = dataStreamOutput.closeAsync().join();
    Assert.assertTrue(reply.isSuccess());
    // check writeAsync requests
    for (int i = 0; i < futures.size(); i++) {
        reply = futures.get(i).join();
        Assert.assertTrue(reply.isSuccess());
        Assert.assertEquals(sizes.get(i).longValue(), reply.getBytesWritten());
        Assert.assertEquals(reply.getType(), RaftProtos.DataStreamPacketHeaderProto.Type.STREAM_DATA);
    }
    return this;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) DataStreamReply(org.apache.ratis.protocol.DataStreamReply) DataStreamOutput(org.apache.ratis.client.api.DataStreamOutput)

Example 5 with DataStreamReply

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

the class DataStreamClusterTests method writeAsyncDefaultFileRegion.

static CheckedConsumer<DataStreamOutputImpl, Exception> writeAsyncDefaultFileRegion(File f, int size) {
    return new CheckedConsumer<DataStreamOutputImpl, Exception>() {

        @Override
        public void accept(DataStreamOutputImpl out) {
            final DataStreamReply dataStreamReply = out.writeAsync(f).join();
            DataStreamTestUtils.assertSuccessReply(Type.STREAM_DATA, size, dataStreamReply);
        }

        @Override
        public String toString() {
            return "writeAsyncDefaultFileRegion";
        }
    };
}
Also used : CheckedConsumer(org.apache.ratis.util.function.CheckedConsumer) DataStreamOutputImpl(org.apache.ratis.client.impl.DataStreamClientImpl.DataStreamOutputImpl) DataStreamReply(org.apache.ratis.protocol.DataStreamReply)

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