Search in sources :

Example 31 with ManagedLedgerImpl

use of org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl in project incubator-pulsar by apache.

the class MessageDispatchThrottlingTest method testBytesRateLimitingReceiveAllMessagesAfterThrottling.

/**
 * verify rate-limiting should throttle message-dispatching based on byte-rate
 *
 * <pre>
 *  1. dispatch-byte-rate = 100 bytes/sec
 *  2. send 20 msgs : each with 10 byte
 *  3. it should take up to 2 second to receive all messages
 * </pre>
 *
 * @param subscription
 * @throws Exception
 */
@Test(dataProvider = "subscriptions", timeOut = 5000)
public void testBytesRateLimitingReceiveAllMessagesAfterThrottling(SubscriptionType subscription) throws Exception {
    log.info("-- Starting {} test --", methodName);
    final String namespace = "my-property/use/throttling_ns";
    final String topicName = "persistent://" + namespace + "/throttlingAll";
    final int byteRate = 100;
    DispatchRate dispatchRate = new DispatchRate(-1, byteRate, 1);
    admin.namespaces().createNamespace(namespace);
    admin.namespaces().setDispatchRate(namespace, dispatchRate);
    // create producer and topic
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
    PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService().getTopic(topicName).get();
    boolean isMessageRateUpdate = false;
    int retry = 5;
    for (int i = 0; i < retry; i++) {
        if (topic.getDispatchRateLimiter().getDispatchRateOnByte() > 0) {
            isMessageRateUpdate = true;
            break;
        } else {
            if (i != retry - 1) {
                Thread.sleep(100);
            }
        }
    }
    Assert.assertTrue(isMessageRateUpdate);
    Assert.assertEquals(admin.namespaces().getDispatchRate(namespace), dispatchRate);
    final int numProducedMessages = 20;
    final CountDownLatch latch = new CountDownLatch(numProducedMessages);
    final AtomicInteger totalReceived = new AtomicInteger(0);
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName("my-subscriber-name").subscriptionType(subscription).messageListener((c1, msg) -> {
        Assert.assertNotNull(msg, "Message cannot be null");
        String receivedMessage = new String(msg.getData());
        log.debug("Received message [{}] in the listener", receivedMessage);
        totalReceived.incrementAndGet();
        latch.countDown();
    }).subscribe();
    // deactive cursors
    deactiveCursors((ManagedLedgerImpl) topic.getManagedLedger());
    // Asynchronously produce messages
    for (int i = 0; i < numProducedMessages; i++) {
        producer.send(new byte[byteRate / 10]);
    }
    latch.await();
    Assert.assertEquals(totalReceived.get(), numProducedMessages);
    consumer.close();
    producer.close();
    log.info("-- Exiting {} test --", methodName);
}
Also used : ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) DispatchRate(org.apache.pulsar.common.policies.data.DispatchRate) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) DataProvider(org.testng.annotations.DataProvider) LoggerFactory(org.slf4j.LoggerFactory) BeforeMethod(org.testng.annotations.BeforeMethod) Test(org.testng.annotations.Test) BrokerService(org.apache.pulsar.broker.service.BrokerService) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) Field(java.lang.reflect.Field) AfterMethod(org.testng.annotations.AfterMethod) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) Lists(com.google.common.collect.Lists) Assert(org.testng.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) LinkedList(java.util.LinkedList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) DispatchRate(org.apache.pulsar.common.policies.data.DispatchRate) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 32 with ManagedLedgerImpl

use of org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl in project incubator-pulsar by apache.

the class ManagedLedgerMetrics method groupLedgersByDimension.

/**
 * Build a map of dimensions key to list of topic stats (not thread-safe)
 * <p>
 *
 * @return
 */
private Map<Metrics, List<ManagedLedgerImpl>> groupLedgersByDimension() {
    ledgersByDimensionMap.clear();
    for (Entry<String, ManagedLedgerImpl> e : getManagedLedgers().entrySet()) {
        String ledgerName = e.getKey();
        ManagedLedgerImpl ledger = e.getValue();
        // we want to aggregate by NS dimension
        String namespace = parseNamespaceFromLedgerName(ledgerName);
        Metrics metrics = createMetricsByDimension(namespace);
        populateDimensionMap(ledgersByDimensionMap, metrics, ledger);
    }
    return ledgersByDimensionMap;
}
Also used : ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) Metrics(org.apache.pulsar.common.stats.Metrics)

Aggregations

ManagedLedgerImpl (org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl)32 Test (org.testng.annotations.Test)24 Field (java.lang.reflect.Field)17 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)17 CountDownLatch (java.util.concurrent.CountDownLatch)11 List (java.util.List)9 Lists (com.google.common.collect.Lists)7 BeforeMethod (org.testng.annotations.BeforeMethod)7 Arrays (java.util.Arrays)6 LinkedList (java.util.LinkedList)6 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 BrokerService (org.apache.pulsar.broker.service.BrokerService)6 ClusterData (org.apache.pulsar.common.policies.data.ClusterData)6 DispatchRate (org.apache.pulsar.common.policies.data.DispatchRate)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 Assert (org.testng.Assert)6 AfterMethod (org.testng.annotations.AfterMethod)6 DataProvider (org.testng.annotations.DataProvider)6