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.OnComplete;
12 import com.google.common.base.Preconditions;
13 import java.util.Collection;
14 import java.util.Optional;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.ConcurrentMap;
17 import java.util.concurrent.atomic.AtomicLong;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.controller.cluster.access.concepts.MemberName;
20 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
21 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
22 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import scala.concurrent.Future;
30 import scala.util.Try;
33 * Factory for creating local and remote TransactionContext instances. Maintains a cache of known local
34 * transaction factories.
36 abstract class AbstractTransactionContextFactory<F extends LocalTransactionFactory> implements AutoCloseable {
37 private static final Logger LOG = LoggerFactory.getLogger(AbstractTransactionContextFactory.class);
38 private static final MemberName UNKNOWN_MEMBER = MemberName.forName("UNKNOWN-MEMBER");
40 protected static final AtomicLong TX_COUNTER = new AtomicLong();
42 private final ConcurrentMap<String, F> knownLocal = new ConcurrentHashMap<>();
43 private final ActorContext actorContext;
45 protected AbstractTransactionContextFactory(final ActorContext actorContext) {
46 this.actorContext = Preconditions.checkNotNull(actorContext);
49 final ActorContext getActorContext() {
53 private TransactionContext maybeCreateLocalTransactionContext(final TransactionProxy parent, final String shardName) {
54 final LocalTransactionFactory local = knownLocal.get(shardName);
56 if(LOG.isDebugEnabled()) {
57 LOG.debug("Tx {} - Creating local component for shard {} using factory {}",
58 parent.getIdentifier(), shardName, local);
62 return createLocalTransactionContext(local, parent);
63 } catch(Exception e) {
64 return new NoOpTransactionContext(e, parent.getIdentifier());
71 private void onFindPrimaryShardSuccess(PrimaryShardInfo primaryShardInfo, TransactionProxy parent,
72 String shardName, TransactionContextWrapper transactionContextWrapper) {
73 if(LOG.isDebugEnabled()) {
74 LOG.debug("Tx {}: Found primary {} for shard {}", parent.getIdentifier(),
75 primaryShardInfo.getPrimaryShardActor(), shardName);
78 updateShardInfo(shardName, primaryShardInfo);
81 TransactionContext localContext = maybeCreateLocalTransactionContext(parent, shardName);
82 if(localContext != null) {
83 transactionContextWrapper.executePriorTransactionOperations(localContext);
85 RemoteTransactionContextSupport remote = new RemoteTransactionContextSupport(transactionContextWrapper,
87 remote.setPrimaryShard(primaryShardInfo);
90 onTransactionContextCreated(parent.getIdentifier());
94 private void onFindPrimaryShardFailure(Throwable failure, TransactionProxy parent,
95 String shardName, TransactionContextWrapper transactionContextWrapper) {
96 LOG.debug("Tx {}: Find primary for shard {} failed", parent.getIdentifier(), shardName, failure);
99 transactionContextWrapper.executePriorTransactionOperations(new NoOpTransactionContext(failure,
100 parent.getIdentifier()));
102 onTransactionContextCreated(parent.getIdentifier());
106 final TransactionContextWrapper newTransactionContextWrapper(final TransactionProxy parent, final String shardName) {
107 final TransactionContextWrapper transactionContextWrapper =
108 new TransactionContextWrapper(parent.getIdentifier(), actorContext);
110 Future<PrimaryShardInfo> findPrimaryFuture = findPrimaryShard(shardName, parent.getIdentifier());
111 if(findPrimaryFuture.isCompleted()) {
112 Try<PrimaryShardInfo> maybe = findPrimaryFuture.value().get();
113 if(maybe.isSuccess()) {
114 onFindPrimaryShardSuccess(maybe.get(), parent, shardName, transactionContextWrapper);
116 onFindPrimaryShardFailure(maybe.failed().get(), parent, shardName, transactionContextWrapper);
119 findPrimaryFuture.onComplete(new OnComplete<PrimaryShardInfo>() {
121 public void onComplete(final Throwable failure, final PrimaryShardInfo primaryShardInfo) {
122 if (failure == null) {
123 onFindPrimaryShardSuccess(primaryShardInfo, parent, shardName, transactionContextWrapper);
125 onFindPrimaryShardFailure(failure, parent, shardName, transactionContextWrapper);
128 }, actorContext.getClientDispatcher());
131 return transactionContextWrapper;
134 private void updateShardInfo(final String shardName, final PrimaryShardInfo primaryShardInfo) {
135 final Optional<DataTree> maybeDataTree = primaryShardInfo.getLocalShardDataTree();
136 if (maybeDataTree.isPresent()) {
137 if(!knownLocal.containsKey(shardName)) {
138 LOG.debug("Shard {} resolved to local data tree - adding local factory", shardName);
140 F factory = factoryForShard(shardName, primaryShardInfo.getPrimaryShardActor(), maybeDataTree.get());
141 knownLocal.putIfAbsent(shardName, factory);
143 } else if(knownLocal.containsKey(shardName)) {
144 LOG.debug("Shard {} invalidating local data tree", shardName);
146 knownLocal.remove(shardName);
150 protected MemberName getMemberName() {
151 final MemberName ret = getActorContext().getCurrentMemberName();
152 return ret == null ? UNKNOWN_MEMBER : ret;
156 * Create an identifier for the next TransactionProxy attached to this component
158 * @return Transaction identifier, may not be null.
160 protected abstract TransactionIdentifier nextIdentifier();
163 * Find the primary shard actor.
165 * @param shardName Shard name
166 * @return Future containing shard information.
168 protected abstract Future<PrimaryShardInfo> findPrimaryShard(@Nonnull String shardName,
169 @Nonnull TransactionIdentifier txId);
172 * Create local transaction factory for specified shard, backed by specified shard leader
173 * and data tree instance.
177 * @param dataTree Backing data tree instance. The data tree may only be accessed in
179 * @return Transaction factory for local use.
181 protected abstract F factoryForShard(String shardName, ActorSelection shardLeader, DataTree dataTree);
184 * Callback invoked from child transactions to push any futures, which need to
185 * be waited for before the next transaction is allocated.
186 * @param cohortFutures Collection of futures
188 protected abstract <T> void onTransactionReady(@Nonnull TransactionIdentifier transaction, @Nonnull Collection<Future<T>> cohortFutures);
191 * Callback invoked when the internal TransactionContext has been created for a transaction.
193 * @param transactionId the ID of the transaction.
195 protected abstract void onTransactionContextCreated(@Nonnull TransactionIdentifier transactionId);
197 private static TransactionContext createLocalTransactionContext(final LocalTransactionFactory factory,
198 final TransactionProxy parent) {
200 switch(parent.getType()) {
202 final DOMStoreReadTransaction readOnly = factory.newReadOnlyTransaction(parent.getIdentifier());
203 return new LocalTransactionContext(readOnly, parent.getIdentifier(), factory) {
205 protected DOMStoreWriteTransaction getWriteDelegate() {
206 throw new UnsupportedOperationException();
210 protected DOMStoreReadTransaction getReadDelegate() {
215 final DOMStoreReadWriteTransaction readWrite = factory.newReadWriteTransaction(parent.getIdentifier());
216 return new LocalTransactionContext(readWrite, parent.getIdentifier(), factory) {
218 protected DOMStoreWriteTransaction getWriteDelegate() {
223 protected DOMStoreReadTransaction getReadDelegate() {
228 final DOMStoreWriteTransaction writeOnly = factory.newWriteOnlyTransaction(parent.getIdentifier());
229 return new LocalTransactionContext(writeOnly, parent.getIdentifier(), factory) {
231 protected DOMStoreWriteTransaction getWriteDelegate() {
236 protected DOMStoreReadTransaction getReadDelegate() {
237 throw new UnsupportedOperationException();
241 throw new IllegalArgumentException("Invalid transaction type: " + parent.getType());