11/*
2- * Copyright 2017-2018 the original author or authors.
2+ * Copyright 2017-2019 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
3636import org .apache .kafka .clients .admin .NewPartitions ;
3737import org .apache .kafka .clients .admin .NewTopic ;
3838import org .apache .kafka .clients .admin .TopicDescription ;
39+ import org .apache .kafka .common .errors .InvalidPartitionsException ;
40+ import org .apache .kafka .common .errors .TopicExistsException ;
3941import org .apache .kafka .common .errors .UnsupportedVersionException ;
4042
4143import org .springframework .beans .BeansException ;
@@ -206,6 +208,7 @@ private void addTopicsIfNeeded(AdminClient adminClient, Collection<NewTopic> top
206208
207209 private Map <String , NewPartitions > checkPartitions (Map <String , NewTopic > topicNameToTopic ,
208210 DescribeTopicsResult topicInfo , List <NewTopic > topicsToAdd ) {
211+
209212 Map <String , NewPartitions > topicsToModify = new HashMap <>();
210213 topicInfo .values ().forEach ((n , f ) -> {
211214 NewTopic topic = topicNameToTopic .get (n );
@@ -254,8 +257,13 @@ private void addTopics(AdminClient adminClient, List<NewTopic> topicsToAdd) {
254257 throw new KafkaException ("Timed out waiting for create topics results" , e );
255258 }
256259 catch (ExecutionException e ) {
257- logger .error ("Failed to create topics" , e .getCause ());
258- throw new KafkaException ("Failed to create topics" , e .getCause ()); // NOSONAR
260+ if (e .getCause () instanceof TopicExistsException ) { // Possible race with another app instance
261+ logger .debug ("Failed to create topics" , e .getCause ());
262+ }
263+ else {
264+ logger .error ("Failed to create topics" , e .getCause ());
265+ throw new KafkaException ("Failed to create topics" , e .getCause ()); // NOSONAR
266+ }
259267 }
260268 }
261269
@@ -272,9 +280,14 @@ private void modifyTopics(AdminClient adminClient, Map<String, NewPartitions> to
272280 throw new KafkaException ("Timed out waiting for create partitions results" , e );
273281 }
274282 catch (ExecutionException e ) {
275- logger .error ("Failed to create partitions" , e .getCause ());
276- if (!(e .getCause () instanceof UnsupportedVersionException )) {
277- throw new KafkaException ("Failed to create partitions" , e .getCause ()); // NOSONAR
283+ if (e .getCause () instanceof InvalidPartitionsException ) { // Possible race with another app instance
284+ logger .debug ("Failed to create partitions" , e .getCause ());
285+ }
286+ else {
287+ logger .error ("Failed to create partitions" , e .getCause ());
288+ if (!(e .getCause () instanceof UnsupportedVersionException )) {
289+ throw new KafkaException ("Failed to create partitions" , e .getCause ()); // NOSONAR
290+ }
278291 }
279292 }
280293 }
0 commit comments