BUG-5626: remove CompositeModification(ByteString)Payload
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / TransactionContextWrapper.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import akka.actor.ActorSelection;
11 import akka.dispatch.Futures;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Lists;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.concurrent.TimeUnit;
18 import javax.annotation.concurrent.GuardedBy;
19 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
20 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import scala.concurrent.Future;
24 import scala.concurrent.Promise;
25
26 /**
27  * A helper class that wraps an eventual TransactionContext instance. Operations destined for the target
28  * TransactionContext instance are cached until the TransactionContext instance becomes available at which
29  * time they are executed.
30  *
31  * @author Thomas Pantelis
32  */
33 class TransactionContextWrapper {
34     private static final Logger LOG = LoggerFactory.getLogger(TransactionContextWrapper.class);
35
36     /**
37      * The list of transaction operations to execute once the TransactionContext becomes available.
38      */
39     @GuardedBy("queuedTxOperations")
40     private final List<TransactionOperation> queuedTxOperations = Lists.newArrayList();
41
42     private final TransactionIdentifier identifier;
43
44     /**
45      * The resulting TransactionContext.
46      */
47     private volatile TransactionContext transactionContext;
48
49     private final OperationLimiter limiter;
50
51     TransactionContextWrapper(TransactionIdentifier identifier, final ActorContext actorContext) {
52         this.identifier = Preconditions.checkNotNull(identifier);
53         this.limiter = new OperationLimiter(identifier,
54                 actorContext.getDatastoreContext().getShardBatchedModificationCount() + 1, // 1 extra permit for the ready operation
55                 TimeUnit.MILLISECONDS.toSeconds(actorContext.getDatastoreContext().getOperationTimeoutInMillis()));
56     }
57
58     TransactionContext getTransactionContext() {
59         return transactionContext;
60     }
61
62     TransactionIdentifier getIdentifier() {
63         return identifier;
64     }
65
66     /**
67      * Adds a TransactionOperation to be executed once the TransactionContext becomes available.
68      */
69     private void enqueueTransactionOperation(final TransactionOperation operation) {
70         final boolean invokeOperation;
71         synchronized (queuedTxOperations) {
72             if (transactionContext == null) {
73                 LOG.debug("Tx {} Queuing TransactionOperation", getIdentifier());
74
75                 queuedTxOperations.add(operation);
76                 invokeOperation = false;
77             }  else {
78                 invokeOperation = true;
79             }
80         }
81
82         if (invokeOperation) {
83             operation.invoke(transactionContext);
84         } else {
85             limiter.acquire();
86         }
87     }
88
89     void maybeExecuteTransactionOperation(final TransactionOperation op) {
90
91         if (transactionContext != null) {
92             op.invoke(transactionContext);
93         } else {
94             // The shard Tx hasn't been created yet so add the Tx operation to the Tx Future
95             // callback to be executed after the Tx is created.
96             enqueueTransactionOperation(op);
97         }
98     }
99
100     void executePriorTransactionOperations(final TransactionContext localTransactionContext) {
101         while(true) {
102             // Access to queuedTxOperations and transactionContext must be protected and atomic
103             // (ie synchronized) with respect to #addTxOperationOnComplete to handle timing
104             // issues and ensure no TransactionOperation is missed and that they are processed
105             // in the order they occurred.
106
107             // We'll make a local copy of the queuedTxOperations list to handle re-entrancy
108             // in case a TransactionOperation results in another transaction operation being
109             // queued (eg a put operation from a client read Future callback that is notified
110             // synchronously).
111             Collection<TransactionOperation> operationsBatch = null;
112             synchronized (queuedTxOperations) {
113                 if (queuedTxOperations.isEmpty()) {
114                     // We're done invoking the TransactionOperations so we can now publish the
115                     // TransactionContext.
116                     localTransactionContext.operationHandOffComplete();
117                     if(!localTransactionContext.usesOperationLimiting()){
118                         limiter.releaseAll();
119                     }
120                     transactionContext = localTransactionContext;
121                     break;
122                 }
123
124                 operationsBatch = new ArrayList<>(queuedTxOperations);
125                 queuedTxOperations.clear();
126             }
127
128             // Invoke TransactionOperations outside the sync block to avoid unnecessary blocking.
129             // A slight down-side is that we need to re-acquire the lock below but this should
130             // be negligible.
131             for (TransactionOperation oper : operationsBatch) {
132                 oper.invoke(localTransactionContext);
133             }
134         }
135     }
136
137     Future<ActorSelection> readyTransaction() {
138         // avoid the creation of a promise and a TransactionOperation
139         if (transactionContext != null) {
140             return transactionContext.readyTransaction();
141         }
142
143         final Promise<ActorSelection> promise = Futures.promise();
144         enqueueTransactionOperation(new TransactionOperation() {
145             @Override
146             public void invoke(TransactionContext transactionContext) {
147                 promise.completeWith(transactionContext.readyTransaction());
148             }
149         });
150
151         return promise.future();
152     }
153
154     public OperationLimiter getLimiter() {
155         return limiter;
156     }
157
158
159 }