use of com.yahoo.pulsar.client.api.ProducerConfiguration in project pulsar by yahoo.
the class PersistentTopicE2ETest method testProducerQueueFullNonBlocking.
@Test
public void testProducerQueueFullNonBlocking() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/topic-xyzx";
final int messages = 10;
// 1. Producer connect
PulsarClient client = PulsarClient.create(brokerUrl.toString());
ProducerConfiguration producerConfiguration = new ProducerConfiguration().setMaxPendingMessages(messages).setBlockIfQueueFull(false).setSendTimeout(1, TimeUnit.SECONDS);
ProducerImpl producer = (ProducerImpl) client.createProducer(topicName, producerConfiguration);
// 2. Stop broker
cleanup();
// 2. producer publish messages
long startTime = System.nanoTime();
for (int i = 0; i < messages; i++) {
// Should never block
producer.sendAsync("msg".getBytes());
}
// Verify thread was not blocked
long delayNs = System.nanoTime() - startTime;
assertTrue(delayNs < TimeUnit.SECONDS.toNanos(1));
assertEquals(producer.getPendingQueueSize(), messages);
// Next send operation must fail and not block
startTime = System.nanoTime();
try {
producer.send("msg".getBytes());
fail("Send should have failed");
} catch (PulsarClientException.ProducerQueueIsFullError e) {
// Expected
}
delayNs = System.nanoTime() - startTime;
assertTrue(delayNs < TimeUnit.SECONDS.toNanos(1));
assertEquals(producer.getPendingQueueSize(), messages);
// 4. producer disconnect
producer.close();
client.close();
// 5. Restart broker
setup();
}
use of com.yahoo.pulsar.client.api.ProducerConfiguration in project pulsar by yahoo.
the class BacklogQuotaManagerTest method testAheadProducerOnHold.
@Test
public void testAheadProducerOnHold() throws Exception {
assertEquals(admin.namespaces().getBacklogQuotaMap("prop/usc/quotahold"), Maps.newTreeMap());
admin.namespaces().setBacklogQuota("prop/usc/quotahold", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_request_hold));
final ClientConfiguration clientConf = new ClientConfiguration();
clientConf.setStatsInterval(0, TimeUnit.SECONDS);
final PulsarClient client = PulsarClient.create(adminUrl.toString(), clientConf);
final String topic1 = "persistent://prop/usc/quotahold/hold";
final String subName1 = "c1hold";
final int numMsgs = 10;
Consumer consumer = client.subscribe(topic1, subName1);
ProducerConfiguration producerConfiguration = new ProducerConfiguration();
producerConfiguration.setSendTimeout(2, TimeUnit.SECONDS);
byte[] content = new byte[1024];
Producer producer = client.createProducer(topic1, producerConfiguration);
for (int i = 0; i <= numMsgs; i++) {
try {
producer.send(content);
LOG.info("sent [{}]", i);
} catch (PulsarClientException.TimeoutException cte) {
// producer close may cause a timeout on send
LOG.info("timeout on [{}]", i);
}
}
for (int i = 0; i < numMsgs; i++) {
consumer.receive();
LOG.info("received [{}]", i);
}
Thread.sleep((TIME_TO_CHECK_BACKLOG_QUOTA + 1) * 1000);
rolloverStats();
PersistentTopicStats stats = admin.persistentTopics().getStats(topic1);
Assert.assertEquals(stats.publishers.size(), 0, "Number of producers on topic " + topic1 + " are [" + stats.publishers.size() + "]");
client.close();
}
use of com.yahoo.pulsar.client.api.ProducerConfiguration in project pulsar by yahoo.
the class BacklogQuotaManagerTest method testProducerException.
@Test
public void testProducerException() 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 ClientConfiguration clientConf = new ClientConfiguration();
clientConf.setStatsInterval(0, TimeUnit.SECONDS);
final PulsarClient client = PulsarClient.create(adminUrl.toString(), clientConf);
final String topic1 = "persistent://prop/usc/quotahold/except";
final String subName1 = "c1except";
boolean gotException = false;
client.subscribe(topic1, subName1);
ProducerConfiguration producerConfiguration = new ProducerConfiguration();
producerConfiguration.setSendTimeout(2, TimeUnit.SECONDS);
byte[] content = new byte[1024];
Producer producer = client.createProducer(topic1, producerConfiguration);
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");
client.close();
}
use of com.yahoo.pulsar.client.api.ProducerConfiguration in project pulsar by yahoo.
the class BacklogQuotaManagerTest method testAheadProducerOnHoldTimeout.
@Test
public void testAheadProducerOnHoldTimeout() throws Exception {
assertEquals(admin.namespaces().getBacklogQuotaMap("prop/usc/quotahold"), Maps.newTreeMap());
admin.namespaces().setBacklogQuota("prop/usc/quotahold", new BacklogQuota(10 * 1024, BacklogQuota.RetentionPolicy.producer_request_hold));
final ClientConfiguration clientConf = new ClientConfiguration();
clientConf.setStatsInterval(0, TimeUnit.SECONDS);
final PulsarClient client = PulsarClient.create(adminUrl.toString(), clientConf);
final String topic1 = "persistent://prop/usc/quotahold/holdtimeout";
final String subName1 = "c1holdtimeout";
boolean gotException = false;
client.subscribe(topic1, subName1);
ProducerConfiguration producerConfiguration = new ProducerConfiguration();
producerConfiguration.setSendTimeout(2, TimeUnit.SECONDS);
byte[] content = new byte[1024];
Producer producer = client.createProducer(topic1, producerConfiguration);
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.TimeoutException te) {
gotException = true;
}
Assert.assertTrue(gotException, "timeout did not occur");
client.close();
}
use of com.yahoo.pulsar.client.api.ProducerConfiguration in project pulsar by yahoo.
the class BatchMessageTest method testNonBatchCumulativeAckAfterBatchPublish.
@Test
public void testNonBatchCumulativeAckAfterBatchPublish() throws Exception {
int numMsgs = 10;
int numMsgsInBatch = numMsgs;
final String topicName = "persistent://prop/use/ns-abc/testNonBatchCumulativeAckAfterBatchPublish";
final String subscriptionName = "nbcaabp-sub-1";
Consumer consumer = pulsarClient.subscribe(topicName, subscriptionName);
consumer.close();
ProducerConfiguration producerConf = new ProducerConfiguration();
producerConf.setBatchingMaxPublishDelay(5000, TimeUnit.MILLISECONDS);
producerConf.setBatchingMaxMessages(numMsgsInBatch);
producerConf.setBatchingEnabled(true);
Producer producer = pulsarClient.createProducer(topicName, producerConf);
// create producer to publish non batch messages
Producer noBatchProducer = pulsarClient.createProducer(topicName);
List<CompletableFuture<MessageId>> sendFutureList = Lists.newArrayList();
for (int i = 0; i < numMsgs; i++) {
byte[] message = ("msg-" + i).getBytes();
Message msg = MessageBuilder.create().setContent(message).build();
sendFutureList.add(producer.sendAsync(msg));
}
FutureUtil.waitForAll(sendFutureList).get();
sendFutureList.clear();
byte[] nobatchmsg = ("nobatch").getBytes();
Message nmsg = MessageBuilder.create().setContent(nobatchmsg).build();
noBatchProducer.sendAsync(nmsg).get();
PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
rolloverPerIntervalStats();
assertTrue(topic.getProducers().values().iterator().next().getStats().msgRateIn > 0.0);
assertEquals(topic.getPersistentSubscription(subscriptionName).getNumberOfEntriesInBacklog(), 2);
consumer = pulsarClient.subscribe(topicName, subscriptionName);
Message lastunackedMsg = null;
for (int i = 0; i <= numMsgs; i++) {
Message msg = consumer.receive(5, TimeUnit.SECONDS);
assertNotNull(msg);
lastunackedMsg = msg;
}
if (lastunackedMsg != null) {
consumer.acknowledgeCumulative(lastunackedMsg);
}
Thread.sleep(100);
rolloverPerIntervalStats();
assertEquals(topic.getPersistentSubscription(subscriptionName).getNumberOfEntriesInBacklog(), 0);
assertTrue(((ConsumerImpl) consumer).isBatchingAckTrackerEmpty());
consumer.close();
producer.close();
noBatchProducer.close();
}
Aggregations