Search in sources :

Example 11 with BacklogQuota

use of com.yahoo.pulsar.common.policies.data.BacklogQuota in project pulsar by yahoo.

the class BacklogQuotaManagerTest method testAheadProducerOnHold.

@Test
public void testAheadProducerOnHold() throws Exception {
    assertEquals(admin.namespaces().getBacklogQuotaMap("prop/usc/quotahold"), Maps.newTreeMap());
    admin.namespaces().setBacklogQuota("prop/usc/quotahold", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_request_hold));
    final ClientConfiguration clientConf = new ClientConfiguration();
    clientConf.setStatsInterval(0, TimeUnit.SECONDS);
    final PulsarClient client = PulsarClient.create(adminUrl.toString(), clientConf);
    final String topic1 = "persistent://prop/usc/quotahold/hold";
    final String subName1 = "c1hold";
    final int numMsgs = 10;
    Consumer consumer = client.subscribe(topic1, subName1);
    ProducerConfiguration producerConfiguration = new ProducerConfiguration();
    producerConfiguration.setSendTimeout(2, TimeUnit.SECONDS);
    byte[] content = new byte[1024];
    Producer producer = client.createProducer(topic1, producerConfiguration);
    for (int i = 0; i <= numMsgs; i++) {
        try {
            producer.send(content);
            LOG.info("sent [{}]", i);
        } catch (PulsarClientException.TimeoutException cte) {
            // producer close may cause a timeout on send
            LOG.info("timeout on [{}]", i);
        }
    }
    for (int i = 0; i < numMsgs; i++) {
        consumer.receive();
        LOG.info("received [{}]", i);
    }
    Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);
    rolloverStats();
    PersistentTopicStats stats = admin.persistentTopics().getStats(topic1);
    Assert.assertEquals(stats.publishers.size(), 0, "Number of producers on topic " + topic1 + " are [" + stats.publishers.size() + "]");
    client.close();
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) Producer(com.yahoo.pulsar.client.api.Producer) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) ProducerConfiguration(com.yahoo.pulsar.client.api.ProducerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) BacklogQuota(com.yahoo.pulsar.common.policies.data.BacklogQuota) PersistentTopicStats(com.yahoo.pulsar.common.policies.data.PersistentTopicStats) ClientConfiguration(com.yahoo.pulsar.client.api.ClientConfiguration) Test(org.testng.annotations.Test)

Example 12 with BacklogQuota

use of com.yahoo.pulsar.common.policies.data.BacklogQuota in project pulsar by yahoo.

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 ClientConfiguration clientConf = new ClientConfiguration();
    clientConf.setStatsInterval(0, TimeUnit.SECONDS);
    final PulsarClient client = PulsarClient.create(adminUrl.toString(), clientConf);
    final String topic1 = "persistent://prop/usc/quotahold/except";
    final String subName1 = "c1except";
    boolean gotException = false;
    client.subscribe(topic1, subName1);
    ProducerConfiguration producerConfiguration = new ProducerConfiguration();
    producerConfiguration.setSendTimeout(2, TimeUnit.SECONDS);
    byte[] content = new byte[1024];
    Producer producer = client.createProducer(topic1, producerConfiguration);
    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 : Producer(com.yahoo.pulsar.client.api.Producer) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) ProducerConfiguration(com.yahoo.pulsar.client.api.ProducerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) BacklogQuota(com.yahoo.pulsar.common.policies.data.BacklogQuota) ClientConfiguration(com.yahoo.pulsar.client.api.ClientConfiguration) Test(org.testng.annotations.Test)

Example 13 with BacklogQuota

use of com.yahoo.pulsar.common.policies.data.BacklogQuota in project pulsar by yahoo.

the class BacklogQuotaManagerTest method testAheadProducerOnHoldTimeout.

@Test
public void testAheadProducerOnHoldTimeout() throws Exception {
    assertEquals(admin.namespaces().getBacklogQuotaMap("prop/usc/quotahold"), Maps.newTreeMap());
    admin.namespaces().setBacklogQuota("prop/usc/quotahold", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_request_hold));
    final ClientConfiguration clientConf = new ClientConfiguration();
    clientConf.setStatsInterval(0, TimeUnit.SECONDS);
    final PulsarClient client = PulsarClient.create(adminUrl.toString(), clientConf);
    final String topic1 = "persistent://prop/usc/quotahold/holdtimeout";
    final String subName1 = "c1holdtimeout";
    boolean gotException = false;
    client.subscribe(topic1, subName1);
    ProducerConfiguration producerConfiguration = new ProducerConfiguration();
    producerConfiguration.setSendTimeout(2, TimeUnit.SECONDS);
    byte[] content = new byte[1024];
    Producer producer = client.createProducer(topic1, producerConfiguration);
    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.TimeoutException te) {
        gotException = true;
    }
    Assert.assertTrue(gotException, "timeout did not occur");
    client.close();
}
Also used : Producer(com.yahoo.pulsar.client.api.Producer) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) ProducerConfiguration(com.yahoo.pulsar.client.api.ProducerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) BacklogQuota(com.yahoo.pulsar.common.policies.data.BacklogQuota) ClientConfiguration(com.yahoo.pulsar.client.api.ClientConfiguration) Test(org.testng.annotations.Test)

Example 14 with BacklogQuota

use of com.yahoo.pulsar.common.policies.data.BacklogQuota in project pulsar by yahoo.

the class Namespaces method checkQuotas.

private boolean checkQuotas(Policies policies, RetentionPolicies retention) {
    Map<BacklogQuota.BacklogQuotaType, BacklogQuota> backlog_quota_map = policies.backlog_quota_map;
    if (backlog_quota_map.isEmpty() || retention.getRetentionSizeInMB() == 0) {
        return true;
    }
    BacklogQuota quota = backlog_quota_map.get(BacklogQuotaType.destination_storage);
    if (quota == null) {
        quota = pulsar().getBrokerService().getBacklogQuotaManager().getDefaultQuota();
    }
    if (quota.getLimit() >= ((long) retention.getRetentionSizeInMB() * 1024 * 1024)) {
        return false;
    }
    return true;
}
Also used : BacklogQuotaType(com.yahoo.pulsar.common.policies.data.BacklogQuota.BacklogQuotaType) BacklogQuota(com.yahoo.pulsar.common.policies.data.BacklogQuota)

Example 15 with BacklogQuota

use of com.yahoo.pulsar.common.policies.data.BacklogQuota in project pulsar by yahoo.

the class BacklogQuotaManager method handleExceededBacklogQuota.

/**
     * Handle exceeded backlog by using policies set in the zookeeper for given topic
     *
     * @param persistentTopic
     *            Topic on which backlog has been exceeded
     */
public void handleExceededBacklogQuota(PersistentTopic persistentTopic) {
    DestinationName destination = DestinationName.get(persistentTopic.getName());
    String namespace = destination.getNamespace();
    String policyPath = AdminResource.path("policies", namespace);
    BacklogQuota quota = getBacklogQuota(namespace, policyPath);
    log.info("Backlog quota exceeded for topic [{}]. Applying [{}] policy", persistentTopic.getName(), quota.getPolicy());
    switch(quota.getPolicy()) {
        case consumer_backlog_eviction:
            dropBacklog(persistentTopic, quota);
            break;
        case producer_exception:
        case producer_request_hold:
            disconnectProducers(persistentTopic);
            break;
        default:
            break;
    }
}
Also used : DestinationName(com.yahoo.pulsar.common.naming.DestinationName) BacklogQuota(com.yahoo.pulsar.common.policies.data.BacklogQuota)

Aggregations

BacklogQuota (com.yahoo.pulsar.common.policies.data.BacklogQuota)15 Test (org.testng.annotations.Test)12 Producer (com.yahoo.pulsar.client.api.Producer)9 PulsarClient (com.yahoo.pulsar.client.api.PulsarClient)9 ClientConfiguration (com.yahoo.pulsar.client.api.ClientConfiguration)8 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)8 Consumer (com.yahoo.pulsar.client.api.Consumer)7 PersistentTopicStats (com.yahoo.pulsar.common.policies.data.PersistentTopicStats)6 ProducerConfiguration (com.yahoo.pulsar.client.api.ProducerConfiguration)4 DestinationName (com.yahoo.pulsar.common.naming.DestinationName)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 BacklogQuotaType (com.yahoo.pulsar.common.policies.data.BacklogQuota.BacklogQuotaType)2 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)1 PersistentReplicator (com.yahoo.pulsar.broker.service.persistent.PersistentReplicator)1 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)1 Lookup (com.yahoo.pulsar.client.admin.Lookup)1 Namespaces (com.yahoo.pulsar.client.admin.Namespaces)1 PulsarAdmin (com.yahoo.pulsar.client.admin.PulsarAdmin)1