use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.
the class BrokerServiceTest method testLookupThrottlingForClientByClient.
/**
* Verifies: client side throttling.
*
* @throws Exception
*/
@Test
public void testLookupThrottlingForClientByClient() throws Exception {
final String topicName = "persistent://prop/usw/my-ns/newTopic";
String lookupUrl = new URI("pulsar://localhost:" + BROKER_PORT).toString();
PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(lookupUrl).statsInterval(0, TimeUnit.SECONDS).maxConcurrentLookupRequests(0).build();
try {
pulsarClient.newConsumer().topic(topicName).subscriptionName("mysub").subscribe();
fail("It should fail as throttling should not receive any request");
} catch (org.apache.pulsar.client.api.PulsarClientException.TooManyRequestsException e) {
// ok as throttling set to 0
}
}
use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.
the class BrokerServiceTest method testTlsDisabled.
@Test
public void testTlsDisabled() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/newTopic";
final String subName = "newSub";
PulsarClient pulsarClient = null;
conf.setAuthenticationEnabled(false);
conf.setTlsEnabled(false);
restartBroker();
// Case 1: Access without TLS
try {
pulsarClient = PulsarClient.builder().serviceUrl(brokerUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
@Cleanup Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
} catch (Exception e) {
fail("should not fail");
} finally {
pulsarClient.close();
}
// Case 2: Access with TLS
try {
pulsarClient = PulsarClient.builder().serviceUrl(brokerUrlTls.toString()).enableTls(true).statsInterval(0, TimeUnit.SECONDS).build();
@Cleanup Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
fail("TLS connection should fail");
} catch (Exception e) {
assertTrue(e.getMessage().contains("ConnectException"));
} finally {
pulsarClient.close();
}
}
use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.
the class BrokerServiceTest method testTlsEnabled.
@Test
public void testTlsEnabled() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/newTopic";
final String subName = "newSub";
conf.setAuthenticationEnabled(false);
conf.setTlsEnabled(true);
conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
restartBroker();
// Case 1: Access without TLS
PulsarClient pulsarClient = null;
try {
pulsarClient = PulsarClient.builder().serviceUrl(brokerUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
@Cleanup Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
} catch (Exception e) {
fail("should not fail");
} finally {
pulsarClient.close();
}
// Case 2: Access with TLS (Allow insecure TLS connection)
try {
pulsarClient = PulsarClient.builder().serviceUrl(brokerUrlTls.toString()).enableTls(true).allowTlsInsecureConnection(true).statsInterval(0, TimeUnit.SECONDS).build();
@Cleanup Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
} catch (Exception e) {
fail("should not fail");
} finally {
pulsarClient.close();
}
// Case 3: Access with TLS (Disallow insecure TLS connection)
try {
pulsarClient = PulsarClient.builder().serviceUrl(brokerUrlTls.toString()).enableTls(true).allowTlsInsecureConnection(false).statsInterval(0, TimeUnit.SECONDS).build();
@Cleanup Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
fail("should fail");
} catch (Exception e) {
assertTrue(e.getMessage().contains("General OpenSslEngine problem"));
} finally {
pulsarClient.close();
}
// Case 4: Access with TLS (Use trusted certificates)
try {
pulsarClient = PulsarClient.builder().serviceUrl(brokerUrlTls.toString()).enableTls(true).allowTlsInsecureConnection(false).tlsTrustCertsFilePath(TLS_SERVER_CERT_FILE_PATH).statsInterval(0, TimeUnit.SECONDS).build();
@Cleanup Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
} catch (Exception e) {
fail("should not fail");
} finally {
pulsarClient.close();
}
}
use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.
the class BrokerServiceThrottlingTest method testLookupThrottlingForClientByBrokerInternalRetry.
/**
* This testcase make sure that once consumer lost connection with broker, it always reconnects with broker by
* retrying on throttling-error exception also.
*
* <pre>
* 1. all consumers get connected
* 2. broker restarts with maxConcurrentLookupRequest = 1
* 3. consumers reconnect and some get TooManyRequestException and again retries
* 4. eventually all consumers will successfully connect to broker
* </pre>
*
* @throws Exception
*/
@Test
public void testLookupThrottlingForClientByBrokerInternalRetry() throws Exception {
final String topicName = "persistent://prop/usw/my-ns/newTopic";
String lookupUrl = new URI("pulsar://localhost:" + BROKER_PORT).toString();
PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(lookupUrl).statsInterval(0, TimeUnit.SECONDS).ioThreads(20).connectionsPerBroker(20).build();
upsertLookupPermits(100);
List<Consumer<byte[]>> consumers = Collections.synchronizedList(Lists.newArrayList());
ExecutorService executor = Executors.newFixedThreadPool(10);
final int totalConsumers = 8;
CountDownLatch latch = new CountDownLatch(totalConsumers);
for (int i = 0; i < totalConsumers; i++) {
executor.execute(() -> {
try {
consumers.add(pulsarClient.newConsumer().topic(topicName).subscriptionName("mysub").subscriptionType(SubscriptionType.Shared).subscribe());
} catch (PulsarClientException.TooManyRequestsException e) {
// ok
} catch (Exception e) {
fail("it shouldn't failed");
}
latch.countDown();
});
}
latch.await();
stopBroker();
conf.setMaxConcurrentLookupRequest(1);
startBroker();
// wait strategically for all consumers to reconnect
retryStrategically((test) -> areAllConsumersConnected(consumers), 5, 500);
int totalConnectedConsumers = 0;
for (int i = 0; i < consumers.size(); i++) {
if (((ConsumerImpl<?>) consumers.get(i)).isConnected()) {
totalConnectedConsumers++;
}
consumers.get(i).close();
}
assertEquals(totalConnectedConsumers, totalConsumers);
executor.shutdown();
pulsarClient.close();
}
use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.
the class PeerReplicatorTest method testPeerClusterTopicLookup.
/**
* It verifies that lookup/admin requests for global-namespace would be redirected to peer-cluster if local cluster
* doesn't own it and peer-cluster owns it, else request will be failed.
* <pre>
* 1. Create global-namespace ns1 for replication cluster-r1
* 2. Try to create producer using broker in cluster r3
* 3. Reject lookup: "r3" receives request and doesn't find namespace in local/peer cluster
* 4. Add "r1" as a peer-cluster into "r3"
* 5. Try to create producer using broker in cluster r3
* 6. Success : "r3" finds "r1" in peer cluster which owns n1 and redirects to "r1"
* 7. call admin-api to "r3" which redirects request to "r1"
*
* </pre>
*
* @param protocol
* @throws Exception
*/
@Test(dataProvider = "lookupType")
public void testPeerClusterTopicLookup(String protocol) throws Exception {
final String serviceUrl = protocol.equalsIgnoreCase("http") ? pulsar3.getWebServiceAddress() : pulsar3.getBrokerServiceUrl();
final String namespace1 = "pulsar/global/peer1-" + protocol;
final String namespace2 = "pulsar/global/peer2-" + protocol;
admin1.namespaces().createNamespace(namespace1);
admin1.namespaces().createNamespace(namespace2);
// add replication cluster
admin1.namespaces().setNamespaceReplicationClusters(namespace1, Lists.newArrayList("r1"));
admin1.namespaces().setNamespaceReplicationClusters(namespace2, Lists.newArrayList("r2"));
admin1.clusters().updatePeerClusterNames("r3", null);
// disable tls as redirection url is prepared according tls configuration
pulsar1.getConfiguration().setTlsEnabled(false);
pulsar2.getConfiguration().setTlsEnabled(false);
pulsar3.getConfiguration().setTlsEnabled(false);
final String topic1 = "persistent://" + namespace1 + "/topic1";
final String topic2 = "persistent://" + namespace2 + "/topic2";
PulsarClient client3 = PulsarClient.builder().serviceUrl(serviceUrl).statsInterval(0, TimeUnit.SECONDS).build();
try {
// try to create producer for topic1 (part of cluster: r1) by calling cluster: r3
client3.newProducer().topic(topic1).create();
fail("should have failed as cluster:r3 doesn't own namespace");
} catch (PulsarClientException e) {
// Ok
}
try {
// try to create producer for topic2 (part of cluster: r2) by calling cluster: r3
client3.newProducer().topic(topic2).create();
fail("should have failed as cluster:r3 doesn't own namespace");
} catch (PulsarClientException e) {
// Ok
}
// set peer-clusters : r3->r1
admin1.clusters().updatePeerClusterNames("r3", Sets.newLinkedHashSet(Lists.newArrayList("r1")));
Producer<byte[]> producer = client3.newProducer().topic(topic1).create();
PersistentTopic topic = (PersistentTopic) pulsar1.getBrokerService().getTopic(topic1).get();
assertNotNull(topic);
pulsar1.getBrokerService().updateRates();
// get stats for topic1 using cluster-r3's admin3
PersistentTopicStats stats = admin1.persistentTopics().getStats(topic1);
assertNotNull(stats);
assertEquals(stats.publishers.size(), 1);
stats = admin3.persistentTopics().getStats(topic1);
assertNotNull(stats);
assertEquals(stats.publishers.size(), 1);
producer.close();
// set peer-clusters : r3->r2
admin2.clusters().updatePeerClusterNames("r3", Sets.newLinkedHashSet(Lists.newArrayList("r2")));
producer = client3.newProducer().topic(topic2).create();
topic = (PersistentTopic) pulsar2.getBrokerService().getTopic(topic2).get();
assertNotNull(topic);
pulsar2.getBrokerService().updateRates();
// get stats for topic1 using cluster-r3's admin3
stats = admin3.persistentTopics().getStats(topic2);
assertNotNull(stats);
assertEquals(stats.publishers.size(), 1);
stats = admin3.persistentTopics().getStats(topic2);
assertNotNull(stats);
assertEquals(stats.publishers.size(), 1);
producer.close();
client3.close();
}
Aggregations