use of org.apache.pulsar.client.api.PulsarClientException.ProducerFencedException in project pulsar by yahoo.
the class ExclusiveProducerTest method testProducerTasksCleanupWhenUsingExclusiveProducers.
@Test(dataProvider = "topics")
public void testProducerTasksCleanupWhenUsingExclusiveProducers(String type, boolean partitioned) throws Exception {
String topic = newTopic(type, partitioned);
Producer<String> p1 = pulsarClient.newProducer(Schema.STRING).topic(topic).accessMode(ProducerAccessMode.Exclusive).create();
try {
pulsarClient.newProducer(Schema.STRING).topic(topic).accessMode(ProducerAccessMode.Exclusive).create();
fail("Should have failed");
} catch (ProducerFencedException e) {
// Expected
}
p1.close();
HashedWheelTimer timer = (HashedWheelTimer) ((PulsarClientImpl) pulsarClient).timer();
Awaitility.await().untilAsserted(() -> Assert.assertEquals(timer.pendingTimeouts(), 0));
}
use of org.apache.pulsar.client.api.PulsarClientException.ProducerFencedException in project pulsar by yahoo.
the class ExclusiveProducerTest method simpleTest.
private void simpleTest(String topic) throws Exception {
Producer<String> p1 = pulsarClient.newProducer(Schema.STRING).topic(topic).producerName("p1").accessMode(ProducerAccessMode.Exclusive).create();
try {
pulsarClient.newProducer(Schema.STRING).topic(topic).producerName("p-fail-1").accessMode(ProducerAccessMode.Exclusive).create();
fail("Should have failed");
} catch (ProducerFencedException e) {
// Expected
}
try {
pulsarClient.newProducer(Schema.STRING).topic(topic).producerName("p-fail-2").accessMode(ProducerAccessMode.Shared).create();
fail("Should have failed");
} catch (ProducerBusyException e) {
// Expected
}
p1.close();
// Now producer should be allowed to get in
Producer<String> p2 = pulsarClient.newProducer(Schema.STRING).topic(topic).producerName("p2").accessMode(ProducerAccessMode.Exclusive).create();
Producer<String> p3 = pulsarClient.newProducer(Schema.STRING).topic(topic).producerName("p3").accessMode(ProducerAccessMode.ExclusiveWithFencing).create();
try {
p2.send("test");
fail("Should have failed");
} catch (ProducerFencedException expected) {
}
// this should work
p3.send("test");
p3.close();
// test now WaitForExclusive vs ExclusiveWithFencing
// use two different Clients, because sometimes fencing a Producer triggers connection close
// making the test unreliable.
@Cleanup PulsarClient pulsarClient2 = PulsarClient.builder().serviceUrl(pulsar.getBrokerServiceUrl()).operationTimeout(2, TimeUnit.SECONDS).build();
Producer<String> p4 = pulsarClient2.newProducer(Schema.STRING).topic(topic).producerName("p4").accessMode(ProducerAccessMode.Exclusive).create();
p4.send("test");
// p5 will be waiting for the lock to be released
CompletableFuture<Producer<String>> p5 = pulsarClient2.newProducer(Schema.STRING).topic(topic).producerName("p5").accessMode(ProducerAccessMode.WaitForExclusive).createAsync();
// wait for all the Producers to be enqueued in order to prevent races
Thread.sleep(2000);
// p6 fences out all the current producers, even p5
Producer<String> p6 = pulsarClient.newProducer(Schema.STRING).topic(topic).producerName("p6").accessMode(ProducerAccessMode.ExclusiveWithFencing).create();
p6.send("test");
// p7 is enqueued after p6
CompletableFuture<Producer<String>> p7 = pulsarClient2.newProducer(Schema.STRING).topic(topic).producerName("p7").accessMode(ProducerAccessMode.WaitForExclusive).createAsync();
// this should work, p6 is the owner
p6.send("test");
try {
p4.send("test");
fail("Should have failed");
} catch (ProducerFencedException expected) {
}
// this should work, p6 is the owner
p6.send("test");
// p5 fails
try {
p5.get();
fail("Should have failed");
} catch (ExecutionException expected) {
assertTrue(expected.getCause() instanceof ProducerFencedException, "unexpected exception " + expected.getCause());
}
// this should work, p6 is the owner
p6.send("test");
p6.close();
// p7 finally acquires the lock
p7.get().send("test");
p7.get().close();
}
use of org.apache.pulsar.client.api.PulsarClientException.ProducerFencedException in project incubator-pulsar by apache.
the class ExclusiveProducerTest method testProducerTasksCleanupWhenUsingExclusiveProducers.
@Test(dataProvider = "topics")
public void testProducerTasksCleanupWhenUsingExclusiveProducers(String type, boolean partitioned) throws Exception {
String topic = newTopic(type, partitioned);
Producer<String> p1 = pulsarClient.newProducer(Schema.STRING).topic(topic).accessMode(ProducerAccessMode.Exclusive).create();
try {
pulsarClient.newProducer(Schema.STRING).topic(topic).accessMode(ProducerAccessMode.Exclusive).create();
fail("Should have failed");
} catch (ProducerFencedException e) {
// Expected
}
p1.close();
HashedWheelTimer timer = (HashedWheelTimer) ((PulsarClientImpl) pulsarClient).timer();
Awaitility.await().untilAsserted(() -> Assert.assertEquals(timer.pendingTimeouts(), 0));
}
use of org.apache.pulsar.client.api.PulsarClientException.ProducerFencedException in project incubator-pulsar by apache.
the class ExclusiveProducerTest method simpleTest.
private void simpleTest(String topic) throws Exception {
Producer<String> p1 = pulsarClient.newProducer(Schema.STRING).topic(topic).producerName("p1").accessMode(ProducerAccessMode.Exclusive).create();
try {
pulsarClient.newProducer(Schema.STRING).topic(topic).producerName("p-fail-1").accessMode(ProducerAccessMode.Exclusive).create();
fail("Should have failed");
} catch (ProducerFencedException e) {
// Expected
}
try {
pulsarClient.newProducer(Schema.STRING).topic(topic).producerName("p-fail-2").accessMode(ProducerAccessMode.Shared).create();
fail("Should have failed");
} catch (ProducerBusyException e) {
// Expected
}
p1.close();
// Now producer should be allowed to get in
Producer<String> p2 = pulsarClient.newProducer(Schema.STRING).topic(topic).producerName("p2").accessMode(ProducerAccessMode.Exclusive).create();
Producer<String> p3 = pulsarClient.newProducer(Schema.STRING).topic(topic).producerName("p3").accessMode(ProducerAccessMode.ExclusiveWithFencing).create();
try {
p2.send("test");
fail("Should have failed");
} catch (ProducerFencedException expected) {
}
// this should work
p3.send("test");
p3.close();
// test now WaitForExclusive vs ExclusiveWithFencing
// use two different Clients, because sometimes fencing a Producer triggers connection close
// making the test unreliable.
@Cleanup PulsarClient pulsarClient2 = PulsarClient.builder().serviceUrl(pulsar.getBrokerServiceUrl()).operationTimeout(2, TimeUnit.SECONDS).build();
Producer<String> p4 = pulsarClient2.newProducer(Schema.STRING).topic(topic).producerName("p4").accessMode(ProducerAccessMode.Exclusive).create();
p4.send("test");
// p5 will be waiting for the lock to be released
CompletableFuture<Producer<String>> p5 = pulsarClient2.newProducer(Schema.STRING).topic(topic).producerName("p5").accessMode(ProducerAccessMode.WaitForExclusive).createAsync();
// wait for all the Producers to be enqueued in order to prevent races
Thread.sleep(2000);
// p6 fences out all the current producers, even p5
Producer<String> p6 = pulsarClient.newProducer(Schema.STRING).topic(topic).producerName("p6").accessMode(ProducerAccessMode.ExclusiveWithFencing).create();
p6.send("test");
// p7 is enqueued after p6
CompletableFuture<Producer<String>> p7 = pulsarClient2.newProducer(Schema.STRING).topic(topic).producerName("p7").accessMode(ProducerAccessMode.WaitForExclusive).createAsync();
// this should work, p6 is the owner
p6.send("test");
try {
p4.send("test");
fail("Should have failed");
} catch (ProducerFencedException expected) {
}
// this should work, p6 is the owner
p6.send("test");
// p5 fails
try {
p5.get();
fail("Should have failed");
} catch (ExecutionException expected) {
assertTrue(expected.getCause() instanceof ProducerFencedException, "unexpected exception " + expected.getCause());
}
// this should work, p6 is the owner
p6.send("test");
p6.close();
// p7 finally acquires the lock
p7.get().send("test");
p7.get().close();
}
use of org.apache.pulsar.client.api.PulsarClientException.ProducerFencedException in project pulsar by apache.
the class ExclusiveProducerTest method testProducerTasksCleanupWhenUsingExclusiveProducers.
@Test(dataProvider = "topics")
public void testProducerTasksCleanupWhenUsingExclusiveProducers(String type, boolean partitioned) throws Exception {
String topic = newTopic(type, partitioned);
Producer<String> p1 = pulsarClient.newProducer(Schema.STRING).topic(topic).accessMode(ProducerAccessMode.Exclusive).create();
try {
pulsarClient.newProducer(Schema.STRING).topic(topic).accessMode(ProducerAccessMode.Exclusive).create();
fail("Should have failed");
} catch (ProducerFencedException e) {
// Expected
}
p1.close();
HashedWheelTimer timer = (HashedWheelTimer) ((PulsarClientImpl) pulsarClient).timer();
Awaitility.await().untilAsserted(() -> Assert.assertEquals(timer.pendingTimeouts(), 0));
}
Aggregations