sunjincheng121 commented on a change in pull request #8653: 
[FLINK-10455][Connectors/Kafka] Ensure all the KafkaProducers are recycled when 
FlinkKafk…
URL: https://github.com/apache/flink/pull/8653#discussion_r291848024
 
 

 ##########
 File path: 
flink-connectors/flink-connector-kafka-0.11/src/main/java/org/apache/flink/streaming/connectors/kafka/FlinkKafkaProducer011.java
 ##########
 @@ -656,33 +656,40 @@ public void invoke(KafkaTransactionState transaction, IN 
next, Context context)
 
        @Override
        public void close() throws FlinkKafka011Exception {
-               final KafkaTransactionState currentTransaction = 
currentTransaction();
-               if (currentTransaction != null) {
-                       // to avoid exceptions on aborting transactions with 
some pending records
-                       flush(currentTransaction);
-
-                       // normal abort for AT_LEAST_ONCE and NONE do not clean 
up resources because of producer reusing, thus
-                       // we need to close it manually
-                       switch (semantic) {
-                               case EXACTLY_ONCE:
-                                       break;
-                               case AT_LEAST_ONCE:
-                               case NONE:
-                                       currentTransaction.producer.close();
-                                       break;
-                       }
-               }
+               // First close the producer for current transaction.
                try {
+                       final KafkaTransactionState currentTransaction = 
currentTransaction();
+                       if (currentTransaction != null) {
+                               // to avoid exceptions on aborting transactions 
with some pending records
+                               flush(currentTransaction);
+
+                               // normal abort for AT_LEAST_ONCE and NONE do 
not clean up resources because of producer reusing, thus
+                               // we need to close it manually
+                               switch (semantic) {
+                                       case EXACTLY_ONCE:
+                                               break;
+                                       case AT_LEAST_ONCE:
+                                       case NONE:
+                                               
currentTransaction.producer.close();
+                                               break;
+                               }
+                       }
                        super.close();
-               }
-               catch (Exception e) {
+               } catch (Exception e) {
                        asyncException = ExceptionUtils.firstOrSuppressed(e, 
asyncException);
+               } finally {
+                       // We may have to close producer of the current 
transaction in case some exception was thrown before
+                       // the normal close routine finishes.
+                       if (currentTransaction() != null) {
+                               
IOUtils.closeQuietly(currentTransaction().producer);
+                       }
+                       // Make sure all the producers for pending transactions 
are closed.
+                       pendingTransactions().forEach(transaction ->
+                                                                               
          IOUtils.closeQuietly(transaction.getValue().producer)
 
 Review comment:
   Code format error, detail can be found 
[here](https://api.travis-ci.org/v3/job/542303676/log.txt):
   FlinkKafkaProducer011.java:[688] (regexp) RegexpSinglelineJava: Line has 
leading space characters; indentation should be performed with tabs only.
   

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