Skip to content

seeking on a timestamp, blocks the reader  #7775

Closed

Description

@daddykotex

Hi, thanks for the amazing project.

I have an issue with my setup and I can't figure out what is wrong.

TLDR: I have a topic, with messages in it, something like 30 messages. The messages were published between 2020-07-31T21:28:56.739Z and 2020-08-01T11:03:04.323Z. When I try to read the messages after calling Reader#seek on a timestamp after the last message publish time, the reader hangs.

Describe the bug

I use a Reader to connect to the cluster and grab messages between some dates. When I connect to the cluster and I read all messages I have no problem. But when I use the reader to seek to a specific timestamp, the Reader#readNext method will hang, even though Reader#hasMessage method returns true.

I can't reproduce in a test by creating a topic, posting a few messages and then trying to seek and read, the problem does not happen. In a test, when I Reader#seek to a timestamp much after the last message was published, I still receive the last message I published to the topic.

I wrote an example, and I can share the output so you can see the issue.

Code:

import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.MessageId;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.Reader;
import org.apache.pulsar.client.api.ReaderBuilder;
import org.apache.pulsar.client.impl.schema.BytesSchema;

import java.io.IOException;
import java.time.Instant;
import java.util.concurrent.TimeUnit;

public class ReadProblem {

    static final String topic = "persistent://namespace.lagame/sandbox.ingestion_v1/udd.ffd.1";

    Reader<byte[]> newReader(PulsarClient client, boolean startMessageIdInclusive, MessageId startMessageId) throws PulsarClientException {
        ReaderBuilder<byte[]> builder = client.newReader(BytesSchema.of()).topic(topic).startMessageId(startMessageId);
        if (startMessageIdInclusive) {
            builder.startMessageIdInclusive();
        }
        return builder.create();
    }

    static Instant asDate(long ts) {
        return Instant.ofEpochMilli(ts);
    }

    void readMessages(PulsarClient client, int timeoutInSeconds, boolean startMessageIdInclusive, MessageId startMessageId, Long seekTime) {
        try (Reader<byte[]> reader = newReader(client, startMessageIdInclusive, startMessageId)) {

            if (seekTime != null) {
                reader.seek(seekTime);
            }

            while(reader.hasMessageAvailable()) {
                Message<byte[]> message = reader.readNext(timeoutInSeconds, TimeUnit.SECONDS);
                if (message == null) {
                    System.out.println("ERROR! hasMessageAvailable returned true, but no message was read after " + timeoutInSeconds + " seconds");
                    break;
                } else {
                    if (seekTime == null) {
                        System.out.println("Publish time: " + asDate(message.getPublishTime()));
                    } else {
                        System.out.println("Publish time: " + asDate(message.getPublishTime()) + ", seek time: " + asDate(seekTime));
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        try {
            final PulsarClient client = PulsarClient.builder()
                    .serviceUrl("pulsar://localhost:6650")
                    .build();
            final ReadProblem rp = new ReadProblem();
            System.out.println("earliest no seek");
            rp.readMessages(client, 5, false, MessageId.earliest, null);
            System.out.println("earliest no seek done");
            System.out.println("latest no seek");
            rp.readMessages(client, 5, false, MessageId.latest, null);
            System.out.println("latest no seek done");


            long[] seekTimestamps = new long[] {
                    // some time after last message published - will fail
                    Instant.parse("2020-08-01T23:03:00Z").toEpochMilli(),
                    // 2 minutes after last message published - will fail
                    Instant.parse("2020-08-01T11:05:00Z").toEpochMilli(),
                    // on the minute when last message published - will succeed
                    Instant.parse("2020-08-01T11:03:00Z").toEpochMilli()
            };

            for (long seekTimestamp : seekTimestamps) {
                System.out.println("earliest w/ seek " + asDate(seekTimestamp));
                rp.readMessages(client, 5, false, MessageId.earliest, seekTimestamp);
                System.out.println("earliest w/ seek done");
                System.out.println("latest w/ seek " + asDate(seekTimestamp));
                rp.readMessages(client, 5, false, MessageId.latest, seekTimestamp);
                System.out.println("latest w/ seek done");

            }
        } catch (PulsarClientException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
}

Running the following program against our cluster will output the following:

earliest no seek
2020-08-06 12:23:19,078 [pulsar-client-io-1-1] WARN  c.s.circe.checksum.Crc32cIntChecksum - Failed to load Circe JNI library. Falling back to Java based CRC32c provider
Publish time: 2020-07-31T21:28:56.739Z
Publish time: 2020-07-31T21:28:56.764Z
Publish time: 2020-07-31T21:28:56.764Z
Publish time: 2020-07-31T21:28:56.764Z
Publish time: 2020-07-31T21:28:56.764Z
Publish time: 2020-07-31T21:28:56.824Z
Publish time: 2020-07-31T21:28:56.860Z
Publish time: 2020-07-31T21:28:56.860Z
Publish time: 2020-07-31T21:28:56.861Z
Publish time: 2020-07-31T21:28:56.861Z
Publish time: 2020-07-31T21:45:33.788Z
Publish time: 2020-07-31T21:45:33.814Z
Publish time: 2020-07-31T21:45:33.814Z
Publish time: 2020-07-31T21:45:33.817Z
Publish time: 2020-07-31T21:45:33.817Z
Publish time: 2020-07-31T21:45:33.886Z
Publish time: 2020-07-31T21:45:33.886Z
Publish time: 2020-07-31T21:45:33.886Z
Publish time: 2020-07-31T21:45:33.886Z
Publish time: 2020-07-31T21:45:33.886Z
Publish time: 2020-08-01T11:03:04.225Z
Publish time: 2020-08-01T11:03:04.245Z
Publish time: 2020-08-01T11:03:04.245Z
Publish time: 2020-08-01T11:03:04.245Z
Publish time: 2020-08-01T11:03:04.245Z
Publish time: 2020-08-01T11:03:04.312Z
Publish time: 2020-08-01T11:03:04.323Z
Publish time: 2020-08-01T11:03:04.323Z
Publish time: 2020-08-01T11:03:04.323Z
Publish time: 2020-08-01T11:03:04.323Z
earliest no seek done
latest no seek
latest no seek done
earliest w/ seek 2020-08-01T23:03:00Z
ERROR! hasMessageAvailable returned true, but no message was read after 5 seconds
earliest w/ seek done
latest w/ seek 2020-08-01T23:03:00Z
ERROR! hasMessageAvailable returned true, but no message was read after 5 seconds
latest w/ seek done
earliest w/ seek 2020-08-01T11:05:00Z
ERROR! hasMessageAvailable returned true, but no message was read after 5 seconds
earliest w/ seek done
latest w/ seek 2020-08-01T11:05:00Z
ERROR! hasMessageAvailable returned true, but no message was read after 5 seconds
latest w/ seek done
earliest w/ seek 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.225Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.245Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.245Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.245Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.245Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.312Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.323Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.323Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.323Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.323Z, seek time: 2020-08-01T11:03:00Z
earliest w/ seek done
latest w/ seek 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.225Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.245Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.245Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.245Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.245Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.312Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.323Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.323Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.323Z, seek time: 2020-08-01T11:03:00Z
Publish time: 2020-08-01T11:03:04.323Z, seek time: 2020-08-01T11:03:00Z
latest w/ seek done

From the output, we can see that:

  1. only when seeking on the exact minute, I get the messages that were published at that minute
  2. other seeking time (much after or two minutes after) will cause the Reader#readNext to timeout

I'm not really looking for a solution (great if we have one), I'm trying to understand what exactly is going on. So maybe insights from the dev team so I can continue my journey/investigation and understand/figure out what exactly is going on.

Expected behaviour

I'm expecting (per the example above) that:

  • either the Reader#hasMessage returns false when the seekTime is after the publishTime
  • either the Reader#hasMessage returns true but Reader#readNext does not block and return the last message

Desktop (please complete the following information):

OS: macOS Catalina 10.15.5

I have the issue when running code from my MAC, but also when my code is running in our cluster on Linux machines.

Activity

  1. added
    type/bug
    on Aug 6, 2020
  2. daddykotex commented on Aug 6, 2020

    @daddykotex
    Author

    As I've said, I'm unable to reproduce in a test.

    Are there some information about the topic/cluster/partitions I could share that could help anyone reproduce the cluster and the problem?

    As far as I know, this topic:

    • as no active publishers
    • as 1 other consumer
  3. aloyszhang commented on Aug 7, 2020

    @aloyszhang
    Contributor

    @daddykotex
    If seekTime is after the last message has been published, then Reader#readNext will be blocked since there is no more message. For non-blocking, you can use readNext(int timeout, TimeUnit unit) instead.
    Why Reader#hasMessageAvailable return true even the seekTime is after the publishTime, I think it's a bug.
    The reason is that Reader#hasMessageAvailable calls ConsumerImpl#getLastMessageIdAsync which will return the last messageId of topic, and 'Reader#seek' has no effect to Reader#hasMessageAvailable.

  4. daddykotex commented on Aug 7, 2020

    @daddykotex
    Author

    Thanks @aloyszhang

    As you can see in the example above, I use readNext(int timeout, TimeUnit unit) and it times out. So it works as expected if I understand what you're saying. But there is still an issue since, as you said, there are no more messages yet hasMessageAvailable still gives true.

    So the following example, in pseudo-code, :

    seek(future) // after all published messages
    if (hasMessageAvailable) {
      msg = readNext()
      if (msg == null) {
        raise "Got null when expecting a message"
      }
    }
    

    Thanks for the information, it confirms what my coworker and I thought.

    But I believe there is still an issue with hasMessageAvailable in this case.

  5. aloyszhang commented on Aug 7, 2020

    @aloyszhang
    Contributor

    @daddykotex
    I also think hasMessageAvailable is an issue.
    It should give false if there are no more (new) messages.
    And I am trying to figure out how to fix it.

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

Metadata

Metadata

Assignees

Labels

area/clienttype/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions