Github user zsxwing commented on a diff in the pull request:
https://github.com/apache/spark/pull/15527#discussion_r85265053
--- Diff:
external/kafka-0-10-sql/src/main/scala/org/apache/spark/sql/kafka010/KafkaSource.scala
---
@@ -121,16 +124,61 @@ private[kafka010] case class KafkaSource(
}.partitionToOffsets
}
+ private var currentPartitionOffsets: Option[Map[TopicPartition, Long]] =
None
+
override def schema: StructType = KafkaSource.kafkaSchema
/** Returns the maximum available offset for this source. */
override def getOffset: Option[Offset] = {
// Make sure initialPartitionOffsets is initialized
initialPartitionOffsets
- val offset = KafkaSourceOffset(fetchLatestOffsets())
- logDebug(s"GetOffset:
${offset.partitionToOffsets.toSeq.map(_.toString).sorted}")
- Some(offset)
+ val latest = fetchLatestOffsets()
+ val offsets = maxOffsetsPerTrigger match {
+ case None =>
+ latest
+ case Some(limit) if currentPartitionOffsets.isEmpty =>
+ rateLimit(limit, initialPartitionOffsets, latest)
+ case Some(limit) =>
+ rateLimit(limit, currentPartitionOffsets.get, latest)
+ }
+
+ currentPartitionOffsets = Some(offsets)
+ logDebug(s"GetOffset: ${offsets.toSeq.map(_.toString).sorted}")
+ Some(KafkaSourceOffset(offsets))
+ }
+
+ /** Proportionally distribute limit number of offsets among
topicpartitions */
+ private def rateLimit(
+ limit: Long,
+ from: Map[TopicPartition, Long],
+ until: Map[TopicPartition, Long]): Map[TopicPartition, Long] = {
+ val fromNew =
fetchNewPartitionEarliestOffsets(until.keySet.diff(from.keySet).toSeq)
+ val sizes = until.flatMap { case (tp, end) =>
+ // If begin isn't defined, something's wrong, but let alert logic
in getBatch handle it
+ from.get(tp).orElse(fromNew.get(tp)).flatMap { begin =>
+ val size = end - begin
+ logDebug(s"rateLimit $tp size is $size")
+ if (size > 0) Some(tp -> size) else None
+ }
+ }
+ val total = sizes.values.sum.toDouble
+ if (total < 1) {
+ until
+ } else {
+ until.map { case (tp, end) =>
+ tp -> sizes.get(tp).map { size =>
--- End diff --
nit: use 2 spaces
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]