-
Notifications
You must be signed in to change notification settings - Fork 0
[WIP] RATIS-2546. Add input stream to DataStreamApi for read operations in Client #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: RATIS-1240-Add-input-stream-to-DataStreamApi-for-read-operations
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.ratis.client.api; | ||
|
|
||
| import org.apache.ratis.protocol.DataStreamReply; | ||
| import org.apache.ratis.protocol.RaftClientReply; | ||
|
|
||
| import java.io.Closeable; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| /** | ||
| * An asynchronous input stream supporting zero buffer copying. | ||
| */ | ||
| public interface DataStreamInput extends Closeable { | ||
| /** | ||
| * Read the next chunk in the stream asynchronously. | ||
| * The caller owns the returned {@link DataStreamReply} and should call | ||
| * {@link DataStreamReply#release()} after consuming it. | ||
| * | ||
| * @return a future of the reply. | ||
| */ | ||
| CompletableFuture<DataStreamReply> readAsync(); | ||
|
|
||
| /** | ||
| * Return the future of the {@link RaftClientReply} | ||
| * which will be received once the read-only stream has received a reply. | ||
| * | ||
| * @return the future of the {@link RaftClientReply}. | ||
| */ | ||
| CompletableFuture<RaftClientReply> getRaftClientReplyFuture(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,9 +23,11 @@ | |
| import org.apache.ratis.client.DataStreamClientRpc; | ||
| import org.apache.ratis.client.DataStreamOutputRpc; | ||
| import org.apache.ratis.client.RaftClient; | ||
| import org.apache.ratis.client.api.DataStreamInput; | ||
| import org.apache.ratis.conf.RaftProperties; | ||
| import org.apache.ratis.datastream.impl.DataStreamPacketByteBuffer; | ||
| import org.apache.ratis.datastream.impl.DataStreamReplyByteBuffer; | ||
| import org.apache.ratis.datastream.impl.DataStreamRequestByteBuffer; | ||
| import org.apache.ratis.io.FilePositionCount; | ||
| import org.apache.ratis.io.StandardWriteOption; | ||
| import org.apache.ratis.io.WriteOption; | ||
|
|
@@ -34,30 +36,34 @@ | |
| import org.apache.ratis.protocol.ClientInvocationId; | ||
| import org.apache.ratis.protocol.DataStreamReply; | ||
| import org.apache.ratis.protocol.DataStreamRequestHeader; | ||
| import org.apache.ratis.protocol.Message; | ||
| import org.apache.ratis.protocol.RaftClientReply; | ||
| import org.apache.ratis.protocol.RaftClientRequest; | ||
| import org.apache.ratis.protocol.RaftGroupId; | ||
| import org.apache.ratis.protocol.RaftPeer; | ||
| import org.apache.ratis.protocol.RoutingTable; | ||
| import org.apache.ratis.protocol.exceptions.AlreadyClosedException; | ||
| import org.apache.ratis.rpc.CallId; | ||
| import org.apache.ratis.thirdparty.com.google.protobuf.ByteString; | ||
| import org.apache.ratis.thirdparty.io.netty.buffer.ByteBuf; | ||
| import org.apache.ratis.util.IOUtils; | ||
| import org.apache.ratis.protocol.*; | ||
| import org.apache.ratis.thirdparty.com.google.protobuf.ByteString; | ||
| import org.apache.ratis.util.JavaUtils; | ||
| import org.apache.ratis.util.MemoizedSupplier; | ||
| import org.apache.ratis.util.Preconditions; | ||
| import org.apache.ratis.util.SlidingWindow; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.EOFException; | ||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.channels.WritableByteChannel; | ||
| import java.util.ArrayDeque; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.Queue; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| /** | ||
|
|
@@ -237,6 +243,117 @@ private CompletableFuture<DataStreamReply> sendForward(DataStreamReply writeRepl | |
| } | ||
| } | ||
|
|
||
| public final class DataStreamInputImpl implements DataStreamInput { | ||
| private final RaftClientRequest header; | ||
| private final CompletableFuture<DataStreamReply> replyFuture; | ||
| private final CompletableFuture<RaftClientReply> raftClientReplyFuture = new CompletableFuture<>(); | ||
| private final Queue<DataStreamReply> replies = new ArrayDeque<>(); | ||
| private final Queue<CompletableFuture<DataStreamReply>> pendingReads = new ArrayDeque<>(); | ||
| private Throwable readException; | ||
| private boolean endOfStream; | ||
| private boolean closed; | ||
|
|
||
| private DataStreamInputImpl(RaftClientRequest request) { | ||
| this.header = request; | ||
| final ByteBuffer buffer = ClientProtoUtils.toRaftClientRequestProtoByteBuffer(header); | ||
| final DataStreamRequestHeader h = new DataStreamRequestHeader(header.getClientId(), Type.STREAM_HEADER, | ||
| header.getCallId(), 0, buffer.remaining(), StandardWriteOption.FLUSH, StandardWriteOption.CLOSE); | ||
| this.replyFuture = dataStreamClientRpc.streamAsync(new DataStreamRequestByteBuffer(h, buffer), this::receive); | ||
| replyFuture.thenApply(ClientProtoUtils::getRaftClientReply) | ||
| .whenComplete(JavaUtils.asBiConsumer(raftClientReplyFuture)); | ||
| replyFuture.whenComplete((reply, exception) -> { | ||
| if (exception != null) { | ||
| failReads(exception); | ||
| } else { | ||
| markEndOfStream(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private void receive(DataStreamReply reply) { | ||
| final CompletableFuture<DataStreamReply> pending; | ||
| synchronized (this) { | ||
| if (closed) { | ||
| reply.release(); | ||
| return; | ||
| } | ||
| pending = pendingReads.poll(); | ||
| if (pending == null) { | ||
| replies.add(reply); | ||
| return; | ||
| } | ||
| } | ||
| pending.complete(reply); | ||
| } | ||
|
|
||
| private void failReads(Throwable t) { | ||
| for (;;) { | ||
| final CompletableFuture<DataStreamReply> pending; | ||
| synchronized (this) { | ||
| readException = t; | ||
| pending = pendingReads.poll(); | ||
| if (pending == null) { | ||
| return; | ||
| } | ||
| } | ||
| pending.completeExceptionally(t); | ||
| } | ||
| } | ||
|
|
||
| private EOFException newEndOfStreamException() { | ||
| return new EOFException(clientId + ": end of stream, request=" + header); | ||
| } | ||
|
|
||
| private void markEndOfStream() { | ||
| for (;;) { | ||
| final CompletableFuture<DataStreamReply> pending; | ||
| synchronized (this) { | ||
| endOfStream = true; | ||
| pending = pendingReads.poll(); | ||
| if (pending == null) { | ||
| return; | ||
| } | ||
| } | ||
| pending.completeExceptionally(newEndOfStreamException()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized CompletableFuture<DataStreamReply> readAsync() { | ||
| if (closed) { | ||
| return JavaUtils.completeExceptionally(new AlreadyClosedException( | ||
| clientId + ": stream already closed, request=" + header)); | ||
| } | ||
| final DataStreamReply reply = replies.poll(); | ||
| if (reply != null) { | ||
| return CompletableFuture.completedFuture(reply); | ||
| } | ||
| if (readException != null) { | ||
| return JavaUtils.completeExceptionally(readException); | ||
| } | ||
| if (endOfStream) { | ||
| return JavaUtils.completeExceptionally(newEndOfStreamException()); | ||
| } | ||
| final CompletableFuture<DataStreamReply> f = new CompletableFuture<>(); | ||
| pendingReads.add(f); | ||
| return f; | ||
| } | ||
|
|
||
| @Override | ||
| public CompletableFuture<RaftClientReply> getRaftClientReplyFuture() { | ||
| return raftClientReplyFuture; | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void close() { | ||
| closed = true; | ||
| for (DataStreamReply reply; (reply = replies.poll()) != null;) { | ||
| reply.release(); | ||
| } | ||
| failReads(new AlreadyClosedException(clientId + ": stream already closed, request=" + header)); | ||
|
Comment on lines
+349
to
+353
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public DataStreamClientRpc getClientRpc() { | ||
| return dataStreamClientRpc; | ||
|
|
@@ -274,6 +391,21 @@ public DataStreamOutputRpc stream(ByteBuffer headerMessage, RoutingTable routing | |
| return new DataStreamOutputImpl(request); | ||
| } | ||
|
|
||
| @Override | ||
| public DataStreamInput streamReadOnly(ByteBuffer headerMessage) { | ||
| final Message message = | ||
| Optional.ofNullable(headerMessage).map(ByteString::copyFrom).map(Message::valueOf).orElse(null); | ||
| final RaftClientRequest request = RaftClientRequest.newBuilder() | ||
| .setClientId(clientId) | ||
| .setServerId(dataStreamServer.getId()) | ||
| .setGroupId(groupId) | ||
| .setCallId(CallId.getAndIncrement()) | ||
| .setMessage(message) | ||
| .setType(RaftClientRequest.readRequestType()) | ||
| .build(); | ||
| return new DataStreamInputImpl(request); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| dataStreamClientRpc.close(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential memory leak of
DataStreamReply(which holds NettyByteBufresources) in two scenarios:readAsync(),pending.complete(reply)will returnfalsebecause the future is already completed. The reply is never released.repliesqueue and might be leaked if the client does not callclose()immediately.We should release the reply immediately if the stream is closed/failed/ended, or if the pending future was already completed.