skonto commented on a change in pull request #19096: [SPARK-21869][SS] A cached 
Kafka producer should not be closed if any task is using it - adds inuse 
tracking.
URL: https://github.com/apache/spark/pull/19096#discussion_r263327384
 
 

 ##########
 File path: 
external/kafka-0-10-sql/src/main/scala/org/apache/spark/sql/kafka010/CachedKafkaProducer.scala
 ##########
 @@ -86,27 +112,38 @@ private[kafka010] object CachedKafkaProducer extends 
Logging {
     paramsSeq
   }
 
-  /** For explicitly closing kafka producer */
+  /** For explicitly closing kafka producer, will not close an inuse producer 
until released. */
   private[kafka010] def close(kafkaParams: ju.Map[String, Object]): Unit = {
     val paramsSeq = paramsToSeq(kafkaParams)
     guavaCache.invalidate(paramsSeq)
   }
 
-  /** Auto close on cache evict */
-  private def close(paramsSeq: Seq[(String, Object)], producer: Producer): 
Unit = {
+  /** Close this producer and process pending closes. */
+  private def close(producer: CachedKafkaProducer): Unit = synchronized {
     try {
-      logInfo(s"Closing the KafkaProducer with params: 
${paramsSeq.mkString("\n")}.")
       producer.close()
+      // Check and close any producers evicted, and pending to be closed.
+      for (p <- closeQueue.iterator().asScala) {
+        if (p.inUseCount.intValue() <= 0) {
+          producer.close()
 
 Review comment:
   Could you print the producer UUID here as well for tracking reasons?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to