Search in sources :

Example 76 with DestinationName

use of com.yahoo.pulsar.common.naming.DestinationName in project pulsar by yahoo.

the class PartitionedProducerConsumerTest method testKeyBasedProducer.

@Test
public void testKeyBasedProducer() throws Exception {
    log.info("-- Starting {} test --", methodName);
    int numPartitions = 4;
    DestinationName dn = DestinationName.get("persistent://my-property/use/my-ns/my-partitionedtopic3");
    String dummyKey1 = "dummykey1";
    String dummyKey2 = "dummykey2";
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Exclusive);
    admin.persistentTopics().createPartitionedTopic(dn.toString(), numPartitions);
    Producer producer = pulsarClient.createProducer(dn.toString());
    Consumer consumer = pulsarClient.subscribe(dn.toString(), "my-partitioned-subscriber", conf);
    Message msg = null;
    for (int i = 0; i < 5; i++) {
        String message = "my-message-" + i;
        msg = MessageBuilder.create().setContent(message.getBytes()).setKey(dummyKey1).build();
        producer.send(msg);
    }
    for (int i = 5; i < 10; i++) {
        String message = "my-message-" + i;
        msg = MessageBuilder.create().setContent(message.getBytes()).setKey(dummyKey2).build();
        producer.send(msg);
    }
    Set<String> messageSet = Sets.newHashSet();
    for (int i = 0; i < 10; i++) {
        msg = consumer.receive(5, TimeUnit.SECONDS);
        Assert.assertNotNull(msg, "Message should not be null");
        consumer.acknowledge(msg);
        String receivedMessage = new String(msg.getData());
        log.debug("Received message: [{}]", receivedMessage);
        testKeyBasedOrder(messageSet, receivedMessage);
    }
    producer.close();
    consumer.unsubscribe();
    consumer.close();
    admin.persistentTopics().deletePartitionedTopic(dn.toString());
    log.info("-- Exiting {} test --", methodName);
}
Also used : DestinationName(com.yahoo.pulsar.common.naming.DestinationName) Test(org.testng.annotations.Test)

Example 77 with DestinationName

use of com.yahoo.pulsar.common.naming.DestinationName in project pulsar by yahoo.

the class PartitionedProducerConsumerTest method testDeletePartitionedTopic.

@Test
public void testDeletePartitionedTopic() throws Exception {
    int numPartitions = 4;
    DestinationName dn = DestinationName.get("persistent://my-property/use/my-ns/my-partitionedtopic6");
    admin.persistentTopics().createPartitionedTopic(dn.toString(), numPartitions);
    Producer producer = pulsarClient.createProducer(dn.toString());
    Consumer consumer = pulsarClient.subscribe(dn.toString(), "my-sub");
    consumer.unsubscribe();
    consumer.close();
    producer.close();
    admin.persistentTopics().deletePartitionedTopic(dn.toString());
    Producer producer1 = pulsarClient.createProducer(dn.toString());
    if (producer1 instanceof PartitionedProducerImpl) {
        Assert.fail("should fail since partitioned topic was deleted");
    }
}
Also used : DestinationName(com.yahoo.pulsar.common.naming.DestinationName) PartitionedProducerImpl(com.yahoo.pulsar.client.impl.PartitionedProducerImpl) Test(org.testng.annotations.Test)

Example 78 with DestinationName

use of com.yahoo.pulsar.common.naming.DestinationName in project pulsar by yahoo.

the class PartitionedProducerConsumerTest method testPartitionedTopicNameWithSpecialCharacter.

@Test
public void testPartitionedTopicNameWithSpecialCharacter() throws Exception {
    log.info("-- Starting {} test --", methodName);
    int numPartitions = 4;
    final String specialCharacter = "! * ' ( ) ; : @ & = + $ , /\\ ? % # [ ]";
    final String topicName = "my-partitionedtopic1" + specialCharacter;
    DestinationName dn = DestinationName.get("persistent://my-property/use/my-ns/" + topicName);
    admin.persistentTopics().createPartitionedTopic(dn.toString(), numPartitions);
    ProducerConfiguration producerConf = new ProducerConfiguration();
    producerConf.setMessageRoutingMode(MessageRoutingMode.RoundRobinPartition);
    // Try to create producer which does lookup and create connection with broker
    Producer producer = pulsarClient.createProducer(dn.toString(), producerConf);
    producer.close();
    admin.persistentTopics().deletePartitionedTopic(dn.toString());
    log.info("-- Exiting {} test --", methodName);
}
Also used : DestinationName(com.yahoo.pulsar.common.naming.DestinationName) Test(org.testng.annotations.Test)

Example 79 with DestinationName

use of com.yahoo.pulsar.common.naming.DestinationName in project pulsar by yahoo.

the class LoadBalancerTest method testLoadReportsWrittenOnZK.

/*
     * tests that load manager creates its node and writes the initial load report a /loadbalance/brokers tests that
     * those load reports can be deserialized and are in valid format tests if the rankings are populated from the load
     * reports are not, both broker will have zero rank
     */
@Test
public void testLoadReportsWrittenOnZK() throws Exception {
    ZooKeeper zkc = bkEnsemble.getZkClient();
    try {
        for (int i = 0; i < BROKER_COUNT; i++) {
            String znodePath = String.format("%s/%s", SimpleLoadManagerImpl.LOADBALANCE_BROKERS_ROOT, lookupAddresses[i]);
            byte[] loadReportData = zkc.getData(znodePath, false, null);
            assert (loadReportData.length > 0);
            log.info("LoadReport {}, {}", lookupAddresses[i], new String(loadReportData));
            LoadReport loadReport = objectMapper.readValue(loadReportData, LoadReport.class);
            assert (loadReport.getName().equals(lookupAddresses[i]));
            assertTrue(loadReport.isUnderLoaded());
            assertFalse(loadReport.isOverLoaded());
            // Check Initial Ranking is populated in both the brokers
            Field ranking = ((SimpleLoadManagerImpl) pulsarServices[i].getLoadManager()).getClass().getDeclaredField("sortedRankings");
            ranking.setAccessible(true);
            AtomicReference<Map<Long, Set<ResourceUnit>>> sortedRanking = (AtomicReference<Map<Long, Set<ResourceUnit>>>) ranking.get(pulsarServices[i].getLoadManager());
            printSortedRanking(sortedRanking);
            // all brokers have same rank to it would be 0 --> set-of-all-the-brokers
            int brokerCount = 0;
            for (Map.Entry<Long, Set<ResourceUnit>> entry : sortedRanking.get().entrySet()) {
                brokerCount += entry.getValue().size();
            }
            assertEquals(brokerCount, BROKER_COUNT);
            DestinationName fqdn = DestinationName.get("persistent://pulsar/use/primary-ns/test-topic");
            ResourceUnit found = pulsarServices[i].getLoadManager().getLeastLoaded(pulsarServices[i].getNamespaceService().getBundle(fqdn));
            assertTrue(found != null);
        }
    } catch (InterruptedException | KeeperException e) {
        fail("Unable to read the data from Zookeeper - [{}]", e);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) AtomicReference(java.util.concurrent.atomic.AtomicReference) SimpleResourceUnit(com.yahoo.pulsar.broker.loadbalance.impl.SimpleResourceUnit) Field(java.lang.reflect.Field) ZooKeeper(org.apache.zookeeper.ZooKeeper) LoadReport(com.yahoo.pulsar.common.policies.data.loadbalancer.LoadReport) DestinationName(com.yahoo.pulsar.common.naming.DestinationName) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 80 with DestinationName

use of com.yahoo.pulsar.common.naming.DestinationName in project pulsar by yahoo.

the class PulsarClientImpl method getPartitionedTopicMetadata.

private CompletableFuture<PartitionedTopicMetadata> getPartitionedTopicMetadata(String topic) {
    CompletableFuture<PartitionedTopicMetadata> metadataFuture;
    try {
        DestinationName destinationName = DestinationName.get(topic);
        metadataFuture = lookup.getPartitionedTopicMetadata(destinationName);
    } catch (IllegalArgumentException e) {
        return FutureUtil.failedFuture(e);
    }
    return metadataFuture;
}
Also used : DestinationName(com.yahoo.pulsar.common.naming.DestinationName) PartitionedTopicMetadata(com.yahoo.pulsar.common.partition.PartitionedTopicMetadata)

Aggregations

DestinationName (com.yahoo.pulsar.common.naming.DestinationName)91 Test (org.testng.annotations.Test)36 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)33 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)26 CompletableFuture (java.util.concurrent.CompletableFuture)24 KeeperException (org.apache.zookeeper.KeeperException)23 PreconditionFailedException (com.yahoo.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)22 WebApplicationException (javax.ws.rs.WebApplicationException)21 IOException (java.io.IOException)20 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)19 RestException (com.yahoo.pulsar.broker.web.RestException)18 NotFoundException (com.yahoo.pulsar.client.admin.PulsarAdminException.NotFoundException)18 PartitionedTopicMetadata (com.yahoo.pulsar.common.partition.PartitionedTopicMetadata)17 SubscriptionBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)16 TopicBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.TopicBusyException)16 Path (javax.ws.rs.Path)16 NotAllowedException (com.yahoo.pulsar.broker.service.BrokerServiceException.NotAllowedException)14 ApiOperation (io.swagger.annotations.ApiOperation)14 ApiResponses (io.swagger.annotations.ApiResponses)14 Field (java.lang.reflect.Field)14