peterxcli commented on code in PR #1481: URL: https://github.com/apache/ratis/pull/1481#discussion_r3458207452
########## ratis-client/src/main/java/org/apache/ratis/client/impl/DataStreamInputImpl.java: ########## @@ -0,0 +1,141 @@ +/* + * 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.impl; + +import org.apache.ratis.client.DataStreamClientRpc; +import org.apache.ratis.client.api.DataStreamInput; +import org.apache.ratis.datastream.DataStreamObserver; +import org.apache.ratis.datastream.impl.DataStreamRequestByteBuffer; +import org.apache.ratis.io.StandardWriteOption; +import org.apache.ratis.proto.RaftProtos.DataStreamPacketHeaderProto.Type; +import org.apache.ratis.protocol.ClientId; +import org.apache.ratis.protocol.DataStreamReply; +import org.apache.ratis.protocol.DataStreamRequestHeader; +import org.apache.ratis.protocol.RaftClientRequest; +import org.apache.ratis.protocol.exceptions.AlreadyClosedException; +import org.apache.ratis.util.JavaUtils; +import org.apache.ratis.util.ReferenceCountedObject; + +import java.io.EOFException; +import java.nio.ByteBuffer; +import java.util.LinkedList; +import java.util.Objects; +import java.util.Queue; +import java.util.concurrent.CompletableFuture; + +final class DataStreamInputImpl implements DataStreamInput, + DataStreamObserver<ReferenceCountedObject<DataStreamReply>> { + private final RaftClientRequest header; + private final ClientId clientId; + private final Queue<ReferenceCountedObject<DataStreamReply>> replies = new LinkedList<>(); + private final Queue<CompletableFuture<ReferenceCountedObject<DataStreamReply>>> pendingReads = new LinkedList<>(); + + /* + * null : the stream is open. + * AlreadyClosedException: the stream is closed. + * Other exception : the stream is failed. + */ + private Throwable readException; + + DataStreamInputImpl(DataStreamClientRpc dataStreamClientRpc, RaftClientRequest request) { + this.header = request; + this.clientId = request.getClientId(); + final ByteBuffer buffer = ClientProtoUtils.toRaftClientRequestProtoByteBuffer(header); + final DataStreamRequestHeader h = new DataStreamRequestHeader(clientId, Type.STREAM_HEADER, + header.getCallId(), 0, buffer.remaining(), StandardWriteOption.FLUSH, StandardWriteOption.CLOSE); + dataStreamClientRpc.streamAsync(new DataStreamRequestByteBuffer(h, buffer), this) + .whenComplete(asWhenCompleteBiConsumer()); + } + + @Override + public synchronized void onNext(ReferenceCountedObject<DataStreamReply> reply) { + if (readException != null) { + return; + } + + reply.retain(); + for (CompletableFuture<ReferenceCountedObject<DataStreamReply>> pending; + (pending = pendingReads.poll()) != null; ) { + if (pending.complete(reply)) { + return; + } + } + replies.add(reply); Review Comment: But if the `onNext` API caller cancel the one of the future, the other future might never get the reply Related testcase added in this patch: https://github.com/apache/ratis/blob/05e55403a5ef53d29be4afb9c197e83f56d3ac03/ratis-test/src/test/java/org/apache/ratis/client/impl/TestDataStreamClientImpl.java#L112-L141 And since you assertion for that case, should we remove the above testcase and add document and tell user DONT CANCEL the return future? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
