chihsuan commented on code in PR #11195:
URL: https://github.com/apache/ozone/pull/11195#discussion_r3940714481
##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/XceiverClientGrpc.java:
##########
@@ -573,16 +573,13 @@ public void streamRead(ContainerCommandRequestProto
request,
request.getReadBlock().getBlockID().getLocalID(),
request.getReadBlock().getOffset(),
request.getReadBlock().getLength());
- final long deadlineNs = System.nanoTime() + streamReadTimeoutNanos;
- while (!obs.isReady() && System.nanoTime() - deadlineNs < 0) {
- LockSupport.parkNanos(10_000_000L);
- if (Thread.currentThread().isInterrupted()) {
- Thread.currentThread().interrupt();
- throw new InterruptedIOException("Interrupted while waiting for
stream to become ready: " + streamObserver);
+ try {
+ if (!streamObserver.awaitReady(streamReadTimeoutNanos)) {
+ throw new TimeoutIOException("Timed out waiting for stream to become
ready: " + streamObserver);
}
- }
- if (!obs.isReady()) {
- throw new TimeoutIOException("Timed out waiting for stream to become
ready: " + streamObserver);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new InterruptedIOException("Interrupted while waiting for stream
to become ready: " + streamObserver);
Review Comment:
nit: this line predates the PR, but `awaitReady()` now gives us a real
`InterruptedException` to work with. Would it be worth preserving the interrupt
cause here?
##########
hadoop-hdds/common/src/test/java/org/apache/hadoop/hdds/scm/TestStreamingReadResponse.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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.hadoop.hdds.scm;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.hadoop.hdds.protocol.MockDatanodeDetails;
+import
org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ContainerCommandRequestProto;
+import org.apache.ratis.thirdparty.io.grpc.stub.ClientCallStreamObserver;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests for {@link StreamingReadResponse#awaitReady(long)}.
+ */
+class TestStreamingReadResponse {
+
+ private static final class FakeRequestObserver extends
ClientCallStreamObserver<ContainerCommandRequestProto> {
+ private final AtomicBoolean ready = new AtomicBoolean();
+
+ @Override
+ public boolean isReady() {
+ return ready.get();
+ }
+
+ @Override
+ public void setOnReadyHandler(Runnable onReadyHandler) {
+ }
+
+ @Override
+ public void disableAutoInboundFlowControl() {
+ }
+
+ @Override
+ public void request(int count) {
+ }
+
+ @Override
+ public void setMessageCompression(boolean enable) {
+ }
+
+ @Override
+ public void cancel(String message, Throwable cause) {
+ }
+
+ @Override
+ public void onNext(ContainerCommandRequestProto value) {
+ }
+
+ @Override
+ public void onError(Throwable t) {
+ }
+
+ @Override
+ public void onCompleted() {
+ }
+ }
+
+ private static StreamingReadResponse newResponse(FakeRequestObserver
observer) {
+ return new
StreamingReadResponse(MockDatanodeDetails.randomDatanodeDetails(), observer);
+ }
+
+ @Test
+ void returnsImmediatelyWhenAlreadyReady() throws Exception {
+ final FakeRequestObserver observer = new FakeRequestObserver();
+ observer.ready.set(true);
+ assertTrue(newResponse(observer).awaitReady(TimeUnit.SECONDS.toNanos(10)));
+ }
+
+ @Test
+ void timesOutWithoutASignal() throws Exception {
+ final FakeRequestObserver observer = new FakeRequestObserver();
+ final long start = System.nanoTime();
+
assertFalse(newResponse(observer).awaitReady(TimeUnit.MILLISECONDS.toNanos(50)));
+ assertTrue(System.nanoTime() - start >= TimeUnit.MILLISECONDS.toNanos(50),
"must wait out the timeout");
+ }
+
+ @Test
+ void wakesOnSignalReady() throws Exception {
+ final FakeRequestObserver observer = new FakeRequestObserver();
+ final StreamingReadResponse response = newResponse(observer);
+ final CountDownLatch waiting = new CountDownLatch(1);
+ final AtomicBoolean result = new AtomicBoolean();
+ final Thread waiter = new Thread(() -> {
+ try {
+ waiting.countDown();
Review Comment:
I wonder if there's a race here. `countDown()` fires before the waiter
calls `awaitReady()`, so this could pass even with a broken `notifyAll()` if
the main thread wins that race. It may be helpful to add a spurious
notification case.
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]