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