Improve error reporting in WriteTransactionsHandler 11/78311/2
authorTom Pantelis <tompantelis@gmail.com>
Fri, 30 Nov 2018 03:32:48 +0000 (22:32 -0500)
committerTom Pantelis <tompantelis@gmail.com>
Fri, 30 Nov 2018 04:31:48 +0000 (23:31 -0500)
The Leader Isolation CSIT is failing intermittently with a failed
tx and it would help to log the exception trace as restconf no longer
reports it. Also modified some fields to be thread-safe as they are
accessed by multiple threads although not concurrently/

Change-Id: Ifce3cd177e5e0cc7c7020866000cb915b9f9c4bf
Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/AbstractTransactionHandler.java
opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/ProduceTransactionsHandler.java
opendaylight/md-sal/samples/clustering-test-app/provider/src/main/java/org/opendaylight/controller/clustering/it/provider/impl/WriteTransactionsHandler.java

index 90cdce3..1a0f39e 100644 (file)
@@ -19,6 +19,7 @@ import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicLong;
 import org.opendaylight.yang.gen.v1.tag.opendaylight.org._2017.controller.yang.lowlevel.control.rev170215.TransactionsParams;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -76,7 +77,7 @@ abstract class AbstractTransactionHandler {
 
     private ScheduledFuture<?> writingFuture;
     private ScheduledFuture<?> completingFuture;
-    private long txCounter;
+    private final AtomicLong txCounter = new AtomicLong();
     private volatile State state;
 
     AbstractTransactionHandler(final TransactionsParams params) {
@@ -117,7 +118,7 @@ abstract class AbstractTransactionHandler {
         }
 
         // Not completed yet: create a transaction and hook it up
-        final long txId = txCounter++;
+        final long txId = txCounter.incrementAndGet();
         final ListenableFuture<?> execFuture = execWrite(txId);
         LOG.debug("New future #{} allocated", txId);
 
@@ -150,7 +151,7 @@ abstract class AbstractTransactionHandler {
             LOG.debug("Completed waiting for all futures");
             state = State.SUCCESSFUL;
             completingFuture.cancel(false);
-            runSuccessful(txCounter);
+            runSuccessful(txCounter.get());
             return true;
         }
 
@@ -176,7 +177,7 @@ abstract class AbstractTransactionHandler {
     }
 
     final void txFailure(final ListenableFuture<?> execFuture, final long txId, final Throwable cause) {
-        LOG.debug("Future #{} failed", txId, cause);
+        LOG.error("Commit future failed for tx # {}", txId, cause);
         futures.remove(execFuture);
 
         final State local = state;
@@ -188,7 +189,7 @@ abstract class AbstractTransactionHandler {
             case WAITING:
                 state = State.FAILED;
                 writingFuture.cancel(false);
-                runFailed(cause);
+                runFailed(cause, txId);
                 break;
             default:
                 throw new IllegalStateException("Unhandled state " + local);
@@ -221,14 +222,14 @@ abstract class AbstractTransactionHandler {
         }
 
         state = State.FAILED;
-        runTimedOut(new TimeoutException("Collection did not finish in " + DEAD_TIMEOUT_SECONDS + " seconds"));
+        runTimedOut("Transactions did not finish in " + DEAD_TIMEOUT_SECONDS + " seconds");
     }
 
     abstract ListenableFuture<?> execWrite(long txId);
 
-    abstract void runFailed(Throwable cause);
+    abstract void runFailed(Throwable cause, long txId);
 
     abstract void runSuccessful(long allTx);
 
-    abstract void runTimedOut(Exception cause);
+    abstract void runTimedOut(String cause);
 }
index 99e48b2..db29583 100644 (file)
@@ -135,10 +135,10 @@ public final class ProduceTransactionsHandler extends AbstractTransactionHandler
     }
 
     @Override
-    void runFailed(final Throwable cause) {
+    void runFailed(final Throwable cause, final long txId) {
         closeProducer(itemProducer);
         future.set(RpcResultBuilder.<ProduceTransactionsOutput>failed()
-            .withError(RpcError.ErrorType.APPLICATION, "Submit failed", cause).build());
+            .withError(RpcError.ErrorType.APPLICATION, "Commit failed for tx # " + txId, cause).build());
     }
 
     @Override
@@ -154,10 +154,9 @@ public final class ProduceTransactionsHandler extends AbstractTransactionHandler
     }
 
     @Override
-    void runTimedOut(final Exception cause) {
+    void runTimedOut(final String 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());
+            .withError(RpcError.ErrorType.APPLICATION, cause).build());
     }
 }
index 6426bc0..e2418dc 100644 (file)
@@ -12,13 +12,14 @@ import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.SettableFuture;
-import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.Set;
 import java.util.SplittableRandom;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicLong;
 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;
@@ -115,11 +116,11 @@ public abstract class WriteTransactionsHandler extends AbstractTransactionHandle
     private static final Logger LOG = LoggerFactory.getLogger(WriteTransactionsHandler.class);
 
     final SettableFuture<RpcResult<WriteTransactionsOutput>> completionFuture = SettableFuture.create();
-    private final Set<Integer> usedValues = new HashSet<>();
+    private final Set<Integer> usedValues = ConcurrentHashMap.newKeySet();
     private final YangInstanceIdentifier idListItem;
 
-    private long insertTx = 0;
-    private long deleteTx = 0;
+    private final AtomicLong insertTx = new AtomicLong();
+    private final AtomicLong deleteTx = new AtomicLong();
 
     WriteTransactionsHandler(final YangInstanceIdentifier idListItem, final WriteTransactionsInput input) {
         super(input);
@@ -205,13 +206,13 @@ public abstract class WriteTransactionsHandler extends AbstractTransactionHandle
 
         if (usedValues.contains(i)) {
             LOG.debug("Deleting item: {}", i);
-            deleteTx++;
+            deleteTx.incrementAndGet();
             tx.delete(LogicalDatastoreType.CONFIGURATION, entryId);
             usedValues.remove(i);
 
         } else {
             LOG.debug("Inserting item: {}", i);
-            insertTx++;
+            insertTx.incrementAndGet();
             final MapEntryNode entry = ImmutableNodes.mapEntry(ITEM, NUMBER, i);
             tx.put(LogicalDatastoreType.CONFIGURATION, entryId, entry);
             usedValues.add(i);
@@ -221,17 +222,17 @@ public abstract class WriteTransactionsHandler extends AbstractTransactionHandle
     }
 
     @Override
-    void runFailed(final Throwable cause) {
+    void runFailed(final Throwable cause, final long txId) {
         completionFuture.set(RpcResultBuilder.<WriteTransactionsOutput>failed()
-            .withError(RpcError.ErrorType.APPLICATION, "Submit failed", cause).build());
+            .withError(RpcError.ErrorType.APPLICATION, "Commit failed for tx # " + txId, cause).build());
     }
 
     @Override
     void runSuccessful(final long allTx) {
         final WriteTransactionsOutput output = new WriteTransactionsOutputBuilder()
                 .setAllTx(allTx)
-                .setInsertTx(insertTx)
-                .setDeleteTx(deleteTx)
+                .setInsertTx(insertTx.get())
+                .setDeleteTx(deleteTx.get())
                 .build();
 
         completionFuture.set(RpcResultBuilder.<WriteTransactionsOutput>success()
@@ -239,10 +240,9 @@ public abstract class WriteTransactionsHandler extends AbstractTransactionHandle
     }
 
     @Override
-    void runTimedOut(final Exception cause) {
+    void runTimedOut(final String cause) {
         completionFuture.set(RpcResultBuilder.<WriteTransactionsOutput>failed()
-            .withError(RpcError.ErrorType.APPLICATION,
-                    "Final submit was timed out by the test provider or was interrupted", cause).build());
+            .withError(RpcError.ErrorType.APPLICATION, cause).build());
     }
 
     abstract DOMDataWriteTransaction createTransaction();

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.