Search in sources :

Example 26 with PulsarClient

use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.

the class ProxyWithoutServiceDiscoveryTest method testDiscoveryService.

/**
 * <pre>
 * It verifies e2e tls + Authentication + Authorization (client -> proxy -> broker>
 *
 * 1. client connects to proxy over tls and pass auth-data
 * 2. proxy authenticate client and retrieve client-role
 *    and send it to broker as originalPrincipal over tls
 * 3. client creates producer/consumer via proxy
 * 4. broker authorize producer/consumer create request using originalPrincipal
 *
 * </pre>
 *
 * @throws Exception
 */
@Test
public void testDiscoveryService() throws Exception {
    log.info("-- Starting {} test --", methodName);
    final String proxyServiceUrl = "pulsar://localhost:" + proxyConfig.getServicePortTls();
    Map<String, String> authParams = Maps.newHashMap();
    authParams.put("tlsCertFile", TLS_CLIENT_CERT_FILE_PATH);
    authParams.put("tlsKeyFile", TLS_CLIENT_KEY_FILE_PATH);
    Authentication authTls = new AuthenticationTls();
    authTls.configure(authParams);
    // create a client which connects to proxy over tls and pass authData
    PulsarClient proxyClient = createPulsarClient(authTls, proxyServiceUrl);
    admin.properties().createProperty("my-property", new PropertyAdmin(Lists.newArrayList("appid1", "appid2"), Sets.newHashSet("without-service-discovery")));
    admin.namespaces().createNamespace("my-property/without-service-discovery/my-ns");
    Consumer<byte[]> consumer = proxyClient.newConsumer().topic("persistent://my-property/without-service-discovery/my-ns/my-topic1").subscriptionName("my-subscriber-name").subscribe();
    Producer<byte[]> producer = proxyClient.newProducer().topic("persistent://my-property/without-service-discovery/my-ns/my-topic1").create();
    final int msgs = 10;
    for (int i = 0; i < msgs; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }
    Message<byte[]> msg = null;
    Set<String> messageSet = Sets.newHashSet();
    int count = 0;
    for (int i = 0; i < 10; i++) {
        msg = consumer.receive(5, TimeUnit.SECONDS);
        String receivedMessage = new String(msg.getData());
        log.debug("Received message: [{}]", receivedMessage);
        String expectedMessage = "my-message-" + i;
        testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
        count++;
    }
    // Acknowledge the consumption of all messages at once
    Assert.assertEquals(msgs, count);
    consumer.acknowledgeCumulative(msg);
    consumer.close();
    log.info("-- Exiting {} test --", methodName);
}
Also used : AuthenticationTls(org.apache.pulsar.client.impl.auth.AuthenticationTls) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) Authentication(org.apache.pulsar.client.api.Authentication) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Example 27 with PulsarClient

use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.

the class PersistentTopicTest method testClosingReplicationProducerTwice.

@SuppressWarnings("unchecked")
@Test
public void testClosingReplicationProducerTwice() throws Exception {
    final String globalTopicName = "persistent://prop/global/ns/testClosingReplicationProducerTwice";
    String localCluster = "local";
    String remoteCluster = "remote";
    final ManagedLedger ledgerMock = mock(ManagedLedger.class);
    doNothing().when(ledgerMock).asyncDeleteCursor(anyObject(), anyObject(), anyObject());
    doReturn(new ArrayList<Object>()).when(ledgerMock).getCursors();
    PersistentTopic topic = new PersistentTopic(globalTopicName, ledgerMock, brokerService);
    final URL brokerUrl = new URL("http://" + pulsar.getAdvertisedAddress() + ":" + pulsar.getConfiguration().getBrokerServicePort());
    PulsarClient client = spy(PulsarClient.builder().serviceUrl(brokerUrl.toString()).build());
    PulsarClientImpl clientImpl = (PulsarClientImpl) client;
    doReturn(new CompletableFuture<Producer>()).when(clientImpl).createProducerAsync(any(ProducerConfigurationData.class), any(Schema.class));
    ManagedCursor cursor = mock(ManagedCursorImpl.class);
    doReturn(remoteCluster).when(cursor).getName();
    brokerService.getReplicationClients().put(remoteCluster, client);
    PersistentReplicator replicator = new PersistentReplicator(topic, cursor, localCluster, remoteCluster, brokerService);
    // PersistentReplicator constructor calls startProducer()
    verify(clientImpl).createProducerAsync(any(ProducerConfigurationData.class), any(Schema.class));
    replicator.disconnect(false);
    replicator.disconnect(false);
    replicator.startProducer();
    verify(clientImpl, Mockito.times(2)).createProducerAsync(any(ProducerConfigurationData.class), any(Schema.class));
}
Also used : PersistentReplicator(org.apache.pulsar.broker.service.persistent.PersistentReplicator) NonPersistentReplicator(org.apache.pulsar.broker.service.nonpersistent.NonPersistentReplicator) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) Schema(org.apache.pulsar.client.api.Schema) Matchers.anyString(org.mockito.Matchers.anyString) URL(java.net.URL) ManagedCursor(org.apache.bookkeeper.mledger.ManagedCursor) ProducerConfigurationData(org.apache.pulsar.client.impl.conf.ProducerConfigurationData) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) Matchers.anyObject(org.mockito.Matchers.anyObject) PulsarClient(org.apache.pulsar.client.api.PulsarClient) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) Test(org.testng.annotations.Test)

Example 28 with PulsarClient

use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.

the class ReplicatorTest method testConcurrentReplicator.

@SuppressWarnings("unchecked")
@Test(timeOut = 30000)
public void testConcurrentReplicator() throws Exception {
    log.info("--- Starting ReplicatorTest::testConcurrentReplicator ---");
    final String namespace = "pulsar/global/concurrent";
    admin1.namespaces().createNamespace(namespace);
    admin1.namespaces().setNamespaceReplicationClusters(namespace, Lists.newArrayList("r1", "r2"));
    final TopicName topicName = TopicName.get(String.format("persistent://" + namespace + "/topic-%d", 0));
    PulsarClient client1 = PulsarClient.builder().serviceUrl(url1.toString()).statsInterval(0, TimeUnit.SECONDS).build();
    Producer<byte[]> producer = client1.newProducer().topic(topicName.toString()).create();
    producer.close();
    PersistentTopic topic = (PersistentTopic) pulsar1.getBrokerService().getTopic(topicName.toString()).get();
    PulsarClientImpl pulsarClient = spy((PulsarClientImpl) pulsar1.getBrokerService().getReplicationClient("r3"));
    final Method startRepl = PersistentTopic.class.getDeclaredMethod("startReplicator", String.class);
    startRepl.setAccessible(true);
    Field replClientField = BrokerService.class.getDeclaredField("replicationClients");
    replClientField.setAccessible(true);
    ConcurrentOpenHashMap<String, PulsarClient> replicationClients = (ConcurrentOpenHashMap<String, PulsarClient>) replClientField.get(pulsar1.getBrokerService());
    replicationClients.put("r3", pulsarClient);
    admin1.namespaces().setNamespaceReplicationClusters(namespace, Lists.newArrayList("r1", "r2", "r3"));
    ExecutorService executor = Executors.newFixedThreadPool(5);
    for (int i = 0; i < 5; i++) {
        executor.submit(() -> {
            try {
                startRepl.invoke(topic, "r3");
            } catch (Exception e) {
                fail("setting replicator failed", e);
            }
        });
    }
    Thread.sleep(3000);
    Mockito.verify(pulsarClient, Mockito.times(1)).createProducerAsync(Mockito.any(ProducerConfigurationData.class), Mockito.any(Schema.class));
    client1.shutdown();
}
Also used : ConcurrentOpenHashMap(org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap) Schema(org.apache.pulsar.client.api.Schema) Method(java.lang.reflect.Method) BeforeMethod(org.testng.annotations.BeforeMethod) NamingException(org.apache.pulsar.broker.service.BrokerServiceException.NamingException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) CursorAlreadyClosedException(org.apache.bookkeeper.mledger.ManagedLedgerException.CursorAlreadyClosedException) TopicName(org.apache.pulsar.common.naming.TopicName) ProducerConfigurationData(org.apache.pulsar.client.impl.conf.ProducerConfigurationData) Field(java.lang.reflect.Field) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) ExecutorService(java.util.concurrent.ExecutorService) PulsarClient(org.apache.pulsar.client.api.PulsarClient) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) Test(org.testng.annotations.Test)

Example 29 with PulsarClient

use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.

the class PersistentTopicE2ETest method testMultipleClientsMultipleSubscriptions.

@Test
public void testMultipleClientsMultipleSubscriptions() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/topic7";
    final String subName = "sub7";
    PulsarClient client1 = PulsarClient.builder().serviceUrl(brokerUrl.toString()).build();
    PulsarClient client2 = PulsarClient.builder().serviceUrl(brokerUrl.toString()).build();
    try {
        client1.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
        client1.newProducer().topic(topicName).create();
        client2.newProducer().topic(topicName).create();
        client2.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
        fail("Should have thrown an exception since one consumer is already connected");
    } catch (PulsarClientException cce) {
        Assert.assertTrue(cce.getMessage().contains("Exclusive consumer is already connected"));
    } finally {
        client2.shutdown();
        client1.shutdown();
    }
}
Also used : PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Example 30 with PulsarClient

use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.

the class PersistentTopicE2ETest method testProducerQueueFullBlocking.

@Test
public void testProducerQueueFullBlocking() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/topic-xyzx";
    final int messages = 10;
    PulsarClient client = PulsarClient.builder().serviceUrl(brokerUrl.toString()).build();
    // 1. Producer connect
    ProducerImpl<byte[]> producer = (ProducerImpl<byte[]>) client.newProducer().topic(topicName).maxPendingMessages(messages).blockIfQueueFull(true).sendTimeout(1, TimeUnit.SECONDS).create();
    // 2. Stop broker
    cleanup();
    // 2. producer publish messages
    long startTime = System.nanoTime();
    for (int i = 0; i < messages; i++) {
        // Should never block
        producer.sendAsync("msg".getBytes());
    }
    // Verify thread was not blocked
    long delayNs = System.nanoTime() - startTime;
    assertTrue(delayNs < TimeUnit.SECONDS.toNanos(1));
    assertEquals(producer.getPendingQueueSize(), messages);
    // Next send operation must block, until all the messages in the queue expire
    startTime = System.nanoTime();
    producer.sendAsync("msg".getBytes());
    delayNs = System.nanoTime() - startTime;
    assertTrue(delayNs > TimeUnit.MILLISECONDS.toNanos(500));
    assertTrue(delayNs < TimeUnit.MILLISECONDS.toNanos(1500));
    assertEquals(producer.getPendingQueueSize(), 1);
    // 4. producer disconnect
    producer.close();
    client.close();
    // 5. Restart broker
    setup();
}
Also used : ProducerImpl(org.apache.pulsar.client.impl.ProducerImpl) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Aggregations

PulsarClient (org.apache.pulsar.client.api.PulsarClient)86 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