Fix a race in PingPong transaction scheduling 36/15636/3
authorRobert Varga <rovarga@cisco.com>
Mon, 23 Feb 2015 22:56:38 +0000 (23:56 +0100)
committerRobert Varga <rovarga@cisco.com>
Tue, 24 Feb 2015 10:13:45 +0000 (11:13 +0100)
We have failed to re-check inflighTx after taking the lock, which meant
that completion and readiness may have raced resulting in warnings about
transaction being submitted while there are others in-flight.

Fix also the shutdown case, which could trigger an unexpected warnings
if there is an inflight transaction.

Change-Id: Ie3a3845335754860a831070d5a73dbc9a4d67d3c
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/PingPongTransactionChain.java

index 961b6c7b9312948ad862fa5236f7ab66ed834534..9895ff9ad5e87af9901b7ffe03b4a2e82726b307 100644 (file)
@@ -158,13 +158,16 @@ public final class PingPongTransactionChain implements DOMTransactionChain {
 
     /*
      * This forces allocateTransaction() on a slow path, which has to happen after
-     * this method has completed executing.
+     * this method has completed executing. Also inflightTx may be updated outside
+     * the lock, hence we need to re-check.
      */
     @GuardedBy("this")
     private void processIfReady() {
-        final PingPongTransaction tx = READY_UPDATER.getAndSet(this, null);
-        if (tx != null) {
-            processTransaction(tx);
+        if (inflightTx == null) {
+            final PingPongTransaction tx = READY_UPDATER.getAndSet(this, null);
+            if (tx != null) {
+                processTransaction(tx);
+            }
         }
     }
 
@@ -251,14 +254,27 @@ public final class PingPongTransactionChain implements DOMTransactionChain {
     }
 
     @Override
-    public void close() {
+    public synchronized void close() {
         final PingPongTransaction notLocked = lockedTx;
         Preconditions.checkState(notLocked == null, "Attempted to close chain with outstanding transaction %s", notLocked);
 
-        synchronized (this) {
-            processIfReady();
-            delegate.close();
+        // Force allocations on slow path. We will complete the rest
+        final PingPongTransaction tx = READY_UPDATER.getAndSet(this, null);
+
+        // Make sure no transaction is outstanding. Otherwise sleep a bit and retry
+        while (inflightTx != null) {
+            LOG.debug("Busy-waiting for in-flight transaction {} to complete", inflightTx);
+            Thread.yield();
+            continue;
         }
+
+        // If we have an outstanding transaction, send it down
+        if (tx != null) {
+            processTransaction(tx);
+        }
+
+        // All done, close the delegate. All new allocations should fail.
+        delegate.close();
     }
 
     @Override