Search in sources :

Example 71 with PulsarClient

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
    }
}
Also used : PulsarClient(org.apache.pulsar.client.api.PulsarClient) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 72 with PulsarClient

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();
    }
}
Also used : PulsarClient(org.apache.pulsar.client.api.PulsarClient) Cleanup(lombok.Cleanup) TimeoutException(java.util.concurrent.TimeoutException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) IOException(java.io.IOException) PersistenceException(org.apache.pulsar.broker.service.BrokerServiceException.PersistenceException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Example 73 with PulsarClient

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();
    }
}
Also used : PulsarClient(org.apache.pulsar.client.api.PulsarClient) Cleanup(lombok.Cleanup) TimeoutException(java.util.concurrent.TimeoutException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) IOException(java.io.IOException) PersistenceException(org.apache.pulsar.broker.service.BrokerServiceException.PersistenceException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Example 74 with PulsarClient

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();
}
Also used : ConsumerImpl(org.apache.pulsar.client.impl.ConsumerImpl) Consumer(org.apache.pulsar.client.api.Consumer) ExecutorService(java.util.concurrent.ExecutorService) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) Test(org.testng.annotations.Test)

Example 75 with PulsarClient

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();
}
Also used : PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) Test(org.testng.annotations.Test)

Aggregations

PulsarClient (org.apache.pulsar.client.api.PulsarClient)87 Test (org.testng.annotations.Test)69 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)27 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)14 IOException (java.io.IOException)12 PersistentTopicStats (org.apache.pulsar.common.policies.data.PersistentTopicStats)12 PropertyAdmin (org.apache.pulsar.common.policies.data.PropertyAdmin)12 URL (java.net.URL)9 LinkedList (java.util.LinkedList)9 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)9 BacklogQuota (org.apache.pulsar.common.policies.data.BacklogQuota)9 Function (org.apache.pulsar.functions.proto.Function)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 Map (java.util.Map)7 Producer (org.apache.pulsar.client.api.Producer)7 Reader (org.apache.pulsar.client.api.Reader)7 ReaderBuilder (org.apache.pulsar.client.api.ReaderBuilder)7 URI (java.net.URI)6 HashMap (java.util.HashMap)6 CompletableFuture (java.util.concurrent.CompletableFuture)6