You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/08/17 07:41:56 UTC

[GitHub] cbickel closed pull request #3972: Log possible errors around creation of kafka clients.

cbickel closed pull request #3972: Log possible errors around creation of kafka clients.
URL: https://github.com/apache/incubator-openwhisk/pull/3972
 
 
   

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 1f2ea2bacc..0891cee806 100644
--- a/common/scala/src/main/scala/whisk/connector/kafka/KafkaConsumerConnector.scala
+++ b/common/scala/src/main/scala/whisk/connector/kafka/KafkaConsumerConnector.scala
@@ -27,6 +27,7 @@ import whisk.common.{Logging, LoggingMarkers, MetricEmitter, Scheduler}
 import whisk.connector.kafka.KafkaConfiguration._
 import whisk.core.ConfigKeys
 import whisk.core.connector.MessageConsumer
+import whisk.utils.Exceptions
 import whisk.utils.TimeHelpers._
 
 import scala.collection.JavaConverters._
@@ -41,7 +42,8 @@ class KafkaConsumerConnector(
   groupid: String,
   topic: String,
   override val maxPeek: Int = Int.MaxValue)(implicit logging: Logging, actorSystem: ActorSystem)
-    extends MessageConsumer {
+    extends MessageConsumer
+    with Exceptions {
 
   implicit val ec: ExecutionContext = actorSystem.dispatcher
   private val gracefulWaitTime = 100.milliseconds
@@ -148,28 +150,27 @@ class KafkaConsumerConnector(
 
     verifyConfig(config, ConsumerConfig.configNames().asScala.toSet)
 
-    val consumer = new KafkaConsumer(config, new ByteArrayDeserializer, new ByteArrayDeserializer)
+    val consumer = tryAndThrow(s"creating consumer for $topic") {
+      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)
+    tryAndThrow(s"subscribing to $topic")(consumer.subscribe(Seq(topic).asJavaCollection))
+
     consumer
   }
 
   private def recreateConsumer(): Unit = synchronized {
     logging.info(this, s"recreating consumer for '$topic'")
-    try {
-      consumer.close()
-    } catch {
-      // According to documentation, the consumer is force closed if it cannot be closed gracefully.
-      // See https://kafka.apache.org/11/javadoc/index.html?org/apache/kafka/clients/consumer/KafkaConsumer.html
-      //
-      // For the moment, we have no special handling of 'InterruptException' - it may be possible or even
-      // needed to re-try the 'close()' when being interrupted.
-      case t: Throwable =>
-        logging.error(this, s"failed to close old consumer while recreating: $t")
-    }
+    // According to documentation, the consumer is force closed if it cannot be closed gracefully.
+    // See https://kafka.apache.org/11/javadoc/index.html?org/apache/kafka/clients/consumer/KafkaConsumer.html
+    //
+    // For the moment, we have no special handling of 'InterruptException' - it may be possible or even
+    // needed to re-try the 'close()' when being interrupted.
+    tryAndSwallow("closing old consumer")(consumer.close())
     logging.info(this, s"old consumer closed for '$topic'")
+
     consumer = createConsumer(topic)
   }
 
diff --git a/common/scala/src/main/scala/whisk/connector/kafka/KafkaProducerConnector.scala b/common/scala/src/main/scala/whisk/connector/kafka/KafkaProducerConnector.scala
index aea6b3c275..692a149c82 100644
--- a/common/scala/src/main/scala/whisk/connector/kafka/KafkaProducerConnector.scala
+++ b/common/scala/src/main/scala/whisk/connector/kafka/KafkaProducerConnector.scala
@@ -28,6 +28,7 @@ import whisk.connector.kafka.KafkaConfiguration._
 import whisk.core.ConfigKeys
 import whisk.core.connector.{Message, MessageProducer}
 import whisk.core.entity.UUIDs
+import whisk.utils.Exceptions
 
 import scala.collection.JavaConverters._
 import scala.concurrent.duration._
@@ -36,7 +37,8 @@ import scala.util.{Failure, Success}
 
 class KafkaProducerConnector(kafkahosts: String, id: String = UUIDs.randomUUID().toString)(implicit logging: Logging,
                                                                                            actorSystem: ActorSystem)
-    extends MessageProducer {
+    extends MessageProducer
+    with Exceptions {
 
   implicit val ec: ExecutionContext = actorSystem.dispatcher
   private val gracefulWaitTime = 100.milliseconds
@@ -99,12 +101,12 @@ class KafkaProducerConnector(kafkahosts: String, id: String = UUIDs.randomUUID()
 
     verifyConfig(config, ProducerConfig.configNames().asScala.toSet)
 
-    new KafkaProducer(config, new StringSerializer, new StringSerializer)
+    tryAndThrow("creating producer")(new KafkaProducer(config, new StringSerializer, new StringSerializer))
   }
 
   private def recreateProducer(): Unit = {
-    val oldProducer = producer
-    oldProducer.close()
+    logging.info(this, s"recreating producer")
+    tryAndSwallow("closing old producer")(producer.close())
     logging.info(this, s"old producer closed")
     producer = createProducer()
   }
diff --git a/common/scala/src/main/scala/whisk/utils/Exceptions.scala b/common/scala/src/main/scala/whisk/utils/Exceptions.scala
new file mode 100644
index 0000000000..f56f5fbcbd
--- /dev/null
+++ b/common/scala/src/main/scala/whisk/utils/Exceptions.scala
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package whisk.utils
+
+import whisk.common.Logging
+
+import scala.util.control.NonFatal
+
+trait Exceptions {
+
+  /**
+   * Executes the block, catches and logs a NonFatal exception and swallows it.
+   *
+   * @param task description of the task that's being executed
+   * @param block the block to execute
+   */
+  def tryAndSwallow(task: String)(block: => Any)(implicit logging: Logging): Unit = {
+    try block
+    catch {
+      case NonFatal(t) => logging.error(this, s"$task failed: $t")
+    }
+  }
+
+  /**
+   * Executes the block, catches and logs a NonFatal exception and rethrows it.
+   *
+   * @param task description of the task that's being executed
+   * @param block the block to execute
+   */
+  def tryAndThrow[T](task: String)(block: => T)(implicit logging: Logging): T = {
+    try block
+    catch {
+      case NonFatal(t) =>
+        logging.error(this, s"$task failed: $t")
+        throw t
+    }
+  }
+
+}


 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services