Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issue of getting initial offset with user provided start #149

Merged
merged 5 commits into from
Aug 14, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/main/scala/org/apache/spark/sql/pulsar/PulsarHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import scala.language.postfixOps
import scala.util.control.NonFatal

import org.apache.pulsar.client.api.{MessageId, PulsarClient}
import org.apache.pulsar.client.api.schema.GenericRecord
import org.apache.pulsar.client.impl.{ConsumerImpl, MessageIdImpl, PulsarClientImpl}
import org.apache.pulsar.client.impl.{MessageIdImpl, PulsarClientImpl}
import org.apache.pulsar.client.impl.schema.BytesSchema
import org.apache.pulsar.common.api.proto.CommandGetTopicsOfNamespace
import org.apache.pulsar.common.naming.TopicName
Expand Down Expand Up @@ -448,16 +447,21 @@ private[pulsar] case class PulsarHelper(

// seek to specified offset or time and get the actual corresponding message id
// we don't call consumer.acknowledge() here because the message is not processed yet
if (consumer.asInstanceOf[ConsumerImpl[GenericRecord]].hasMessageAvailable) {
if (time.isDefined || offset.isDefined) {
// Call getLastMessageId to reconnect the consumer before seeking.
if (!consumer.isConnected) consumer.getLastMessageId
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call seems weird to me. Shouldn't the seek method also handle reconnecting?

(time, offset) match {
case (None, Some(o)) => consumer.seek(o)
case (Some(t), None) => consumer.seek(t)
case _ =>
throw new IllegalArgumentException(
s"one of time and offset should be set. time: $time, offset: $offset")
}

PulsarSourceUtils.mid2Impl(consumer.receive().getMessageId)
val message = consumer.receive()
// This must be wrapped in UserProvidedMessageId to prevent first message from being skipped
// because start offset is exclusive by default.
if (message != null) UserProvidedMessageId(PulsarSourceUtils.mid2Impl(message.getMessageId))
else UserProvidedMessageId(MessageId.earliest)
} else {
UserProvidedMessageId(MessageId.earliest)
}
Expand Down