Catch all exceptions when submitting in tx handlers
[controller.git] / opendaylight / md-sal / samples / clustering-test-app / provider / src / main / java / org / opendaylight / controller / clustering / it / provider / impl / WriteTransactionsHandler.java
index a6f687b5b6e0742427543d101be59704f7064b5b..7b5726315784b674737656687b828a377177fe1e 100644 (file)
@@ -9,7 +9,6 @@
 package org.opendaylight.controller.clustering.it.provider.impl;
 
 import com.google.common.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.SettableFuture;
@@ -19,11 +18,12 @@ import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Set;
 import java.util.SplittableRandom;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
-import javax.annotation.Nullable;
+import java.util.concurrent.TimeoutException;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
@@ -47,7 +47,6 @@ import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -144,12 +143,12 @@ public class WriteTransactionsHandler implements Runnable {
         // write only the top list
         tx.merge(LogicalDatastoreType.CONFIGURATION, ID_INTS_YID, containerNode);
         try {
-            tx.submit().checkedGet();
+            tx.submit().checkedGet(125, TimeUnit.SECONDS);
         } catch (final OptimisticLockFailedException e) {
             // when multiple write-transactions are executed concurrently we need to ignore this.
             // If we get optimistic lock here it means id-ints already exists and we can continue.
             LOG.debug("Got an optimistic lock when writing initial top level list element.", e);
-        } catch (final TransactionCommitFailedException e) {
+        } catch (final TransactionCommitFailedException | TimeoutException e) {
             LOG.warn("Unable to ensure IdInts list for id: {} exists.", id, e);
             settableFuture.set(RpcResultBuilder.<WriteTransactionsOutput>failed()
                     .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build());
@@ -165,8 +164,8 @@ public class WriteTransactionsHandler implements Runnable {
         tx.merge(LogicalDatastoreType.CONFIGURATION, idListWithKey, entry);
 
         try {
-            tx.submit().checkedGet();
-        } catch (final TransactionCommitFailedException e) {
+            tx.submit().checkedGet(125, TimeUnit.SECONDS);
+        } catch (final Exception e) {
             LOG.warn("Unable to ensure IdInts list for id: {} exists.", id, e);
             settableFuture.set(RpcResultBuilder.<WriteTransactionsOutput>failed()
                     .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build());
@@ -180,18 +179,14 @@ public class WriteTransactionsHandler implements Runnable {
         LOG.debug("Filling the item list with initial values.");
 
         final CollectionNodeBuilder<MapEntryNode, MapNode> mapBuilder = ImmutableNodes.mapNodeBuilder(ITEM);
-        for (int i = 0; i < MAX_ITEM / 2; i++) {
-            usedValues.add(i);
-            mapBuilder.withChild(ImmutableNodes.mapEntry(ITEM, NUMBER, i));
-        }
 
         final YangInstanceIdentifier itemListId = idListWithKey.node(ITEM);
         final DOMDataWriteTransaction tx = txProvider.createTransaction();
         tx.put(LogicalDatastoreType.CONFIGURATION, itemListId, mapBuilder.build());
 
         try {
-            tx.submit().checkedGet();
-        } catch (final TransactionCommitFailedException e) {
+            tx.submit().checkedGet(125, TimeUnit.SECONDS);
+        } catch (final Exception e) {
             LOG.warn("Unable to fill the initial item list.", e);
             settableFuture.set(RpcResultBuilder.<WriteTransactionsOutput>failed()
                     .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", e).build());
@@ -234,32 +229,29 @@ public class WriteTransactionsHandler implements Runnable {
 
             final ListenableFuture<List<Void>> allFutures = Futures.allAsList(futures);
 
-            Futures.addCallback(allFutures, new FutureCallback<List<Void>>() {
-                @Override
-                public void onSuccess(@Nullable final List<Void> result) {
-                    LOG.debug("All futures completed successfully.");
+            try {
+                // Timeout from cds should be 2 minutes so leave some leeway.
+                allFutures.get(125, TimeUnit.SECONDS);
 
-                    final WriteTransactionsOutput output = new WriteTransactionsOutputBuilder()
-                            .setAllTx(allTx)
-                            .setInsertTx(insertTx)
-                            .setDeleteTx(deleteTx)
-                            .build();
+                LOG.debug("All futures completed successfully.");
 
-                    completionFuture.set(RpcResultBuilder.<WriteTransactionsOutput>success()
-                            .withResult(output).build());
+                final WriteTransactionsOutput output = new WriteTransactionsOutputBuilder()
+                        .setAllTx(allTx)
+                        .setInsertTx(insertTx)
+                        .setDeleteTx(deleteTx)
+                        .build();
 
-                    executor.shutdown();
-                }
+                completionFuture.set(RpcResultBuilder.<WriteTransactionsOutput>success()
+                        .withResult(output).build());
 
-                @Override
-                public void onFailure(final Throwable t) {
-                    LOG.error("Write transactions failed.", t);
-                    completionFuture.set(RpcResultBuilder.<WriteTransactionsOutput>failed()
-                            .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", t).build());
+                executor.shutdown();
+            } catch (Exception exception) {
+                LOG.error("Write transactions failed.", exception);
+                completionFuture.set(RpcResultBuilder.<WriteTransactionsOutput>failed()
+                        .withError(RpcError.ErrorType.APPLICATION, "Unexpected-exception", exception).build());
 
-                    executor.shutdown();
-                }
-            });
+                executor.shutdown();
+            }
         }
     }