use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.
the class PersistentTopicTest method testMaxProducers.
public void testMaxProducers() throws Exception {
PersistentTopic topic = new PersistentTopic(successTopicName, ledgerMock, brokerService);
String role = "appid1";
// 1. add producer1
Producer producer = new Producer(topic, serverCnx, 1, /* producer id */
"prod-name1", role, false, null, SchemaVersion.Latest);
topic.addProducer(producer);
assertEquals(topic.getProducers().size(), 1);
// 2. add producer2
Producer producer2 = new Producer(topic, serverCnx, 2, /* producer id */
"prod-name2", role, false, null, SchemaVersion.Latest);
topic.addProducer(producer2);
assertEquals(topic.getProducers().size(), 2);
// 3. add producer3 but reached maxProducersPerTopic
try {
Producer producer3 = new Producer(topic, serverCnx, 3, /* producer id */
"prod-name3", role, false, null, SchemaVersion.Latest);
topic.addProducer(producer3);
fail("should have failed");
} catch (BrokerServiceException e) {
assertTrue(e instanceof BrokerServiceException.ProducerBusyException);
}
}
use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.
the class PersistentTopicTest method testMaxConsumersFailover.
public void testMaxConsumersFailover() throws Exception {
PersistentTopic topic = new PersistentTopic(successTopicName, ledgerMock, brokerService);
PersistentSubscription sub = new PersistentSubscription(topic, "sub-1", cursorMock);
PersistentSubscription sub2 = new PersistentSubscription(topic, "sub-2", cursorMock);
// for count consumers on topic
ConcurrentOpenHashMap<String, PersistentSubscription> subscriptions = new ConcurrentOpenHashMap<>(16, 1);
subscriptions.put("sub-1", sub);
subscriptions.put("sub-2", sub2);
Field field = topic.getClass().getDeclaredField("subscriptions");
field.setAccessible(true);
field.set(topic, subscriptions);
// 1. add consumer1
Consumer consumer = new Consumer(sub, SubType.Failover, topic.getName(), 1, /* consumer id */
0, "Cons1", /* consumer name */
50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
InitialPosition.Latest);
sub.addConsumer(consumer);
assertEquals(sub.getConsumers().size(), 1);
// 2. add consumer2
Consumer consumer2 = new Consumer(sub, SubType.Failover, topic.getName(), 2, /* consumer id */
0, "Cons2", /* consumer name */
50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
InitialPosition.Latest);
sub.addConsumer(consumer2);
assertEquals(sub.getConsumers().size(), 2);
// 3. add consumer3 but reach maxConsumersPerSubscription
try {
Consumer consumer3 = new Consumer(sub, SubType.Failover, topic.getName(), 3, /* consumer id */
0, "Cons3", /* consumer name */
50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
InitialPosition.Latest);
sub.addConsumer(consumer3);
fail("should have failed");
} catch (BrokerServiceException e) {
assertTrue(e instanceof BrokerServiceException.ConsumerBusyException);
}
// check number of consumers on topic
assertEquals(topic.getNumberOfConsumers(), 2);
// 4. add consumer4 to sub2
Consumer consumer4 = new Consumer(sub2, SubType.Failover, topic.getName(), 4, /* consumer id */
0, "Cons4", /* consumer name */
50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
InitialPosition.Latest);
sub2.addConsumer(consumer4);
assertEquals(sub2.getConsumers().size(), 1);
// check number of consumers on topic
assertEquals(topic.getNumberOfConsumers(), 3);
// 5. add consumer5 to sub2 but reach maxConsumersPerTopic
try {
Consumer consumer5 = new Consumer(sub2, SubType.Failover, topic.getName(), 5, /* consumer id */
0, "Cons5", /* consumer name */
50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
InitialPosition.Latest);
sub2.addConsumer(consumer5);
fail("should have failed");
} catch (BrokerServiceException e) {
assertTrue(e instanceof BrokerServiceException.ConsumerBusyException);
}
}
use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.
the class PersistentTopicTest method testDeleteAndUnsubscribeTopic.
@Test
public void testDeleteAndUnsubscribeTopic() throws Exception {
// create topic
final PersistentTopic topic = (PersistentTopic) brokerService.getTopic(successTopicName).get();
CommandSubscribe cmd = CommandSubscribe.newBuilder().setConsumerId(1).setTopic(successTopicName).setSubscription(successSubName).setRequestId(1).setSubType(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() {
@Override
public void run() {
try {
barrier.await();
assertFalse(topic.delete().isCompletedExceptionally());
} catch (Exception e) {
e.printStackTrace();
gotException.set(true);
} finally {
counter.countDown();
}
}
};
Thread unsubscriber = new Thread() {
@Override
public void run() {
try {
barrier.await();
// do topic unsubscribe
topic.unsubscribe(successSubName);
} 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 PersistentTopicTest method testClosingReplicationProducerTwice.
@SuppressWarnings("unchecked")
@Test
public void testClosingReplicationProducerTwice() throws Exception {
final String globalTopicName = "persistent://prop/global/ns/testClosingReplicationProducerTwice";
String localCluster = "local";
String remoteCluster = "remote";
final ManagedLedger ledgerMock = mock(ManagedLedger.class);
doNothing().when(ledgerMock).asyncDeleteCursor(anyObject(), anyObject(), anyObject());
doReturn(new ArrayList<Object>()).when(ledgerMock).getCursors();
PersistentTopic topic = new PersistentTopic(globalTopicName, ledgerMock, brokerService);
final URL brokerUrl = new URL("http://" + pulsar.getAdvertisedAddress() + ":" + pulsar.getConfiguration().getBrokerServicePort());
PulsarClient client = spy(PulsarClient.builder().serviceUrl(brokerUrl.toString()).build());
PulsarClientImpl clientImpl = (PulsarClientImpl) client;
doReturn(new CompletableFuture<Producer>()).when(clientImpl).createProducerAsync(any(ProducerConfigurationData.class), any(Schema.class));
ManagedCursor cursor = mock(ManagedCursorImpl.class);
doReturn(remoteCluster).when(cursor).getName();
brokerService.getReplicationClients().put(remoteCluster, client);
PersistentReplicator replicator = new PersistentReplicator(topic, cursor, localCluster, remoteCluster, brokerService);
// PersistentReplicator constructor calls startProducer()
verify(clientImpl).createProducerAsync(any(ProducerConfigurationData.class), any(Schema.class));
replicator.disconnect(false);
replicator.disconnect(false);
replicator.startProducer();
verify(clientImpl, Mockito.times(2)).createProducerAsync(any(ProducerConfigurationData.class), any(Schema.class));
}
use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.
the class PersistentTopicTest method testUbsubscribeRaceConditions.
@Test
public void testUbsubscribeRaceConditions() throws Exception {
PersistentTopic topic = new PersistentTopic(successTopicName, ledgerMock, brokerService);
PersistentSubscription sub = new PersistentSubscription(topic, "sub-1", cursorMock);
Consumer consumer1 = new Consumer(sub, SubType.Exclusive, topic.getName(), 1, /* consumer id */
0, "Cons1", /* consumer name */
50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
InitialPosition.Latest);
sub.addConsumer(consumer1);
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
((DeleteCursorCallback) invocationOnMock.getArguments()[1]).deleteCursorComplete(null);
Thread.sleep(1000);
return null;
}
}).when(ledgerMock).asyncDeleteCursor(matches(".*success.*"), any(DeleteCursorCallback.class), anyObject());
ExecutorService executor = Executors.newCachedThreadPool();
executor.submit(() -> {
sub.doUnsubscribe(consumer1);
return null;
}).get();
try {
Thread.sleep(10);
/* delay to ensure that the ubsubscribe gets executed first */
new Consumer(sub, SubType.Exclusive, topic.getName(), 2, /* consumer id */
0, "Cons2", /* consumer name */
50000, serverCnx, "myrole-1", Collections.emptyMap(), false, /* read compacted */
InitialPosition.Latest);
} catch (BrokerServiceException e) {
assertTrue(e instanceof BrokerServiceException.SubscriptionFencedException);
}
}
Aggregations