Search in sources :

Example 6 with PulsarClientImpl

use of org.apache.pulsar.client.impl.PulsarClientImpl 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 7 with PulsarClientImpl

use of org.apache.pulsar.client.impl.PulsarClientImpl in project incubator-pulsar by apache.

the class ReplicatorTlsTest method testReplicationClient.

@Test
public void testReplicationClient() throws Exception {
    log.info("--- Starting ReplicatorTlsTest::testReplicationClient ---");
    for (BrokerService ns : Lists.newArrayList(ns1, ns2, ns3)) {
        ns.getReplicationClients().forEach((cluster, client) -> {
            assertTrue(((PulsarClientImpl) client).getConfiguration().isUseTls());
            assertEquals(((PulsarClientImpl) client).getConfiguration().getTlsTrustCertsFilePath(), TLS_SERVER_CERT_FILE_PATH);
        });
    }
}
Also used : PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) BrokerService(org.apache.pulsar.broker.service.BrokerService) Test(org.testng.annotations.Test)

Example 8 with PulsarClientImpl

use of org.apache.pulsar.client.impl.PulsarClientImpl in project incubator-pulsar by apache.

the class LookupProtocolTest method binaryLookupTest.

@Test(timeOut = 10000)
public void binaryLookupTest() throws Exception {
    WebSocketProxyConfiguration conf = new WebSocketProxyConfiguration();
    conf.setServiceUrl("http://localhost:8080");
    conf.setServiceUrlTls("https://localhost:8443");
    conf.setBrokerServiceUrl("pulsar://localhost:6650");
    conf.setBrokerServiceUrlTls("pulsar+ssl://localhost:6651");
    WebSocketService service = new WebSocketService(conf);
    PulsarClientImpl testClient = (PulsarClientImpl) service.getPulsarClient();
    Field lookupField = PulsarClientImpl.class.getDeclaredField("lookup");
    lookupField.setAccessible(true);
    Assert.assertEquals(lookupField.get(testClient).getClass().getName(), "org.apache.pulsar.client.impl.BinaryProtoLookupService");
    Assert.assertFalse(testClient.getConfiguration().isUseTls());
    service.close();
}
Also used : Field(java.lang.reflect.Field) WebSocketProxyConfiguration(org.apache.pulsar.websocket.service.WebSocketProxyConfiguration) WebSocketService(org.apache.pulsar.websocket.WebSocketService) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) Test(org.testng.annotations.Test)

Example 9 with PulsarClientImpl

use of org.apache.pulsar.client.impl.PulsarClientImpl in project incubator-pulsar by apache.

the class PulsarKafkaConsumer method subscribe.

@Override
public void subscribe(Collection<String> topics, ConsumerRebalanceListener callback) {
    List<CompletableFuture<org.apache.pulsar.client.api.Consumer<byte[]>>> futures = new ArrayList<>();
    List<TopicPartition> topicPartitions = new ArrayList<>();
    try {
        for (String topic : topics) {
            // Create individual subscription on each partition, that way we can keep using the
            // acknowledgeCumulative()
            int numberOfPartitions = ((PulsarClientImpl) client).getNumberOfPartitions(topic).get();
            ConsumerBuilder<byte[]> consumerBuilder = PulsarConsumerKafkaConfig.getConsumerBuilder(client, properties);
            consumerBuilder.subscriptionType(SubscriptionType.Failover);
            consumerBuilder.messageListener(this);
            consumerBuilder.subscriptionName(groupId);
            if (numberOfPartitions > 1) {
                // Subscribe to each partition
                consumerBuilder.consumerName(ConsumerName.generateRandomName());
                for (int i = 0; i < numberOfPartitions; i++) {
                    String partitionName = TopicName.get(topic).getPartition(i).toString();
                    CompletableFuture<org.apache.pulsar.client.api.Consumer<byte[]>> future = consumerBuilder.clone().topic(partitionName).subscribeAsync();
                    int partitionIndex = i;
                    TopicPartition tp = new TopicPartition(topic, partitionIndex);
                    future.thenAccept(consumer -> consumers.putIfAbsent(tp, consumer));
                    futures.add(future);
                    topicPartitions.add(tp);
                }
            } else {
                // Topic has a single partition
                CompletableFuture<org.apache.pulsar.client.api.Consumer<byte[]>> future = consumerBuilder.topic(topic).subscribeAsync();
                TopicPartition tp = new TopicPartition(topic, 0);
                future.thenAccept(consumer -> consumers.putIfAbsent(tp, consumer));
                futures.add(future);
                topicPartitions.add(tp);
            }
        }
        // Wait for all consumers to be ready
        futures.forEach(CompletableFuture::join);
        // Notify the listener is now owning all topics/partitions
        if (callback != null) {
            callback.onPartitionsAssigned(topicPartitions);
        }
    } catch (Exception e) {
        // Close all consumer that might have been successfully created
        futures.forEach(f -> {
            try {
                f.get().close();
            } catch (Exception e1) {
            // Ignore. Consumer already had failed
            }
        });
        throw new RuntimeException(e);
    }
}
Also used : TopicName(org.apache.pulsar.common.naming.TopicName) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ConsumerBuilder(org.apache.pulsar.client.api.ConsumerBuilder) PulsarConsumerKafkaConfig(org.apache.pulsar.client.kafka.compat.PulsarConsumerKafkaConfig) Message(org.apache.pulsar.client.api.Message) PulsarClientKafkaConfig(org.apache.pulsar.client.kafka.compat.PulsarClientKafkaConfig) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConsumerName(org.apache.pulsar.client.util.ConsumerName) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) Map(java.util.Map) Metric(org.apache.kafka.common.Metric) MetricName(org.apache.kafka.common.MetricName) Deserializer(org.apache.kafka.common.serialization.Deserializer) PulsarClient(org.apache.pulsar.client.api.PulsarClient) ProducerConfig(org.apache.kafka.clients.producer.ProducerConfig) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) TimestampType(org.apache.kafka.common.record.TimestampType) TopicPartition(org.apache.kafka.common.TopicPartition) Properties(java.util.Properties) MessageIdUtils(org.apache.pulsar.client.kafka.compat.MessageIdUtils) Collection(java.util.Collection) MessageListener(org.apache.pulsar.client.api.MessageListener) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) PartitionInfo(org.apache.kafka.common.PartitionInfo) SubscriptionType(org.apache.pulsar.client.api.SubscriptionType) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Base64(java.util.Base64) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) MessageId(org.apache.pulsar.client.api.MessageId) ClientBuilder(org.apache.pulsar.client.api.ClientBuilder) Pattern(java.util.regex.Pattern) ArrayList(java.util.ArrayList) TimeoutException(java.util.concurrent.TimeoutException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ExecutionException(java.util.concurrent.ExecutionException) CompletableFuture(java.util.concurrent.CompletableFuture) TopicPartition(org.apache.kafka.common.TopicPartition)

Aggregations

PulsarClientImpl (org.apache.pulsar.client.impl.PulsarClientImpl)9 Test (org.testng.annotations.Test)6 Field (java.lang.reflect.Field)5 PulsarClient (org.apache.pulsar.client.api.PulsarClient)4 WebSocketService (org.apache.pulsar.websocket.WebSocketService)3 WebSocketProxyConfiguration (org.apache.pulsar.websocket.service.WebSocketProxyConfiguration)3 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)2 NamingException (org.apache.pulsar.broker.service.BrokerServiceException.NamingException)2 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)2 ClientBuilder (org.apache.pulsar.client.api.ClientBuilder)2 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)2 Schema (org.apache.pulsar.client.api.Schema)2 ProducerConfigurationData (org.apache.pulsar.client.impl.conf.ProducerConfigurationData)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Base64 (java.util.Base64)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1