|
90 | 90 | import java.util.List; |
91 | 91 | import java.util.concurrent.CopyOnWriteArrayList; |
92 | 92 | import java.util.concurrent.CountDownLatch; |
| 93 | +import java.util.concurrent.CyclicBarrier; |
93 | 94 | import java.util.concurrent.Executor; |
94 | 95 | import java.util.concurrent.ExecutorService; |
95 | 96 | import java.util.concurrent.Executors; |
| 97 | +import java.util.concurrent.Future; |
96 | 98 | import java.util.concurrent.ScheduledExecutorService; |
97 | 99 | import java.util.concurrent.TimeUnit; |
98 | 100 | import java.util.concurrent.atomic.AtomicBoolean; |
@@ -21129,6 +21131,164 @@ public void onClose(Status status, Metadata trailers) { |
21129 | 21131 | channelManager.close(); |
21130 | 21132 | } |
21131 | 21133 |
|
| 21134 | + @Test |
| 21135 | + public void whenCallCancelledConcurrently_underlyingCallCancelledExactlyOnceAndNoExtProcLeak() |
| 21136 | + throws Exception { |
| 21137 | + String uniqueExtProcServerName = InProcessServerBuilder.generateName(); |
| 21138 | + String uniqueDataPlaneServerName = InProcessServerBuilder.generateName(); |
| 21139 | + |
| 21140 | + ExternalProcessor proto = createBaseProto(uniqueExtProcServerName) |
| 21141 | + .setProcessingMode(ProcessingMode.newBuilder() |
| 21142 | + .setRequestHeaderMode(ProcessingMode.HeaderSendMode.SEND) |
| 21143 | + .setResponseHeaderMode(ProcessingMode.HeaderSendMode.SEND) |
| 21144 | + .build()) |
| 21145 | + .build(); |
| 21146 | + ExternalProcessorFilterConfig filterConfig = |
| 21147 | + provider.parseFilterConfig(Any.pack(proto), filterContext).config; |
| 21148 | + |
| 21149 | + final AtomicInteger extProcErrorCount = new AtomicInteger(); |
| 21150 | + final AtomicInteger extProcRequestsAfterError = new AtomicInteger(); |
| 21151 | + final AtomicBoolean extProcHadError = new AtomicBoolean(false); |
| 21152 | + |
| 21153 | + ExternalProcessorGrpc.ExternalProcessorImplBase extProcImpl = |
| 21154 | + new ExternalProcessorGrpc.ExternalProcessorImplBase() { |
| 21155 | + @Override |
| 21156 | + public StreamObserver<ProcessingRequest> process( |
| 21157 | + StreamObserver<ProcessingResponse> responseObserver) { |
| 21158 | + return new StreamObserver<ProcessingRequest>() { |
| 21159 | + @Override |
| 21160 | + public void onNext(ProcessingRequest request) { |
| 21161 | + if (extProcHadError.get()) { |
| 21162 | + extProcRequestsAfterError.incrementAndGet(); |
| 21163 | + } |
| 21164 | + if (request.hasRequestHeaders()) { |
| 21165 | + responseObserver.onNext(ProcessingResponse.newBuilder() |
| 21166 | + .setRequestHeaders(HeadersResponse.newBuilder().build()) |
| 21167 | + .build()); |
| 21168 | + } |
| 21169 | + } |
| 21170 | + |
| 21171 | + @Override |
| 21172 | + public void onError(Throwable t) { |
| 21173 | + extProcHadError.set(true); |
| 21174 | + extProcErrorCount.incrementAndGet(); |
| 21175 | + } |
| 21176 | + |
| 21177 | + @Override |
| 21178 | + public void onCompleted() { |
| 21179 | + responseObserver.onCompleted(); |
| 21180 | + } |
| 21181 | + }; |
| 21182 | + } |
| 21183 | + }; |
| 21184 | + |
| 21185 | + grpcCleanup.register(InProcessServerBuilder.forName(uniqueExtProcServerName) |
| 21186 | + .addService(extProcImpl) |
| 21187 | + .directExecutor() |
| 21188 | + .build().start()); |
| 21189 | + |
| 21190 | + CachedChannelManager channelManager = new CachedChannelManager(config -> { |
| 21191 | + return grpcCleanup.register( |
| 21192 | + InProcessChannelBuilder.forName(uniqueExtProcServerName).directExecutor().build()); |
| 21193 | + }); |
| 21194 | + |
| 21195 | + ExternalProcessorClientInterceptor interceptor = new ExternalProcessorClientInterceptor( |
| 21196 | + filterConfig, channelManager, scheduler, FAKE_CONTEXT); |
| 21197 | + |
| 21198 | + final CountDownLatch dataPlaneServerStartedLatch = new CountDownLatch(1); |
| 21199 | + final CountDownLatch releaseServerResponseLatch = new CountDownLatch(1); |
| 21200 | + |
| 21201 | + MutableHandlerRegistry dataPlaneRegistry = new MutableHandlerRegistry(); |
| 21202 | + dataPlaneRegistry.addService(ServerServiceDefinition.builder("test.TestService") |
| 21203 | + .addMethod(METHOD_SAY_HELLO, ServerCalls.asyncUnaryCall((request, responseObserver) -> { |
| 21204 | + dataPlaneServerStartedLatch.countDown(); |
| 21205 | + try { |
| 21206 | + releaseServerResponseLatch.await(5, TimeUnit.SECONDS); |
| 21207 | + } catch (InterruptedException e) { |
| 21208 | + Thread.currentThread().interrupt(); |
| 21209 | + } |
| 21210 | + responseObserver.onNext("Hello " + request); |
| 21211 | + responseObserver.onCompleted(); |
| 21212 | + })) |
| 21213 | + .build()); |
| 21214 | + |
| 21215 | + grpcCleanup.register(InProcessServerBuilder.forName(uniqueDataPlaneServerName) |
| 21216 | + .fallbackHandlerRegistry(dataPlaneRegistry) |
| 21217 | + .directExecutor() |
| 21218 | + .build().start()); |
| 21219 | + |
| 21220 | + final AtomicInteger rawCallCancelCount = new AtomicInteger(); |
| 21221 | + ClientInterceptor countingInterceptor = new ClientInterceptor() { |
| 21222 | + @Override |
| 21223 | + public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( |
| 21224 | + MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) { |
| 21225 | + return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) { |
| 21226 | + @Override |
| 21227 | + public void cancel(String message, Throwable cause) { |
| 21228 | + rawCallCancelCount.incrementAndGet(); |
| 21229 | + super.cancel(message, cause); |
| 21230 | + } |
| 21231 | + }; |
| 21232 | + } |
| 21233 | + }; |
| 21234 | + |
| 21235 | + ManagedChannel dataPlaneChannel = grpcCleanup.register( |
| 21236 | + InProcessChannelBuilder.forName(uniqueDataPlaneServerName) |
| 21237 | + .intercept(countingInterceptor) |
| 21238 | + .directExecutor() |
| 21239 | + .build()); |
| 21240 | + |
| 21241 | + ClientCall<String, String> proxyCall = interceptCall( |
| 21242 | + interceptor, |
| 21243 | + METHOD_SAY_HELLO, |
| 21244 | + DEFAULT_CALL_OPTIONS.withExecutor(MoreExecutors.directExecutor()), |
| 21245 | + dataPlaneChannel); |
| 21246 | + |
| 21247 | + proxyCall.start(new ClientCall.Listener<String>() {}, new Metadata()); |
| 21248 | + |
| 21249 | + proxyCall.request(1); |
| 21250 | + proxyCall.sendMessage("ping"); |
| 21251 | + proxyCall.halfClose(); |
| 21252 | + |
| 21253 | + // Wait until the data plane call has started and is active |
| 21254 | + assertThat(dataPlaneServerStartedLatch.await(5, TimeUnit.SECONDS)).isTrue(); |
| 21255 | + |
| 21256 | + // Concurrently release server response and trigger multiple concurrent cancellations |
| 21257 | + int numCancelThreads = 8; |
| 21258 | + ExecutorService executor = Executors.newFixedThreadPool(numCancelThreads + 1); |
| 21259 | + CyclicBarrier barrier = new CyclicBarrier(numCancelThreads + 1); |
| 21260 | + List<Future<?>> futures = new ArrayList<>(); |
| 21261 | + |
| 21262 | + futures.add(executor.submit(() -> { |
| 21263 | + barrier.await(); |
| 21264 | + releaseServerResponseLatch.countDown(); |
| 21265 | + return null; |
| 21266 | + })); |
| 21267 | + |
| 21268 | + for (int i = 0; i < numCancelThreads; i++) { |
| 21269 | + final int threadId = i; |
| 21270 | + futures.add(executor.submit(() -> { |
| 21271 | + barrier.await(); |
| 21272 | + proxyCall.cancel("Cancel from thread " + threadId, null); |
| 21273 | + return null; |
| 21274 | + })); |
| 21275 | + } |
| 21276 | + |
| 21277 | + for (Future<?> f : futures) { |
| 21278 | + f.get(5, TimeUnit.SECONDS); |
| 21279 | + } |
| 21280 | + |
| 21281 | + // Underlying rawCall was cancelled exactly once despite multiple concurrent cancellations |
| 21282 | + assertThat(rawCallCancelCount.get()).isEqualTo(1); |
| 21283 | + // Ext-proc received onError at most once |
| 21284 | + assertThat(extProcErrorCount.get()).isAtMost(1); |
| 21285 | + // Ext-proc never received any requests after being cancelled |
| 21286 | + assertThat(extProcRequestsAfterError.get()).isEqualTo(0); |
| 21287 | + |
| 21288 | + channelManager.close(); |
| 21289 | + shutdownAndAwaitTermination(executor); |
| 21290 | + } |
| 21291 | + |
21132 | 21292 | private static List<ProcessingRequest> filterClientRequests(List<ProcessingRequest> requests) { |
21133 | 21293 | List<ProcessingRequest> clientRequests = new ArrayList<>(); |
21134 | 21294 | for (ProcessingRequest r : requests) { |
|
0 commit comments