Bug 8768: Close itemProducer for every code path 50/59850/2
authorVratko Polak <vrpolak@cisco.com>
Thu, 29 Jun 2017 11:50:07 +0000 (13:50 +0200)
committerRobert Varga <nite@hq.sk>
Mon, 3 Jul 2017 11:08:19 +0000 (11:08 +0000)
Change-Id: Ib87de13e2a0e6f128f74a05b80ffb4331e345d2c
Signed-off-by: Vratko Polak <vrpolak@cisco.com>
(cherry picked from commit 35b7e595945a1386047c1af73c94b70fbdaf9a59)

opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/ProduceTransactionsHandler.java

index b1348c2fc42c2fc03f6be77bb9aafe9de864d734..05fd59d218753da08237da80a101975c1999cdbb 100644 (file)
@@ -79,12 +79,7 @@ public class ProduceTransactionsHandler extends AbstractTransactionHandler {
             tx.submit().checkedGet(INIT_TX_TIMEOUT_SECONDS, TimeUnit.SECONDS);
         } catch (final Exception e) {
             LOG.warn("Unable to fill the initial item list.", e);
             tx.submit().checkedGet(INIT_TX_TIMEOUT_SECONDS, TimeUnit.SECONDS);
         } catch (final Exception e) {
             LOG.warn("Unable to fill the initial item list.", e);
-
-            try {
-                itemProducer.close();
-            } catch (final DOMDataTreeProducerException exception) {
-                LOG.warn("Failure while closing producer.", exception);
-            }
+            closeProducer(itemProducer);
 
             return Futures.immediateFuture(RpcResultBuilder.<ProduceTransactionsOutput>failed()
                 .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build());
 
             return Futures.immediateFuture(RpcResultBuilder.<ProduceTransactionsOutput>failed()
                 .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build());
@@ -93,10 +88,19 @@ public class ProduceTransactionsHandler extends AbstractTransactionHandler {
         final ProduceTransactionsHandler handler = new ProduceTransactionsHandler(itemProducer,
             new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, idListWithKey.node(list.getIdentifier())
                 .toOptimized()), input);
         final ProduceTransactionsHandler handler = new ProduceTransactionsHandler(itemProducer,
             new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, idListWithKey.node(list.getIdentifier())
                 .toOptimized()), input);
+        // It is handler's responsibility to close itemProducer when the work is finished.
         handler.doStart();
         return handler.future;
     }
 
         handler.doStart();
         return handler.future;
     }
 
+    private static void closeProducer(final DOMDataTreeProducer producer) {
+        try {
+            producer.close();
+        } catch (final DOMDataTreeProducerException exception) {
+            LOG.warn("Failure while closing producer.", exception);
+        }
+    }
+
     @Override
     ListenableFuture<Void> execWrite(final long txId) {
         final int i = random.nextInt(MAX_ITEM + 1);
     @Override
     ListenableFuture<Void> execWrite(final long txId) {
         final int i = random.nextInt(MAX_ITEM + 1);
@@ -127,12 +131,14 @@ public class ProduceTransactionsHandler extends AbstractTransactionHandler {
 
     @Override
     void runFailed(final Throwable cause) {
 
     @Override
     void runFailed(final Throwable cause) {
+        closeProducer(itemProducer);
         future.set(RpcResultBuilder.<ProduceTransactionsOutput>failed()
             .withError(RpcError.ErrorType.APPLICATION, "Submit failed", cause).build());
     }
 
     @Override
     void runSuccessful(final long allTx) {
         future.set(RpcResultBuilder.<ProduceTransactionsOutput>failed()
             .withError(RpcError.ErrorType.APPLICATION, "Submit failed", cause).build());
     }
 
     @Override
     void runSuccessful(final long allTx) {
+        closeProducer(itemProducer);
         final ProduceTransactionsOutput output = new ProduceTransactionsOutputBuilder()
                 .setAllTx(allTx)
                 .setInsertTx(insertTx)
         final ProduceTransactionsOutput output = new ProduceTransactionsOutputBuilder()
                 .setAllTx(allTx)
                 .setInsertTx(insertTx)
@@ -144,6 +150,7 @@ public class ProduceTransactionsHandler extends AbstractTransactionHandler {
 
     @Override
     void runTimedOut(final Exception cause) {
 
     @Override
     void runTimedOut(final Exception cause) {
+        closeProducer(itemProducer);
         future.set(RpcResultBuilder.<ProduceTransactionsOutput>failed()
             .withError(RpcError.ErrorType.APPLICATION,
                     "Final submit was timed out by the test provider or was interrupted", cause).build());
         future.set(RpcResultBuilder.<ProduceTransactionsOutput>failed()
             .withError(RpcError.ErrorType.APPLICATION,
                     "Final submit was timed out by the test provider or was interrupted", cause).build());