Java Code Examples for org.apache.pulsar.client.impl.MessageIdImpl#getEntryId()

The following examples show how to use org.apache.pulsar.client.impl.MessageIdImpl#getEntryId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PulsarRecordsStorage.java    From liiklus with MIT License 5 votes vote down vote up
public static long toOffset(MessageId messageId) {
    MessageIdImpl msgId = (MessageIdImpl) messageId;
    // Combine ledger id and entry id to form offset
    // Use less than 32 bits to represent entry id since it will get
    // rolled over way before overflowing the max int range
    return (msgId.getLedgerId() << 28) | msgId.getEntryId();
}
 
Example 2
Source File: FunctionCommon.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public static final long getSequenceId(MessageId messageId) {
    MessageIdImpl msgId = (MessageIdImpl) ((messageId instanceof TopicMessageIdImpl)
            ? ((TopicMessageIdImpl) messageId).getInnerMessageId()
            : messageId);
    long ledgerId = msgId.getLedgerId();
    long entryId = msgId.getEntryId();

    // Combine ledger id and entry id to form offset
    // Use less than 32 bits to represent entry id since it will get
    // rolled over way before overflowing the max int range
    long offset = (ledgerId << 28) | entryId;
    return offset;
}
 
Example 3
Source File: MessageIdUtils.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public static final long getOffset(MessageId messageId) {
    MessageIdImpl msgId = (MessageIdImpl) messageId;
    long ledgerId = msgId.getLedgerId();
    long entryId = msgId.getEntryId();

    // Combine ledger id and entry id to form offset
    // Use less than 32 bits to represent entry id since it will get
    // rolled over way before overflowing the max int range
    long offset = (ledgerId << 28) | entryId;
    return offset;
}
 
Example 4
Source File: ReaderThread.java    From pulsar-flink with Apache License 2.0 4 votes vote down vote up
protected void skipFirstMessageIfNeeded() throws org.apache.pulsar.client.api.PulsarClientException {
    Message<?> currentMessage = null;
    MessageId currentId;
    boolean failOnDataLoss = this.failOnDataLoss;
    if (!startMessageId.equals(MessageId.earliest)
            && !startMessageId.equals(MessageId.latest)
            && ((MessageIdImpl) startMessageId).getEntryId() != -1) {
        MessageIdImpl lastMessageId = (MessageIdImpl) this.owner.getMetadataReader().getLastMessageId(reader.getTopic());

        if (!messageIdRoughEquals(startMessageId, lastMessageId) && !reader.hasMessageAvailable()) {
            MessageIdImpl startMsgIdImpl = (MessageIdImpl) startMessageId;
            long startMsgLedgerId = startMsgIdImpl.getLedgerId();
            long startMsgEntryId = startMsgIdImpl.getEntryId();

            // startMessageId is bigger than lastMessageId
            if (startMsgLedgerId > lastMessageId.getLedgerId()
                    || (startMsgLedgerId == lastMessageId.getLedgerId() && startMsgEntryId > lastMessageId.getEntryId())) {
                log.error("the start message id is beyond the last commit message id, with topic:{}", reader.getTopic());
                throw new RuntimeException("start message id beyond the last commit");
            } else if (!failOnDataLoss) {
                log.info("reset message to valid offset {}", startMessageId);
                this.owner.getMetadataReader().resetCursor(reader.getTopic(), startMessageId);
            }
        }

        while (currentMessage == null && running) {
            currentMessage = reader.readNext(pollTimeoutMs, TimeUnit.MILLISECONDS);
            if (failOnDataLoss) {
                break;
            }
        }
        if (currentMessage == null) {
            reportDataLoss(String.format("Cannot read data at offset %s from topic: %s",
                    startMessageId.toString(),
                    topic));
        } else {
            currentId = currentMessage.getMessageId();
            if (!messageIdRoughEquals(currentId, startMessageId) && failOnDataLoss) {
                reportDataLoss(
                        String.format(
                                "Potential Data Loss in reading %s: intended to start at %s, actually we get %s",
                                topic, startMessageId.toString(), currentId.toString()));
            }

            if (startMessageId instanceof BatchMessageIdImpl && currentId instanceof BatchMessageIdImpl) {
                // we seek using a batch message id, we can read next directly later
            } else if (startMessageId instanceof MessageIdImpl && currentId instanceof BatchMessageIdImpl) {
                // we seek using a message id, this is supposed to be read by previous task since it's
                // inclusive for the checkpoint, so we skip this batch
                BatchMessageIdImpl cbmid = (BatchMessageIdImpl) currentId;

                MessageIdImpl newStart =
                        new MessageIdImpl(cbmid.getLedgerId(), cbmid.getEntryId() + 1, cbmid.getPartitionIndex());
                reader.seek(newStart);
            } else if (startMessageId instanceof MessageIdImpl && currentId instanceof MessageIdImpl) {
                // current entry is a non-batch entry, we can read next directly later
            }
        }
    }
}
 
Example 5
Source File: PersistentTopic.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private CompletableFuture<? extends Subscription> getNonDurableSubscription(String subscriptionName,
        MessageId startMessageId, long startMessageRollbackDurationSec) {
    log.info("[{}][{}] Creating non-durable subscription at msg id {}", topic, subscriptionName, startMessageId);

    synchronized (ledger) {
        // Create a new non-durable cursor only for the first consumer that connects
        PersistentSubscription subscription = subscriptions.get(subscriptionName);

        if (subscription == null) {
            MessageIdImpl msgId = startMessageId != null ? (MessageIdImpl) startMessageId
                    : (MessageIdImpl) MessageId.latest;

            long ledgerId = msgId.getLedgerId();
            long entryId = msgId.getEntryId();
            // Ensure that the start message id starts from a valid entry.
            if (ledgerId >= 0 && entryId >= 0
                    && msgId instanceof BatchMessageIdImpl) {
                // When the start message is relative to a batch, we need to take one step back on the previous
                // message,
                // because the "batch" might not have been consumed in its entirety.
                // The client will then be able to discard the first messages if needed.
                entryId = msgId.getEntryId() - 1;
            }

            Position startPosition = new PositionImpl(ledgerId, entryId);
            ManagedCursor cursor = null;
            try {
                cursor = ledger.newNonDurableCursor(startPosition, subscriptionName);
            } catch (ManagedLedgerException e) {
                return FutureUtil.failedFuture(e);
            }

            subscription = new PersistentSubscription(this, subscriptionName, cursor, false);
            subscriptions.put(subscriptionName, subscription);
        }

        if (startMessageRollbackDurationSec > 0) {
            long timestamp = System.currentTimeMillis()
                    - TimeUnit.SECONDS.toMillis(startMessageRollbackDurationSec);
            CompletableFuture<Subscription> subscriptionFuture = new CompletableFuture<>();
            final Subscription finalSubscription = subscription;
            subscription.resetCursor(timestamp).handle((s, ex) -> {
                if (ex != null) {
                    log.warn("[{}] Failed to reset cursor {} position at timestamp {}", topic, subscriptionName,
                            startMessageRollbackDurationSec);
                }
                subscriptionFuture.complete(finalSubscription);
                return null;
            });
            return subscriptionFuture;
        } else {
            return CompletableFuture.completedFuture(subscription);
        }
    }
}
 
Example 6
Source File: SubscriptionSeekTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test
public void testSeek() throws Exception {
    final String topicName = "persistent://prop/use/ns-abc/testSeek";

    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();

    // Disable pre-fetch in consumer to track the messages received
    org.apache.pulsar.client.api.Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName)
            .subscriptionName("my-subscription").receiverQueueSize(0).subscribe();

    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName).get();
    assertNotNull(topicRef);
    assertEquals(topicRef.getProducers().size(), 1);
    assertEquals(topicRef.getSubscriptions().size(), 1);

    List<MessageId> messageIds = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        String message = "my-message-" + i;
        MessageId msgId = producer.send(message.getBytes());
        messageIds.add(msgId);
    }

    PersistentSubscription sub = topicRef.getSubscription("my-subscription");
    assertEquals(sub.getNumberOfEntriesInBacklog(false), 10);

    consumer.seek(MessageId.latest);
    assertEquals(sub.getNumberOfEntriesInBacklog(false), 0);

    // Wait for consumer to reconnect
    Thread.sleep(500);
    consumer.seek(MessageId.earliest);
    assertEquals(sub.getNumberOfEntriesInBacklog(false), 10);

    Thread.sleep(500);
    consumer.seek(messageIds.get(5));
    assertEquals(sub.getNumberOfEntriesInBacklog(false), 5);

    MessageIdImpl messageId = (MessageIdImpl) messageIds.get(5);
    MessageIdImpl beforeEarliest = new MessageIdImpl(
            messageId.getLedgerId() - 1, messageId.getEntryId(), messageId.getPartitionIndex());
    MessageIdImpl afterLatest = new MessageIdImpl(
            messageId.getLedgerId() + 1, messageId.getEntryId(), messageId.getPartitionIndex());

    log.info("MessageId {}: beforeEarliest: {}, afterLatest: {}", messageId, beforeEarliest, afterLatest);

    Thread.sleep(500);
    consumer.seek(beforeEarliest);
    assertEquals(sub.getNumberOfEntriesInBacklog(false), 10);

    Thread.sleep(500);
    consumer.seek(afterLatest);
    assertEquals(sub.getNumberOfEntriesInBacklog(false), 0);
}