Search in sources :

Example 1 with PulsarClientImpl

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

the class BrokerService method getReplicationClient.

public PulsarClient getReplicationClient(String cluster) {
    PulsarClient client = replicationClients.get(cluster);
    if (client != null) {
        return client;
    }
    return replicationClients.computeIfAbsent(cluster, key -> {
        try {
            String path = PulsarWebResource.path("clusters", cluster);
            ClusterData data = this.pulsar.getConfigurationCache().clustersCache().get(path).orElseThrow(() -> new KeeperException.NoNodeException(path));
            ClientBuilder clientBuilder = PulsarClient.builder().enableTcpNoDelay(false).connectionsPerBroker(pulsar.getConfiguration().getReplicationConnectionsPerBroker()).statsInterval(0, TimeUnit.SECONDS);
            if (pulsar.getConfiguration().isAuthenticationEnabled()) {
                clientBuilder.authentication(pulsar.getConfiguration().getBrokerClientAuthenticationPlugin(), pulsar.getConfiguration().getBrokerClientAuthenticationParameters());
            }
            if (pulsar.getConfiguration().isReplicationTlsEnabled()) {
                clientBuilder.serviceUrl(isNotBlank(data.getBrokerServiceUrlTls()) ? data.getBrokerServiceUrlTls() : data.getServiceUrlTls()).enableTls(true).tlsTrustCertsFilePath(pulsar.getConfiguration().getBrokerClientTrustCertsFilePath()).allowTlsInsecureConnection(pulsar.getConfiguration().isTlsAllowInsecureConnection());
            } else {
                clientBuilder.serviceUrl(isNotBlank(data.getBrokerServiceUrl()) ? data.getBrokerServiceUrl() : data.getServiceUrl());
            }
            // Share all the IO threads across broker and client connections
            ClientConfigurationData conf = ((ClientBuilderImpl) clientBuilder).getClientConfigurationData();
            return new PulsarClientImpl(conf, workerGroup);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    });
}
Also used : ClientConfigurationData(org.apache.pulsar.client.impl.conf.ClientConfigurationData) ClientBuilderImpl(org.apache.pulsar.client.impl.ClientBuilderImpl) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PulsarClient(org.apache.pulsar.client.api.PulsarClient) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) KeeperException(org.apache.zookeeper.KeeperException) ServiceUnitNotReadyException(org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) NamingException(org.apache.pulsar.broker.service.BrokerServiceException.NamingException) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) ServerMetadataException(org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException) IOException(java.io.IOException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) KeeperException(org.apache.zookeeper.KeeperException) PersistenceException(org.apache.pulsar.broker.service.BrokerServiceException.PersistenceException) ClientBuilder(org.apache.pulsar.client.api.ClientBuilder)

Example 2 with PulsarClientImpl

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

the class LookupProtocolTest method httpLookupTest.

@Test(timeOut = 10000)
public void httpLookupTest() throws Exception {
    WebSocketProxyConfiguration conf = new WebSocketProxyConfiguration();
    conf.setServiceUrl("http://localhost:8080");
    conf.setServiceUrlTls("https://localhost:8443");
    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.HttpLookupService");
    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 3 with PulsarClientImpl

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

the class LookupProtocolTest method httpsLookupTest.

@Test(timeOut = 10000)
public void httpsLookupTest() throws Exception {
    WebSocketProxyConfiguration conf = new WebSocketProxyConfiguration();
    conf.setServiceUrl("http://localhost:8080");
    conf.setServiceUrlTls("https://localhost:8443");
    conf.setBrokerServiceUrl("pulsar://localhost:6650");
    conf.setTlsEnabled(true);
    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.HttpLookupService");
    Assert.assertTrue(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 4 with PulsarClientImpl

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

the class LookupProtocolTest method binaryTlsLookupTest.

@Test(timeOut = 10000)
public void binaryTlsLookupTest() 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");
    conf.setTlsEnabled(true);
    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.assertTrue(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 5 with PulsarClientImpl

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

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