Search in sources :

Example 1 with ConnectionPool

use of org.apache.pulsar.client.impl.ConnectionPool in project pulsar by apache.

the class ProxyTest method getClientActiveConsumerChangeNotSupported.

private static PulsarClient getClientActiveConsumerChangeNotSupported(ClientConfigurationData conf) throws Exception {
    ThreadFactory threadFactory = new DefaultThreadFactory("pulsar-client-io", Thread.currentThread().isDaemon());
    EventLoopGroup eventLoopGroup = EventLoopUtil.newEventLoopGroup(conf.getNumIoThreads(), false, threadFactory);
    ConnectionPool cnxPool = new ConnectionPool(conf, eventLoopGroup, () -> {
        return new ClientCnx(conf, eventLoopGroup, ProtocolVersion.v11_VALUE) {

            @Override
            protected void handleActiveConsumerChange(CommandActiveConsumerChange change) {
                throw new UnsupportedOperationException();
            }
        };
    });
    return new PulsarClientImpl(conf, eventLoopGroup, cnxPool);
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ConnectionPool(org.apache.pulsar.client.impl.ConnectionPool) CommandActiveConsumerChange(org.apache.pulsar.common.api.proto.CommandActiveConsumerChange) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) ClientCnx(org.apache.pulsar.client.impl.ClientCnx) EventLoopGroup(io.netty.channel.EventLoopGroup) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl)

Example 2 with ConnectionPool

use of org.apache.pulsar.client.impl.ConnectionPool in project pulsar by apache.

the class ProxyParserTest method getClientActiveConsumerChangeNotSupported.

private static PulsarClient getClientActiveConsumerChangeNotSupported(ClientConfigurationData conf) throws Exception {
    ThreadFactory threadFactory = new DefaultThreadFactory("pulsar-client-io", Thread.currentThread().isDaemon());
    EventLoopGroup eventLoopGroup = EventLoopUtil.newEventLoopGroup(conf.getNumIoThreads(), false, threadFactory);
    ConnectionPool cnxPool = new ConnectionPool(conf, eventLoopGroup, () -> {
        return new ClientCnx(conf, eventLoopGroup, ProtocolVersion.v11_VALUE) {

            @Override
            protected void handleActiveConsumerChange(CommandActiveConsumerChange change) {
                throw new UnsupportedOperationException();
            }
        };
    });
    return new PulsarClientImpl(conf, eventLoopGroup, cnxPool);
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ConnectionPool(org.apache.pulsar.client.impl.ConnectionPool) CommandActiveConsumerChange(org.apache.pulsar.common.api.proto.CommandActiveConsumerChange) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) ClientCnx(org.apache.pulsar.client.impl.ClientCnx) EventLoopGroup(io.netty.channel.EventLoopGroup) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl)

Example 3 with ConnectionPool

use of org.apache.pulsar.client.impl.ConnectionPool in project pulsar by yahoo.

the class ProxyParserTest method getClientActiveConsumerChangeNotSupported.

private static PulsarClient getClientActiveConsumerChangeNotSupported(ClientConfigurationData conf) throws Exception {
    ThreadFactory threadFactory = new DefaultThreadFactory("pulsar-client-io", Thread.currentThread().isDaemon());
    EventLoopGroup eventLoopGroup = EventLoopUtil.newEventLoopGroup(conf.getNumIoThreads(), false, threadFactory);
    ConnectionPool cnxPool = new ConnectionPool(conf, eventLoopGroup, () -> {
        return new ClientCnx(conf, eventLoopGroup, ProtocolVersion.v11_VALUE) {

            @Override
            protected void handleActiveConsumerChange(CommandActiveConsumerChange change) {
                throw new UnsupportedOperationException();
            }
        };
    });
    return new PulsarClientImpl(conf, eventLoopGroup, cnxPool);
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ConnectionPool(org.apache.pulsar.client.impl.ConnectionPool) CommandActiveConsumerChange(org.apache.pulsar.common.api.proto.CommandActiveConsumerChange) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) ClientCnx(org.apache.pulsar.client.impl.ClientCnx) EventLoopGroup(io.netty.channel.EventLoopGroup) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl)

Example 4 with ConnectionPool

use of org.apache.pulsar.client.impl.ConnectionPool in project pulsar by yahoo.

the class BrokerServiceThrottlingTest method testLookupThrottlingForClientByBroker.

/**
 * Verifies: Broker side throttling:
 *
 * <pre>
 * 1. concurrent_consumer_creation > maxConcurrentLookupRequest at broker
 * 2. few of the consumer creation must fail with TooManyLookupRequestException.
 * </pre>
 *
 * @throws Exception
 */
@Test
public void testLookupThrottlingForClientByBroker() throws Exception {
    final String topicName = "persistent://prop/ns-abc/newTopic";
    @Cleanup PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(pulsar.getBrokerServiceUrl()).statsInterval(0, TimeUnit.SECONDS).ioThreads(20).connectionsPerBroker(20).build();
    int newPermits = 1;
    admin.brokers().updateDynamicConfiguration("maxConcurrentLookupRequest", Integer.toString(newPermits));
    // wait config to be updated
    for (int i = 0; i < 5; i++) {
        if (pulsar.getConfiguration().getMaxConcurrentLookupRequest() != newPermits) {
            Thread.sleep(100 + (i * 10));
        } else {
            break;
        }
    }
    PulsarServiceNameResolver resolver = new PulsarServiceNameResolver();
    resolver.updateServiceUrl(pulsar.getBrokerServiceUrl());
    ClientConfigurationData conf = new ClientConfigurationData();
    conf.setConnectionsPerBroker(20);
    EventLoopGroup eventLoop = EventLoopUtil.newEventLoopGroup(20, false, new DefaultThreadFactory("test-pool", Thread.currentThread().isDaemon()));
    ExecutorService executor = Executors.newFixedThreadPool(10);
    try (ConnectionPool pool = new ConnectionPool(conf, eventLoop)) {
        final int totalConsumers = 20;
        List<Future<?>> futures = new ArrayList<>();
        // test for partitionMetadataRequest
        for (int i = 0; i < totalConsumers; i++) {
            long reqId = 0xdeadbeef + i;
            Future<?> f = executor.submit(() -> {
                ByteBuf request = Commands.newPartitionMetadataRequest(topicName, reqId);
                pool.getConnection(resolver.resolveHost()).thenCompose(clientCnx -> clientCnx.newLookup(request, reqId)).get();
                return null;
            });
            futures.add(f);
        }
        int rejects = 0;
        for (Future<?> f : futures) {
            try {
                f.get();
            } catch (ExecutionException e) {
                Throwable rootCause = e;
                while (rootCause instanceof ExecutionException) {
                    rootCause = rootCause.getCause();
                }
                if (rootCause instanceof org.apache.pulsar.client.api.PulsarClientException.TooManyRequestsException) {
                    rejects++;
                } else {
                    throw e;
                }
            }
        }
        assertTrue(rejects > 0);
        futures.clear();
        // test for lookup
        for (int i = 0; i < totalConsumers; i++) {
            long reqId = 0xdeadfeef + i;
            Future<?> f = executor.submit(() -> {
                ByteBuf request = Commands.newLookup(topicName, true, reqId);
                pool.getConnection(resolver.resolveHost()).thenCompose(clientCnx -> clientCnx.newLookup(request, reqId)).get();
                return null;
            });
            futures.add(f);
        }
        rejects = 0;
        for (Future<?> f : futures) {
            try {
                f.get();
            } catch (ExecutionException e) {
                Throwable rootCause = e;
                while (rootCause instanceof ExecutionException) {
                    rootCause = rootCause.getCause();
                }
                if (rootCause instanceof org.apache.pulsar.client.api.PulsarClientException.TooManyRequestsException) {
                    rejects++;
                } else {
                    throw e;
                }
            }
        }
        assertTrue(rejects > 0);
    } finally {
        executor.shutdownNow();
        eventLoop.shutdownNow();
    }
}
Also used : ClientConfigurationData(org.apache.pulsar.client.impl.conf.ClientConfigurationData) ConnectionPool(org.apache.pulsar.client.impl.ConnectionPool) PulsarServiceNameResolver(org.apache.pulsar.client.impl.PulsarServiceNameResolver) ConnectionPool(org.apache.pulsar.client.impl.ConnectionPool) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) Assert.assertEquals(org.testng.Assert.assertEquals) Cleanup(lombok.Cleanup) Test(org.testng.annotations.Test) AfterMethod(org.testng.annotations.AfterMethod) ArrayList(java.util.ArrayList) Commands(org.apache.pulsar.common.protocol.Commands) Future(java.util.concurrent.Future) Lists(com.google.common.collect.Lists) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) PulsarClient(org.apache.pulsar.client.api.PulsarClient) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ExecutorService(java.util.concurrent.ExecutorService) Assert.assertNotEquals(org.testng.Assert.assertNotEquals) EventLoopGroup(io.netty.channel.EventLoopGroup) EventLoopUtil(org.apache.pulsar.common.util.netty.EventLoopUtil) Assert.fail(org.testng.Assert.fail) BeforeMethod(org.testng.annotations.BeforeMethod) UUID(java.util.UUID) SubscriptionType(org.apache.pulsar.client.api.SubscriptionType) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Consumer(org.apache.pulsar.client.api.Consumer) List(java.util.List) TreeMap(java.util.TreeMap) ClientConfigurationData(org.apache.pulsar.client.impl.conf.ClientConfigurationData) Assert.assertTrue(org.testng.Assert.assertTrue) Collections(java.util.Collections) ArrayList(java.util.ArrayList) PulsarServiceNameResolver(org.apache.pulsar.client.impl.PulsarServiceNameResolver) ByteBuf(io.netty.buffer.ByteBuf) Cleanup(lombok.Cleanup) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) EventLoopGroup(io.netty.channel.EventLoopGroup) ExecutorService(java.util.concurrent.ExecutorService) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) Future(java.util.concurrent.Future) PulsarClient(org.apache.pulsar.client.api.PulsarClient) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Example 5 with ConnectionPool

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

the class ProxyParserTest method getClientActiveConsumerChangeNotSupported.

private static PulsarClient getClientActiveConsumerChangeNotSupported(ClientConfigurationData conf) throws Exception {
    ThreadFactory threadFactory = new DefaultThreadFactory("pulsar-client-io", Thread.currentThread().isDaemon());
    EventLoopGroup eventLoopGroup = EventLoopUtil.newEventLoopGroup(conf.getNumIoThreads(), false, threadFactory);
    ConnectionPool cnxPool = new ConnectionPool(conf, eventLoopGroup, () -> {
        return new ClientCnx(conf, eventLoopGroup, ProtocolVersion.v11_VALUE) {

            @Override
            protected void handleActiveConsumerChange(CommandActiveConsumerChange change) {
                throw new UnsupportedOperationException();
            }
        };
    });
    return new PulsarClientImpl(conf, eventLoopGroup, cnxPool);
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ConnectionPool(org.apache.pulsar.client.impl.ConnectionPool) CommandActiveConsumerChange(org.apache.pulsar.common.api.proto.CommandActiveConsumerChange) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ThreadFactory(java.util.concurrent.ThreadFactory) ClientCnx(org.apache.pulsar.client.impl.ClientCnx) EventLoopGroup(io.netty.channel.EventLoopGroup) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl)

Aggregations

ConnectionPool (org.apache.pulsar.client.impl.ConnectionPool)14 EventLoopGroup (io.netty.channel.EventLoopGroup)12 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)12 ClientCnx (org.apache.pulsar.client.impl.ClientCnx)8 Lists (com.google.common.collect.Lists)6 ByteBuf (io.netty.buffer.ByteBuf)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Map (java.util.Map)6 UUID (java.util.UUID)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 ExecutionException (java.util.concurrent.ExecutionException)6 ExecutorService (java.util.concurrent.ExecutorService)6 Executors (java.util.concurrent.Executors)6 TimeUnit (java.util.concurrent.TimeUnit)6 Cleanup (lombok.Cleanup)6 Consumer (org.apache.pulsar.client.api.Consumer)6 PulsarClient (org.apache.pulsar.client.api.PulsarClient)6 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)6 SubscriptionType (org.apache.pulsar.client.api.SubscriptionType)6