use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.
the class PersistentQueueE2ETest method testSharedSingleAckedNormalTopic.
@Test(timeOut = 300000)
public void testSharedSingleAckedNormalTopic() throws Exception {
String key = "test1";
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 = 50;
// 1. producer connect
Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
assertNotNull(topicRef);
assertEquals(topicRef.getProducers().size(), 1);
// 2. Create consumer
ConsumerBuilder<byte[]> consumerBuilder = pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).receiverQueueSize(10).subscriptionType(SubscriptionType.Shared);
Consumer<byte[]> consumer1 = consumerBuilder.subscribe();
Consumer<byte[]> consumer2 = consumerBuilder.subscribe();
// 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<byte[]> message1 = consumer1.receive();
Message<byte[]> 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(10000, TimeUnit.MILLISECONDS);
message2 = consumer2.receive(10000, TimeUnit.MILLISECONDS);
} while (message1 != null || message2 != null);
log.info("Total receives = " + (receivedConsumer2 + receivedConsumer1));
assertEquals(receivedConsumer2 + receivedConsumer1, totalMessages);
// 5. Close Consumer 1
log.info("Consumer 1 closed");
consumer1.close();
// 6. Consumer 1's unAcked messages should be sent to Consumer 2
for (int i = 0; i < totalMessages; i++) {
message2 = consumer2.receive(100, TimeUnit.MILLISECONDS);
if (message2 == null) {
log.info("Consumer 2 - No Message in Incoming Message Queue, will try again");
continue;
}
log.info("Consumer 2 Received: " + new String(message2.getData()));
receivedConsumer2 += 1;
}
log.info("Total receives by Consumer 2 = " + receivedConsumer2);
assertEquals(receivedConsumer2, totalMessages);
}
use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.
the class PersistentTopicConcurrentTest method testConcurrentTopicAndSubscriptionDelete.
// @Test
public void testConcurrentTopicAndSubscriptionDelete() throws Exception {
// create topic
final PersistentTopic topic = (PersistentTopic) brokerService.getTopic(successTopicName).get();
PulsarApi.CommandSubscribe cmd = PulsarApi.CommandSubscribe.newBuilder().setConsumerId(1).setTopic(successTopicName).setSubscription(successSubName).setRequestId(1).setSubType(PulsarApi.CommandSubscribe.SubType.Exclusive).build();
Future<Consumer> f1 = topic.subscribe(serverCnx, cmd.getSubscription(), cmd.getConsumerId(), cmd.getSubType(), 0, cmd.getConsumerName(), cmd.getDurable(), null, Collections.emptyMap(), cmd.getReadCompacted(), InitialPosition.Latest);
f1.get();
final CyclicBarrier barrier = new CyclicBarrier(2);
final CountDownLatch counter = new CountDownLatch(2);
final AtomicBoolean gotException = new AtomicBoolean(false);
Thread deleter = new Thread() {
public void run() {
try {
barrier.await();
// assertTrue(topic.unsubscribe(successSubName).isDone());
Thread.sleep(5, 0);
log.info("deleter outcome is {}", topic.delete().get());
} catch (Exception e) {
e.printStackTrace();
gotException.set(true);
} finally {
counter.countDown();
}
}
};
Thread unsubscriber = new Thread() {
public void run() {
try {
barrier.await();
// do subscription delete
ConcurrentOpenHashMap<String, PersistentSubscription> subscriptions = topic.getSubscriptions();
PersistentSubscription ps = subscriptions.get(successSubName);
// Thread.sleep(2,0);
log.info("unsubscriber outcome is {}", ps.doUnsubscribe(ps.getConsumers().get(0)).get());
// assertFalse(ps.delete().isCompletedExceptionally());
} catch (Exception e) {
e.printStackTrace();
gotException.set(true);
} finally {
counter.countDown();
}
}
};
deleter.start();
unsubscriber.start();
counter.await();
assertEquals(gotException.get(), false);
}
use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.
the class PersistentTopicE2ETest method testTopicDeleteWithDisconnectedSubscription.
@Test
public void testTopicDeleteWithDisconnectedSubscription() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/topic8";
final String subName = "sub1";
// 1. client connect
Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
PersistentSubscription subRef = topicRef.getSubscription(subName);
assertNotNull(topicRef);
assertNotNull(subRef);
assertTrue(subRef.getDispatcher().isConsumerConnected());
// 2. client disconnect
consumer.close();
assertFalse(subRef.getDispatcher().isConsumerConnected());
// 3. delete topic
admin.persistentTopics().delete(topicName);
try {
admin.persistentTopics().getStats(topicName);
} catch (PulsarAdminException e) {
// ok
}
}
use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.
the class PersistentTopicE2ETest method testSubscriptionTypeTransitions.
@Test
public void testSubscriptionTypeTransitions() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/shared-topic2";
final String subName = "sub2";
Consumer<byte[]> consumer1 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscriptionType(SubscriptionType.Exclusive).subscribe();
Consumer<byte[]> consumer2 = null;
Consumer<byte[]> consumer3 = null;
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
PersistentSubscription subRef = topicRef.getSubscription(subName);
// 1. shared consumer on an exclusive sub fails
try {
consumer2 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscriptionType(SubscriptionType.Shared).subscribe();
fail("should have failed");
} catch (PulsarClientException e) {
assertTrue(e.getMessage().contains("Subscription is of different type"));
}
// 2. failover consumer on an exclusive sub fails
try {
consumer3 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscriptionType(SubscriptionType.Failover).subscribe();
fail("should have failed");
} catch (PulsarClientException e) {
assertTrue(e.getMessage().contains("Subscription is of different type"));
}
// 3. disconnected sub can be converted in shared
consumer1.close();
try {
consumer2 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscriptionType(SubscriptionType.Shared).subscribe();
assertEquals(subRef.getDispatcher().getType(), SubType.Shared);
} catch (PulsarClientException e) {
fail("should not fail");
}
// 4. exclusive fails on shared sub
try {
consumer1 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscriptionType(SubscriptionType.Exclusive).subscribe();
fail("should have failed");
} catch (PulsarClientException e) {
assertTrue(e.getMessage().contains("Subscription is of different type"));
}
// 5. disconnected sub can be converted in failover
consumer2.close();
try {
consumer3 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscriptionType(SubscriptionType.Failover).subscribe();
assertEquals(subRef.getDispatcher().getType(), SubType.Failover);
} catch (PulsarClientException e) {
fail("should not fail");
}
// 5. exclusive consumer can connect after failover disconnects
consumer3.close();
try {
consumer1 = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscriptionType(SubscriptionType.Exclusive).subscribe();
assertEquals(subRef.getDispatcher().getType(), SubType.Exclusive);
} catch (PulsarClientException e) {
fail("should not fail");
}
consumer1.close();
admin.persistentTopics().delete(topicName);
}
use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.
the class PersistentTopicE2ETest method testMessageExpiryWithFewExpiredBacklog.
@Test
public void testMessageExpiryWithFewExpiredBacklog() throws Exception {
int messageTTLSecs = 10;
String namespaceName = "prop/use/expiry-check-1";
admin.namespaces().createNamespace(namespaceName);
admin.namespaces().setNamespaceMessageTTL(namespaceName, messageTTLSecs);
final String topicName = "persistent://prop/use/expiry-check-1/topic1";
final String subName = "sub1";
final int numMsgs = 10;
pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe();
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
PersistentSubscription subRef = topicRef.getSubscription(subName);
assertTrue(subRef.getDispatcher().isConsumerConnected());
Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
for (int i = 0; i < numMsgs; i++) {
String message = "my-message-" + i;
producer.send(message.getBytes());
}
rolloverPerIntervalStats();
assertEquals(subRef.getNumberOfEntriesInBacklog(), numMsgs);
Thread.sleep(TimeUnit.SECONDS.toMillis(messageTTLSecs));
runMessageExpiryCheck();
assertEquals(subRef.getNumberOfEntriesInBacklog(), numMsgs);
Thread.sleep(TimeUnit.SECONDS.toMillis(messageTTLSecs / 2));
runMessageExpiryCheck();
assertEquals(subRef.getNumberOfEntriesInBacklog(), 0);
}
Aggregations