*/
package org.opendaylight.controller.cluster.datastore;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+
import akka.actor.ActorSelection;
import akka.dispatch.Futures;
import akka.dispatch.OnComplete;
-import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
-import javax.annotation.Nonnull;
import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionChain;
import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
-import org.opendaylight.mdsal.common.api.TransactionChainClosedException;
+import org.opendaylight.mdsal.dom.api.DOMTransactionChainClosedException;
import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.ReadOnlyDataTree;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.concurrent.Future;
Pending(final TransactionIdentifier transaction, final Future<?> previousFuture) {
this.previousFuture = previousFuture;
- this.transaction = Preconditions.checkNotNull(transaction);
+ this.transaction = requireNonNull(transaction);
}
@Override
private static final State CLOSED_STATE = new DefaultState() {
@Override
void checkReady() {
- throw new TransactionChainClosedException("Transaction chain has been closed");
+ throw new DOMTransactionChainClosedException("Transaction chain has been closed");
}
};
new ConcurrentHashMap<>();
TransactionChainProxy(final TransactionContextFactory parent, final LocalHistoryIdentifier historyId) {
- super(parent.getActorContext(), historyId);
+ super(parent.getActorUtils(), historyId);
this.parent = parent;
}
@Override
public DOMStoreReadWriteTransaction newReadWriteTransaction() {
- getActorContext().acquireTxCreationPermit();
+ getActorUtils().acquireTxCreationPermit();
return allocateWriteTransaction(TransactionType.READ_WRITE);
}
@Override
public DOMStoreWriteTransaction newWriteOnlyTransaction() {
- getActorContext().acquireTxCreationPermit();
+ getActorUtils().acquireTxCreationPermit();
return allocateWriteTransaction(TransactionType.WRITE_ONLY);
}
// Send a close transaction chain request to each and every shard
- getActorContext().broadcast(version -> new CloseTransactionChain(getHistoryId(), version).toSerializable(),
+ getActorUtils().broadcast(version -> new CloseTransactionChain(getHistoryId(), version).toSerializable(),
CloseTransactionChain.class);
}
@Override
protected LocalTransactionChain factoryForShard(final String shardName, final ActorSelection shardLeader,
- final DataTree dataTree) {
+ final ReadOnlyDataTree dataTree) {
final LocalTransactionChain ret = new LocalTransactionChain(this, shardLeader, dataTree);
LOG.debug("Allocated transaction chain {} for shard {} leader {}", ret, shardName, shardLeader);
return ret;
}
};
- previous.onComplete(onComplete, getActorContext().getClientDispatcher());
+ previous.onComplete(onComplete, getActorUtils().getClientDispatcher());
return returnPromise.future();
}
private <T> Future<T> combineFutureWithPossiblePriorReadOnlyTxFutures(final Future<T> future,
final TransactionIdentifier txId) {
- if (!priorReadOnlyTxPromises.containsKey(txId) && !priorReadOnlyTxPromises.isEmpty()) {
- Collection<Entry<TransactionIdentifier, Promise<Object>>> priorReadOnlyTxPromiseEntries =
- new ArrayList<>(priorReadOnlyTxPromises.entrySet());
- if (priorReadOnlyTxPromiseEntries.isEmpty()) {
- return future;
- }
+ return priorReadOnlyTxPromises.isEmpty() || priorReadOnlyTxPromises.containsKey(txId) ? future
+ // Tough luck, we need do some work
+ : combineWithPriorReadOnlyTxFutures(future, txId);
+ }
- List<Future<Object>> priorReadOnlyTxFutures = new ArrayList<>(priorReadOnlyTxPromiseEntries.size());
- for (Entry<TransactionIdentifier, Promise<Object>> entry: priorReadOnlyTxPromiseEntries) {
- LOG.debug("Tx: {} - waiting on future for prior read-only Tx {}", txId, entry.getKey());
- priorReadOnlyTxFutures.add(entry.getValue().future());
- }
+ // Split out of the common path
+ private <T> Future<T> combineWithPriorReadOnlyTxFutures(final Future<T> future, final TransactionIdentifier txId) {
+ // Take a stable snapshot, and check if we raced
+ final List<Entry<TransactionIdentifier, Promise<Object>>> priorReadOnlyTxPromiseEntries =
+ new ArrayList<>(priorReadOnlyTxPromises.entrySet());
+ if (priorReadOnlyTxPromiseEntries.isEmpty()) {
+ return future;
+ }
- Future<Iterable<Object>> combinedFutures = Futures.sequence(priorReadOnlyTxFutures,
- getActorContext().getClientDispatcher());
+ final List<Future<Object>> priorReadOnlyTxFutures = new ArrayList<>(priorReadOnlyTxPromiseEntries.size());
+ for (Entry<TransactionIdentifier, Promise<Object>> entry: priorReadOnlyTxPromiseEntries) {
+ LOG.debug("Tx: {} - waiting on future for prior read-only Tx {}", txId, entry.getKey());
+ priorReadOnlyTxFutures.add(entry.getValue().future());
+ }
- final Promise<T> returnPromise = Futures.promise();
- final OnComplete<Iterable<Object>> onComplete = new OnComplete<Iterable<Object>>() {
- @Override
- public void onComplete(final Throwable failure, final Iterable<Object> notUsed) {
- LOG.debug("Tx: {} - prior read-only Tx futures complete", txId);
+ final Future<Iterable<Object>> combinedFutures = Futures.sequence(priorReadOnlyTxFutures,
+ getActorUtils().getClientDispatcher());
- // Complete the returned Promise with the original Future.
- returnPromise.completeWith(future);
- }
- };
+ final Promise<T> returnPromise = Futures.promise();
+ final OnComplete<Iterable<Object>> onComplete = new OnComplete<>() {
+ @Override
+ public void onComplete(final Throwable failure, final Iterable<Object> notUsed) {
+ LOG.debug("Tx: {} - prior read-only Tx futures complete", txId);
- combinedFutures.onComplete(onComplete, getActorContext().getClientDispatcher());
- return returnPromise.future();
- } else {
- return future;
- }
+ // Complete the returned Promise with the original Future.
+ returnPromise.completeWith(future);
+ }
+ };
+
+ combinedFutures.onComplete(onComplete, getActorUtils().getClientDispatcher());
+ return returnPromise.future();
}
@Override
protected <T> void onTransactionReady(final TransactionIdentifier transaction,
final Collection<Future<T>> cohortFutures) {
final State localState = currentState;
- Preconditions.checkState(localState instanceof Allocated, "Readying transaction %s while state is %s",
- transaction, localState);
+ checkState(localState instanceof Allocated, "Readying transaction %s while state is %s", transaction,
+ localState);
final TransactionIdentifier currentTx = ((Allocated)localState).getIdentifier();
- Preconditions.checkState(transaction.equals(currentTx), "Readying transaction %s while %s is allocated",
- transaction, currentTx);
+ checkState(transaction.equals(currentTx), "Readying transaction %s while %s is allocated", transaction,
+ currentTx);
// Transaction ready and we are not waiting for futures -- go to idle
if (cohortFutures.isEmpty()) {
}
// Combine the ready Futures into 1
- final Future<Iterable<T>> combined = Futures.sequence(cohortFutures, getActorContext().getClientDispatcher());
+ final Future<Iterable<T>> combined = Futures.sequence(cohortFutures, getActorUtils().getClientDispatcher());
// Record the we have outstanding futures
final State newState = new Submitted(transaction, combined);
public void onComplete(final Throwable arg0, final Iterable<T> arg1) {
STATE_UPDATER.compareAndSet(TransactionChainProxy.this, newState, IDLE_STATE);
}
- }, getActorContext().getClientDispatcher());
+ }, getActorUtils().getClientDispatcher());
}
@Override
- protected void onTransactionContextCreated(@Nonnull TransactionIdentifier transactionId) {
+ protected void onTransactionContextCreated(final TransactionIdentifier transactionId) {
Promise<Object> promise = priorReadOnlyTxPromises.remove(transactionId);
if (promise != null) {
promise.success(null);