Search in sources :

Example 1 with RetentionPolicy

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

the class ReplicatorTest method testResumptionAfterBacklogRelaxed.

/**
 * Issue #199
 *
 * It verifies that: if the remote cluster reaches backlog quota limit, replicator temporarily stops and once the
 * backlog drains it should resume replication.
 *
 * @throws Exception
 */
@Test(timeOut = 60000, enabled = true, priority = -1)
public void testResumptionAfterBacklogRelaxed() throws Exception {
    List<RetentionPolicy> policies = Lists.newArrayList();
    policies.add(RetentionPolicy.producer_exception);
    policies.add(RetentionPolicy.producer_request_hold);
    for (RetentionPolicy policy : policies) {
        // Use 1Mb quota by default
        admin1.namespaces().setBacklogQuota("pulsar/global/ns1", new BacklogQuota(1 * 1024 * 1024, policy));
        Thread.sleep(200);
        TopicName dest = TopicName.get(String.format("persistent://pulsar/global/ns1/%s-%d", policy, System.currentTimeMillis()));
        // Producer on r1
        MessageProducer producer1 = new MessageProducer(url1, dest);
        // Consumer on r2
        MessageConsumer consumer2 = new MessageConsumer(url2, dest);
        // Replicator for r1 -> r2
        PersistentTopic topic = (PersistentTopic) pulsar1.getBrokerService().getTopicReference(dest.toString());
        Replicator replicator = topic.getPersistentReplicator("r2");
        // Produce 1 message in r1. This message will be replicated immediately into r2 and it will become part of
        // local backlog
        producer1.produce(1);
        Thread.sleep(500);
        // Restrict backlog quota limit to 1 byte to stop replication
        admin1.namespaces().setBacklogQuota("pulsar/global/ns1", new BacklogQuota(1, policy));
        Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);
        assertEquals(replicator.getStats().replicationBacklog, 0);
        // Next message will not be replicated, because r2 has reached the quota
        producer1.produce(1);
        Thread.sleep(500);
        assertEquals(replicator.getStats().replicationBacklog, 1);
        // Consumer will now drain 1 message and the replication backlog will be cleared
        consumer2.receive(1);
        // Wait until the 2nd message got delivered to consumer
        consumer2.receive(1);
        int retry = 10;
        for (int i = 0; i < retry && replicator.getStats().replicationBacklog > 0; i++) {
            if (i != retry - 1) {
                Thread.sleep(100);
            }
        }
        assertEquals(replicator.getStats().replicationBacklog, 0);
        producer1.close();
        consumer2.close();
    }
}
Also used : PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) PersistentReplicator(org.apache.pulsar.broker.service.persistent.PersistentReplicator) BacklogQuota(org.apache.pulsar.common.policies.data.BacklogQuota) RetentionPolicy(org.apache.pulsar.common.policies.data.BacklogQuota.RetentionPolicy) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test)

Aggregations

PersistentReplicator (org.apache.pulsar.broker.service.persistent.PersistentReplicator)1 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)1 TopicName (org.apache.pulsar.common.naming.TopicName)1 BacklogQuota (org.apache.pulsar.common.policies.data.BacklogQuota)1 RetentionPolicy (org.apache.pulsar.common.policies.data.BacklogQuota.RetentionPolicy)1 Test (org.testng.annotations.Test)1