Skip to content

Add ability to seek from current position in Reader interface #10488

Closed as not planned

Description

@megfigura

Is your enhancement request related to a problem? Please describe.
The Reader interface has the ability to seek to an arbitrary timestamp, MessageId, first or last message in a topic, or to read the next message. However, there is no API to seek fowards/backwards from the current position in a topic.

Ours I'm sure is not a common use-case, but you might imagine other situations where this could be useful. Ours is that we originally used Kafka for our pub/sub system and implemented cluster-wide logging via Kafka's Log4jAppender. We tried to implement the same thing using Pulsar, but we are unable to request the last n logs because we can't seek backwards n messages from the end of the topic. (Requesting logs from a particular timestamp onward was much easier to implement than in Kafka though!)

Describe the solution you'd like
Add a method to seek backwards (and forwards) from the current position by a number of messages.

Describe alternatives you've considered
Edit: See the workaround in my comment below.
I tried finding the last available MessageId and then manipulating the fields. Decrementing the MessageIdImpl.entryId seems to work, but only to the beginning of the ledger. I could also decrement the ledgerId, but I wouldn't know the Ledger's top entryId to start with. Plus, I'm not sure this is always a safe thing to do!

Activity

  1. added
    type/enhancement
    on May 5, 2021
  2. lhotari commented on May 6, 2021

    @lhotari
    Member

    There are other ways to solve the use case without having support in Pulsar to seek backwards.

    One possibility is to have some component consume the messages from the topic and publish index type of messages in another topic. Since the component could keep state while building the index messages, it can add message ids to previous index messages.

    The application can be build to use these application specific index messages to achieve what you are describing. If it wants to read the last n log messages, it would go the the "index topic" and read the index message and which would contain the message id of the message where to start reading from.

    A variation of this pattern would be to enhance the original messages to contain such "indexing information" instead of having a separate topic and messages for the purpose.

  3. megfigura commented on May 6, 2021

    @megfigura
    Author

    Hi @lhotari , thanks for the suggestion. Yes, this could work, but it would make things a bit more complex of course. I don't think we could put the previous-index information into the same topic as the log data because there are multiple writers. Each writer would only know its own previous index and not the global previous across all writers.

  4. MarvinCai commented on May 12, 2021

    @MarvinCai
    Contributor

    Hi there, as reader already support seek by messageId, maybe we can expose admin API to get message position before/after N message from given position then we can utilize the seek to position our reader. Currently we already have ManagedLedgerImpl.getPositionAfterN probably we can expose it and add getPositionBeforeN

  5. megfigura commented on May 12, 2021

    @megfigura
    Author

    Hi @MarvinCai, sounds like that could work. Of course it's not quite as discoverable as a new method on the Reader, but I understand that would be a bigger change as it would need to be implemented across all the clients, right?

  6. lhotari commented on May 18, 2021

    @lhotari
    Member

    Related to #6523

  7. megfigura commented on May 19, 2021

    @megfigura
    Author

    BTW, I was able to find a workaround for my use-case. I can use the Admin client to check the internalStats for the topic I'm interested in. This contains information on the ledgers that contain the data for the topic, including the ledgerIds and the top entryId for each ledger. With that information, I can create a MessageId to initialize the Reader's position.

    Of course this doesn't work in all cases, but it's enough for this use-case where I'm not using batching or any exotic configurations.

  8. codelipenghui commented on Mar 4, 2022

    @codelipenghui
    Contributor

    The issue had no activity for 30 days, mark with Stale label.

  9. merlimat commented on Oct 24, 2026

    @merlimat
    Contributor

    Thanks @megfigura. This still isn't available. Either option from the thread, a relative seek on the Reader or an admin endpoint that returns the position N entries before or after a given one, adds public client/admin API (and likely protocol support for the client path), so it needs a PIP first: https://github.com/apache/pulsar/blob/master/pip/README.md. Closing this for now. A PIP is welcome if someone wants to pick it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/clientlifecycle/staletype/enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions