2 * Copyright (c) 2015 Cisco 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 akka.dispatch.OnComplete;
13 import com.google.common.base.Preconditions;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Map.Entry;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
20 import java.util.concurrent.atomic.AtomicInteger;
21 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
22 import java.util.function.Function;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionChainIdentifier;
25 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
26 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
27 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import scala.concurrent.Future;
37 import scala.concurrent.Promise;
40 * A chain of {@link TransactionProxy}s. It allows a single open transaction to be open
41 * at a time. For remote transactions, it also tracks the outstanding readiness requests
42 * towards the shard and unblocks operations only after all have completed.
44 final class TransactionChainProxy extends AbstractTransactionContextFactory<LocalTransactionChain> implements DOMStoreTransactionChain {
45 private static abstract class State {
47 * Check if it is okay to allocate a new transaction.
48 * @throws IllegalStateException if a transaction may not be allocated.
50 abstract void checkReady();
53 * Return the future which needs to be waited for before shard information
54 * is returned (which unblocks remote transactions).
55 * @return Future to wait for, or null of no wait is necessary
57 abstract Future<?> previousFuture();
60 private static abstract class Pending extends State {
61 private final TransactionIdentifier transaction;
62 private final Future<?> previousFuture;
64 Pending(final TransactionIdentifier transaction, final Future<?> previousFuture) {
65 this.previousFuture = previousFuture;
66 this.transaction = Preconditions.checkNotNull(transaction);
70 final Future<?> previousFuture() {
71 return previousFuture;
74 final TransactionIdentifier getIdentifier() {
79 private static final class Allocated extends Pending {
80 Allocated(final TransactionIdentifier transaction, final Future<?> previousFuture) {
81 super(transaction, previousFuture);
86 throw new IllegalStateException(String.format("Previous transaction %s is not ready yet", getIdentifier()));
90 private static final class Submitted extends Pending {
91 Submitted(final TransactionIdentifier transaction, final Future<?> previousFuture) {
92 super(transaction, previousFuture);
101 private static abstract class DefaultState extends State {
103 final Future<?> previousFuture() {
108 private static final State IDLE_STATE = new DefaultState() {
115 private static final State CLOSED_STATE = new DefaultState() {
118 throw new TransactionChainClosedException("Transaction chain has been closed");
122 private static final Logger LOG = LoggerFactory.getLogger(TransactionChainProxy.class);
123 private static final AtomicInteger CHAIN_COUNTER = new AtomicInteger();
124 private static final AtomicReferenceFieldUpdater<TransactionChainProxy, State> STATE_UPDATER =
125 AtomicReferenceFieldUpdater.newUpdater(TransactionChainProxy.class, State.class, "currentState");
127 private final TransactionChainIdentifier transactionChainId;
128 private final TransactionContextFactory parent;
129 private volatile State currentState = IDLE_STATE;
132 * This map holds Promise instances for each read-only tx. It is used to maintain ordering of tx creates
133 * wrt to read-only tx's between this class and a LocalTransactionChain since they're bridged by
134 * asynchronous futures. Otherwise, in the following scenario, eg:
136 * 1) Create write tx1 on chain
137 * 2) do write and submit
138 * 3) Create read-only tx2 on chain and issue read
139 * 4) Create write tx3 on chain, do write but do not submit
141 * if the sequence/timing is right, tx3 may create its local tx on the LocalTransactionChain before tx2,
142 * which results in tx2 failing b/c tx3 isn't ready yet. So maintaining ordering prevents this issue
145 * A Promise is added via newReadOnlyTransaction. When the parent class completes the primary shard
146 * lookup and creates the TransactionContext (either success or failure), onTransactionContextCreated is
147 * called which completes the Promise. A write tx that is created prior to completion will wait on the
148 * Promise's Future via findPrimaryShard.
150 private final ConcurrentMap<TransactionIdentifier, Promise<Object>> priorReadOnlyTxPromises = new ConcurrentHashMap<>();
152 TransactionChainProxy(final TransactionContextFactory parent) {
153 super(parent.getActorContext());
155 transactionChainId = new TransactionChainIdentifier(parent.getActorContext().getCurrentMemberName(), CHAIN_COUNTER.incrementAndGet());
156 this.parent = parent;
159 public String getTransactionChainId() {
160 return transactionChainId.toString();
164 public DOMStoreReadTransaction newReadOnlyTransaction() {
165 currentState.checkReady();
166 TransactionProxy transactionProxy = new TransactionProxy(this, TransactionType.READ_ONLY);
167 priorReadOnlyTxPromises.put(transactionProxy.getIdentifier(), Futures.<Object>promise());
168 return transactionProxy;
172 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
173 getActorContext().acquireTxCreationPermit();
174 return allocateWriteTransaction(TransactionType.READ_WRITE);
178 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
179 getActorContext().acquireTxCreationPermit();
180 return allocateWriteTransaction(TransactionType.WRITE_ONLY);
184 public void close() {
185 currentState = CLOSED_STATE;
187 // Send a close transaction chain request to each and every shard
189 getActorContext().broadcast(new Function<Short, Object>() {
191 public Object apply(Short version) {
192 return new CloseTransactionChain(transactionChainId.toString(), version).toSerializable();
197 private TransactionProxy allocateWriteTransaction(final TransactionType type) {
198 State localState = currentState;
199 localState.checkReady();
201 final TransactionProxy ret = new TransactionProxy(this, type);
202 currentState = new Allocated(ret.getIdentifier(), localState.previousFuture());
207 protected LocalTransactionChain factoryForShard(final String shardName, final ActorSelection shardLeader, final DataTree dataTree) {
208 final LocalTransactionChain ret = new LocalTransactionChain(this, shardLeader, dataTree);
209 LOG.debug("Allocated transaction chain {} for shard {} leader {}", ret, shardName, shardLeader);
214 * This method is overridden to ensure the previous Tx's ready operations complete
215 * before we initiate the next Tx in the chain to avoid creation failures if the
216 * previous Tx's ready operations haven't completed yet.
218 @SuppressWarnings({ "unchecked", "rawtypes" })
220 protected Future<PrimaryShardInfo> findPrimaryShard(final String shardName, final TransactionIdentifier txId) {
221 // Read current state atomically
222 final State localState = currentState;
224 // There are no outstanding futures, shortcut
225 Future<?> previous = localState.previousFuture();
226 if (previous == null) {
227 return combineFutureWithPossiblePriorReadOnlyTxFutures(parent.findPrimaryShard(shardName, txId), txId);
230 final String previousTransactionId;
232 if(localState instanceof Pending){
233 previousTransactionId = ((Pending) localState).getIdentifier().toString();
234 LOG.debug("Tx: {} - waiting for ready futures with pending Tx {}", txId, previousTransactionId);
236 previousTransactionId = "";
237 LOG.debug("Waiting for ready futures on chain {}", getTransactionChainId());
240 previous = combineFutureWithPossiblePriorReadOnlyTxFutures(previous, txId);
242 // Add a callback for completion of the combined Futures.
243 final Promise<PrimaryShardInfo> returnPromise = Futures.promise();
245 final OnComplete onComplete = new OnComplete() {
247 public void onComplete(final Throwable failure, final Object notUsed) {
248 if (failure != null) {
249 // A Ready Future failed so fail the returned Promise.
250 LOG.error("Tx: {} - ready future failed for previous Tx {}", txId, previousTransactionId);
251 returnPromise.failure(failure);
253 LOG.debug("Tx: {} - previous Tx {} readied - proceeding to FindPrimaryShard",
254 txId, previousTransactionId);
256 // Send the FindPrimaryShard message and use the resulting Future to complete the
258 returnPromise.completeWith(parent.findPrimaryShard(shardName, txId));
263 previous.onComplete(onComplete, getActorContext().getClientDispatcher());
264 return returnPromise.future();
267 private <T> Future<T> combineFutureWithPossiblePriorReadOnlyTxFutures(final Future<T> future,
268 final TransactionIdentifier txId) {
269 if(!priorReadOnlyTxPromises.containsKey(txId) && !priorReadOnlyTxPromises.isEmpty()) {
270 Collection<Entry<TransactionIdentifier, Promise<Object>>> priorReadOnlyTxPromiseEntries =
271 new ArrayList<>(priorReadOnlyTxPromises.entrySet());
272 if(priorReadOnlyTxPromiseEntries.isEmpty()) {
276 List<Future<Object>> priorReadOnlyTxFutures = new ArrayList<>(priorReadOnlyTxPromiseEntries.size());
277 for(Entry<TransactionIdentifier, Promise<Object>> entry: priorReadOnlyTxPromiseEntries) {
278 LOG.debug("Tx: {} - waiting on future for prior read-only Tx {}", txId, entry.getKey());
279 priorReadOnlyTxFutures.add(entry.getValue().future());
282 Future<Iterable<Object>> combinedFutures = Futures.sequence(priorReadOnlyTxFutures,
283 getActorContext().getClientDispatcher());
285 final Promise<T> returnPromise = Futures.promise();
286 final OnComplete<Iterable<Object>> onComplete = new OnComplete<Iterable<Object>>() {
288 public void onComplete(final Throwable failure, final Iterable<Object> notUsed) {
289 LOG.debug("Tx: {} - prior read-only Tx futures complete", txId);
291 // Complete the returned Promise with the original Future.
292 returnPromise.completeWith(future);
296 combinedFutures.onComplete(onComplete, getActorContext().getClientDispatcher());
297 return returnPromise.future();
304 protected <T> void onTransactionReady(final TransactionIdentifier transaction, final Collection<Future<T>> cohortFutures) {
305 final State localState = currentState;
306 Preconditions.checkState(localState instanceof Allocated, "Readying transaction %s while state is %s", transaction, localState);
307 final TransactionIdentifier currentTx = ((Allocated)localState).getIdentifier();
308 Preconditions.checkState(transaction.equals(currentTx), "Readying transaction %s while %s is allocated", transaction, currentTx);
310 // Transaction ready and we are not waiting for futures -- go to idle
311 if (cohortFutures.isEmpty()) {
312 currentState = IDLE_STATE;
316 // Combine the ready Futures into 1
317 final Future<Iterable<T>> combined = Futures.sequence(cohortFutures, getActorContext().getClientDispatcher());
319 // Record the we have outstanding futures
320 final State newState = new Submitted(transaction, combined);
321 currentState = newState;
323 // Attach a completion reset, but only if we do not allocate a transaction
325 combined.onComplete(new OnComplete<Iterable<T>>() {
327 public void onComplete(final Throwable arg0, final Iterable<T> arg1) {
328 STATE_UPDATER.compareAndSet(TransactionChainProxy.this, newState, IDLE_STATE);
330 }, getActorContext().getClientDispatcher());
334 protected void onTransactionContextCreated(@Nonnull TransactionIdentifier transactionId) {
335 Promise<Object> promise = priorReadOnlyTxPromises.remove(transactionId);
336 if(promise != null) {
337 promise.success(null);
342 protected TransactionIdentifier nextIdentifier() {
343 return transactionChainId.newTransactionIdentifier();