mcdan commented on a change in pull request #4804: Check to see if the user can 
see the topic before creating it,
URL: https://github.com/apache/openwhisk/pull/4804#discussion_r370672251
 
 

 ##########
 File path: 
common/scala/src/main/scala/org/apache/openwhisk/connector/kafka/KafkaMessagingProvider.scala
 ##########
 @@ -68,19 +68,25 @@ object KafkaMessagingProvider extends MessagingProvider {
         val nt = new NewTopic(topic, partitions, 
kafkaConfig.replicationFactor).configs(topicConfig.asJava)
 
         def createTopic(retries: Int = 5): Try[Unit] = {
-          Try(client.createTopics(List(nt).asJava).values().get(topic).get())
-            .map(_ => logging.info(this, s"created topic $topic"))
-            .recoverWith {
-              case CausedBy(_: TopicExistsException) =>
-                Success(logging.info(this, s"topic $topic already existed"))
-              case CausedBy(t: RetriableException) if retries > 0 =>
-                logging.warn(this, s"topic $topic could not be created because 
of $t, retries left: $retries")
-                Thread.sleep(1.second.toMillis)
-                createTopic(retries - 1)
-              case t =>
-                logging.error(this, s"ensureTopic for $topic failed due to $t")
-                Failure(t)
-            }
+          Try(client.listTopics().names().get())
+            .map(topics =>
+              if (topics.contains(topic)) {
+                Success(logging.info(this, s"$topic already exists and the 
user can see it, skipping creation."))
+              } else {
+                
Try(client.createTopics(List(nt).asJava).values().get(topic).get())
+                  .map(_ => logging.info(this, s"created topic $topic"))
+                  .recoverWith {
+                    case CausedBy(_: TopicExistsException) =>
 
 Review comment:
   I didn't want to use exceptions for flow control.
   Right now if the user can't create a topic it will throw a message that says:
   ```
   [2020-01-23T13:03:59.094Z] [INFO] [#tid_sid_unknown] 
[KafkaMessagingProvider] completedstandalone already exists and the user can 
see it, skipping creation.
   [2020-01-23T13:04:00.383Z] [ERROR] [#tid_sid_unknown] 
[KafkaMessagingProvider] ensureTopic for health failed due to 
java.util.concurrent.ExecutionException: 
org.apache.kafka.common.errors.TopicAuthorizationException: Topic authorization 
failed.
   ```
   In this case for this controller startup I didn't pre-create the `health` 
topic. I think that points to a pretty obvious problem.

----------------------------------------------------------------
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

Reply via email to