Search in sources :

Example 21 with PulsarClientException

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);
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) PersistentTopic(com.yahoo.pulsar.broker.service.persistent.PersistentTopic) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) PersistentSubscription(com.yahoo.pulsar.broker.service.persistent.PersistentSubscription) Test(org.testng.annotations.Test)

Example 22 with PulsarClientException

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();
}
Also used : Producer(com.yahoo.pulsar.client.api.Producer) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) ProducerConfiguration(com.yahoo.pulsar.client.api.ProducerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) BacklogQuota(com.yahoo.pulsar.common.policies.data.BacklogQuota) ClientConfiguration(com.yahoo.pulsar.client.api.ClientConfiguration) Test(org.testng.annotations.Test)

Example 23 with PulsarClientException

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;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) ByteBuf(io.netty.buffer.ByteBuf)

Example 24 with PulsarClientException

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;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) CompletableFuture(java.util.concurrent.CompletableFuture) Future(io.netty.util.concurrent.Future) ByteBuf(io.netty.buffer.ByteBuf) IOException(java.io.IOException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException)

Example 25 with PulsarClientException

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;
}
Also used : PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException)

Aggregations

PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)37 Message (com.yahoo.pulsar.client.api.Message)18 Test (org.testng.annotations.Test)14 Consumer (com.yahoo.pulsar.client.api.Consumer)12 ConsumerConfiguration (com.yahoo.pulsar.client.api.ConsumerConfiguration)12 Producer (com.yahoo.pulsar.client.api.Producer)11 CompletableFuture (java.util.concurrent.CompletableFuture)11 PulsarClient (com.yahoo.pulsar.client.api.PulsarClient)7 ByteBuf (io.netty.buffer.ByteBuf)6 IOException (java.io.IOException)5 ProducerConfiguration (com.yahoo.pulsar.client.api.ProducerConfiguration)4 PersistentSubscription (com.yahoo.pulsar.broker.service.persistent.PersistentSubscription)3 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)3 ClientConfiguration (com.yahoo.pulsar.client.api.ClientConfiguration)3 MessageId (com.yahoo.pulsar.client.api.MessageId)3 ProducerConsumerBase (com.yahoo.pulsar.client.api.ProducerConsumerBase)3 DoubleByteBuf (com.yahoo.pulsar.common.api.DoubleByteBuf)3 MessageMetadata (com.yahoo.pulsar.common.api.proto.PulsarApi.MessageMetadata)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ParameterException (com.beust.jcommander.ParameterException)2