BUG 3340 : Improve logging
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / TransactionChainProxy.java
index b44f0b15b29fb33620c14c4b1d38c8ba3d3a906d..0946b402fd9bb02b3129d48d5f5236ea605510a5 100644 (file)
@@ -13,6 +13,7 @@ import com.google.common.base.Preconditions;
 import java.util.Collection;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+import org.opendaylight.controller.cluster.datastore.identifiers.TransactionChainIdentifier;
 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
@@ -115,18 +116,19 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory<Loca
     private static final AtomicReferenceFieldUpdater<TransactionChainProxy, State> STATE_UPDATER =
             AtomicReferenceFieldUpdater.newUpdater(TransactionChainProxy.class, State.class, "currentState");
 
-    private final String transactionChainId;
+    private final TransactionChainIdentifier transactionChainId;
     private final TransactionContextFactory parent;
     private volatile State currentState = IDLE_STATE;
 
     TransactionChainProxy(final TransactionContextFactory parent) {
         super(parent.getActorContext());
-        transactionChainId = parent.getActorContext().getCurrentMemberName() + "-txn-chain-" + CHAIN_COUNTER.incrementAndGet();
+
+        transactionChainId = new TransactionChainIdentifier(parent.getActorContext().getCurrentMemberName(), CHAIN_COUNTER.incrementAndGet());
         this.parent = parent;
     }
 
     public String getTransactionChainId() {
-        return transactionChainId;
+        return transactionChainId.toString();
     }
 
     @Override
@@ -152,8 +154,7 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory<Loca
         currentState = CLOSED_STATE;
 
         // Send a close transaction chain request to each and every shard
-        getActorContext().broadcast(new CloseTransactionChain(transactionChainId).toSerializable());
-        parent.removeTransactionChain(this);
+        getActorContext().broadcast(new CloseTransactionChain(transactionChainId.toString()).toSerializable());
     }
 
     private TransactionProxy allocateWriteTransaction(final TransactionType type) {
@@ -172,11 +173,6 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory<Loca
         return ret;
     }
 
-    @Override
-    protected DataTree dataTreeForFactory(final LocalTransactionChain factory) {
-        return factory.getDataTree();
-    }
-
     /**
      * This method is overridden to ensure the previous Tx's ready operations complete
      * before we initiate the next Tx in the chain to avoid creation failures if the
@@ -193,7 +189,15 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory<Loca
             return parent.findPrimaryShard(shardName);
         }
 
-        LOG.debug("Waiting for ready futures for on chain {}", getTransactionChainId());
+        final String previousTransactionId;
+
+        if(localState instanceof Pending){
+            previousTransactionId = ((Pending) localState).getIdentifier().toString();
+            LOG.debug("Waiting for ready futures with pending Tx {}", previousTransactionId);
+        } else {
+            previousTransactionId = "";
+            LOG.debug("Waiting for ready futures on chain {}", getTransactionChainId());
+        }
 
         // Add a callback for completion of the combined Futures.
         final Promise<PrimaryShardInfo> returnPromise = akka.dispatch.Futures.promise();
@@ -203,10 +207,11 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory<Loca
             public void onComplete(final Throwable failure, final Object notUsed) {
                 if (failure != null) {
                     // A Ready Future failed so fail the returned Promise.
+                    LOG.error("Ready future failed for Tx {}", previousTransactionId);
                     returnPromise.failure(failure);
                 } else {
-                    LOG.debug("Previous Tx readied - proceeding to FindPrimaryShard on chain {}",
-                            getTransactionChainId());
+                    LOG.debug("Previous Tx {} readied - proceeding to FindPrimaryShard",
+                            previousTransactionId);
 
                     // Send the FindPrimaryShard message and use the resulting Future to complete the
                     // returned Promise.
@@ -252,6 +257,6 @@ final class TransactionChainProxy extends AbstractTransactionContextFactory<Loca
 
     @Override
     protected TransactionIdentifier nextIdentifier() {
-        return TransactionIdentifier.create(getMemberName(), TX_COUNTER.getAndIncrement(), transactionChainId);
+        return transactionChainId.newTransactionIdentifier();
     }
 }