use of com.yahoo.pulsar.broker.service.persistent.PersistentTopic in project pulsar by yahoo.
the class ResendRequestTest method testFailoverInactiveConsumer.
@Test(timeOut = testTimeout)
public void testFailoverInactiveConsumer() throws Exception {
String key = "testFailoverInactiveConsumer";
final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
final String subscriptionName = "my-failover-subscription-" + key;
final String messagePredicate = "my-message-" + key + "-";
final int totalMessages = 10;
// 1. producer connect
Producer producer = pulsarClient.createProducer(topicName);
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
assertNotNull(topicRef);
assertEquals(topicRef.getProducers().size(), 1);
// 2. Create consumer
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setReceiverQueueSize(10);
conf.setSubscriptionType(SubscriptionType.Failover);
conf.setConsumerName("consumer-1");
Consumer consumer1 = pulsarClient.subscribe(topicName, subscriptionName, conf);
conf.setConsumerName("consumer-2");
Consumer consumer2 = pulsarClient.subscribe(topicName, subscriptionName, conf);
// 3. Producer publishes messages
for (int i = 0; i < totalMessages; i++) {
String message = messagePredicate + i;
producer.send(message.getBytes());
log.info("Producer produced " + message);
}
// 4. Receive messages
int receivedConsumer1 = 0, receivedConsumer2 = 0;
Message message1;
Message message2;
do {
message1 = consumer1.receive(500, TimeUnit.MILLISECONDS);
if (message1 != null) {
log.info("Consumer 1 Received: " + new String(message1.getData()));
receivedConsumer1 += 1;
}
} while (message1 != null);
log.info("Consumer 1 receives = " + receivedConsumer1);
log.info("Consumer 2 receives = " + receivedConsumer2);
log.info("Total receives = " + (receivedConsumer2 + receivedConsumer1));
assertEquals(receivedConsumer2 + receivedConsumer1, totalMessages);
// Consumer 2 is on Stand By
assertEquals(receivedConsumer2, 0);
// 5. Consumer 2 asks for a redelivery but the request is ignored
log.info("Consumer 2 asks for resend");
consumer2.redeliverUnacknowledgedMessages();
Thread.sleep(1000);
message1 = consumer1.receive(500, TimeUnit.MILLISECONDS);
message2 = consumer2.receive(500, TimeUnit.MILLISECONDS);
assertEquals(message1, null);
assertEquals(message2, null);
}
use of com.yahoo.pulsar.broker.service.persistent.PersistentTopic in project pulsar by yahoo.
the class ResendRequestTest method testExclusiveSingleAckedNormalTopic.
@Test(timeOut = testTimeout)
public void testExclusiveSingleAckedNormalTopic() throws Exception {
String key = "testExclusiveSingleAckedNormalTopic";
final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
final String subscriptionName = "my-ex-subscription-" + key;
final String messagePredicate = "my-message-" + key + "-";
final int totalMessages = 10;
HashSet<MessageId> messageIdHashSet = new HashSet<MessageId>();
HashSet<String> messageDataHashSet = new HashSet<String>();
// 1. producer connect
Producer producer = pulsarClient.createProducer(topicName);
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
assertNotNull(topicRef);
assertEquals(topicRef.getProducers().size(), 1);
// 2. Create consumer
ConsumerConfiguration conf = new ConsumerConfiguration();
conf.setReceiverQueueSize(7);
Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName, conf);
// 3. producer publish messages
for (int i = 0; i < totalMessages; i++) {
String message = messagePredicate + i;
producer.send(message.getBytes());
}
// 4. Receive messages
Message message = consumer.receive();
log.info("Message received " + new String(message.getData()));
for (int i = 1; i < totalMessages; i++) {
Message msg = consumer.receive();
log.info("Message received " + new String(msg.getData()));
messageDataHashSet.add(new String(msg.getData()));
}
printIncomingMessageQueue(consumer);
// 5. Ack 1st message and ask for resend
consumer.acknowledge(message);
log.info("Message acked " + new String(message.getData()));
messageIdHashSet.add(message.getMessageId());
messageDataHashSet.add(new String(message.getData()));
consumer.redeliverUnacknowledgedMessages();
log.info("Resend Messages Request sent");
// 6. Check if messages resent in correct order
for (int i = 0; i < totalMessages - 1; i++) {
message = consumer.receive();
log.info("Message received " + new String(message.getData()));
if (i < 2) {
messageIdHashSet.add(message.getMessageId());
consumer.acknowledge(message);
}
log.info("Message acked " + new String(message.getData()));
assertTrue(messageDataHashSet.contains(new String(message.getData())));
}
assertEquals(messageIdHashSet.size(), 3);
assertEquals(messageDataHashSet.size(), totalMessages);
printIncomingMessageQueue(consumer);
// 7. Request resend 2nd time - you should receive 4 messages
consumer.redeliverUnacknowledgedMessages();
log.info("Resend Messages Request sent");
message = consumer.receive(2000, TimeUnit.MILLISECONDS);
while (message != null) {
log.info("Message received " + new String(message.getData()));
consumer.acknowledge(message);
log.info("Message acked " + new String(message.getData()));
messageIdHashSet.add(message.getMessageId());
messageDataHashSet.add(new String(message.getData()));
message = consumer.receive(5000, TimeUnit.MILLISECONDS);
}
assertEquals(messageIdHashSet.size(), totalMessages);
assertEquals(messageDataHashSet.size(), totalMessages);
printIncomingMessageQueue(consumer);
// 9. Calling resend after acking all messages - expectin 0 messages
consumer.redeliverUnacknowledgedMessages();
assertEquals(consumer.receive(2000, TimeUnit.MILLISECONDS), null);
// 10. Checking message contents
for (int i = 0; i < totalMessages; i++) {
assertTrue(messageDataHashSet.contains(messagePredicate + i));
}
}
use of com.yahoo.pulsar.broker.service.persistent.PersistentTopic in project pulsar by yahoo.
the class ServerCnxTest method testProducerCommand.
@Test(timeOut = 30000)
public void testProducerCommand() throws Exception {
resetChannel();
setChannelConnected();
// test PRODUCER success case
ByteBuf clientCommand = Commands.newProducer(successTopicName, 1, /* producer id */
1, /* request id */
"prod-name");
channel.writeInbound(clientCommand);
assertTrue(getResponse() instanceof CommandProducerSuccess);
PersistentTopic topicRef = (PersistentTopic) brokerService.getTopicReference(successTopicName);
assertNotNull(topicRef);
assertEquals(topicRef.getProducers().size(), 1);
// test PRODUCER error case
clientCommand = Commands.newProducer(failTopicName, 2, 2, "prod-name-2");
channel.writeInbound(clientCommand);
assertTrue(getResponse() instanceof CommandError);
assertNull(brokerService.getTopicReference(failTopicName));
channel.finish();
assertEquals(topicRef.getProducers().size(), 0);
}
use of com.yahoo.pulsar.broker.service.persistent.PersistentTopic in project pulsar by yahoo.
the class ReplicatorTest method testReplicatorProducerClosing.
@Test(priority = 5)
public void testReplicatorProducerClosing() throws Exception {
log.info("--- Starting ReplicatorTest::testDeleteReplicatorFailure ---");
final String topicName = "persistent://pulsar/global/ns/repltopicbatch";
final DestinationName dest = DestinationName.get(topicName);
MessageProducer producer1 = new MessageProducer(url1, dest);
PersistentTopic topic = (PersistentTopic) pulsar1.getBrokerService().getTopicReference(topicName);
final String replicatorClusterName = topic.getReplicators().keys().get(0);
PersistentReplicator replicator = topic.getPersistentReplicator(replicatorClusterName);
pulsar2.close();
pulsar3.close();
replicator.disconnect(false);
Thread.sleep(100);
Field field = PersistentReplicator.class.getDeclaredField("producer");
field.setAccessible(true);
ProducerImpl producer = (ProducerImpl) field.get(replicator);
assertNull(producer);
}
use of com.yahoo.pulsar.broker.service.persistent.PersistentTopic in project pulsar by yahoo.
the class ReplicatorTest method testReplicatePeekAndSkip.
@Test
public void testReplicatePeekAndSkip() throws Exception {
SortedSet<String> testDests = new TreeSet<String>();
final DestinationName dest = DestinationName.get("persistent://pulsar/global/ns/peekAndSeekTopic");
testDests.add(dest.toString());
MessageProducer producer1 = new MessageProducer(url1, dest);
MessageConsumer consumer1 = new MessageConsumer(url3, dest);
// Produce from cluster1 and consume from the rest
producer1.produce(2);
producer1.close();
PersistentTopic topic = (PersistentTopic) pulsar1.getBrokerService().getTopicReference(dest.toString());
PersistentReplicator replicator = topic.getReplicators().get(topic.getReplicators().keys().get(0));
replicator.skipMessages(2);
CompletableFuture<Entry> result = replicator.peekNthMessage(1);
Entry entry = result.get(50, TimeUnit.MILLISECONDS);
assertNull(entry);
consumer1.close();
}
Aggregations