You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add ability to seek from current position in Reader interface #10488
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!
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.
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.
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
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?
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.
Activity
lhotari commentedon May 6, 2021
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.
megfigura commentedon May 6, 2021
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.
MarvinCai commentedon May 12, 2021
Hi there, as reader already support
seekby 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 addgetPositionBeforeNmegfigura commentedon May 12, 2021
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?
lhotari commentedon May 18, 2021
Related to #6523
megfigura commentedon May 19, 2021
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.
codelipenghui commentedon Mar 4, 2022
The issue had no activity for 30 days, mark with Stale label.
merlimat commentedon Oct 24, 2026
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.