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 @@ -31,6 +31,8 @@
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.ProducerConsumerBase;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.common.policies.data.PersistentTopicInternalStats;
import org.apache.pulsar.common.policies.data.PersistentTopicInternalStats.CursorStats;
import org.slf4j.Logger;
Expand All @@ -47,6 +49,7 @@
import static org.mockito.Mockito.spy;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

public class PulsarBrokerStatsClientTest extends ProducerConsumerBase {

Expand Down Expand Up @@ -132,5 +135,65 @@ public void testTopicInternalStats() throws Exception {
log.info("-- Exiting {} test --", methodName);
}

@Test
public void testGetPartitionedTopicMetaData() throws Exception {
log.info("-- Starting {} test --", methodName);

final String topicName = "persistent://my-property/my-ns/my-topic1";
final String subscriptionName = "my-subscriber-name";



try {
String url = "http://localhost:51000,localhost:" + BROKER_WEBSERVICE_PORT;
if (isTcpLookup) {
url = "pulsar://localhost:51000,localhost:" + BROKER_PORT;
}
PulsarClient client = newPulsarClient(url, 0);

Consumer<byte[]> consumer = client.newConsumer().topic(topicName).subscriptionName(subscriptionName)
.acknowledgmentGroupTime(0, TimeUnit.SECONDS).subscribe();
Producer<byte[]> producer = client.newProducer().topic(topicName).create();

consumer.close();
producer.close();
client.close();
} catch (PulsarClientException pce) {
log.error("create producer or consumer error: ", pce);
fail();
}

log.info("-- Exiting {} test --", methodName);
}

@Test (timeOut = 4000)
public void testGetPartitionedTopicDataTimeout() {
log.info("-- Starting {} test --", methodName);

final String topicName = "persistent://my-property/my-ns/my-topic1";

String url = "http://localhost:51000,localhost:51001";
if (isTcpLookup) {
url = "pulsar://localhost:51000,localhost:51001";
}

PulsarClient client;
try {
client = PulsarClient.builder()
.serviceUrl(url)
.statsInterval(0, TimeUnit.SECONDS)
.operationTimeout(3, TimeUnit.SECONDS)
.build();

Producer<byte[]> producer = client.newProducer().topic(topicName).create();

fail();
} catch (PulsarClientException pce) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert the exception time.

log.error("create producer error: ", pce);
}

log.info("-- Exiting {} test --", methodName);
}

private static final Logger log = LoggerFactory.getLogger(PulsarBrokerStatsClientTest.class);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.Closeable;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -127,8 +128,9 @@ public void close() throws IOException {
public <T> CompletableFuture<T> get(String path, Class<T> clazz) {
final CompletableFuture<T> future = new CompletableFuture<>();
try {
String requestUrl = new URL(serviceNameResolver.resolveHostUri().toURL(), path).toString();
String remoteHostName = serviceNameResolver.resolveHostUri().getHost();
Comment on lines -130 to -131

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sijie maybe a bug here, the host in requestUrl not equal with remoteHostName if multi-broker service url provided, and will step forward two service url every request. please take a look.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I don't quite get this. Can you show me an example?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the PulsarServiceNameResolver.resolveHostUri() called twice(every time it will return the next address) inside HttpClient.get() ,
if service url is http://ip1:port1,ip2:port2, we assume requestUrl is ip1:port1, the remoteHostName will be ip2, and the next time call HttpClient.get() the requestUrl is still ip1:port1, the remoteHostName is ip2. so HttpClient.get() will only request ip1:port1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I see. yes. the original code has a bug there.

URI hostUri = serviceNameResolver.resolveHostUri();
String requestUrl = new URL(hostUri.toURL(), path).toString();
String remoteHostName = hostUri.getHost();
AuthenticationDataProvider authData = authentication.getAuthData(remoteHostName);

CompletableFuture<Map<String, String>> authFuture = new CompletableFuture<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,17 +641,45 @@ public CompletableFuture<Integer> getNumberOfPartitions(String topic) {

public CompletableFuture<PartitionedTopicMetadata> getPartitionedTopicMetadata(String topic) {

CompletableFuture<PartitionedTopicMetadata> metadataFuture;
CompletableFuture<PartitionedTopicMetadata> metadataFuture = new CompletableFuture<>();

try {
TopicName topicName = TopicName.get(topic);
metadataFuture = lookup.getPartitionedTopicMetadata(topicName);
AtomicLong opTimeoutMs = new AtomicLong(conf.getOperationTimeoutMs());
Backoff backoff = new BackoffBuilder()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not direct related to this change. but we should consider adding backoff for other places as well.

.setInitialTime(100, TimeUnit.MILLISECONDS)
.setMandatoryStop(opTimeoutMs.get() * 2, TimeUnit.MILLISECONDS)
.setMax(0, TimeUnit.MILLISECONDS)
.useUserConfiguredIntervals(conf.getDefaultBackoffIntervalNanos(),
conf.getMaxBackoffIntervalNanos())
.create();
getPartitionedTopicMetadata(topicName, backoff, opTimeoutMs, metadataFuture);
} catch (IllegalArgumentException e) {
return FutureUtil.failedFuture(new PulsarClientException.InvalidConfigurationException(e.getMessage()));
}
return metadataFuture;
}

private void getPartitionedTopicMetadata(TopicName topicName,
Backoff backoff,
AtomicLong remainingTime,
CompletableFuture<PartitionedTopicMetadata> future) {
lookup.getPartitionedTopicMetadata(topicName).thenAccept(future::complete).exceptionally(e -> {
long nextDelay = Math.min(backoff.next(), remainingTime.get());
if (nextDelay <= 0) {
future.completeExceptionally(new PulsarClientException
.TimeoutException("Could not getPartitionedTopicMetadata within configured timeout."));
return null;
}

timer.newTimeout( task -> {
remainingTime.addAndGet(-nextDelay);
getPartitionedTopicMetadata(topicName, backoff, remainingTime, future);
}, nextDelay, TimeUnit.MILLISECONDS);
return null;
});
}

@Override
public CompletableFuture<List<String>> getPartitionsForTopic(String topic) {
return getPartitionedTopicMetadata(topic).thenApply(metadata -> {
Expand Down