ss77892 commented on code in PR #11874: URL: https://github.com/apache/ozone/pull/11874#discussion_r4744659967
########## hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/TestXceiverClientGrpcCallerRuns.java: ########## @@ -0,0 +1,679 @@ +/* + * 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.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +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 static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanContext; +import io.opentelemetry.api.trace.TraceFlags; +import io.opentelemetry.api.trace.TraceState; +import io.opentelemetry.context.Scope; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.protocol.DatanodeDetails; +import org.apache.hadoop.hdds.protocol.DatanodeID; +import org.apache.hadoop.hdds.protocol.MockDatanodeDetails; +import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos; +import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ContainerCommandRequestProto; +import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ContainerCommandResponseProto; +import org.apache.hadoop.hdds.protocol.datanode.proto.XceiverClientProtocolServiceGrpc; +import org.apache.hadoop.hdds.ratis.conf.RatisClientConfig; +import org.apache.hadoop.hdds.scm.pipeline.MockPipeline; +import org.apache.hadoop.ozone.common.utils.BufferUtils; +import org.apache.ozone.test.GenericTestUtils; +import org.apache.ratis.thirdparty.com.google.protobuf.ByteString; +import org.apache.ratis.thirdparty.io.grpc.Server; +import org.apache.ratis.thirdparty.io.grpc.Status; +import org.apache.ratis.thirdparty.io.grpc.netty.NettyChannelBuilder; +import org.apache.ratis.thirdparty.io.grpc.netty.NettyServerBuilder; +import org.apache.ratis.thirdparty.io.grpc.stub.ClientCallStreamObserver; +import org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Tests that {@link XceiverClientGrpc} delivers the reply of a blocking call + * on the waiting thread, and that the async path is unchanged. + */ +class TestXceiverClientGrpcCallerRuns { + + private final List<Server> servers = new ArrayList<>(); + private Server server; + private volatile boolean fail; + private volatile ContainerCommandResponseProto response; + private volatile boolean hang; + private volatile boolean duplicateReply; + private volatile Server hangingServer; + private volatile CountDownLatch barrier; + private final AtomicInteger streamsOpened = new AtomicInteger(); + private final AtomicInteger streamsEnded = new AtomicInteger(); + private DatanodeDetails dn; + private XceiverClientGrpc client; + + @BeforeEach + void setUp() throws Exception { + dn = startServer(); + server = servers.get(0); + client = newClient(Collections.singletonList(dn), new OzoneConfiguration()); + } + + /** Starts a server and returns the datanode that it stands for. */ + private DatanodeDetails startServer() throws IOException { + final AtomicReference<Server> self = new AtomicReference<>(); + final Server started = NettyServerBuilder.forPort(0) + .addService(new XceiverClientProtocolServiceGrpc.XceiverClientProtocolServiceImplBase() { + @Override + public StreamObserver<ContainerCommandRequestProto> send( + StreamObserver<ContainerCommandResponseProto> responseObserver) { + streamsOpened.incrementAndGet(); + return new StreamObserver<ContainerCommandRequestProto>() { + @Override + public void onNext(ContainerCommandRequestProto request) { + final CountDownLatch latch = barrier; + if (latch != null) { + latch.countDown(); + try { + latch.await(30, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + if (hang || self.get() == hangingServer) { + return; + } + if (fail) { + responseObserver.onError(Status.INTERNAL.withDescription("injected").asException()); + return; + } + final ContainerCommandResponseProto reply = response; + responseObserver.onNext(reply != null ? reply : ContainerCommandResponseProto.newBuilder() + .setCmdType(request.getCmdType()) + .setResult(ContainerProtos.Result.SUCCESS) + .build()); + if (duplicateReply) { + responseObserver.onNext(ContainerCommandResponseProto.newBuilder() + .setCmdType(request.getCmdType()) + .setResult(ContainerProtos.Result.SUCCESS) + .build()); + } + } + + @Override + public void onError(Throwable t) { + streamsEnded.incrementAndGet(); + } + + @Override + public void onCompleted() { + streamsEnded.incrementAndGet(); + // like a datanode, a server that is still serving the request does not complete the stream + if (!fail && !hang && self.get() != hangingServer) { + responseObserver.onCompleted(); + } + } + }; + } + }) + .build() + .start(); + self.set(started); + servers.add(started); + return MockDatanodeDetails.createDatanodeDetails(DatanodeID.randomID(), "localhost", "127.0.0.1", null, + started.getPort()); + } + + private static XceiverClientGrpc newClient(List<DatanodeDetails> nodes, OzoneConfiguration config) + throws Exception { + XceiverClientGrpc created = new XceiverClientGrpc(MockPipeline.createPipeline(nodes), config); + created.connect(); + return created; + } + + @AfterEach + void tearDown() throws Exception { + client.close(); + for (Server s : servers) { + s.shutdownNow(); + s.awaitTermination(5, TimeUnit.SECONDS); + } + } + + private ContainerCommandRequestProto readContainer() { + return ContainerCommandRequestProto.newBuilder() + .setCmdType(ContainerProtos.Type.ReadContainer) + .setContainerID(1) + .setDatanodeUuid(dn.getUuidString()) + .setReadContainer(ContainerProtos.ReadContainerRequestProto.getDefaultInstance()) + .build(); + } + + /** Holds the next replies until the test thread has been waiting for a while. */ + private void releaseRepliesLater() { + CountDownLatch gate = new CountDownLatch(2); + barrier = gate; + Thread release = new Thread(() -> { + try { + Thread.sleep(50); Review Comment: The reply is now released only after thenRun is registered, with no sleep. With callbacks moved to gRPC's executor, both tests fail 5/5. -- 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]
