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 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;
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.
31 * @author Thomas Pantelis
33 class TransactionContextWrapper {
34 private static final Logger LOG = LoggerFactory.getLogger(TransactionContextWrapper.class);
37 * The list of transaction operations to execute once the TransactionContext becomes available.
39 @GuardedBy("queuedTxOperations")
40 private final List<TransactionOperation> queuedTxOperations = Lists.newArrayList();
42 private final TransactionIdentifier identifier;
45 * The resulting TransactionContext.
47 private volatile TransactionContext transactionContext;
49 private final OperationLimiter limiter;
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()));
58 TransactionContext getTransactionContext() {
59 return transactionContext;
62 TransactionIdentifier getIdentifier() {
67 * Adds a TransactionOperation to be executed once the TransactionContext becomes available.
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());
75 queuedTxOperations.add(operation);
76 invokeOperation = false;
78 invokeOperation = true;
82 if (invokeOperation) {
83 operation.invoke(transactionContext);
89 void maybeExecuteTransactionOperation(final TransactionOperation op) {
91 if (transactionContext != null) {
92 op.invoke(transactionContext);
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);
100 void executePriorTransactionOperations(final TransactionContext localTransactionContext) {
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.
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
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();
120 transactionContext = localTransactionContext;
124 operationsBatch = new ArrayList<>(queuedTxOperations);
125 queuedTxOperations.clear();
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
131 for (TransactionOperation oper : operationsBatch) {
132 oper.invoke(localTransactionContext);
137 Future<ActorSelection> readyTransaction() {
138 // avoid the creation of a promise and a TransactionOperation
139 if (transactionContext != null) {
140 return transactionContext.readyTransaction();
143 final Promise<ActorSelection> promise = Futures.promise();
144 enqueueTransactionOperation(new TransactionOperation() {
146 public void invoke(TransactionContext transactionContext) {
147 promise.completeWith(transactionContext.readyTransaction());
151 return promise.future();
154 public OperationLimiter getLimiter() {