|
18 | 18 | */
|
19 | 19 | package org.apache.pulsar.broker.service;
|
20 | 20 |
|
| 21 | +import static org.testng.Assert.assertEquals; |
21 | 22 | import static org.testng.Assert.assertFalse;
|
22 | 23 | import static org.testng.Assert.assertTrue;
|
23 | 24 | import static org.testng.Assert.fail;
|
24 | 25 |
|
| 26 | + |
| 27 | +import java.util.List; |
| 28 | +import java.util.UUID; |
25 | 29 | import lombok.Cleanup;
|
26 | 30 | import org.apache.pulsar.client.admin.ListNamespaceTopicsOptions;
|
27 | 31 | import org.apache.pulsar.client.admin.PulsarAdminException;
|
|
30 | 34 | import org.apache.pulsar.client.api.PulsarClientException;
|
31 | 35 | import org.apache.pulsar.common.naming.SystemTopicNames;
|
32 | 36 | import org.apache.pulsar.common.naming.TopicName;
|
| 37 | +import org.apache.pulsar.common.partition.PartitionedTopicMetadata; |
33 | 38 | import org.apache.pulsar.common.policies.data.AutoTopicCreationOverride;
|
34 | 39 | import org.apache.pulsar.common.policies.data.TopicType;
|
35 | 40 | import org.testng.Assert;
|
@@ -420,4 +425,100 @@ public void testAutoCreationOfSystemTopicNamespaceEvents() throws Exception {
|
420 | 425 | ListNamespaceTopicsOptions.builder().includeSystemTopic(true).build()).contains(topicString));
|
421 | 426 | assertFalse(admin.topics().getPartitionedTopicList("prop/ns-abc").contains(topicString));
|
422 | 427 | }
|
| 428 | + |
| 429 | + @Test |
| 430 | + public void testDynamicConfigurationTopicAutoCreationDisable() throws PulsarAdminException { |
| 431 | + // test disable AllowAutoTopicCreation |
| 432 | + pulsar.getConfiguration().setAllowAutoTopicCreation(true); |
| 433 | + admin.brokers().updateDynamicConfiguration("allowAutoTopicCreation", "false"); |
| 434 | + final String namespaceName = "prop/ns-abc"; |
| 435 | + final String topic = "persistent://" + namespaceName + "/test-dynamicConfiguration-topic-auto-creation-" |
| 436 | + + UUID.randomUUID(); |
| 437 | + Assert.assertThrows(PulsarClientException.NotFoundException.class, |
| 438 | + ()-> pulsarClient.newProducer().topic(topic).create()); |
| 439 | + } |
| 440 | + |
| 441 | + @Test |
| 442 | + public void testDynamicConfigurationTopicAutoCreationNonPartitioned() throws PulsarAdminException, PulsarClientException { |
| 443 | + pulsar.getConfiguration().setAllowAutoTopicCreation(false); |
| 444 | + pulsar.getConfiguration().setAllowAutoTopicCreationType("partitioned"); |
| 445 | + final String namespaceName = "prop/ns-abc"; |
| 446 | + final String topic = "persistent://" + namespaceName + "/test-dynamicConfiguration-topic-auto-creation-" |
| 447 | + + UUID.randomUUID(); |
| 448 | + // test enable AllowAutoTopicCreation, non-partitioned |
| 449 | + admin.brokers().updateDynamicConfiguration("allowAutoTopicCreation", "true"); |
| 450 | + admin.brokers().updateDynamicConfiguration("allowAutoTopicCreationType", "non-partitioned"); |
| 451 | + Producer<byte[]> producer = pulsarClient.newProducer() |
| 452 | + .topic(topic) |
| 453 | + .create(); |
| 454 | + List<String> topics = admin.topics().getList(namespaceName); |
| 455 | + List<String> partitionedTopicList = admin.topics().getPartitionedTopicList(namespaceName); |
| 456 | + assertEquals(topics.size(), 1); |
| 457 | + assertEquals(partitionedTopicList.size(), 0); |
| 458 | + producer.close(); |
| 459 | + admin.topics().delete(topic); |
| 460 | + } |
| 461 | + |
| 462 | + @Test |
| 463 | + public void testDynamicConfigurationTopicAutoCreationPartitioned() throws PulsarAdminException, PulsarClientException { |
| 464 | + pulsar.getConfiguration().setAllowAutoTopicCreation(false); |
| 465 | + pulsar.getConfiguration().setAllowAutoTopicCreationType("non-partitioned"); |
| 466 | + pulsar.getConfiguration().setMaxNumPartitionsPerPartitionedTopic(0); |
| 467 | + final String namespaceName = "prop/ns-abc"; |
| 468 | + final String topic = "persistent://" + namespaceName + "/test-dynamicConfiguration-topic-auto-creation-" |
| 469 | + + UUID.randomUUID(); |
| 470 | + // test enable AllowAutoTopicCreation, partitioned |
| 471 | + admin.brokers().updateDynamicConfigurationAsync("allowAutoTopicCreation", "true"); |
| 472 | + admin.brokers().updateDynamicConfiguration("maxNumPartitionsPerPartitionedTopic", "6"); |
| 473 | + admin.brokers().updateDynamicConfiguration("allowAutoTopicCreationType", "partitioned"); |
| 474 | + admin.brokers().updateDynamicConfiguration("defaultNumPartitions", "4"); |
| 475 | + Producer<byte[]> producer = pulsarClient.newProducer().topic(topic).create(); |
| 476 | + List<String> topics = admin.topics().getList(namespaceName); |
| 477 | + List<String> partitionedTopicList = admin.topics().getPartitionedTopicList(namespaceName); |
| 478 | + PartitionedTopicMetadata partitionedTopicMetadata = admin.topics().getPartitionedTopicMetadata(topic); |
| 479 | + assertEquals(topics.size(), 4); |
| 480 | + assertEquals(partitionedTopicList.size(), 1); |
| 481 | + assertEquals(partitionedTopicMetadata.partitions, 4); |
| 482 | + producer.close(); |
| 483 | + for (String t : topics) { |
| 484 | + admin.topics().delete(t); |
| 485 | + } |
| 486 | + } |
| 487 | + |
| 488 | + @Test |
| 489 | + public void testDynamicConfigurationTopicAutoCreationPartitionedWhenDefaultMoreThanMax() throws PulsarAdminException, PulsarClientException { |
| 490 | + pulsar.getConfiguration().setAllowAutoTopicCreation(true); |
| 491 | + pulsar.getConfiguration().setAllowAutoTopicCreationType("partitioned"); |
| 492 | + pulsar.getConfiguration().setMaxNumPartitionsPerPartitionedTopic(0); |
| 493 | + final String namespaceName = "prop/ns-abc"; |
| 494 | + String topic = "persistent://" + namespaceName + "/test-dynamicConfiguration-topic-auto-creation-" |
| 495 | + + UUID.randomUUID(); |
| 496 | + // test enable AllowAutoTopicCreation, partitioned when maxNumPartitionsPerPartitionedTopic < defaultNumPartitions |
| 497 | + admin.brokers().updateDynamicConfiguration("maxNumPartitionsPerPartitionedTopic", "2"); |
| 498 | + admin.brokers().updateDynamicConfiguration("defaultNumPartitions", "6"); |
| 499 | + Producer<byte[]> producer = pulsarClient.newProducer().topic(topic).create(); |
| 500 | + List<String> topics = admin.topics().getList(namespaceName); |
| 501 | + List<String> partitionedTopicList = admin.topics().getPartitionedTopicList(namespaceName); |
| 502 | + PartitionedTopicMetadata partitionedTopicMetadata = admin.topics().getPartitionedTopicMetadata(topic); |
| 503 | + assertEquals(topics.size(), 2); |
| 504 | + assertEquals(partitionedTopicList.size(), 1); |
| 505 | + assertEquals(partitionedTopicMetadata.partitions, 2); |
| 506 | + producer.close(); |
| 507 | + for (String t : topics) { |
| 508 | + admin.topics().delete(t); |
| 509 | + } |
| 510 | + |
| 511 | + // set maxNumPartitionsPerPartitionedTopic, make maxNumPartitionsPerPartitionedTopic < defaultNumPartitions |
| 512 | + admin.brokers().updateDynamicConfiguration("maxNumPartitionsPerPartitionedTopic", "1"); |
| 513 | + topic = "persistent://" + namespaceName + "/test-dynamicConfiguration-topic-auto-creation-" |
| 514 | + + UUID.randomUUID(); |
| 515 | + producer = pulsarClient.newProducer().topic(topic).create(); |
| 516 | + topics = admin.topics().getList(namespaceName); |
| 517 | + assertEquals(topics.size(), 1); |
| 518 | + producer.close(); |
| 519 | + for (String t : topics) { |
| 520 | + admin.topics().delete(t); |
| 521 | + } |
| 522 | + } |
| 523 | + |
423 | 524 | }
|
0 commit comments