Search in sources :

Example 16 with BacklogQuota

use of org.apache.pulsar.common.policies.data.BacklogQuota in project incubator-pulsar by apache.

the class BacklogQuotaManagerTest method testProducerException.

@Test
public void testProducerException() throws Exception {
    assertEquals(admin.namespaces().getBacklogQuotaMap("prop/usc/quotahold"), Maps.newTreeMap());
    admin.namespaces().setBacklogQuota("prop/usc/quotahold", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_exception));
    final PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
    final String topic1 = "persistent://prop/usc/quotahold/except";
    final String subName1 = "c1except";
    boolean gotException = false;
    client.newConsumer().topic(topic1).subscriptionName(subName1).subscribe();
    byte[] content = new byte[1024];
    Producer<byte[]> producer = client.newProducer().topic(topic1).sendTimeout(2, TimeUnit.SECONDS).create();
    for (int i = 0; i < 10; i++) {
        producer.send(content);
    }
    Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);
    try {
        // try to send over backlog quota and make sure it fails
        producer.send(content);
        producer.send(content);
        Assert.fail("backlog quota did not exceed");
    } catch (PulsarClientException ce) {
        Assert.assertTrue(ce instanceof PulsarClientException.ProducerBlockedQuotaExceededException || ce instanceof PulsarClientException.TimeoutException, ce.getMessage());
        gotException = true;
    }
    Assert.assertTrue(gotException, "backlog exceeded exception did not occur");
    client.close();
}
Also used : PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) Test(org.testng.annotations.Test)

Example 17 with BacklogQuota

use of org.apache.pulsar.common.policies.data.BacklogQuota in project incubator-pulsar by apache.

the class BacklogQuotaManagerTest method testConsumerBacklogEvictionWithAck.

@Test
public void testConsumerBacklogEvictionWithAck() throws Exception {
    assertEquals(admin.namespaces().getBacklogQuotaMap("prop/usc/ns-quota"), Maps.newTreeMap());
    admin.namespaces().setBacklogQuota("prop/usc/ns-quota", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.consumer_backlog_eviction));
    PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()).build();
    final String topic1 = "persistent://prop/usc/ns-quota/topic11";
    final String subName1 = "c11";
    final String subName2 = "c21";
    final int numMsgs = 20;
    Consumer<byte[]> consumer1 = client.newConsumer().topic(topic1).subscriptionName(subName1).subscribe();
    Consumer<byte[]> consumer2 = client.newConsumer().topic(topic1).subscriptionName(subName2).subscribe();
    org.apache.pulsar.client.api.Producer<byte[]> producer = client.newProducer().topic(topic1).create();
    byte[] content = new byte[1024];
    for (int i = 0; i < numMsgs; i++) {
        producer.send(content);
        // only one consumer acknowledges the message
        consumer1.acknowledge(consumer1.receive());
        consumer2.receive();
    }
    Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);
    rolloverStats();
    PersistentTopicStats stats = admin.persistentTopics().getStats(topic1);
    Assert.assertTrue(stats.storageSize <= 10 * 1024, "Storage size is [" + stats.storageSize + "]");
    client.close();
}
Also used : PulsarClient(org.apache.pulsar.client.api.PulsarClient) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) Test(org.testng.annotations.Test)

Aggregations

BacklogQuota (org.apache.pulsar.common.policies.data.BacklogQuota)17 Test (org.testng.annotations.Test)14 PulsarClient (org.apache.pulsar.client.api.PulsarClient)9 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)7 PersistentTopicStats (org.apache.pulsar.common.policies.data.PersistentTopicStats)6 CountDownLatch (java.util.concurrent.CountDownLatch)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 TopicName (org.apache.pulsar.common.naming.TopicName)3 BacklogQuotaType (org.apache.pulsar.common.policies.data.BacklogQuota.BacklogQuotaType)2 URI (java.net.URI)1 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)1 PersistentReplicator (org.apache.pulsar.broker.service.persistent.PersistentReplicator)1 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)1 Lookup (org.apache.pulsar.client.admin.Lookup)1 Namespaces (org.apache.pulsar.client.admin.Namespaces)1 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)1 RetentionPolicy (org.apache.pulsar.common.policies.data.BacklogQuota.RetentionPolicy)1 PersistencePolicies (org.apache.pulsar.common.policies.data.PersistencePolicies)1 RetentionPolicies (org.apache.pulsar.common.policies.data.RetentionPolicies)1