Search in sources :

Example 1 with AbstractTopic

use of org.apache.pulsar.broker.service.AbstractTopic in project pulsar by apache.

the class TopicPoliciesTest method waitTopicPoliciesApplied.

private void waitTopicPoliciesApplied(String topic, int partitions, java.util.function.Consumer<HierarchyTopicPolicies> condition) {
    TopicName topicName = TopicName.get(topic);
    if (partitions > 0) {
        for (int i = 0; i < partitions; i++) {
            String partition = topicName.getPartition(i).toString();
            Awaitility.await().untilAsserted(() -> {
                Topic t = pulsar.getBrokerService().getTopicIfExists(partition).get().get();
                assertTrue(t instanceof AbstractTopic);
                condition.accept(((AbstractTopic) t).getHierarchyTopicPolicies());
            });
        }
    } else {
        Awaitility.await().untilAsserted(() -> {
            Topic t = pulsar.getBrokerService().getTopicIfExists(topic).get().get();
            assertTrue(t instanceof AbstractTopic);
            condition.accept(((AbstractTopic) t).getHierarchyTopicPolicies());
        });
    }
}
Also used : Topic(org.apache.pulsar.broker.service.Topic) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) AbstractTopic(org.apache.pulsar.broker.service.AbstractTopic) AbstractTopic(org.apache.pulsar.broker.service.AbstractTopic) TopicName(org.apache.pulsar.common.naming.TopicName)

Example 2 with AbstractTopic

use of org.apache.pulsar.broker.service.AbstractTopic in project pulsar by apache.

the class TopicPoliciesTest method testTopicMaxMessageSize.

@Test(dataProvider = "persistentAndPartition")
public void testTopicMaxMessageSize(TopicDomain topicDomain, boolean isPartitioned) throws Exception {
    final String topic = TopicName.get(topicDomain.value(), NamespaceName.get(myNamespace), "test-" + UUID.randomUUID()).toString();
    if (isPartitioned) {
        admin.topics().createPartitionedTopic(topic, 3);
    }
    // init cache
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topic).create();
    assertNull(admin.topicPolicies().getMaxMessageSize(topic));
    // set msg size
    admin.topicPolicies().setMaxMessageSize(topic, 10);
    Awaitility.await().until(() -> pulsar.getTopicPoliciesService().getTopicPolicies(TopicName.get(topic)) != null);
    if (isPartitioned) {
        for (int i = 0; i < 3; i++) {
            String partitionName = TopicName.get(topic).getPartition(i).toString();
            Awaitility.await().untilAsserted(() -> {
                AbstractTopic partition = (AbstractTopic) pulsar.getBrokerService().getTopicIfExists(partitionName).get().get();
                assertEquals(partition.getHierarchyTopicPolicies().getTopicMaxMessageSize().get(), Integer.valueOf(10));
            });
        }
    } else {
        Awaitility.await().untilAsserted(() -> {
            AbstractTopic abstractTopic = (AbstractTopic) pulsar.getBrokerService().getTopicIfExists(topic).get().get();
            assertEquals(abstractTopic.getHierarchyTopicPolicies().getTopicMaxMessageSize().get(), Integer.valueOf(10));
        });
    }
    assertEquals(admin.topicPolicies().getMaxMessageSize(topic).intValue(), 10);
    try {
        producer.send(new byte[1024]);
    } catch (PulsarClientException e) {
        assertTrue(e instanceof PulsarClientException.NotAllowedException);
    }
    admin.topicPolicies().removeMaxMessageSize(topic);
    assertNull(admin.topicPolicies().getMaxMessageSize(topic));
    try {
        admin.topicPolicies().setMaxMessageSize(topic, Integer.MAX_VALUE);
        fail("should fail");
    } catch (PulsarAdminException e) {
        assertEquals(e.getStatusCode(), 412);
    }
    try {
        admin.topicPolicies().setMaxMessageSize(topic, -1);
        fail("should fail");
    } catch (PulsarAdminException e) {
        assertEquals(e.getStatusCode(), 412);
    }
    // make sure policy value take effect.
    if (isPartitioned) {
        for (int i = 0; i < 3; i++) {
            String partitionName = TopicName.get(topic).getPartition(i).toString();
            Awaitility.await().untilAsserted(() -> {
                AbstractTopic partition = (AbstractTopic) pulsar.getBrokerService().getTopicIfExists(partitionName).get().get();
                assertNull(partition.getHierarchyTopicPolicies().getTopicMaxMessageSize().getTopicValue());
            });
        }
    } else {
        Awaitility.await().untilAsserted(() -> {
            AbstractTopic abstractTopic = (AbstractTopic) pulsar.getBrokerService().getTopicIfExists(topic).get().get();
            assertNull(abstractTopic.getHierarchyTopicPolicies().getTopicMaxMessageSize().getTopicValue());
        });
    }
    Awaitility.await().untilAsserted(() -> {
        try {
            MessageId messageId = producer.send(new byte[1024]);
            assertNotNull(messageId);
        } catch (PulsarClientException e) {
            fail("failed to send message");
        }
    });
    producer.close();
}
Also used : PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) AbstractTopic(org.apache.pulsar.broker.service.AbstractTopic) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 3 with AbstractTopic

use of org.apache.pulsar.broker.service.AbstractTopic in project pulsar by apache.

the class TopicPoliciesTest method testTopicPolicyInitialValueWithNamespaceAlreadyLoaded.

@Test
public void testTopicPolicyInitialValueWithNamespaceAlreadyLoaded() throws Exception {
    TopicName topicName = TopicName.get(TopicDomain.persistent.value(), NamespaceName.get(myNamespace), "test-" + UUID.randomUUID());
    String topic = topicName.toString();
    SystemTopicBasedTopicPoliciesService policyService = (SystemTopicBasedTopicPoliciesService) pulsar.getTopicPoliciesService();
    // set up topic with maxSubscriptionsPerTopic = 10
    admin.topics().createNonPartitionedTopic(topic);
    admin.topicPolicies().setMaxSubscriptionsPerTopicAsync(topic, 10).get();
    // wait until topic loaded with right policy value.
    Awaitility.await().untilAsserted(() -> {
        AbstractTopic topic1 = (AbstractTopic) pulsar.getBrokerService().getTopic(topic, true).get().get();
        assertEquals(topic1.getHierarchyTopicPolicies().getMaxSubscriptionsPerTopic().get(), Integer.valueOf(10));
    });
    // unload the topic
    pulsar.getNamespaceService().unloadNamespaceBundle(pulsar.getNamespaceService().getBundle(topicName)).get();
    assertFalse(pulsar.getBrokerService().getTopics().containsKey(topic));
    // load the nameserver, but topic is not init.
    log.info("lookup:{}", admin.lookups().lookupTopic(topic));
    assertTrue(pulsar.getBrokerService().isTopicNsOwnedByBroker(topicName));
    assertFalse(pulsar.getBrokerService().getTopics().containsKey(topic));
    // make sure namespace policy reader is fully started.
    Awaitility.await().untilAsserted(() -> {
        assertTrue(policyService.getPoliciesCacheInit(topicName.getNamespaceObject()));
    });
    // load the topic.
    AbstractTopic topic1 = (AbstractTopic) pulsar.getBrokerService().getTopic(topic, true).get().get();
    assertEquals(topic1.getHierarchyTopicPolicies().getMaxSubscriptionsPerTopic().get(), Integer.valueOf(10));
}
Also used : SystemTopicBasedTopicPoliciesService(org.apache.pulsar.broker.service.SystemTopicBasedTopicPoliciesService) AbstractTopic(org.apache.pulsar.broker.service.AbstractTopic) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 4 with AbstractTopic

use of org.apache.pulsar.broker.service.AbstractTopic in project pulsar by yahoo.

the class TopicPoliciesTest method testTopicPolicyInitialValueWithNamespaceAlreadyLoaded.

@Test
public void testTopicPolicyInitialValueWithNamespaceAlreadyLoaded() throws Exception {
    TopicName topicName = TopicName.get(TopicDomain.persistent.value(), NamespaceName.get(myNamespace), "test-" + UUID.randomUUID());
    String topic = topicName.toString();
    SystemTopicBasedTopicPoliciesService policyService = (SystemTopicBasedTopicPoliciesService) pulsar.getTopicPoliciesService();
    // set up topic with maxSubscriptionsPerTopic = 10
    admin.topics().createNonPartitionedTopic(topic);
    admin.topicPolicies().setMaxSubscriptionsPerTopicAsync(topic, 10).get();
    // wait until topic loaded with right policy value.
    Awaitility.await().untilAsserted(() -> {
        AbstractTopic topic1 = (AbstractTopic) pulsar.getBrokerService().getTopic(topic, true).get().get();
        assertEquals(topic1.getHierarchyTopicPolicies().getMaxSubscriptionsPerTopic().get(), Integer.valueOf(10));
    });
    // unload the topic
    pulsar.getNamespaceService().unloadNamespaceBundle(pulsar.getNamespaceService().getBundle(topicName)).get();
    assertFalse(pulsar.getBrokerService().getTopics().containsKey(topic));
    // load the nameserver, but topic is not init.
    log.info("lookup:{}", admin.lookups().lookupTopic(topic));
    assertTrue(pulsar.getBrokerService().isTopicNsOwnedByBroker(topicName));
    assertFalse(pulsar.getBrokerService().getTopics().containsKey(topic));
    // make sure namespace policy reader is fully started.
    Awaitility.await().untilAsserted(() -> {
        assertTrue(policyService.getPoliciesCacheInit(topicName.getNamespaceObject()));
    });
    // load the topic.
    AbstractTopic topic1 = (AbstractTopic) pulsar.getBrokerService().getTopic(topic, true).get().get();
    assertEquals(topic1.getHierarchyTopicPolicies().getMaxSubscriptionsPerTopic().get(), Integer.valueOf(10));
}
Also used : SystemTopicBasedTopicPoliciesService(org.apache.pulsar.broker.service.SystemTopicBasedTopicPoliciesService) AbstractTopic(org.apache.pulsar.broker.service.AbstractTopic) TopicName(org.apache.pulsar.common.naming.TopicName) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 5 with AbstractTopic

use of org.apache.pulsar.broker.service.AbstractTopic in project pulsar by yahoo.

the class TopicPoliciesTest method testSubscriptionLevelDispatchRate.

@Test
public void testSubscriptionLevelDispatchRate() throws Exception {
    final String topic = testTopic + UUID.randomUUID();
    admin.topics().createNonPartitionedTopic(topic);
    String subscriptionName = "testSubscriptionLevelDispatchRate";
    @Cleanup Consumer<byte[]> consumer = pulsarClient.newConsumer().subscriptionName(subscriptionName).topic(topic).subscribe();
    AbstractTopic abstractTopic = (AbstractTopic) pulsar.getBrokerService().getTopicIfExists(topic).get().get();
    DispatchRate topicLevelRate = DispatchRate.builder().dispatchThrottlingRateInMsg(100).dispatchThrottlingRateInByte(1024).ratePeriodInSecond(1).build();
    admin.topicPolicies().setSubscriptionDispatchRate(topic, topicLevelRate);
    Awaitility.await().untilAsserted(() -> Assert.assertEquals(admin.topicPolicies().getSubscriptionDispatchRate(topic), topicLevelRate));
    // topic level is set, applied value should be topic level setting.
    Assert.assertEquals(admin.topicPolicies().getSubscriptionDispatchRate(topic, subscriptionName, true), topicLevelRate);
    // subscription level setting is not set.
    Assert.assertNull(admin.topicPolicies().getSubscriptionDispatchRate(topic, subscriptionName));
    DispatchRate subLevelRate = DispatchRate.builder().dispatchThrottlingRateInMsg(1000).dispatchThrottlingRateInByte(1024 * 1024).ratePeriodInSecond(1).build();
    admin.topicPolicies().setSubscriptionDispatchRate(topic, subscriptionName, subLevelRate);
    Awaitility.await().untilAsserted(() -> Assert.assertEquals(admin.topicPolicies().getSubscriptionDispatchRate(topic, subscriptionName), subLevelRate));
    Awaitility.await().untilAsserted(() -> Assert.assertEquals(abstractTopic.getSubscriptionDispatchRate(subscriptionName), subLevelRate));
    DispatchRateLimiter dispatchRateLimiter = abstractTopic.getSubscription(subscriptionName).getDispatcher().getRateLimiter().get();
    Assert.assertNotNull(dispatchRateLimiter);
    Assert.assertEquals(dispatchRateLimiter.getDispatchRateOnByte(), subLevelRate.getDispatchThrottlingRateInByte());
    Assert.assertEquals(dispatchRateLimiter.getDispatchRateOnMsg(), subLevelRate.getDispatchThrottlingRateInMsg());
    admin.topicPolicies().removeSubscriptionDispatchRate(topic, subscriptionName);
    Awaitility.await().untilAsserted(() -> Assert.assertNull(admin.topicPolicies().getSubscriptionDispatchRate(topic, subscriptionName)));
    admin.topics().delete(topic, true);
}
Also used : DispatchRateLimiter(org.apache.pulsar.broker.service.persistent.DispatchRateLimiter) DispatchRate(org.apache.pulsar.common.policies.data.DispatchRate) Cleanup(lombok.Cleanup) AbstractTopic(org.apache.pulsar.broker.service.AbstractTopic) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

AbstractTopic (org.apache.pulsar.broker.service.AbstractTopic)18 Test (org.testng.annotations.Test)14 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)11 Topic (org.apache.pulsar.broker.service.Topic)7 TopicName (org.apache.pulsar.common.naming.TopicName)7 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)6 Field (java.lang.reflect.Field)3 HashSet (java.util.HashSet)3 SystemTopicBasedTopicPoliciesService (org.apache.pulsar.broker.service.SystemTopicBasedTopicPoliciesService)3 PersistentTopic (org.apache.pulsar.broker.service.persistent.PersistentTopic)3 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)3 MessageId (org.apache.pulsar.client.api.MessageId)3 SubscriptionType (org.apache.pulsar.client.api.SubscriptionType)3 Cleanup (lombok.Cleanup)2 PulsarService (org.apache.pulsar.broker.PulsarService)2 AbstractBaseDispatcher (org.apache.pulsar.broker.service.AbstractBaseDispatcher)2 Dispatcher (org.apache.pulsar.broker.service.Dispatcher)2 DispatchRateLimiter (org.apache.pulsar.broker.service.persistent.DispatchRateLimiter)2 PersistentSubscription (org.apache.pulsar.broker.service.persistent.PersistentSubscription)2 NarClassLoader (org.apache.pulsar.common.nar.NarClassLoader)2