Skip to content
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

[fix] [pulsar-client] Fix pendingLookupRequestSemaphore leak when Ser… #18219

Merged
merged 2 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,8 @@ public CompletableFuture<LookupDataResult> newLookup(ByteBuf request, long reque

if (pendingLookupRequestSemaphore.tryAcquire()) {
future.whenComplete((lookupDataResult, throwable) -> {
if (throwable instanceof ConnectException) {
if (throwable instanceof ConnectException
|| throwable instanceof PulsarClientException.LookupException) {
pendingLookupRequestSemaphore.release();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,49 @@ public void testPendingLookupRequestSemaphore() throws Exception {
eventLoop.shutdownGracefully();
}

@Test
public void testPendingLookupRequestSemaphoreServiceNotReady() throws Exception {
EventLoopGroup eventLoop = EventLoopUtil.newEventLoopGroup(1, false, new DefaultThreadFactory("testClientCnxTimeout"));
ClientConfigurationData conf = new ClientConfigurationData();
conf.setOperationTimeoutMs(10_000);
conf.setKeepAliveIntervalSeconds(0);
ClientCnx cnx = new ClientCnx(conf, eventLoop);

ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
Channel channel = mock(Channel.class);
when(ctx.channel()).thenReturn(channel);
ChannelFuture listenerFuture = mock(ChannelFuture.class);
when(listenerFuture.addListener(any())).thenReturn(listenerFuture);
when(ctx.writeAndFlush(any())).thenReturn(listenerFuture);
cnx.channelActive(ctx);
cnx.state = ClientCnx.State.Ready;
CountDownLatch countDownLatch = new CountDownLatch(1);
CompletableFuture<Exception> completableFuture = new CompletableFuture<>();
new Thread(() -> {
try {
Thread.sleep(1_000);
CompletableFuture<BinaryProtoLookupService.LookupDataResult> future =
cnx.newLookup(null, 123);
countDownLatch.countDown();
future.get();
} catch (Exception e) {
completableFuture.complete(e);
}
}).start();
countDownLatch.await();
CommandError commandError = new CommandError();
commandError.setRequestId(123L);
commandError.setError(ServerError.ServiceNotReady);
commandError.setMessage("Service not ready");
cnx.handleError(commandError);
assertTrue(completableFuture.get().getCause() instanceof PulsarClientException.LookupException);
// wait for subsequent calls over
Awaitility.await().untilAsserted(() -> {
assertEquals(cnx.getPendingLookupRequestSemaphore().availablePermits(), conf.getConcurrentLookupRequest());
});
eventLoop.shutdownGracefully();
}

@Test
public void testPendingWaitingLookupRequestSemaphore() throws Exception {
EventLoopGroup eventLoop = EventLoopUtil.newEventLoopGroup(1, false, new DefaultThreadFactory("testClientCnxTimeout"));
Expand Down