use of com.yahoo.pulsar.client.api.PulsarClientException in project pulsar by yahoo.
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";
ConsumerConfiguration conf1 = new ConsumerConfiguration();
conf1.setSubscriptionType(SubscriptionType.Exclusive);
ConsumerConfiguration conf2 = new ConsumerConfiguration();
conf2.setSubscriptionType(SubscriptionType.Shared);
ConsumerConfiguration conf3 = new ConsumerConfiguration();
conf3.setSubscriptionType(SubscriptionType.Failover);
Consumer consumer1 = pulsarClient.subscribe(topicName, subName, conf1);
Consumer consumer2 = null;
Consumer consumer3 = null;
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
PersistentSubscription subRef = topicRef.getPersistentSubscription(subName);
// 1. shared consumer on an exclusive sub fails
try {
consumer2 = pulsarClient.subscribe(topicName, subName, conf2);
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.subscribe(topicName, subName, conf3);
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.subscribe(topicName, subName, conf2);
assertEquals(subRef.getDispatcher().getType(), SubType.Shared);
} catch (PulsarClientException e) {
fail("should not fail");
}
// 4. exclusive fails on shared sub
try {
consumer1 = pulsarClient.subscribe(topicName, subName, conf1);
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.subscribe(topicName, subName, conf3);
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.subscribe(topicName, subName, conf1);
assertEquals(subRef.getDispatcher().getType(), SubType.Exclusive);
} catch (PulsarClientException e) {
fail("should not fail");
}
consumer1.close();
admin.persistentTopics().delete(topicName);
}
use of com.yahoo.pulsar.client.api.PulsarClientException 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.PulsarClientException in project pulsar by yahoo.
the class ConsumerImpl method unsubscribeAsync.
@Override
public CompletableFuture<Void> unsubscribeAsync() {
if (getState() == State.Closing || getState() == State.Closed) {
return FutureUtil.failedFuture(new PulsarClientException.AlreadyClosedException("Consumer was already closed"));
}
final CompletableFuture<Void> unsubscribeFuture = new CompletableFuture<>();
if (isConnected()) {
setState(State.Closing);
long requestId = client.newRequestId();
ByteBuf unsubscribe = Commands.newUnsubscribe(consumerId, requestId);
ClientCnx cnx = cnx();
cnx.sendRequestWithId(unsubscribe, requestId).thenRun(() -> {
cnx.removeConsumer(consumerId);
log.info("[{}][{}] Successfully unsubscribed from topic", topic, subscription);
batchMessageAckTracker.clear();
unAckedMessageTracker.close();
unsubscribeFuture.complete(null);
setState(State.Closed);
}).exceptionally(e -> {
log.error("[{}][{}] Failed to unsubscribe: {}", topic, subscription, e.getCause().getMessage());
unsubscribeFuture.completeExceptionally(e.getCause());
setState(State.Ready);
return null;
});
} else {
unsubscribeFuture.completeExceptionally(new PulsarClientException("Not connected to broker"));
}
return unsubscribeFuture;
}
use of com.yahoo.pulsar.client.api.PulsarClientException in project pulsar by yahoo.
the class ConsumerImpl method sendAcknowledge.
private CompletableFuture<Void> sendAcknowledge(MessageId messageId, AckType ackType) {
MessageIdImpl msgId = (MessageIdImpl) messageId;
final ByteBuf cmd = Commands.newAck(consumerId, msgId.getLedgerId(), msgId.getEntryId(), ackType, null);
// There's no actual response from ack messages
final CompletableFuture<Void> ackFuture = new CompletableFuture<Void>();
if (isConnected()) {
cnx().ctx().writeAndFlush(cmd).addListener(new GenericFutureListener<Future<Void>>() {
@Override
public void operationComplete(Future<Void> future) throws Exception {
if (future.isSuccess()) {
if (ackType == AckType.Individual) {
unAckedMessageTracker.remove(msgId);
// increment counter by 1 for non-batch msg
if (!(messageId instanceof BatchMessageIdImpl)) {
stats.incrementNumAcksSent(1);
}
} else if (ackType == AckType.Cumulative) {
stats.incrementNumAcksSent(unAckedMessageTracker.removeMessagesTill(msgId));
}
if (log.isDebugEnabled()) {
log.debug("[{}] [{}] [{}] Successfully acknowledged message - {}, acktype {}", subscription, topic, consumerName, messageId, ackType);
}
ackFuture.complete(null);
} else {
stats.incrementNumAcksFailed();
ackFuture.completeExceptionally(new PulsarClientException(future.cause()));
}
}
});
} else {
stats.incrementNumAcksFailed();
ackFuture.completeExceptionally(new PulsarClientException("Not connected to broker. State: " + getState()));
}
return ackFuture;
}
use of com.yahoo.pulsar.client.api.PulsarClientException in project pulsar by yahoo.
the class HandlerBase method handleConnectionError.
private Void handleConnectionError(Throwable exception) {
log.warn("[{}] [{}] Error connecting to broker: {}", topic, getHandlerName(), exception.getMessage());
connectionFailed(new PulsarClientException(exception));
State state = STATE_UPDATER.get(this);
if (state == State.Uninitialized || state == State.Connecting || state == State.Ready) {
reconnectLater(exception);
}
return null;
}
Aggregations