use of com.yahoo.pulsar.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class ResendRequestTest method testSharedSingleAckedNormalTopic.
@Test(timeOut = testTimeout)
public void testSharedSingleAckedNormalTopic() throws Exception {
String key = "testSharedSingleAckedNormalTopic";
final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
final String subscriptionName = "my-shared-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(totalMessages / 2);
conf.setSubscriptionType(SubscriptionType.Shared);
Consumer consumer1 = pulsarClient.subscribe(topicName, subscriptionName, conf);
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 = consumer1.receive();
Message message2 = consumer2.receive();
do {
if (message1 != null) {
log.info("Consumer 1 Received: " + new String(message1.getData()));
receivedConsumer1 += 1;
}
if (message2 != null) {
log.info("Consumer 2 Received: " + new String(message2.getData()));
receivedConsumer2 += 1;
}
message1 = consumer1.receive(100, TimeUnit.MILLISECONDS);
message2 = consumer2.receive(100, TimeUnit.MILLISECONDS);
} while (message1 != null || message2 != null);
log.info("Consumer 1 receives = " + receivedConsumer1);
log.info("Consumer 2 receives = " + receivedConsumer2);
log.info("Total receives = " + (receivedConsumer2 + receivedConsumer1));
assertEquals(receivedConsumer2 + receivedConsumer1, totalMessages);
// 5. Send a resend request from Consumer 1
log.info("Consumer 1 sent a resend request");
consumer1.redeliverUnacknowledgedMessages();
// 6. Consumer 1's unAcked messages should be sent to Consumer 1 or 2
int receivedMessagesAfterRedelivery = 0;
receivedConsumer1 = 0;
message1 = consumer1.receive(100, TimeUnit.MILLISECONDS);
message2 = consumer2.receive(100, TimeUnit.MILLISECONDS);
do {
if (message1 != null) {
log.info("Consumer 1 Received: " + new String(message1.getData()));
receivedConsumer1 += 1;
++receivedMessagesAfterRedelivery;
}
if (message2 != null) {
log.info("Consumer 2 Received: " + new String(message2.getData()));
receivedConsumer2 += 1;
++receivedMessagesAfterRedelivery;
}
message1 = consumer1.receive(200, TimeUnit.MILLISECONDS);
message2 = consumer2.receive(200, TimeUnit.MILLISECONDS);
} while (message1 != null || message2 != null);
log.info("Additional received = " + receivedMessagesAfterRedelivery);
assertTrue(receivedMessagesAfterRedelivery > 0);
assertEquals(receivedConsumer1 + receivedConsumer2, totalMessages);
}
use of com.yahoo.pulsar.client.api.ConsumerConfiguration 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.client.api.ConsumerConfiguration 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.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class BrokerClientIntegrationTest method testCloseBrokerService.
/**
* Verifies: 1. Closing of Broker service unloads all bundle gracefully and there must not be any connected bundles
* after closing broker service
*
* @throws Exception
*/
@Test
public void testCloseBrokerService() throws Exception {
final String ns1 = "my-property/use/brok-ns1";
final String ns2 = "my-property/use/brok-ns2";
admin.namespaces().createNamespace(ns1);
admin.namespaces().createNamespace(ns2);
final String dn1 = "persistent://" + ns1 + "/my-topic";
final String dn2 = "persistent://" + ns2 + "/my-topic";
ConsumerImpl consumer1 = (ConsumerImpl) pulsarClient.subscribe(dn1, "my-subscriber-name", new ConsumerConfiguration());
ProducerImpl producer1 = (ProducerImpl) pulsarClient.createProducer(dn1, new ProducerConfiguration());
ProducerImpl producer2 = (ProducerImpl) pulsarClient.createProducer(dn2, new ProducerConfiguration());
// unload all other namespace
pulsar.getBrokerService().close();
// [1] OwnershipCache should not contain any more namespaces
OwnershipCache ownershipCache = pulsar.getNamespaceService().getOwnershipCache();
assertTrue(ownershipCache.getOwnedBundles().keySet().isEmpty());
// [2] All clients must be disconnected and in connecting state
// producer1 must not be able to connect again
assertTrue(producer1.getClientCnx() == null);
assertTrue(producer1.getState().equals(State.Connecting));
// consumer1 must not be able to connect again
assertTrue(consumer1.getClientCnx() == null);
assertTrue(consumer1.getState().equals(State.Connecting));
// producer2 must not be able to connect again
assertTrue(producer2.getClientCnx() == null);
assertTrue(producer2.getState().equals(State.Connecting));
producer1.close();
producer2.close();
consumer1.close();
}
use of com.yahoo.pulsar.client.api.ConsumerConfiguration in project pulsar by yahoo.
the class BrokerClientIntegrationTest method testResetCursor.
@Test(timeOut = 10000, dataProvider = "subType")
public void testResetCursor(SubscriptionType subType) throws Exception {
final RetentionPolicies policy = new RetentionPolicies(60, 52 * 1024);
final DestinationName destName = DestinationName.get("persistent://my-property/use/my-ns/unacked-topic");
final int warmup = 20;
final int testSize = 150;
final List<Message> received = new ArrayList<Message>();
final ConsumerConfiguration consConfig = new ConsumerConfiguration();
final String subsId = "sub";
final NavigableMap<Long, TimestampEntryCount> publishTimeIdMap = new ConcurrentSkipListMap<>();
consConfig.setSubscriptionType(subType);
consConfig.setMessageListener((MessageListener) (Consumer consumer, Message msg) -> {
try {
synchronized (received) {
received.add(msg);
}
consumer.acknowledge(msg);
long publishTime = ((MessageImpl) msg).getPublishTime();
log.info(" publish time is " + publishTime + "," + msg.getMessageId());
TimestampEntryCount timestampEntryCount = publishTimeIdMap.computeIfAbsent(publishTime, (k) -> new TimestampEntryCount(publishTime));
timestampEntryCount.incrementAndGet();
} catch (final PulsarClientException e) {
log.warn("Failed to ack!");
}
});
admin.namespaces().setRetention(destName.getNamespace(), policy);
Consumer consumer = pulsarClient.subscribe(destName.toString(), subsId, consConfig);
final Producer producer = pulsarClient.createProducer(destName.toString());
log.info("warm up started for " + destName.toString());
// send warmup msgs
byte[] msgBytes = new byte[1000];
for (Integer i = 0; i < warmup; i++) {
producer.send(msgBytes);
}
log.info("warm up finished.");
// sleep to ensure receiving of msgs
for (int n = 0; n < 10 && received.size() < warmup; n++) {
Thread.sleep(100);
}
// validate received msgs
Assert.assertEquals(received.size(), warmup);
received.clear();
// publish testSize num of msgs
log.info("Sending more messages.");
for (Integer n = 0; n < testSize; n++) {
producer.send(msgBytes);
Thread.sleep(1);
}
log.info("Sending more messages done.");
Thread.sleep(3000);
long begints = publishTimeIdMap.firstEntry().getKey();
long endts = publishTimeIdMap.lastEntry().getKey();
// find reset timestamp
long timestamp = (endts - begints) / 2 + begints;
timestamp = publishTimeIdMap.floorKey(timestamp);
NavigableMap<Long, TimestampEntryCount> expectedMessages = new ConcurrentSkipListMap<>();
expectedMessages.putAll(publishTimeIdMap.tailMap(timestamp, true));
received.clear();
log.info("reset cursor to " + timestamp + " for topic " + destName.toString() + " for subs " + subsId);
log.info("issuing admin operation on " + admin.getServiceUrl().toString());
List<String> subList = admin.persistentTopics().getSubscriptions(destName.toString());
for (String subs : subList) {
log.info("got sub " + subs);
}
publishTimeIdMap.clear();
// reset the cursor to this timestamp
Assert.assertTrue(subList.contains(subsId));
admin.persistentTopics().resetCursor(destName.toString(), subsId, timestamp);
consumer = pulsarClient.subscribe(destName.toString(), subsId, consConfig);
Thread.sleep(3000);
int totalExpected = 0;
for (TimestampEntryCount tec : expectedMessages.values()) {
totalExpected += tec.numMessages;
}
// validate that replay happens after the timestamp
Assert.assertTrue(publishTimeIdMap.firstEntry().getKey() >= timestamp);
consumer.close();
producer.close();
// validate that expected and received counts match
int totalReceived = 0;
for (TimestampEntryCount tec : publishTimeIdMap.values()) {
totalReceived += tec.numMessages;
}
Assert.assertEquals(totalReceived, totalExpected, "did not receive all messages on replay after reset");
}
Aggregations