Search in sources :

Example 1 with ClientConfigurationData

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

the class ConnectionPoolTest method testDoubleIpAddress.

@Test
public void testDoubleIpAddress() throws Exception {
    String serviceUrl = "pulsar://non-existing-dns-name:" + BROKER_PORT;
    ClientConfigurationData conf = new ClientConfigurationData();
    EventLoopGroup eventLoop = EventLoopUtil.newEventLoopGroup(1, new DefaultThreadFactory("test"));
    ConnectionPool pool = Mockito.spy(new ConnectionPool(conf, eventLoop));
    conf.setServiceUrl(serviceUrl);
    PulsarClientImpl client = new PulsarClientImpl(conf, eventLoop, pool);
    List<InetAddress> result = Lists.newArrayList();
    // Add a non existent IP to the response to check that we're trying the 2nd address as well
    result.add(InetAddress.getByName("127.0.0.99"));
    result.add(InetAddress.getByName("127.0.0.1"));
    Mockito.when(pool.resolveName("non-existing-dns-name")).thenReturn(CompletableFuture.completedFuture(result));
    // Create producer should succeed by trying the 2nd IP
    client.createProducer("persistent://sample/standalone/ns/my-topic");
    client.close();
}
Also used : ClientConfigurationData(org.apache.pulsar.client.impl.conf.ClientConfigurationData) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) EventLoopGroup(io.netty.channel.EventLoopGroup) InetAddress(java.net.InetAddress) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 2 with ClientConfigurationData

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

the class MessageIdTest method testCorruptMessageRemove.

/**
 * Verifies: if message is corrupted before sending to broker and if broker gives checksum error: then 1.
 * Client-Producer recomputes checksum with modified data 2. Retry message-send again 3. Broker verifies checksum 4.
 * client receives send-ack success
 *
 * @throws Exception
 */
@Test
public void testCorruptMessageRemove() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/retry-topic";
    // 1. producer connect
    ProducerImpl<byte[]> prod = (ProducerImpl<byte[]>) pulsarClient.newProducer().topic(topicName).sendTimeout(10, TimeUnit.MINUTES).create();
    ProducerImpl<byte[]> producer = spy(prod);
    Field producerIdField = ProducerImpl.class.getDeclaredField("producerId");
    producerIdField.setAccessible(true);
    long producerId = (long) producerIdField.get(producer);
    // registered spy ProducerImpl
    producer.cnx().registerProducer(producerId, producer);
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName("my-sub").subscribe();
    // 2. Stop the broker, and publishes messages. Messages are accumulated in the producer queue and they're
    // checksums
    // would have already been computed. If we change the message content at that point, it should result in a
    // checksum validation error
    // enable checksum at producer
    stopBroker();
    Message<byte[]> msg = MessageBuilder.create().setContent("message-1".getBytes()).build();
    CompletableFuture<MessageId> future = producer.sendAsync(msg);
    // 3. corrupt the message
    // new content would be 'message-3'
    msg.getData()[msg.getData().length - 1] = '2';
    // 4. Restart the broker to have the messages published
    startBroker();
    try {
        future.get();
        fail("send message should have failed with checksum excetion");
    } catch (Exception e) {
        if (e.getCause() instanceof PulsarClientException.ChecksumException) {
        // ok (callback should get checksum exception as message was modified and corrupt)
        } else {
            fail("Callback should have only failed with ChecksumException", e);
        }
    }
    // 5. Verify
    /**
     * verify: ProducerImpl.verifyLocalBufferIsNotCorrupted() => validates if message is corrupt
     */
    MessageImpl<byte[]> msg2 = (MessageImpl<byte[]>) MessageBuilder.create().setContent("message-1".getBytes()).build();
    ByteBuf payload = msg2.getDataBuffer();
    Builder metadataBuilder = ((MessageImpl<byte[]>) msg).getMessageBuilder();
    MessageMetadata msgMetadata = metadataBuilder.setProducerName("test").setSequenceId(1).setPublishTime(10L).build();
    ByteBufPair cmd = Commands.newSend(producerId, 1, 1, ChecksumType.Crc32c, msgMetadata, payload);
    // (a) create OpSendMsg with message-data : "message-1"
    OpSendMsg op = OpSendMsg.create(((MessageImpl<byte[]>) msg), cmd, 1, null);
    // a.verify: as message is not corrupt: no need to update checksum
    assertTrue(producer.verifyLocalBufferIsNotCorrupted(op));
    // (b) corrupt message
    // new content would be 'message-2'
    msg2.getData()[msg2.getData().length - 1] = '2';
    // b. verify: as message is corrupt: update checksum
    assertFalse(producer.verifyLocalBufferIsNotCorrupted(op));
    assertEquals(producer.getPendingQueueSize(), 0);
    // [2] test-recoverChecksumError functionality
    stopBroker();
    MessageImpl<byte[]> msg1 = (MessageImpl<byte[]>) MessageBuilder.create().setContent("message-1".getBytes()).build();
    future = producer.sendAsync(msg1);
    ClientCnx cnx = spy(new ClientCnx(new ClientConfigurationData(), ((PulsarClientImpl) pulsarClient).eventLoopGroup()));
    String exc = "broker is already stopped";
    // when client-try to recover checksum by resending to broker: throw exception as broker is stopped
    doThrow(new IllegalStateException(exc)).when(cnx).ctx();
    try {
        producer.recoverChecksumError(cnx, 1);
        fail("it should call : resendMessages() => which should throw above mocked exception");
    } catch (IllegalStateException e) {
        assertEquals(exc, e.getMessage());
    }
    producer.close();
    consumer.close();
    // clean reference of mocked producer
    producer = null;
}
Also used : ClientConfigurationData(org.apache.pulsar.client.impl.conf.ClientConfigurationData) OpSendMsg(org.apache.pulsar.client.impl.ProducerImpl.OpSendMsg) Builder(org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata.Builder) MessageBuilder(org.apache.pulsar.client.api.MessageBuilder) ByteBuf(io.netty.buffer.ByteBuf) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) ByteBufPair(org.apache.pulsar.common.api.ByteBufPair) Field(java.lang.reflect.Field) MessageMetadata(org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

Example 3 with ClientConfigurationData

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

the class CmdFunctionsTest method setup.

@BeforeMethod
public void setup() throws Exception {
    this.admin = mock(PulsarAdminWithFunctions.class);
    this.functions = mock(Functions.class);
    when(admin.functions()).thenReturn(functions);
    when(admin.getServiceUrl()).thenReturn(URI.create("http://localhost:1234").toURL());
    when(admin.getClientConf()).thenReturn(new ClientConfigurationData());
    this.cmd = new CmdFunctions(admin);
    // mock reflections
    mockStatic(Reflections.class);
    when(Reflections.classExistsInJar(any(File.class), anyString())).thenReturn(true);
    when(Reflections.classExists(anyString())).thenReturn(true);
    when(Reflections.classInJarImplementsIface(any(File.class), anyString(), eq(Function.class))).thenReturn(true);
    when(Reflections.classImplementsIface(anyString(), any())).thenReturn(true);
    when(Reflections.createInstance(eq(DummyFunction.class.getName()), any(File.class))).thenReturn(new DummyFunction());
    when(Reflections.createInstance(eq(DefaultSerDe.class.getName()), any(File.class))).thenReturn(new DefaultSerDe(String.class));
}
Also used : ClientConfigurationData(org.apache.pulsar.client.impl.conf.ClientConfigurationData) PulsarAdminWithFunctions(org.apache.pulsar.client.admin.PulsarAdminWithFunctions) DeleteFunction(org.apache.pulsar.admin.cli.CmdFunctions.DeleteFunction) GetFunction(org.apache.pulsar.admin.cli.CmdFunctions.GetFunction) UpdateFunction(org.apache.pulsar.admin.cli.CmdFunctions.UpdateFunction) CreateFunction(org.apache.pulsar.admin.cli.CmdFunctions.CreateFunction) Function(org.apache.pulsar.functions.api.Function) DefaultSerDe(org.apache.pulsar.functions.api.utils.DefaultSerDe) PulsarAdminWithFunctions(org.apache.pulsar.client.admin.PulsarAdminWithFunctions) Functions(org.apache.pulsar.client.admin.Functions) ListFunctions(org.apache.pulsar.admin.cli.CmdFunctions.ListFunctions) Matchers.anyString(org.mockito.Matchers.anyString) File(java.io.File) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 4 with ClientConfigurationData

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

the class PulsarAdminTool method main.

public static void main(String[] args) throws Exception {
    String configFile = args[0];
    Properties properties = new Properties();
    if (configFile != null) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(configFile);
            properties.load(fis);
        } finally {
            if (fis != null)
                fis.close();
        }
    }
    PulsarAdminTool tool = new PulsarAdminTool(properties);
    int cmdPos;
    for (cmdPos = 1; cmdPos < args.length; cmdPos++) {
        if (tool.commandMap.containsKey(args[cmdPos])) {
            break;
        }
    }
    ++cmdPos;
    boolean isLocalRun = false;
    if (cmdPos < args.length) {
        isLocalRun = "localrun" == args[cmdPos].toLowerCase();
    }
    BiFunction<URL, ClientConfigurationData, ? extends PulsarAdmin> adminFactory;
    if (isLocalRun) {
        // bypass constructing admin client
        adminFactory = (url, config) -> null;
    } else {
        adminFactory = (url, config) -> {
            try {
                return new PulsarAdminWithFunctions(url, config);
            } catch (Exception ex) {
                System.err.println(ex.getClass() + ": " + ex.getMessage());
                System.exit(1);
                return null;
            }
        };
    }
    if (tool.run(Arrays.copyOfRange(args, 1, args.length), adminFactory)) {
        System.exit(0);
    } else {
        System.exit(1);
    }
}
Also used : ClientConfigurationData(org.apache.pulsar.client.impl.conf.ClientConfigurationData) PulsarAdminWithFunctions(org.apache.pulsar.client.admin.PulsarAdminWithFunctions) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException)

Example 5 with ClientConfigurationData

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

Aggregations

ClientConfigurationData (org.apache.pulsar.client.impl.conf.ClientConfigurationData)9 Test (org.testng.annotations.Test)4 EventLoopGroup (io.netty.channel.EventLoopGroup)3 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)3 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)3 Field (java.lang.reflect.Field)2 InetAddress (java.net.InetAddress)2 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)2 PulsarAdminWithFunctions (org.apache.pulsar.client.admin.PulsarAdminWithFunctions)2 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)1