Skip to content
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 @@ -20,9 +20,12 @@

import static org.apache.pulsar.common.protocol.Commands.newLookupErrorResponse;
import static org.apache.pulsar.common.protocol.Commands.newPartitionMetadataResponse;
import static org.testng.AssertJUnit.fail;
import com.google.common.collect.Sets;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -34,13 +37,16 @@
import org.apache.pulsar.client.api.MessageId;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.Reader;
import org.apache.pulsar.common.api.proto.CommandLookupTopic;
import org.apache.pulsar.common.api.proto.CommandPartitionedTopicMetadata;
import org.apache.pulsar.common.api.proto.ServerError;
import org.apache.pulsar.common.naming.TopicName;
import org.apache.pulsar.common.partition.PartitionedTopicMetadata;
import org.apache.pulsar.common.policies.data.ClusterData;
import org.apache.pulsar.common.policies.data.TenantInfoImpl;
import org.apache.pulsar.common.util.FutureUtil;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
Expand All @@ -51,6 +57,7 @@ public class LookupRetryTest extends MockedPulsarServiceBaseTest {
private static final String subscription = "reader-sub";
private final AtomicInteger connectionsCreated = new AtomicInteger(0);
private final ConcurrentHashMap<String, Queue<LookupError>> failureMap = new ConcurrentHashMap<>();
private static final String TEST_TIME_OUT_SEMAPHORE_RELEASE = "testTimeoutReleasePendingLookupRequestSemaphore";

@BeforeClass
@Override
Expand Down Expand Up @@ -110,6 +117,30 @@ public void testGetPartitionedMetadataRetries() throws Exception {
}
}

@Test
public void testTimeoutReleasePendingLookupRequestSemaphore() throws Exception {
PulsarClientImpl client = (PulsarClientImpl) newClient();

LookupService lookup = client.getLookup();

CompletableFuture<PartitionedTopicMetadata> future =
lookup.getPartitionedTopicMetadata(TopicName.get(TEST_TIME_OUT_SEMAPHORE_RELEASE), false);
try {
future.get();
fail();
} catch (Exception e) {
Assert.assertTrue(FutureUtil.unwrapCompletionException(e)
instanceof PulsarClientException.TimeoutException);
}

Set<CompletableFuture<ClientCnx>> clientCnxs = client.getCnxPool().getConnections();
Assert.assertEquals(clientCnxs.size(), 1);
ClientCnx clientCnx = ((CompletableFuture<ClientCnx>) clientCnxs.toArray()[0]).get();
Assert.assertEquals(clientCnx.getPendingLookupRequestSemaphore().availablePermits(),
client.conf.getConcurrentLookupRequest());
client.close();
}

@Test
public void testTimeoutRetriesOnPartitionMetadata() throws Exception {
try (PulsarClient client = newClient();
Expand Down Expand Up @@ -282,6 +313,9 @@ private Queue<LookupError> errorList(String topicName) {
@Override
protected void handlePartitionMetadataRequest(CommandPartitionedTopicMetadata partitionMetadata) {
TopicName t = TopicName.get(partitionMetadata.getTopic());
if (t.getLocalName().equals(TEST_TIME_OUT_SEMAPHORE_RELEASE)) {
Comment thread
congbobo184 marked this conversation as resolved.
return;
}
LookupError error = errorList(t.getLocalName()).poll();
if (error == LookupError.TOO_MANY) {
final long requestId = partitionMetadata.getRequestId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,8 @@ public CompletableFuture<LookupDataResult> newLookup(ByteBuf request, long reque
if (pendingLookupRequestSemaphore.tryAcquire()) {
future.whenComplete((lookupDataResult, throwable) -> {
if (throwable instanceof ConnectException
|| throwable instanceof PulsarClientException.LookupException) {
|| throwable instanceof PulsarClientException.LookupException
|| FutureUtil.unwrapCompletionException(throwable) instanceof TimeoutException) {
pendingLookupRequestSemaphore.release();
}
});
Expand Down
Loading