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.AtomicReferenceFieldUpdater;
21 import java.util.function.Function;
22 import javax.annotation.Nonnull;
23 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
24 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
25 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
26 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
27 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import scala.concurrent.Future;
36 import scala.concurrent.Promise;
39 * A chain of {@link TransactionProxy}s. It allows a single open transaction to be open
40 * at a time. For remote transactions, it also tracks the outstanding readiness requests
41 * towards the shard and unblocks operations only after all have completed.
43 final class TransactionChainProxy extends AbstractTransactionContextFactory<LocalTransactionChain> implements DOMStoreTransactionChain {
44 private static abstract class State {
46 * Check if it is okay to allocate a new transaction.
47 * @throws IllegalStateException if a transaction may not be allocated.
49 abstract void checkReady();
52 * Return the future which needs to be waited for before shard information
53 * is returned (which unblocks remote transactions).
54 * @return Future to wait for, or null of no wait is necessary
56 abstract Future<?> previousFuture();
59 private static abstract class Pending extends State {
60 private final TransactionIdentifier transaction;
61 private final Future<?> previousFuture;
63 Pending(final TransactionIdentifier transaction, final Future<?> previousFuture) {
64 this.previousFuture = previousFuture;
65 this.transaction = Preconditions.checkNotNull(transaction);
69 final Future<?> previousFuture() {
70 return previousFuture;
73 final TransactionIdentifier getIdentifier() {
78 private static final class Allocated extends Pending {
79 Allocated(final TransactionIdentifier transaction, final Future<?> previousFuture) {
80 super(transaction, previousFuture);
85 throw new IllegalStateException(String.format("Previous transaction %s is not ready yet", getIdentifier()));
89 private static final class Submitted extends Pending {
90 Submitted(final TransactionIdentifier transaction, final Future<?> previousFuture) {
91 super(transaction, previousFuture);
100 private static abstract class DefaultState extends State {
102 final Future<?> previousFuture() {
107 private static final State IDLE_STATE = new DefaultState() {
114 private static final State CLOSED_STATE = new DefaultState() {
117 throw new TransactionChainClosedException("Transaction chain has been closed");
121 private static final Logger LOG = LoggerFactory.getLogger(TransactionChainProxy.class);
122 private static final AtomicReferenceFieldUpdater<TransactionChainProxy, State> STATE_UPDATER =
123 AtomicReferenceFieldUpdater.newUpdater(TransactionChainProxy.class, State.class, "currentState");
125 private final TransactionContextFactory parent;
126 private volatile State currentState = IDLE_STATE;
129 * This map holds Promise instances for each read-only tx. It is used to maintain ordering of tx creates
130 * wrt to read-only tx's between this class and a LocalTransactionChain since they're bridged by
131 * asynchronous futures. Otherwise, in the following scenario, eg:
133 * 1) Create write tx1 on chain
134 * 2) do write and submit
135 * 3) Create read-only tx2 on chain and issue read
136 * 4) Create write tx3 on chain, do write but do not submit
138 * if the sequence/timing is right, tx3 may create its local tx on the LocalTransactionChain before tx2,
139 * which results in tx2 failing b/c tx3 isn't ready yet. So maintaining ordering prevents this issue
142 * A Promise is added via newReadOnlyTransaction. When the parent class completes the primary shard
143 * lookup and creates the TransactionContext (either success or failure), onTransactionContextCreated is
144 * called which completes the Promise. A write tx that is created prior to completion will wait on the
145 * Promise's Future via findPrimaryShard.
147 private final ConcurrentMap<TransactionIdentifier, Promise<Object>> priorReadOnlyTxPromises = new ConcurrentHashMap<>();
149 TransactionChainProxy(final TransactionContextFactory parent, final LocalHistoryIdentifier historyId) {
150 super(parent.getActorContext(), historyId);
151 this.parent = parent;
154 public String getTransactionChainId() {
155 return getHistoryId().toString();
159 public DOMStoreReadTransaction newReadOnlyTransaction() {
160 currentState.checkReady();
161 TransactionProxy transactionProxy = new TransactionProxy(this, TransactionType.READ_ONLY);
162 priorReadOnlyTxPromises.put(transactionProxy.getIdentifier(), Futures.<Object>promise());
163 return transactionProxy;
167 public DOMStoreReadWriteTransaction newReadWriteTransaction() {
168 getActorContext().acquireTxCreationPermit();
169 return allocateWriteTransaction(TransactionType.READ_WRITE);
173 public DOMStoreWriteTransaction newWriteOnlyTransaction() {
174 getActorContext().acquireTxCreationPermit();
175 return allocateWriteTransaction(TransactionType.WRITE_ONLY);
179 public void close() {
180 currentState = CLOSED_STATE;
182 // Send a close transaction chain request to each and every shard
184 getActorContext().broadcast(new Function<Short, Object>() {
186 public Object apply(Short version) {
187 return new CloseTransactionChain(getHistoryId().toString(), version).toSerializable();
192 private TransactionProxy allocateWriteTransaction(final TransactionType type) {
193 State localState = currentState;
194 localState.checkReady();
196 final TransactionProxy ret = new TransactionProxy(this, type);
197 currentState = new Allocated(ret.getIdentifier(), localState.previousFuture());
202 protected LocalTransactionChain factoryForShard(final String shardName, final ActorSelection shardLeader, final DataTree dataTree) {
203 final LocalTransactionChain ret = new LocalTransactionChain(this, shardLeader, dataTree);
204 LOG.debug("Allocated transaction chain {} for shard {} leader {}", ret, shardName, shardLeader);
209 * This method is overridden to ensure the previous Tx's ready operations complete
210 * before we initiate the next Tx in the chain to avoid creation failures if the
211 * previous Tx's ready operations haven't completed yet.
213 @SuppressWarnings({ "unchecked", "rawtypes" })
215 protected Future<PrimaryShardInfo> findPrimaryShard(final String shardName, final TransactionIdentifier txId) {
216 // Read current state atomically
217 final State localState = currentState;
219 // There are no outstanding futures, shortcut
220 Future<?> previous = localState.previousFuture();
221 if (previous == null) {
222 return combineFutureWithPossiblePriorReadOnlyTxFutures(parent.findPrimaryShard(shardName, txId), txId);
225 final String previousTransactionId;
227 if(localState instanceof Pending){
228 previousTransactionId = ((Pending) localState).getIdentifier().toString();
229 LOG.debug("Tx: {} - waiting for ready futures with pending Tx {}", txId, previousTransactionId);
231 previousTransactionId = "";
232 LOG.debug("Waiting for ready futures on chain {}", getTransactionChainId());
235 previous = combineFutureWithPossiblePriorReadOnlyTxFutures(previous, txId);
237 // Add a callback for completion of the combined Futures.
238 final Promise<PrimaryShardInfo> returnPromise = Futures.promise();
240 final OnComplete onComplete = new OnComplete() {
242 public void onComplete(final Throwable failure, final Object notUsed) {
243 if (failure != null) {
244 // A Ready Future failed so fail the returned Promise.
245 LOG.error("Tx: {} - ready future failed for previous Tx {}", txId, previousTransactionId);
246 returnPromise.failure(failure);
248 LOG.debug("Tx: {} - previous Tx {} readied - proceeding to FindPrimaryShard",
249 txId, previousTransactionId);
251 // Send the FindPrimaryShard message and use the resulting Future to complete the
253 returnPromise.completeWith(parent.findPrimaryShard(shardName, txId));
258 previous.onComplete(onComplete, getActorContext().getClientDispatcher());
259 return returnPromise.future();
262 private <T> Future<T> combineFutureWithPossiblePriorReadOnlyTxFutures(final Future<T> future,
263 final TransactionIdentifier txId) {
264 if(!priorReadOnlyTxPromises.containsKey(txId) && !priorReadOnlyTxPromises.isEmpty()) {
265 Collection<Entry<TransactionIdentifier, Promise<Object>>> priorReadOnlyTxPromiseEntries =
266 new ArrayList<>(priorReadOnlyTxPromises.entrySet());
267 if(priorReadOnlyTxPromiseEntries.isEmpty()) {
271 List<Future<Object>> priorReadOnlyTxFutures = new ArrayList<>(priorReadOnlyTxPromiseEntries.size());
272 for(Entry<TransactionIdentifier, Promise<Object>> entry: priorReadOnlyTxPromiseEntries) {
273 LOG.debug("Tx: {} - waiting on future for prior read-only Tx {}", txId, entry.getKey());
274 priorReadOnlyTxFutures.add(entry.getValue().future());
277 Future<Iterable<Object>> combinedFutures = Futures.sequence(priorReadOnlyTxFutures,
278 getActorContext().getClientDispatcher());
280 final Promise<T> returnPromise = Futures.promise();
281 final OnComplete<Iterable<Object>> onComplete = new OnComplete<Iterable<Object>>() {
283 public void onComplete(final Throwable failure, final Iterable<Object> notUsed) {
284 LOG.debug("Tx: {} - prior read-only Tx futures complete", txId);
286 // Complete the returned Promise with the original Future.
287 returnPromise.completeWith(future);
291 combinedFutures.onComplete(onComplete, getActorContext().getClientDispatcher());
292 return returnPromise.future();
299 protected <T> void onTransactionReady(final TransactionIdentifier transaction, final Collection<Future<T>> cohortFutures) {
300 final State localState = currentState;
301 Preconditions.checkState(localState instanceof Allocated, "Readying transaction %s while state is %s", transaction, localState);
302 final TransactionIdentifier currentTx = ((Allocated)localState).getIdentifier();
303 Preconditions.checkState(transaction.equals(currentTx), "Readying transaction %s while %s is allocated", transaction, currentTx);
305 // Transaction ready and we are not waiting for futures -- go to idle
306 if (cohortFutures.isEmpty()) {
307 currentState = IDLE_STATE;
311 // Combine the ready Futures into 1
312 final Future<Iterable<T>> combined = Futures.sequence(cohortFutures, getActorContext().getClientDispatcher());
314 // Record the we have outstanding futures
315 final State newState = new Submitted(transaction, combined);
316 currentState = newState;
318 // Attach a completion reset, but only if we do not allocate a transaction
320 combined.onComplete(new OnComplete<Iterable<T>>() {
322 public void onComplete(final Throwable arg0, final Iterable<T> arg1) {
323 STATE_UPDATER.compareAndSet(TransactionChainProxy.this, newState, IDLE_STATE);
325 }, getActorContext().getClientDispatcher());
329 protected void onTransactionContextCreated(@Nonnull TransactionIdentifier transactionId) {
330 Promise<Object> promise = priorReadOnlyTxPromises.remove(transactionId);
331 if(promise != null) {
332 promise.success(null);