2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
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 javax.annotation.concurrent.GuardedBy;
18 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
19 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import scala.concurrent.Future;
23 import scala.concurrent.Promise;
26 * A helper class that wraps an eventual TransactionContext instance. Operations destined for the target
27 * TransactionContext instance are cached until the TransactionContext instance becomes available at which
28 * time they are executed.
30 * @author Thomas Pantelis
32 class TransactionContextWrapper {
33 private static final Logger LOG = LoggerFactory.getLogger(TransactionContextWrapper.class);
36 * The list of transaction operations to execute once the TransactionContext becomes available.
38 @GuardedBy("queuedTxOperations")
39 private final List<TransactionOperation> queuedTxOperations = Lists.newArrayList();
41 private final TransactionIdentifier identifier;
44 * The resulting TransactionContext.
46 private volatile TransactionContext transactionContext;
48 private final OperationLimiter limiter;
50 TransactionContextWrapper(TransactionIdentifier identifier, final ActorContext actorContext) {
51 this.identifier = Preconditions.checkNotNull(identifier);
52 this.limiter = new OperationLimiter(identifier,
53 actorContext.getDatastoreContext().getShardBatchedModificationCount() + 1, // 1 extra permit for the ready operation
54 actorContext.getDatastoreContext().getOperationTimeoutInSeconds());
57 TransactionContext getTransactionContext() {
58 return transactionContext;
61 TransactionIdentifier getIdentifier() {
66 * Adds a TransactionOperation to be executed once the TransactionContext becomes available.
68 private void enqueueTransactionOperation(final TransactionOperation operation) {
69 final boolean invokeOperation;
70 synchronized (queuedTxOperations) {
71 if (transactionContext == null) {
72 LOG.debug("Tx {} Queuing TransactionOperation", getIdentifier());
74 queuedTxOperations.add(operation);
75 invokeOperation = false;
77 invokeOperation = true;
81 if (invokeOperation) {
82 operation.invoke(transactionContext);
88 void maybeExecuteTransactionOperation(final TransactionOperation op) {
90 if (transactionContext != null) {
91 op.invoke(transactionContext);
93 // The shard Tx hasn't been created yet so add the Tx operation to the Tx Future
94 // callback to be executed after the Tx is created.
95 enqueueTransactionOperation(op);
99 void executePriorTransactionOperations(final TransactionContext localTransactionContext) {
101 // Access to queuedTxOperations and transactionContext must be protected and atomic
102 // (ie synchronized) with respect to #addTxOperationOnComplete to handle timing
103 // issues and ensure no TransactionOperation is missed and that they are processed
104 // in the order they occurred.
106 // We'll make a local copy of the queuedTxOperations list to handle re-entrancy
107 // in case a TransactionOperation results in another transaction operation being
108 // queued (eg a put operation from a client read Future callback that is notified
110 Collection<TransactionOperation> operationsBatch = null;
111 synchronized (queuedTxOperations) {
112 if (queuedTxOperations.isEmpty()) {
113 // We're done invoking the TransactionOperations so we can now publish the
114 // TransactionContext.
115 localTransactionContext.operationHandOffComplete();
116 if(!localTransactionContext.usesOperationLimiting()){
117 limiter.releaseAll();
119 transactionContext = localTransactionContext;
123 operationsBatch = new ArrayList<>(queuedTxOperations);
124 queuedTxOperations.clear();
127 // Invoke TransactionOperations outside the sync block to avoid unnecessary blocking.
128 // A slight down-side is that we need to re-acquire the lock below but this should
130 for (TransactionOperation oper : operationsBatch) {
131 oper.invoke(localTransactionContext);
136 Future<ActorSelection> readyTransaction() {
137 // avoid the creation of a promise and a TransactionOperation
138 if (transactionContext != null) {
139 return transactionContext.readyTransaction();
142 final Promise<ActorSelection> promise = Futures.promise();
143 enqueueTransactionOperation(new TransactionOperation() {
145 public void invoke(TransactionContext transactionContext) {
146 promise.completeWith(transactionContext.readyTransaction());
150 return promise.future();
153 public OperationLimiter getLimiter() {