chetanmeh closed pull request #3921: Synchronize access to the KafkaConsumer.
URL: https://github.com/apache/incubator-openwhisk/pull/3921
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/common/scala/src/main/scala/whisk/connector/kafka/KafkaConsumerConnector.scala
 
b/common/scala/src/main/scala/whisk/connector/kafka/KafkaConsumerConnector.scala
index bc88907528..1f2ea2bacc 100644
--- 
a/common/scala/src/main/scala/whisk/connector/kafka/KafkaConsumerConnector.scala
+++ 
b/common/scala/src/main/scala/whisk/connector/kafka/KafkaConsumerConnector.scala
@@ -74,7 +74,7 @@ class KafkaConsumerConnector(
     }
 
     try {
-      val response = consumer.poll(duration).asScala
+      val response = synchronized(consumer.poll(duration)).asScala
 
       // Cancel the scheduled wake-up task immediately.
       wakeUpTask.cancel()
@@ -120,7 +120,7 @@ class KafkaConsumerConnector(
    */
   def commit(retry: Int = 3): Unit =
     try {
-      consumer.commitSync()
+      synchronized(consumer.commitSync())
     } catch {
       case e: RetriableException =>
         if (retry > 0) {
@@ -132,7 +132,7 @@ class KafkaConsumerConnector(
         }
     }
 
-  override def close(): Unit = {
+  override def close(): Unit = synchronized {
     logging.info(this, s"closing consumer for '$topic'")
     consumer.close()
   }
@@ -149,11 +149,14 @@ class KafkaConsumerConnector(
     verifyConfig(config, ConsumerConfig.configNames().asScala.toSet)
 
     val consumer = new KafkaConsumer(config, new ByteArrayDeserializer, new 
ByteArrayDeserializer)
+
+    // subscribe does not need to be synchronized, because the reference to 
the consumer hasn't been returned yet and
+    // thus this is guaranteed only to be called by the calling thread.
     consumer.subscribe(Seq(topic).asJavaCollection)
     consumer
   }
 
-  private def recreateConsumer(): Unit = {
+  private def recreateConsumer(): Unit = synchronized {
     logging.info(this, s"recreating consumer for '$topic'")
     try {
       consumer.close()
@@ -182,10 +185,11 @@ class KafkaConsumerConnector(
       blocking {
         if (offset > 0) {
           val topicAndPartition = new TopicPartition(topic, 0)
-          
consumer.endOffsets(Set(topicAndPartition).asJava).asScala.get(topicAndPartition).foreach
 { endOffset =>
-            // endOffset could lag behind the offset reported by the consumer 
internally resulting in negative numbers
-            val queueSize = (endOffset - offset).max(0)
-            MetricEmitter.emitHistogramMetric(queueMetric, queueSize)
+          
synchronized(consumer.endOffsets(Set(topicAndPartition).asJava)).asScala.get(topicAndPartition).foreach
 {
+            endOffset =>
+              // endOffset could lag behind the offset reported by the 
consumer internally resulting in negative numbers
+              val queueSize = (endOffset - offset).max(0)
+              MetricEmitter.emitHistogramMetric(queueMetric, queueSize)
           }
         }
       }


 

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


With regards,
Apache Git Services

Reply via email to