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 all 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
26 changes: 15 additions & 11 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 @@ -442,22 +441,27 @@ private[pulsar] case class PulsarHelper(
topic: String,
time: Option[Long],
offset: Option[MessageId]): MessageIdImpl = {
val (subscriptionName, _) = extractSubscription(predefinedSubscription, topic)
val consumer =
CachedConsumer.getOrCreate(topic, subscriptionName, client.asInstanceOf[PulsarClient])

// 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) {
val reader = client
.newReader()
.subscriptionRolePrefix(driverGroupIdPrefix)
.topic(topic)
.startMessageId(offset.getOrElse(MessageId.earliest))
.startMessageIdInclusive()
.create()
(time, offset) match {
case (None, Some(o)) => consumer.seek(o)
case (Some(t), None) => consumer.seek(t)
case (None, Some(o)) =>
case (Some(t), None) => reader.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 = reader.readNext()
reader.close()
if (message != null) UserProvidedMessageId(PulsarSourceUtils.mid2Impl(message.getMessageId))
else UserProvidedMessageId(MessageId.earliest)
} else {
UserProvidedMessageId(MessageId.earliest)
}
Expand Down
Loading