Search in sources :

Example 51 with Message

use of com.yahoo.pulsar.client.api.Message in project pulsar by yahoo.

the class ZeroQueueSizeTest method zeroQueueSizeNormalConsumer.

@Test()
public void zeroQueueSizeNormalConsumer() throws PulsarClientException {
    String key = "nonZeroQueueSizeNormalConsumer";
    // 1. Config
    final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
    final String subscriptionName = "my-ex-subscription-" + key;
    final String messagePredicate = "my-message-" + key + "-";
    // 2. Create Producer
    Producer producer = pulsarClient.createProducer(topicName);
    // 3. Create Consumer
    ConsumerConfiguration configuration = new ConsumerConfiguration();
    configuration.setReceiverQueueSize(0);
    ConsumerImpl consumer = (ConsumerImpl) pulsarClient.subscribe(topicName, subscriptionName, configuration);
    // 3. producer publish messages
    for (int i = 0; i < totalMessages; i++) {
        String message = messagePredicate + i;
        log.info("Producer produced: " + message);
        producer.send(message.getBytes());
    }
    // 4. Receiver receives the message
    Message message;
    for (int i = 0; i < totalMessages; i++) {
        assertEquals(consumer.numMessagesInQueue(), 0);
        message = consumer.receive();
        assertEquals(new String(message.getData()), messagePredicate + i);
        assertEquals(consumer.numMessagesInQueue(), 0);
        log.info("Consumer received : " + new String(message.getData()));
    }
}
Also used : ConsumerImpl(com.yahoo.pulsar.client.impl.ConsumerImpl) Producer(com.yahoo.pulsar.client.api.Producer) Message(com.yahoo.pulsar.client.api.Message) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) Test(org.testng.annotations.Test)

Example 52 with Message

use of com.yahoo.pulsar.client.api.Message in project pulsar by yahoo.

the class ZeroQueueSizeTest method zeroQueueSizeSharedSubscription.

@Test()
public void zeroQueueSizeSharedSubscription() throws PulsarClientException {
    String key = "zeroQueueSizeSharedSubscription";
    // 1. Config
    final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
    final String subscriptionName = "my-ex-subscription-" + key;
    final String messagePredicate = "my-message-" + key + "-";
    // 2. Create Producer
    Producer producer = pulsarClient.createProducer(topicName);
    // 3. Create Consumer
    int numOfSubscribers = 4;
    ConsumerConfiguration configuration = new ConsumerConfiguration();
    configuration.setReceiverQueueSize(0);
    configuration.setSubscriptionType(SubscriptionType.Shared);
    ConsumerImpl[] consumers = new ConsumerImpl[numOfSubscribers];
    for (int i = 0; i < numOfSubscribers; i++) {
        consumers[i] = (ConsumerImpl) pulsarClient.subscribe(topicName, subscriptionName, configuration);
    }
    // 4. Produce Messages
    for (int i = 0; i < totalMessages; i++) {
        String message = messagePredicate + i;
        producer.send(message.getBytes());
    }
    // 5. Consume messages
    Message message;
    for (int i = 0; i < totalMessages; i++) {
        assertEquals(consumers[i % numOfSubscribers].numMessagesInQueue(), 0);
        message = consumers[i % numOfSubscribers].receive();
        assertEquals(new String(message.getData()), messagePredicate + i);
        assertEquals(consumers[i % numOfSubscribers].numMessagesInQueue(), 0);
        log.info("Consumer received : " + new String(message.getData()));
    }
}
Also used : ConsumerImpl(com.yahoo.pulsar.client.impl.ConsumerImpl) Producer(com.yahoo.pulsar.client.api.Producer) Message(com.yahoo.pulsar.client.api.Message) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) Test(org.testng.annotations.Test)

Example 53 with Message

use of com.yahoo.pulsar.client.api.Message in project pulsar by yahoo.

the class PulsarSpout method fail.

@Override
public void fail(Object msgId) {
    if (msgId instanceof Message) {
        Message msg = (Message) msgId;
        MessageId id = msg.getMessageId();
        LOG.warn("[{}] Error processing message {}", spoutId, id);
        // Since the message processing failed, we put it in the failed messages queue if there are more retries
        // remaining for the message
        MessageRetries messageRetries = pendingMessageRetries.computeIfAbsent(id, (k) -> new MessageRetries());
        if ((failedRetriesTimeoutNano < 0 || (messageRetries.getTimeStamp() + failedRetriesTimeoutNano) > System.nanoTime()) && (maxFailedRetries < 0 || messageRetries.numRetries < maxFailedRetries)) {
            // since we can retry again, we increment retry count and put it in the queue
            LOG.info("[{}] Putting message {} in the retry queue", spoutId, id);
            messageRetries.incrementAndGet();
            pendingMessageRetries.putIfAbsent(id, messageRetries);
            failedMessages.add(msg);
            --pendingAcks;
        } else {
            LOG.warn("[{}] Number of retries limit reached, dropping the message {}", spoutId, id);
            ack(msg);
        }
    }
}
Also used : Message(com.yahoo.pulsar.client.api.Message) MessageId(com.yahoo.pulsar.client.api.MessageId)

Example 54 with Message

use of com.yahoo.pulsar.client.api.Message in project pulsar by yahoo.

the class PulsarSpout method ack.

@Override
public void ack(Object msgId) {
    if (msgId instanceof Message) {
        Message msg = (Message) msgId;
        if (LOG.isDebugEnabled()) {
            LOG.debug("[{}] Received ack for message {}", spoutId, msg.getMessageId());
        }
        consumer.acknowledgeAsync(msg);
        pendingMessageRetries.remove(msg.getMessageId());
        --pendingAcks;
    }
}
Also used : Message(com.yahoo.pulsar.client.api.Message)

Example 55 with Message

use of com.yahoo.pulsar.client.api.Message in project pulsar by yahoo.

the class PulsarSpout method nextTuple.

/**
     * Emits a tuple received from the Pulsar consumer unless there are any failed messages
     */
@Override
public void nextTuple() {
    Message msg;
    // check if there are any failed messages to re-emit in the topology
    msg = failedMessages.peek();
    if (msg != null) {
        MessageRetries messageRetries = pendingMessageRetries.get(msg.getMessageId());
        if (Backoff.shouldBackoff(messageRetries.getTimeStamp(), TimeUnit.NANOSECONDS, messageRetries.getNumRetries())) {
            Utils.sleep(100);
        } else {
            // remove the message from the queue and emit to the topology, only if it should not be backedoff
            LOG.info("[{}] Retrying failed message {}", spoutId, msg.getMessageId());
            failedMessages.remove();
            mapToValueAndEmit(msg);
        }
        return;
    }
    // receive from consumer if no failed messages
    if (consumer != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("[{}] Receiving the next message from pulsar consumer to emit to the collector", spoutId);
        }
        try {
            msg = consumer.receive(1, TimeUnit.SECONDS);
            if (msg != null) {
                ++messagesReceived;
                messageSizeReceived += msg.getData().length;
            }
            mapToValueAndEmit(msg);
        } catch (PulsarClientException e) {
            LOG.error("[{}] Error receiving message from pulsar consumer", spoutId, e);
        }
    }
}
Also used : Message(com.yahoo.pulsar.client.api.Message) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException)

Aggregations

Message (com.yahoo.pulsar.client.api.Message)90 Test (org.testng.annotations.Test)66 Consumer (com.yahoo.pulsar.client.api.Consumer)61 Producer (com.yahoo.pulsar.client.api.Producer)57 ConsumerConfiguration (com.yahoo.pulsar.client.api.ConsumerConfiguration)47 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)31 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)25 ProducerConfiguration (com.yahoo.pulsar.client.api.ProducerConfiguration)22 CompletableFuture (java.util.concurrent.CompletableFuture)17 PulsarClient (com.yahoo.pulsar.client.api.PulsarClient)10 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)9 MessageId (com.yahoo.pulsar.client.api.MessageId)9 PersistentSubscription (com.yahoo.pulsar.broker.service.persistent.PersistentSubscription)8 ClientConfiguration (com.yahoo.pulsar.client.api.ClientConfiguration)6 Field (java.lang.reflect.Field)6 HashSet (java.util.HashSet)6 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)5 RetentionPolicies (com.yahoo.pulsar.common.policies.data.RetentionPolicies)5 Tuple (backtype.storm.tuple.Tuple)4 ParameterException (com.beust.jcommander.ParameterException)4