Search in sources :

Example 66 with PropertyAdmin

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

the class ConsumerConfigurationTest method setup.

@BeforeMethod
@Override
public void setup() throws Exception {
    super.internalSetup();
    admin.clusters().createCluster("use", new ClusterData("http://127.0.0.1:" + BROKER_WEBSERVICE_PORT));
    admin.properties().createProperty("my-property", new PropertyAdmin(Lists.newArrayList("appid1", "appid2"), Sets.newHashSet("use")));
    admin.namespaces().createNamespace("my-property/use/my-ns");
}
Also used : ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 67 with PropertyAdmin

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

the class PatternTopicsConsumerImplTest method testAutoSubscribePatternConsumer.

// simulate subscribe a pattern which has 3 topics, but then matched topic added in.
@Test(timeOut = testTimeout)
public void testAutoSubscribePatternConsumer() throws Exception {
    String key = "AutoSubscribePatternConsumer";
    String subscriptionName = "my-ex-subscription-" + key;
    String topicName1 = "persistent://prop/use/ns-abc/pattern-topic-1-" + key;
    String topicName2 = "persistent://prop/use/ns-abc/pattern-topic-2-" + key;
    String topicName3 = "persistent://prop/use/ns-abc/pattern-topic-3-" + key;
    Pattern pattern = Pattern.compile("persistent://prop/use/ns-abc/pattern-topic.*");
    // 1. create partition
    admin.properties().createProperty("prop", new PropertyAdmin());
    admin.persistentTopics().createPartitionedTopic(topicName2, 2);
    admin.persistentTopics().createPartitionedTopic(topicName3, 3);
    // 2. create producer
    String messagePredicate = "my-message-" + key + "-";
    int totalMessages = 30;
    Producer<byte[]> producer1 = pulsarClient.newProducer().topic(topicName1).create();
    Producer<byte[]> producer2 = pulsarClient.newProducer().topic(topicName2).messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition).create();
    Producer<byte[]> producer3 = pulsarClient.newProducer().topic(topicName3).messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition).create();
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topicsPattern(pattern).patternAutoDiscoveryPeriod(2).subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).receiverQueueSize(4).subscribe();
    assertTrue(consumer instanceof PatternTopicsConsumerImpl);
    // 4. verify consumer get methods, to get 6 number of partitions and topics: 6=1+2+3
    assertSame(pattern, ((PatternTopicsConsumerImpl<?>) consumer).getPattern());
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getPartitionedTopics().size(), 6);
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getConsumers().size(), 6);
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getTopics().size(), 3);
    // 5. produce data to topic 1,2,3; verify should receive all the message
    for (int i = 0; i < totalMessages / 3; i++) {
        producer1.send((messagePredicate + "producer1-" + i).getBytes());
        producer2.send((messagePredicate + "producer2-" + i).getBytes());
        producer3.send((messagePredicate + "producer3-" + i).getBytes());
    }
    int messageSet = 0;
    Message<byte[]> message = consumer.receive();
    do {
        assertTrue(message instanceof TopicMessageImpl);
        messageSet++;
        consumer.acknowledge(message);
        log.debug("Consumer acknowledged : " + new String(message.getData()));
        message = consumer.receive(500, TimeUnit.MILLISECONDS);
    } while (message != null);
    assertEquals(messageSet, totalMessages);
    // 6. create another producer with 4 partitions
    String topicName4 = "persistent://prop/use/ns-abc/pattern-topic-4-" + key;
    admin.persistentTopics().createPartitionedTopic(topicName4, 4);
    Producer<byte[]> producer4 = pulsarClient.newProducer().topic(topicName4).messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition).create();
    // 7. call recheckTopics to subscribe each added topics above, verify topics number: 10=1+2+3+4
    log.debug("recheck topics change");
    PatternTopicsConsumerImpl<byte[]> consumer1 = ((PatternTopicsConsumerImpl<byte[]>) consumer);
    consumer1.run(consumer1.getRecheckPatternTimeout());
    Thread.sleep(100);
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getPartitionedTopics().size(), 10);
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getConsumers().size(), 10);
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getTopics().size(), 4);
    // 8. produce data to topic3 and topic4, verify should receive all the message
    for (int i = 0; i < totalMessages / 2; i++) {
        producer3.send((messagePredicate + "round2-producer4-" + i).getBytes());
        producer4.send((messagePredicate + "round2-producer4-" + i).getBytes());
    }
    messageSet = 0;
    message = consumer.receive();
    do {
        assertTrue(message instanceof TopicMessageImpl);
        messageSet++;
        consumer.acknowledge(message);
        log.debug("Consumer acknowledged : " + new String(message.getData()));
        message = consumer.receive(500, TimeUnit.MILLISECONDS);
    } while (message != null);
    assertEquals(messageSet, totalMessages);
    consumer.unsubscribe();
    consumer.close();
    producer1.close();
    producer2.close();
    producer3.close();
    producer4.close();
}
Also used : Pattern(java.util.regex.Pattern) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) Test(org.testng.annotations.Test)

Example 68 with PropertyAdmin

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

the class PatternTopicsConsumerImplTest method testAutoUnbubscribePatternConsumer.

@Test(timeOut = testTimeout)
public void testAutoUnbubscribePatternConsumer() throws Exception {
    String key = "AutoUnsubscribePatternConsumer";
    String subscriptionName = "my-ex-subscription-" + key;
    String topicName1 = "persistent://prop/use/ns-abc/pattern-topic-1-" + key;
    String topicName2 = "persistent://prop/use/ns-abc/pattern-topic-2-" + key;
    String topicName3 = "persistent://prop/use/ns-abc/pattern-topic-3-" + key;
    Pattern pattern = Pattern.compile("persistent://prop/use/ns-abc/pattern-topic.*");
    // 1. create partition
    admin.properties().createProperty("prop", new PropertyAdmin());
    admin.persistentTopics().createPartitionedTopic(topicName2, 2);
    admin.persistentTopics().createPartitionedTopic(topicName3, 3);
    // 2. create producer
    String messagePredicate = "my-message-" + key + "-";
    int totalMessages = 30;
    Producer<byte[]> producer1 = pulsarClient.newProducer().topic(topicName1).create();
    Producer<byte[]> producer2 = pulsarClient.newProducer().topic(topicName2).messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition).create();
    Producer<byte[]> producer3 = pulsarClient.newProducer().topic(topicName3).messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition).create();
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topicsPattern(pattern).patternAutoDiscoveryPeriod(2).subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).receiverQueueSize(4).subscribe();
    assertTrue(consumer instanceof PatternTopicsConsumerImpl);
    // 4. verify consumer get methods, to get 0 number of partitions and topics: 6=1+2+3
    assertSame(pattern, ((PatternTopicsConsumerImpl<?>) consumer).getPattern());
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getPartitionedTopics().size(), 6);
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getConsumers().size(), 6);
    assertEquals(((PatternTopicsConsumerImpl<?>) consumer).getTopics().size(), 3);
    // 5. produce data to topic 1,2,3; verify should receive all the message
    for (int i = 0; i < totalMessages / 3; i++) {
        producer1.send((messagePredicate + "producer1-" + i).getBytes());
        producer2.send((messagePredicate + "producer2-" + i).getBytes());
        producer3.send((messagePredicate + "producer3-" + i).getBytes());
    }
    int messageSet = 0;
    Message<byte[]> message = consumer.receive();
    do {
        assertTrue(message instanceof TopicMessageImpl);
        messageSet++;
        consumer.acknowledge(message);
        log.debug("Consumer acknowledged : " + new String(message.getData()));
        message = consumer.receive(500, TimeUnit.MILLISECONDS);
    } while (message != null);
    assertEquals(messageSet, totalMessages);
    // 6. remove producer 1,3; verify only consumer 2 left
    // seems no direct way to verify auto-unsubscribe, because this patternConsumer also referenced the topic.
    List<String> topicNames = Lists.newArrayList(topicName2);
    NamespaceService nss = pulsar.getNamespaceService();
    doReturn(topicNames).when(nss).getListOfTopics(NamespaceName.get("prop", "use", "ns-abc"));
    // 7. call recheckTopics to unsubscribe topic 1,3 , verify topics number: 2=6-1-3
    log.debug("recheck topics change");
    PatternTopicsConsumerImpl<byte[]> consumer1 = ((PatternTopicsConsumerImpl<byte[]>) consumer);
    consumer1.run(consumer1.getRecheckPatternTimeout());
    Thread.sleep(100);
    assertEquals(((PatternTopicsConsumerImpl<byte[]>) consumer).getPartitionedTopics().size(), 2);
    assertEquals(((PatternTopicsConsumerImpl<byte[]>) consumer).getConsumers().size(), 2);
    assertEquals(((PatternTopicsConsumerImpl<byte[]>) consumer).getTopics().size(), 1);
    // 8. produce data to topic2, verify should receive all the message
    for (int i = 0; i < totalMessages; i++) {
        producer2.send((messagePredicate + "round2-producer2-" + i).getBytes());
    }
    messageSet = 0;
    message = consumer.receive();
    do {
        assertTrue(message instanceof TopicMessageImpl);
        messageSet++;
        consumer.acknowledge(message);
        log.debug("Consumer acknowledged : " + new String(message.getData()));
        message = consumer.receive(500, TimeUnit.MILLISECONDS);
    } while (message != null);
    assertEquals(messageSet, totalMessages);
    consumer.unsubscribe();
    consumer.close();
    producer1.close();
    producer2.close();
    producer3.close();
}
Also used : Pattern(java.util.regex.Pattern) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) NamespaceService(org.apache.pulsar.broker.namespace.NamespaceService) Test(org.testng.annotations.Test)

Example 69 with PropertyAdmin

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

the class TopicsConsumerImplTest method testTopicsNameSubscribeWithBuilderFail.

@Test(timeOut = testTimeout)
public void testTopicsNameSubscribeWithBuilderFail() throws Exception {
    String key = "TopicsNameSubscribeWithBuilder";
    final String subscriptionName = "my-ex-subscription-" + key;
    final String topicName2 = "persistent://prop/use/ns-abc/topic-2-" + key;
    final String topicName3 = "persistent://prop/use/ns-abc/topic-3-" + key;
    admin.properties().createProperty("prop", new PropertyAdmin());
    admin.persistentTopics().createPartitionedTopic(topicName2, 2);
    admin.persistentTopics().createPartitionedTopic(topicName3, 3);
    // test failing builder with empty topics
    try {
        pulsarClient.newConsumer().subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).subscribe();
        fail("subscribe1 with no topicName should fail.");
    } catch (PulsarClientException e) {
    // expected
    }
    try {
        pulsarClient.newConsumer().topic().subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).subscribe();
        fail("subscribe2 with no topicName should fail.");
    } catch (IllegalArgumentException e) {
    // expected
    }
    try {
        pulsarClient.newConsumer().topics(null).subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).subscribe();
        fail("subscribe3 with no topicName should fail.");
    } catch (IllegalArgumentException e) {
    // expected
    }
    try {
        pulsarClient.newConsumer().topics(Lists.newArrayList()).subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).subscribe();
        fail("subscribe4 with no topicName should fail.");
    } catch (IllegalArgumentException e) {
    // expected
    }
}
Also used : PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) Test(org.testng.annotations.Test)

Example 70 with PropertyAdmin

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

the class TopicsConsumerImplTest method testSyncProducerAndConsumer.

@Test(timeOut = testTimeout)
public void testSyncProducerAndConsumer() throws Exception {
    String key = "TopicsConsumerSyncTest";
    final String subscriptionName = "my-ex-subscription-" + key;
    final String messagePredicate = "my-message-" + key + "-";
    final int totalMessages = 30;
    final String topicName1 = "persistent://prop/use/ns-abc/topic-1-" + key;
    final String topicName2 = "persistent://prop/use/ns-abc/topic-2-" + key;
    final String topicName3 = "persistent://prop/use/ns-abc/topic-3-" + key;
    List<String> topicNames = Lists.newArrayList(topicName1, topicName2, topicName3);
    admin.properties().createProperty("prop", new PropertyAdmin());
    admin.persistentTopics().createPartitionedTopic(topicName2, 2);
    admin.persistentTopics().createPartitionedTopic(topicName3, 3);
    // 1. producer connect
    Producer<byte[]> producer1 = pulsarClient.newProducer().topic(topicName1).create();
    Producer<byte[]> producer2 = pulsarClient.newProducer().topic(topicName2).messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition).create();
    Producer<byte[]> producer3 = pulsarClient.newProducer().topic(topicName3).messageRoutingMode(org.apache.pulsar.client.api.MessageRoutingMode.RoundRobinPartition).create();
    // 2. Create consumer
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topics(topicNames).subscriptionName(subscriptionName).subscriptionType(SubscriptionType.Shared).ackTimeout(ackTimeOutMillis, TimeUnit.MILLISECONDS).receiverQueueSize(4).subscribe();
    assertTrue(consumer instanceof TopicsConsumerImpl);
    // 3. producer publish messages
    for (int i = 0; i < totalMessages / 3; i++) {
        producer1.send((messagePredicate + "producer1-" + i).getBytes());
        producer2.send((messagePredicate + "producer2-" + i).getBytes());
        producer3.send((messagePredicate + "producer3-" + i).getBytes());
    }
    int messageSet = 0;
    Message<byte[]> message = consumer.receive();
    do {
        assertTrue(message instanceof TopicMessageImpl);
        messageSet++;
        consumer.acknowledge(message);
        log.debug("Consumer acknowledged : " + new String(message.getData()));
        message = consumer.receive(500, TimeUnit.MILLISECONDS);
    } while (message != null);
    assertEquals(messageSet, totalMessages);
    consumer.unsubscribe();
    consumer.close();
    producer1.close();
    producer2.close();
    producer3.close();
}
Also used : PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) Test(org.testng.annotations.Test)

Aggregations

PropertyAdmin (org.apache.pulsar.common.policies.data.PropertyAdmin)83 Test (org.testng.annotations.Test)60 ClusterData (org.apache.pulsar.common.policies.data.ClusterData)29 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)13 PulsarClient (org.apache.pulsar.client.api.PulsarClient)12 BeforeMethod (org.testng.annotations.BeforeMethod)12 PulsarAdmin (org.apache.pulsar.client.admin.PulsarAdmin)11 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)9 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)9 AuthenticationTls (org.apache.pulsar.client.impl.auth.AuthenticationTls)8 HashSet (java.util.HashSet)6 URI (java.net.URI)5 URL (java.net.URL)5 Pattern (java.util.regex.Pattern)5 PulsarService (org.apache.pulsar.broker.PulsarService)5 RestException (org.apache.pulsar.broker.web.RestException)5 Authentication (org.apache.pulsar.client.api.Authentication)5 AuthAction (org.apache.pulsar.common.policies.data.AuthAction)5 KeeperException (org.apache.zookeeper.KeeperException)5 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)4