use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.
the class AntiAffinityNamespaceGroupTest method testLoadSheddingWithAntiAffinityNamespace.
/**
* It verifies that load-manager::shouldAntiAffinityNamespaceUnload checks that unloading should only happen if all
* brokers have same number of anti-affinity namespaces
*
* @throws Exception
*/
@Test
public void testLoadSheddingWithAntiAffinityNamespace() throws Exception {
final String namespace = "my-property/use/my-ns";
final int totalNamespaces = 5;
final String namespaceAntiAffinityGroup = "my-antiaffinity";
final String bundle = "0x00000000_0xffffffff";
admin1.properties().createProperty("my-property", new PropertyAdmin(Lists.newArrayList("appid1", "appid2"), Sets.newHashSet("use")));
for (int i = 0; i < totalNamespaces; i++) {
final String ns = namespace + i;
admin1.namespaces().createNamespace(ns);
admin1.namespaces().setNamespaceAntiAffinityGroup(ns, namespaceAntiAffinityGroup);
}
PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(pulsar1.getWebServiceAddress()).build();
Producer<byte[]> producer = pulsarClient.newProducer().topic("persistent://" + namespace + "0/my-topic1").create();
ModularLoadManagerImpl loadManager = (ModularLoadManagerImpl) ((ModularLoadManagerWrapper) pulsar1.getLoadManager().get()).getLoadManager();
pulsar1.getBrokerService().updateRates();
loadManager.updateAll();
assertTrue(loadManager.shouldAntiAffinityNamespaceUnload(namespace + "0", bundle, primaryHost));
producer.close();
pulsarClient.close();
}
use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.
the class BacklogQuotaManagerTest method testConcurrentAckAndEviction.
@Test
public void testConcurrentAckAndEviction() throws Exception {
assertEquals(admin.namespaces().getBacklogQuotaMap("prop/usc/ns-quota"), Maps.newTreeMap());
admin.namespaces().setBacklogQuota("prop/usc/ns-quota", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.consumer_backlog_eviction));
final String topic1 = "persistent://prop/usc/ns-quota/topic12";
final String subName1 = "c12";
final String subName2 = "c22";
final int numMsgs = 20;
final CyclicBarrier barrier = new CyclicBarrier(2);
final CountDownLatch counter = new CountDownLatch(2);
final AtomicBoolean gotException = new AtomicBoolean(false);
PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
PulsarClient client2 = PulsarClient.builder().serviceUrl(adminUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
Consumer<byte[]> consumer1 = client2.newConsumer().topic(topic1).subscriptionName(subName1).subscribe();
Consumer<byte[]> consumer2 = client2.newConsumer().topic(topic1).subscriptionName(subName2).subscribe();
Thread producerThread = new Thread() {
public void run() {
try {
barrier.await();
org.apache.pulsar.client.api.Producer<byte[]> producer = client.newProducer().topic(topic1).create();
byte[] content = new byte[1024];
for (int i = 0; i < numMsgs; i++) {
producer.send(content);
}
producer.close();
} catch (Exception e) {
gotException.set(true);
} finally {
counter.countDown();
}
}
};
Thread ConsumerThread = new Thread() {
public void run() {
try {
barrier.await();
for (int i = 0; i < numMsgs; i++) {
// only one consumer acknowledges the message
consumer1.acknowledge(consumer1.receive());
consumer2.receive();
}
} catch (Exception e) {
gotException.set(true);
} finally {
counter.countDown();
}
}
};
producerThread.start();
ConsumerThread.start();
// test hangs without timeout since there is nothing to consume due to eviction
counter.await(20, TimeUnit.SECONDS);
assertTrue(!gotException.get());
Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);
rolloverStats();
PersistentTopicStats stats = admin.persistentTopics().getStats(topic1);
Assert.assertTrue(stats.storageSize <= 10 * 1024, "Storage size is [" + stats.storageSize + "]");
client.close();
client2.close();
}
use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.
the class BacklogQuotaManagerTest method testNoEviction.
@Test
public void testNoEviction() throws Exception {
assertEquals(admin.namespaces().getBacklogQuotaMap("prop/usc/ns-quota"), Maps.newTreeMap());
admin.namespaces().setBacklogQuota("prop/usc/ns-quota", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.consumer_backlog_eviction));
final String topic1 = "persistent://prop/usc/ns-quota/topic13";
final String subName1 = "c13";
final String subName2 = "c23";
final int numMsgs = 10;
final CyclicBarrier barrier = new CyclicBarrier(2);
final CountDownLatch counter = new CountDownLatch(2);
final AtomicBoolean gotException = new AtomicBoolean(false);
final PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
final Consumer<byte[]> consumer1 = client.newConsumer().topic(topic1).subscriptionName(subName1).subscribe();
final Consumer<byte[]> consumer2 = client.newConsumer().topic(topic1).subscriptionName(subName2).subscribe();
final PulsarClient client2 = PulsarClient.builder().serviceUrl(adminUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
Thread producerThread = new Thread() {
public void run() {
try {
barrier.await();
org.apache.pulsar.client.api.Producer<byte[]> producer = client2.newProducer().topic(topic1).create();
byte[] content = new byte[1024];
for (int i = 0; i < numMsgs; i++) {
producer.send(content);
}
producer.close();
} catch (Exception e) {
gotException.set(true);
} finally {
counter.countDown();
}
}
};
Thread ConsumerThread = new Thread() {
public void run() {
try {
barrier.await();
for (int i = 0; i < numMsgs; i++) {
consumer1.acknowledge(consumer1.receive());
consumer2.acknowledge(consumer2.receive());
}
} catch (Exception e) {
gotException.set(true);
} finally {
counter.countDown();
}
}
};
producerThread.start();
ConsumerThread.start();
counter.await();
assertTrue(!gotException.get());
client.close();
client2.close();
}
use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.
the class BacklogQuotaManagerTest method testProducerExceptionAndThenUnblock.
@Test
public void testProducerExceptionAndThenUnblock() throws Exception {
assertEquals(admin.namespaces().getBacklogQuotaMap("prop/usc/quotahold"), Maps.newTreeMap());
admin.namespaces().setBacklogQuota("prop/usc/quotahold", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_exception));
final PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
final String topic1 = "persistent://prop/usc/quotahold/exceptandunblock";
final String subName1 = "c1except";
boolean gotException = false;
Consumer<byte[]> consumer = client.newConsumer().topic(topic1).subscriptionName(subName1).subscribe();
byte[] content = new byte[1024];
Producer<byte[]> producer = client.newProducer().topic(topic1).sendTimeout(2, TimeUnit.SECONDS).create();
for (int i = 0; i < 10; i++) {
producer.send(content);
}
Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);
try {
// try to send over backlog quota and make sure it fails
producer.send(content);
producer.send(content);
Assert.fail("backlog quota did not exceed");
} catch (PulsarClientException ce) {
Assert.assertTrue(ce instanceof PulsarClientException.ProducerBlockedQuotaExceededException || ce instanceof PulsarClientException.TimeoutException, ce.getMessage());
gotException = true;
}
Assert.assertTrue(gotException, "backlog exceeded exception did not occur");
// now remove backlog and ensure that producer is unblockedrolloverStats();
PersistentTopicStats stats = admin.persistentTopics().getStats(topic1);
int backlog = (int) stats.subscriptions.get(subName1).msgBacklog;
for (int i = 0; i < backlog; i++) {
Message<?> msg = consumer.receive();
consumer.acknowledge(msg);
}
Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);
// publish should work now
Exception sendException = null;
gotException = false;
try {
for (int i = 0; i < 5; i++) {
producer.send(content);
}
} catch (Exception e) {
gotException = true;
sendException = e;
}
Assert.assertFalse(gotException, "unable to publish due to " + sendException);
client.close();
}
use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.
the class BacklogQuotaManagerTest method testConsumerBacklogEviction.
@Test
public void testConsumerBacklogEviction() throws Exception {
assertEquals(admin.namespaces().getBacklogQuotaMap("prop/usc/ns-quota"), Maps.newTreeMap());
admin.namespaces().setBacklogQuota("prop/usc/ns-quota", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.consumer_backlog_eviction));
PulsarClient client = PulsarClient.builder().serviceUrl(adminUrl.toString()).statsInterval(0, TimeUnit.SECONDS).build();
final String topic1 = "persistent://prop/usc/ns-quota/topic1";
final String subName1 = "c1";
final String subName2 = "c2";
final int numMsgs = 20;
Consumer<byte[]> consumer1 = client.newConsumer().topic(topic1).subscriptionName(subName1).subscribe();
Consumer<byte[]> consumer2 = client.newConsumer().topic(topic1).subscriptionName(subName2).subscribe();
org.apache.pulsar.client.api.Producer<byte[]> producer = client.newProducer().topic(topic1).create();
byte[] content = new byte[1024];
for (int i = 0; i < numMsgs; i++) {
producer.send(content);
consumer1.receive();
consumer2.receive();
}
Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);
rolloverStats();
PersistentTopicStats stats = admin.persistentTopics().getStats(topic1);
Assert.assertTrue(stats.storageSize < 10 * 1024, "Storage size is [" + stats.storageSize + "]");
client.close();
}
Aggregations