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();
}
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);
});
}
}
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();
}
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);
}
}
Aggregations