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());
});
}
}
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();
}
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));
}
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));
}
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);
}
Aggregations