Skip to content
Closed
Show file tree
Hide file tree
Changes from 16 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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private[kafka010] case class CachedKafkaConsumer private(

private val groupId = kafkaParams.get(ConsumerConfig.GROUP_ID_CONFIG).asInstanceOf[String]

private var consumer = createConsumer
var rawConsumer = createConsumer
Copy link
Contributor

Choose a reason for hiding this comment

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

exposing internal var is generally not a good idea. A better approach is be to add the necessary methods (for which you need the consumer) in the class CachedKafkaConsumer.

Copy link
Contributor

Choose a reason for hiding this comment

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

and why renamed?


/** Iterator to the already fetch data */
private var fetchedData = ju.Collections.emptyIterator[ConsumerRecord[Array[Byte], Array[Byte]]]
Expand Down Expand Up @@ -224,8 +224,8 @@ private[kafka010] case class CachedKafkaConsumer private(

/** Create a new consumer and reset cached states */
private def resetConsumer(): Unit = {
consumer.close()
consumer = createConsumer
rawConsumer.close()
rawConsumer = createConsumer
resetFetchedData()
}

Expand Down Expand Up @@ -271,15 +271,15 @@ private[kafka010] case class CachedKafkaConsumer private(
}
}

private def close(): Unit = consumer.close()
private def close(): Unit = rawConsumer.close()

private def seek(offset: Long): Unit = {
logDebug(s"Seeking to $groupId $topicPartition $offset")
consumer.seek(topicPartition, offset)
rawConsumer.seek(topicPartition, offset)
}

private def poll(pollTimeoutMs: Long): Unit = {
val p = consumer.poll(pollTimeoutMs)
val p = rawConsumer.poll(pollTimeoutMs)
val r = p.records(topicPartition)
logDebug(s"Polled $groupId ${p.partitions()} ${r.size}")
fetchedData = r.iterator
Expand All @@ -290,10 +290,10 @@ private[kafka010] case class CachedKafkaConsumer private(
* and the latest offset.
*/
private def getAvailableOffsetRange(): (Long, Long) = {
consumer.seekToBeginning(Set(topicPartition).asJava)
val earliestOffset = consumer.position(topicPartition)
consumer.seekToEnd(Set(topicPartition).asJava)
val latestOffset = consumer.position(topicPartition)
rawConsumer.seekToBeginning(Set(topicPartition).asJava)
val earliestOffset = rawConsumer.position(topicPartition)
rawConsumer.seekToEnd(Set(topicPartition).asJava)
val latestOffset = rawConsumer.position(topicPartition)
(earliestOffset, latestOffset)
}
}
Expand Down Expand Up @@ -334,14 +334,15 @@ private[kafka010] object CachedKafkaConsumer extends Logging {
def getOrCreate(
topic: String,
partition: Int,
kafkaParams: ju.Map[String, Object]): CachedKafkaConsumer = synchronized {
kafkaParams: ju.Map[String, Object],
reuse: Boolean): CachedKafkaConsumer = synchronized {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this mean reuse existing one, OR allow reuse in future?

Copy link
Author

Choose a reason for hiding this comment

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

reuse existing. I changed the name to reuseExistingIfPresent.

val groupId = kafkaParams.get(ConsumerConfig.GROUP_ID_CONFIG).asInstanceOf[String]
val topicPartition = new TopicPartition(topic, partition)
val key = CacheKey(groupId, topicPartition)

// If this is reattempt at running the task, then invalidate cache and start with
// a new consumer
if (TaskContext.get != null && TaskContext.get.attemptNumber > 1) {
if (!reuse || TaskContext.get != null && TaskContext.get.attemptNumber > 1) {
val removedConsumer = cache.remove(key)
if (removedConsumer != null) {
removedConsumer.close()
Expand Down
Loading