Move operation limiter down to TransactionContextWrapper
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / AbstractTransactionContext.java
index df478b063015c44eb2c96d304da2705d42bf785e..571899ba14343f7b8f189571e18f708b397fc86b 100644 (file)
@@ -7,31 +7,78 @@
  */
 package org.opendaylight.controller.cluster.datastore;
 
+import com.google.common.base.Preconditions;
+import javax.annotation.Nonnull;
 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 abstract class AbstractTransactionContext implements TransactionContext {
-
     private static final Logger LOG = LoggerFactory.getLogger(AbstractTransactionContext.class);
-
+    private final OperationLimiter limiter;
     private long modificationCount = 0;
+    private boolean handoffComplete;
+
+    protected AbstractTransactionContext(final OperationLimiter limiter) {
+        this.limiter = Preconditions.checkNotNull(limiter);
+    }
 
-    private final TransactionIdentifier identifier;
+    /**
+     * Get the transaction identifier associated with this context.
+     *
+     * @return Transaction identifier.
+     */
+    @Nonnull protected final TransactionIdentifier getIdentifier() {
+        return limiter.getIdentifier();
+    }
 
-    protected AbstractTransactionContext(TransactionIdentifier identifier) {
-        this.identifier = identifier;
+    /**
+     * Return the operation limiter associated with this context.
+     * @return Operation limiter.
+     */
+    @Nonnull protected final OperationLimiter getLimiter() {
+        return limiter;
     }
 
-    protected final TransactionIdentifier getIdentifier() {
-        return identifier;
+    /**
+     * Indicate whether all operations have been handed off by the {@link TransactionContextWrapper}.
+     *
+     * @return True if this context is responsible for throttling.
+     */
+    protected final boolean isOperationHandoffComplete() {
+        return handoffComplete;
     }
 
-    protected void incrementModificationCount(){
+    /**
+     * Acquire operation from the limiter if the handoff has completed. If
+     * the handoff is still ongoing, this method does nothing.
+     */
+    protected final void acquireOperation() {
+        if (handoffComplete) {
+            limiter.acquire();
+        }
+    }
+
+    /**
+     * Acquire operation from the limiter if the handoff has NOT completed. If
+     * the handoff has completed, this method does nothing.
+     */
+    protected final void releaseOperation() {
+        if (!handoffComplete) {
+            limiter.release();
+        }
+    }
+
+    protected final void incrementModificationCount() {
         modificationCount++;
     }
 
-    protected void logModificationCount(){
-        LOG.debug("Total modifications on Tx {} = [ {} ]", identifier, modificationCount);
+    protected final void logModificationCount() {
+        LOG.debug("Total modifications on Tx {} = [ {} ]", getIdentifier(), modificationCount);
+    }
+
+    @Override
+    public final void operationHandoffComplete() {
+        handoffComplete = true;
     }
-}
\ No newline at end of file
+}