use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.
the class PersistentFailoverE2ETest method testSimpleConsumerEventsWithPartition.
@Test
public void testSimpleConsumerEventsWithPartition() throws Exception {
int numPartitions = 4;
final String topicName = "persistent://prop/use/ns-abc/failover-topic2";
final TopicName destName = TopicName.get(topicName);
final String subName = "sub1";
final int numMsgs = 100;
Set<String> uniqueMessages = new HashSet<>();
admin.persistentTopics().createPartitionedTopic(topicName, numPartitions);
ConsumerBuilder<byte[]> consumerBuilder = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscriptionType(SubscriptionType.Failover);
// 1. two consumers on the same subscription
ActiveInactiveListenerEvent listener1 = new ActiveInactiveListenerEvent();
ActiveInactiveListenerEvent listener2 = new ActiveInactiveListenerEvent();
Consumer<byte[]> consumer1 = consumerBuilder.clone().consumerName("1").consumerEventListener(listener1).subscribe();
Consumer<byte[]> consumer2 = consumerBuilder.clone().consumerName("2").consumerEventListener(listener2).subscribe();
PersistentTopic topicRef;
topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(destName.getPartition(0).toString());
PersistentDispatcherSingleActiveConsumer disp0 = (PersistentDispatcherSingleActiveConsumer) topicRef.getSubscription(subName).getDispatcher();
topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(destName.getPartition(1).toString());
PersistentDispatcherSingleActiveConsumer disp1 = (PersistentDispatcherSingleActiveConsumer) topicRef.getSubscription(subName).getDispatcher();
topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(destName.getPartition(2).toString());
PersistentDispatcherSingleActiveConsumer disp2 = (PersistentDispatcherSingleActiveConsumer) topicRef.getSubscription(subName).getDispatcher();
topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(destName.getPartition(3).toString());
PersistentDispatcherSingleActiveConsumer disp3 = (PersistentDispatcherSingleActiveConsumer) topicRef.getSubscription(subName).getDispatcher();
List<CompletableFuture<MessageId>> futures = Lists.newArrayListWithCapacity(numMsgs);
Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).messageRoutingMode(MessageRoutingMode.RoundRobinPartition).create();
for (int i = 0; i < numMsgs; i++) {
String message = "my-message-" + i;
futures.add(producer.sendAsync(message.getBytes()));
}
FutureUtil.waitForAll(futures).get();
futures.clear();
// equal distribution between both consumers
int totalMessages = 0;
Message<byte[]> msg = null;
Set<Integer> receivedPtns = Sets.newHashSet();
while (true) {
msg = consumer1.receive(1, TimeUnit.SECONDS);
if (msg == null) {
break;
}
totalMessages++;
consumer1.acknowledge(msg);
MessageIdImpl msgId = (MessageIdImpl) msg.getMessageId();
receivedPtns.add(msgId.getPartitionIndex());
}
assertTrue(Sets.difference(listener1.activePtns, receivedPtns).isEmpty());
assertTrue(Sets.difference(listener2.inactivePtns, receivedPtns).isEmpty());
Assert.assertEquals(totalMessages, numMsgs / 2);
receivedPtns = Sets.newHashSet();
while (true) {
msg = consumer2.receive(1, TimeUnit.SECONDS);
if (msg == null) {
break;
}
totalMessages++;
consumer2.acknowledge(msg);
MessageIdImpl msgId = (MessageIdImpl) msg.getMessageId();
receivedPtns.add(msgId.getPartitionIndex());
}
assertTrue(Sets.difference(listener1.inactivePtns, receivedPtns).isEmpty());
assertTrue(Sets.difference(listener2.activePtns, receivedPtns).isEmpty());
Assert.assertEquals(totalMessages, numMsgs);
Assert.assertEquals(disp0.getActiveConsumer().consumerName(), "1");
Assert.assertEquals(disp1.getActiveConsumer().consumerName(), "2");
Assert.assertEquals(disp2.getActiveConsumer().consumerName(), "1");
Assert.assertEquals(disp3.getActiveConsumer().consumerName(), "2");
totalMessages = 0;
for (int i = 0; i < numMsgs; i++) {
String message = "my-message-" + i;
futures.add(producer.sendAsync(message.getBytes()));
}
FutureUtil.waitForAll(futures).get();
futures.clear();
// add a consumer
for (int i = 0; i < 20; i++) {
msg = consumer1.receive(1, TimeUnit.SECONDS);
Assert.assertNotNull(msg);
uniqueMessages.add(new String(msg.getData()));
consumer1.acknowledge(msg);
}
Consumer<byte[]> consumer3 = consumerBuilder.clone().consumerName("3").subscribe();
Thread.sleep(CONSUMER_ADD_OR_REMOVE_WAIT_TIME);
int consumer1Messages = 0;
while (true) {
msg = consumer1.receive(1, TimeUnit.SECONDS);
if (msg == null) {
Assert.assertEquals(consumer1Messages, 55);
break;
}
consumer1Messages++;
uniqueMessages.add(new String(msg.getData()));
consumer1.acknowledge(msg);
}
int consumer2Messages = 0;
while (true) {
msg = consumer2.receive(1, TimeUnit.SECONDS);
if (msg == null) {
Assert.assertEquals(consumer2Messages, 50);
break;
}
consumer2Messages++;
uniqueMessages.add(new String(msg.getData()));
consumer2.acknowledge(msg);
}
int consumer3Messages = 0;
while (true) {
msg = consumer3.receive(1, TimeUnit.SECONDS);
if (msg == null) {
Assert.assertEquals(consumer3Messages, 15, 10);
break;
}
consumer3Messages++;
uniqueMessages.add(new String(msg.getData()));
consumer3.acknowledge(msg);
}
Assert.assertEquals(uniqueMessages.size(), numMsgs);
Assert.assertEquals(disp0.getActiveConsumer().consumerName(), "1");
Assert.assertEquals(disp1.getActiveConsumer().consumerName(), "2");
Assert.assertEquals(disp2.getActiveConsumer().consumerName(), "3");
Assert.assertEquals(disp3.getActiveConsumer().consumerName(), "1");
uniqueMessages.clear();
for (int i = 0; i < numMsgs; i++) {
String message = "my-message-" + i;
futures.add(producer.sendAsync(message.getBytes()));
}
FutureUtil.waitForAll(futures).get();
futures.clear();
// remove a consumer
for (int i = 0; i < 10; i++) {
msg = consumer1.receive(1, TimeUnit.SECONDS);
Assert.assertNotNull(msg);
uniqueMessages.add(new String(msg.getData()));
consumer1.acknowledge(msg);
}
consumer1.close();
Thread.sleep(CONSUMER_ADD_OR_REMOVE_WAIT_TIME);
consumer2Messages = 0;
while (true) {
msg = consumer2.receive(1, TimeUnit.SECONDS);
if (msg == null) {
Assert.assertEquals(consumer2Messages, 70, 5);
break;
}
consumer2Messages++;
uniqueMessages.add(new String(msg.getData()));
consumer2.acknowledge(msg);
}
consumer3Messages = 0;
while (true) {
msg = consumer3.receive(1, TimeUnit.SECONDS);
if (msg == null) {
Assert.assertEquals(consumer3Messages, 70, 5);
break;
}
consumer3Messages++;
uniqueMessages.add(new String(msg.getData()));
consumer3.acknowledge(msg);
}
Assert.assertEquals(uniqueMessages.size(), numMsgs);
Assert.assertEquals(disp0.getActiveConsumer().consumerName(), "2");
Assert.assertEquals(disp1.getActiveConsumer().consumerName(), "3");
Assert.assertEquals(disp2.getActiveConsumer().consumerName(), "2");
Assert.assertEquals(disp3.getActiveConsumer().consumerName(), "3");
producer.close();
consumer2.close();
consumer3.unsubscribe();
admin.persistentTopics().deletePartitionedTopic(topicName);
}
use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.
the class PersistentTopicTest method testCreateTopic.
@Test
public void testCreateTopic() throws Exception {
final ManagedLedger ledgerMock = mock(ManagedLedger.class);
doReturn(new ArrayList<Object>()).when(ledgerMock).getCursors();
final String topicName = "persistent://prop/use/ns-abc/topic1";
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
((OpenLedgerCallback) invocationOnMock.getArguments()[2]).openLedgerComplete(ledgerMock, null);
return null;
}
}).when(mlFactoryMock).asyncOpen(anyString(), any(ManagedLedgerConfig.class), any(OpenLedgerCallback.class), anyObject());
CompletableFuture<Void> future = brokerService.getTopic(topicName).thenAccept(topic -> {
assertTrue(topic.toString().contains(topicName));
}).exceptionally((t) -> {
fail("should not fail");
return null;
});
// wait for completion
try {
future.get(1, TimeUnit.SECONDS);
} catch (Exception e) {
fail("Should not fail or time out");
}
}
use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.
the class NonPersistentTopicTest method testPartitionedNonPersistentTopicWithTcpLookup.
@Test(dataProvider = "subscriptionType")
public void testPartitionedNonPersistentTopicWithTcpLookup(SubscriptionType type) throws Exception {
log.info("-- Starting {} test --", methodName);
final int numPartitions = 5;
final String topic = "non-persistent://my-property/use/my-ns/partitioned-topic";
admin.nonPersistentTopics().createPartitionedTopic(topic, numPartitions);
PulsarClient client = PulsarClient.builder().serviceUrl("pulsar://localhost:" + BROKER_PORT).statsInterval(0, TimeUnit.SECONDS).build();
Consumer<byte[]> consumer = client.newConsumer().topic(topic).subscriptionName("subscriber-1").subscriptionType(type).subscribe();
Producer<byte[]> producer = pulsarClient.newProducer().topic(topic).create();
// Ensure all partitions exist
for (int i = 0; i < numPartitions; i++) {
TopicName partition = TopicName.get(topic).getPartition(i);
assertNotNull(pulsar.getBrokerService().getTopicReference(partition.toString()));
}
int totalProduceMsg = 500;
for (int i = 0; i < totalProduceMsg; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
Thread.sleep(10);
}
Message<?> msg = null;
Set<String> messageSet = Sets.newHashSet();
for (int i = 0; i < totalProduceMsg; i++) {
msg = consumer.receive(1, TimeUnit.SECONDS);
if (msg != null) {
consumer.acknowledge(msg);
String receivedMessage = new String(msg.getData());
log.debug("Received message: [{}]", receivedMessage);
String expectedMessage = "my-message-" + i;
testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
} else {
break;
}
}
assertEquals(messageSet.size(), totalProduceMsg);
producer.close();
consumer.close();
log.info("-- Exiting {} test --", methodName);
client.close();
}
use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.
the class PartitionedProducerConsumerTest method testSinglePartitionProducer.
@Test(timeOut = 30000)
public void testSinglePartitionProducer() throws Exception {
log.info("-- Starting {} test --", methodName);
int numPartitions = 4;
TopicName topicName = TopicName.get("persistent://my-property/use/my-ns/my-partitionedtopic2");
admin.persistentTopics().createPartitionedTopic(topicName.toString(), numPartitions);
Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName.toString()).messageRoutingMode(MessageRoutingMode.SinglePartition).create();
Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName.toString()).subscriptionName("my-partitioned-subscriber").subscribe();
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
Message<byte[]> msg = null;
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);
String expectedMessage = "my-message-" + i;
testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
}
producer.close();
consumer.unsubscribe();
consumer.close();
admin.persistentTopics().deletePartitionedTopic(topicName.toString());
log.info("-- Exiting {} test --", methodName);
}
use of org.apache.pulsar.common.naming.TopicName in project incubator-pulsar by apache.
the class PartitionedProducerConsumerTest method testSillyUser.
@Test(timeOut = 30000)
public void testSillyUser() throws Exception {
int numPartitions = 4;
TopicName topicName = TopicName.get("persistent://my-property/use/my-ns/my-partitionedtopic5");
admin.persistentTopics().createPartitionedTopic(topicName.toString(), numPartitions);
Producer<byte[]> producer = null;
Consumer<byte[]> consumer = null;
try {
pulsarClient.newProducer().messageRouter(null);
Assert.fail("should fail");
} catch (NullPointerException e) {
// ok
}
try {
pulsarClient.newProducer().messageRoutingMode(null);
Assert.fail("should fail");
} catch (NullPointerException e) {
// ok
}
try {
producer = pulsarClient.newProducer().topic(topicName.toString()).create();
consumer = pulsarClient.newConsumer().topic(topicName.toString()).subscriptionName("my-sub").subscribe();
producer.send("message1".getBytes());
producer.send("message2".getBytes());
/* Message<byte[]> msg1 = */
consumer.receive();
Message<byte[]> msg2 = consumer.receive();
consumer.acknowledgeCumulative(msg2);
Assert.fail("should fail since ack cumulative is not supported for partitioned topic");
} catch (PulsarClientException e) {
Assert.assertTrue(e instanceof PulsarClientException.NotSupportedException);
} finally {
producer.close();
consumer.unsubscribe();
consumer.close();
}
admin.persistentTopics().deletePartitionedTopic(topicName.toString());
}
Aggregations