Search in sources :

Example 36 with PulsarClient

use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.

the class BrokerServiceTest method testTlsEnabled.

@Test
public void testTlsEnabled() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/newTopic";
    final String subName = "newSub";
    ClientConfiguration clientConfig;
    ConsumerConfiguration consumerConfig;
    Consumer consumer;
    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 {
        clientConfig = new ClientConfiguration();
        clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
        pulsarClient = PulsarClient.create(brokerUrl.toString(), clientConfig);
        consumerConfig = new ConsumerConfiguration();
        consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
        consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
        consumer.close();
    } catch (Exception e) {
        fail("should not fail");
    } finally {
        pulsarClient.close();
    }
    // Case 2: Access with TLS (Allow insecure TLS connection)
    try {
        clientConfig = new ClientConfiguration();
        clientConfig.setUseTls(true);
        clientConfig.setTlsAllowInsecureConnection(true);
        clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
        pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
        consumerConfig = new ConsumerConfiguration();
        consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
        consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
        consumer.close();
    } catch (Exception e) {
        fail("should not fail");
    } finally {
        pulsarClient.close();
    }
    // Case 3: Access with TLS (Disallow insecure TLS connection)
    try {
        clientConfig = new ClientConfiguration();
        clientConfig.setUseTls(true);
        clientConfig.setTlsAllowInsecureConnection(false);
        clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
        pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
        consumerConfig = new ConsumerConfiguration();
        consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
        consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
        consumer.close();
        fail("should fail");
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("General SSLEngine problem"));
    } finally {
        pulsarClient.close();
    }
    // Case 4: Access with TLS (Use trusted certificates)
    try {
        clientConfig = new ClientConfiguration();
        clientConfig.setUseTls(true);
        clientConfig.setTlsAllowInsecureConnection(false);
        clientConfig.setTlsTrustCertsFilePath(TLS_SERVER_CERT_FILE_PATH);
        clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
        pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
        consumerConfig = new ConsumerConfiguration();
        consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
        consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
        consumer.close();
    } catch (Exception e) {
        fail("should not fail");
    } finally {
        pulsarClient.close();
    }
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) ClientConfiguration(com.yahoo.pulsar.client.api.ClientConfiguration) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 37 with PulsarClient

use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.

the class BrokerServiceTest method testTlsAuthUseTrustCert.

@Test
public void testTlsAuthUseTrustCert() throws Exception {
    final String topicName = "persistent://prop/usw/my-ns/newTopic";
    final String subName = "newSub";
    ClientConfiguration clientConfig;
    ConsumerConfiguration consumerConfig;
    Consumer consumer;
    Authentication auth;
    Set<String> providers = new HashSet<>();
    providers.add("com.yahoo.pulsar.broker.authentication.AuthenticationProviderTls");
    conf.setAuthenticationEnabled(true);
    conf.setAuthenticationProviders(providers);
    conf.setTlsEnabled(true);
    conf.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    conf.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    conf.setTlsAllowInsecureConnection(false);
    conf.setTlsTrustCertsFilePath(TLS_CLIENT_CERT_FILE_PATH);
    restartBroker();
    Map<String, String> authParams = new HashMap<>();
    authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
    authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
    PulsarClient pulsarClient = null;
    // Case 1: Access without client certificate
    try {
        clientConfig = new ClientConfiguration();
        clientConfig.setUseTls(true);
        clientConfig.setTlsAllowInsecureConnection(true);
        clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
        pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
        consumerConfig = new ConsumerConfiguration();
        consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
        consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
        consumer.close();
        fail("should fail");
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("Authentication required"));
    } finally {
        pulsarClient.close();
    }
    // Case 2: Access with client certificate
    try {
        auth = new AuthenticationTls();
        auth.configure(authParams);
        clientConfig = new ClientConfiguration();
        clientConfig.setAuthentication(auth);
        clientConfig.setUseTls(true);
        clientConfig.setTlsAllowInsecureConnection(true);
        clientConfig.setStatsInterval(0, TimeUnit.SECONDS);
        pulsarClient = PulsarClient.create(brokerUrlTls.toString(), clientConfig);
        consumerConfig = new ConsumerConfiguration();
        consumerConfig.setSubscriptionType(SubscriptionType.Exclusive);
        consumer = pulsarClient.subscribe(topicName, subName, consumerConfig);
        consumer.close();
    } catch (Exception e) {
        fail("should not fail");
    } finally {
        pulsarClient.close();
    }
}
Also used : AuthenticationTls(com.yahoo.pulsar.client.impl.auth.AuthenticationTls) Consumer(com.yahoo.pulsar.client.api.Consumer) HashMap(java.util.HashMap) Authentication(com.yahoo.pulsar.client.api.Authentication) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) ClientConfiguration(com.yahoo.pulsar.client.api.ClientConfiguration) IOException(java.io.IOException) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 38 with PulsarClient

use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.

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";
    com.yahoo.pulsar.client.api.ClientConfiguration clientConf = new com.yahoo.pulsar.client.api.ClientConfiguration();
    clientConf.setStatsInterval(0, TimeUnit.SECONDS);
    clientConf.setConcurrentLookupRequest(0);
    String lookupUrl = new URI("pulsar://localhost:" + BROKER_PORT).toString();
    PulsarClient pulsarClient = PulsarClient.create(lookupUrl, clientConf);
    try {
        Consumer consumer = pulsarClient.subscribe(topicName, "mysub", new ConsumerConfiguration());
        fail("It should fail as throttling should not receive any request");
    } catch (com.yahoo.pulsar.client.api.PulsarClientException.TooManyLookupRequestException e) {
    // ok as throttling set to 0
    }
}
Also used : URI(java.net.URI) Consumer(com.yahoo.pulsar.client.api.Consumer) ClientConfiguration(com.yahoo.pulsar.client.api.ClientConfiguration) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) ClientConfiguration(com.yahoo.pulsar.client.api.ClientConfiguration) Test(org.testng.annotations.Test)

Example 39 with PulsarClient

use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.

the class BrokerServiceThrottlingTest method testLookupThrottlingForClientByBroker0Permit.

/**
     * Broker has maxConcurrentLookupRequest = 0 so, it rejects incoming lookup request and it cause consumer creation
     * failure.
     * 
     * @throws Exception
     */
@Test
public void testLookupThrottlingForClientByBroker0Permit() throws Exception {
    // create configuration znode
    ZkUtils.createFullPathOptimistic(mockZookKeeper, BROKER_SERVICE_CONFIGURATION_PATH, "{}".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // Now, znode is created: set the watch and listener on the znode
    setWatchOnThrottlingZnode();
    final String topicName = "persistent://prop/usw/my-ns/newTopic";
    com.yahoo.pulsar.client.api.ClientConfiguration clientConf = new com.yahoo.pulsar.client.api.ClientConfiguration();
    clientConf.setStatsInterval(0, TimeUnit.SECONDS);
    String lookupUrl = new URI("pulsar://localhost:" + BROKER_PORT).toString();
    PulsarClient pulsarClient = PulsarClient.create(lookupUrl, clientConf);
    ConsumerConfiguration consumerConfig = new ConsumerConfiguration();
    Consumer consumer = pulsarClient.subscribe(topicName, "mysub", consumerConfig);
    consumer.close();
    int newPermits = 0;
    admin.brokers().updateDynamicConfiguration("maxConcurrentLookupRequest", Integer.toString(newPermits));
    // wait config to be updated
    for (int i = 0; i < 5; i++) {
        if (pulsar.getConfiguration().getMaxConcurrentLookupRequest() != newPermits) {
            Thread.sleep(100 + (i * 10));
        } else {
            break;
        }
    }
    try {
        consumer = pulsarClient.subscribe(topicName, "mysub", consumerConfig);
        consumer.close();
        fail("It should fail as throttling should not receive any request");
    } catch (com.yahoo.pulsar.client.api.PulsarClientException.TooManyLookupRequestException e) {
    // ok as throttling set to 0
    }
}
Also used : URI(java.net.URI) Consumer(com.yahoo.pulsar.client.api.Consumer) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Example 40 with PulsarClient

use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.

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 {
    // create configuration znode
    ZkUtils.createFullPathOptimistic(mockZookKeeper, BROKER_SERVICE_CONFIGURATION_PATH, "{}".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // Now, znode is created: set the watch and listener on the znode
    setWatchOnThrottlingZnode();
    final String topicName = "persistent://prop/usw/my-ns/newTopic";
    com.yahoo.pulsar.client.api.ClientConfiguration clientConf = new com.yahoo.pulsar.client.api.ClientConfiguration();
    clientConf.setStatsInterval(0, TimeUnit.SECONDS);
    clientConf.setIoThreads(20);
    clientConf.setConnectionsPerBroker(20);
    String lookupUrl = new URI("pulsar://localhost:" + BROKER_PORT).toString();
    PulsarClient pulsarClient = PulsarClient.create(lookupUrl, clientConf);
    upsertLookupPermits(100);
    ConsumerConfiguration consumerConfig = new ConsumerConfiguration();
    consumerConfig.setSubscriptionType(SubscriptionType.Shared);
    List<Consumer> consumers = 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.subscribe(topicName, "mysub", consumerConfig));
            } catch (PulsarClientException.TooManyLookupRequestException 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
    for (int i = 0; i < 5; i++) {
        if (!areAllConsumersConnected(consumers)) {
            Thread.sleep(1000 + (i * 500));
        } else {
            break;
        }
    }
    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);
    pulsarClient.close();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) ConsumerImpl(com.yahoo.pulsar.client.impl.ConsumerImpl) Consumer(com.yahoo.pulsar.client.api.Consumer) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) ExecutorService(java.util.concurrent.ExecutorService) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Aggregations

PulsarClient (com.yahoo.pulsar.client.api.PulsarClient)60 Test (org.testng.annotations.Test)39 Consumer (com.yahoo.pulsar.client.api.Consumer)32 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)32 ClientConfiguration (com.yahoo.pulsar.client.api.ClientConfiguration)28 Producer (com.yahoo.pulsar.client.api.Producer)26 ConsumerConfiguration (com.yahoo.pulsar.client.api.ConsumerConfiguration)24 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 LookupException (com.yahoo.pulsar.client.api.PulsarClientException.LookupException)13 ExecutionException (java.util.concurrent.ExecutionException)13 ProducerConfiguration (com.yahoo.pulsar.client.api.ProducerConfiguration)10 PersistentTopicStats (com.yahoo.pulsar.common.policies.data.PersistentTopicStats)10 Message (com.yahoo.pulsar.client.api.Message)9 BacklogQuota (com.yahoo.pulsar.common.policies.data.BacklogQuota)9 IOException (java.io.IOException)8 URI (java.net.URI)6 URL (java.net.URL)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)5