2 * Copyright (c) 2014 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 static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import akka.actor.ActorSelection;
14 import com.google.common.annotations.VisibleForTesting;
15 import com.google.common.collect.Iterables;
16 import com.google.common.util.concurrent.FluentFuture;
17 import com.google.common.util.concurrent.SettableFuture;
18 import java.util.ArrayList;
19 import java.util.List;
21 import java.util.Map.Entry;
22 import java.util.Optional;
23 import java.util.SortedSet;
24 import java.util.TreeMap;
25 import java.util.TreeSet;
26 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
27 import org.opendaylight.controller.cluster.datastore.TransactionModificationOperation.DeleteOperation;
28 import org.opendaylight.controller.cluster.datastore.TransactionModificationOperation.MergeOperation;
29 import org.opendaylight.controller.cluster.datastore.TransactionModificationOperation.WriteOperation;
30 import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
31 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
32 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
33 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
34 import org.opendaylight.controller.cluster.datastore.utils.RootScatterGather;
35 import org.opendaylight.mdsal.dom.spi.store.AbstractDOMStoreTransaction;
36 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
39 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import scala.concurrent.Future;
44 import scala.concurrent.Promise;
47 * A transaction potentially spanning multiple backend shards.
49 public class TransactionProxy extends AbstractDOMStoreTransaction<TransactionIdentifier>
50 implements DOMStoreReadWriteTransaction {
51 private enum TransactionState {
57 private static final Logger LOG = LoggerFactory.getLogger(TransactionProxy.class);
58 private static final DeleteOperation ROOT_DELETE_OPERATION = new DeleteOperation(YangInstanceIdentifier.empty());
60 private final Map<String, AbstractTransactionContextWrapper> txContextWrappers = new TreeMap<>();
61 private final AbstractTransactionContextFactory<?> txContextFactory;
62 private final TransactionType type;
63 private TransactionState state = TransactionState.OPEN;
66 public TransactionProxy(final AbstractTransactionContextFactory<?> txContextFactory, final TransactionType type) {
67 super(txContextFactory.nextIdentifier(), txContextFactory.getActorUtils().getDatastoreContext()
68 .isTransactionDebugContextEnabled());
69 this.txContextFactory = txContextFactory;
70 this.type = requireNonNull(type);
72 LOG.debug("New {} Tx - {}", type, getIdentifier());
76 public FluentFuture<Boolean> exists(final YangInstanceIdentifier path) {
77 return executeRead(shardNameFromIdentifier(path), new DataExists(path, DataStoreVersions.CURRENT_VERSION));
80 private <T> FluentFuture<T> executeRead(final String shardName, final AbstractRead<T> readCmd) {
81 checkState(type != TransactionType.WRITE_ONLY, "Reads from write-only transactions are not allowed");
83 LOG.trace("Tx {} {} {}", getIdentifier(), readCmd.getClass().getSimpleName(), readCmd.getPath());
85 final SettableFuture<T> proxyFuture = SettableFuture.create();
86 AbstractTransactionContextWrapper contextWrapper = wrapperFromShardName(shardName);
87 contextWrapper.maybeExecuteTransactionOperation(new TransactionOperation() {
89 public void invoke(final TransactionContext transactionContext, final Boolean havePermit) {
90 transactionContext.executeRead(readCmd, proxyFuture, havePermit);
94 return FluentFuture.from(proxyFuture);
98 public FluentFuture<Optional<NormalizedNode>> read(final YangInstanceIdentifier path) {
99 checkState(type != TransactionType.WRITE_ONLY, "Reads from write-only transactions are not allowed");
100 requireNonNull(path, "path should not be null");
102 LOG.trace("Tx {} read {}", getIdentifier(), path);
103 return path.isEmpty() ? readAllData() : singleShardRead(shardNameFromIdentifier(path), path);
106 private FluentFuture<Optional<NormalizedNode>> singleShardRead(final String shardName,
107 final YangInstanceIdentifier path) {
108 return executeRead(shardName, new ReadData(path, DataStoreVersions.CURRENT_VERSION));
111 private FluentFuture<Optional<NormalizedNode>> readAllData() {
112 final var actorUtils = getActorUtils();
113 return RootScatterGather.gather(actorUtils, actorUtils.getConfiguration().getAllShardNames().stream()
114 .map(shardName -> singleShardRead(shardName, YangInstanceIdentifier.empty())));
118 public void delete(final YangInstanceIdentifier path) {
119 checkModificationState("delete", path);
121 if (path.isEmpty()) {
124 executeModification(new DeleteOperation(path));
128 private void deleteAllData() {
129 for (String shardName : getActorUtils().getConfiguration().getAllShardNames()) {
130 wrapperFromShardName(shardName).maybeExecuteTransactionOperation(ROOT_DELETE_OPERATION);
135 public void merge(final YangInstanceIdentifier path, final NormalizedNode data) {
136 checkModificationState("merge", path);
138 if (path.isEmpty()) {
139 mergeAllData(RootScatterGather.castRootNode(data));
141 executeModification(new MergeOperation(path, data));
145 private void mergeAllData(final ContainerNode rootData) {
146 if (!rootData.isEmpty()) {
147 RootScatterGather.scatterTouched(rootData, this::wrapperFromRootChild).forEach(
148 scattered -> scattered.shard().maybeExecuteTransactionOperation(
149 new MergeOperation(YangInstanceIdentifier.empty(), scattered.container())));
154 public void write(final YangInstanceIdentifier path, final NormalizedNode data) {
155 checkModificationState("write", path);
157 if (path.isEmpty()) {
158 writeAllData(RootScatterGather.castRootNode(data));
160 executeModification(new WriteOperation(path, data));
164 private void writeAllData(final ContainerNode rootData) {
165 RootScatterGather.scatterAll(rootData, this::wrapperFromRootChild,
166 getActorUtils().getConfiguration().getAllShardNames().stream().map(this::wrapperFromShardName)).forEach(
167 scattered -> scattered.shard().maybeExecuteTransactionOperation(
168 new WriteOperation(YangInstanceIdentifier.empty(), scattered.container())));
171 private void executeModification(final TransactionModificationOperation operation) {
172 wrapperFromShardName(shardNameFromIdentifier(operation.path())).maybeExecuteTransactionOperation(operation);
175 private void checkModificationState(final String opName, final YangInstanceIdentifier path) {
176 checkState(type != TransactionType.READ_ONLY, "Modification operation on read-only transaction is not allowed");
177 checkState(state == TransactionState.OPEN, "Transaction is sealed - further modifications are not allowed");
178 LOG.trace("Tx {} {} {}", getIdentifier(), opName, path);
181 private boolean seal(final TransactionState newState) {
182 if (state == TransactionState.OPEN) {
190 public final void close() {
191 if (!seal(TransactionState.CLOSED)) {
192 checkState(state == TransactionState.CLOSED, "Transaction %s is ready, it cannot be closed",
194 // Idempotent no-op as per AutoCloseable recommendation
198 for (AbstractTransactionContextWrapper contextWrapper : txContextWrappers.values()) {
199 contextWrapper.maybeExecuteTransactionOperation(new TransactionOperation() {
201 public void invoke(final TransactionContext transactionContext, final Boolean havePermit) {
202 transactionContext.closeTransaction();
208 txContextWrappers.clear();
212 public final AbstractThreePhaseCommitCohort<?> ready() {
213 checkState(type != TransactionType.READ_ONLY, "Read-only transactions cannot be readied");
215 final boolean success = seal(TransactionState.READY);
216 checkState(success, "Transaction %s is %s, it cannot be readied", getIdentifier(), state);
218 LOG.debug("Tx {} Readying {} components for commit", getIdentifier(), txContextWrappers.size());
220 final AbstractThreePhaseCommitCohort<?> ret;
221 switch (txContextWrappers.size()) {
223 ret = NoOpDOMStoreThreePhaseCommitCohort.INSTANCE;
226 final Entry<String, AbstractTransactionContextWrapper> e = Iterables.getOnlyElement(
227 txContextWrappers.entrySet());
228 ret = createSingleCommitCohort(e.getKey(), e.getValue());
231 ret = createMultiCommitCohort();
234 txContextFactory.onTransactionReady(getIdentifier(), ret.getCohortFutures());
236 final Throwable debugContext = getDebugContext();
237 return debugContext == null ? ret : new DebugThreePhaseCommitCohort(getIdentifier(), ret, debugContext);
240 @SuppressWarnings({ "rawtypes", "unchecked" })
241 private AbstractThreePhaseCommitCohort<?> createSingleCommitCohort(final String shardName,
242 final AbstractTransactionContextWrapper contextWrapper) {
244 LOG.debug("Tx {} Readying transaction for shard {}", getIdentifier(), shardName);
246 final OperationCallback.Reference operationCallbackRef =
247 new OperationCallback.Reference(OperationCallback.NO_OP_CALLBACK);
249 final TransactionContext transactionContext = contextWrapper.getTransactionContext();
251 if (transactionContext == null) {
252 final Promise promise = akka.dispatch.Futures.promise();
253 contextWrapper.maybeExecuteTransactionOperation(new TransactionOperation() {
255 public void invoke(final TransactionContext newTransactionContext, final Boolean havePermit) {
256 promise.completeWith(getDirectCommitFuture(newTransactionContext, operationCallbackRef,
260 future = promise.future();
262 // avoid the creation of a promise and a TransactionOperation
263 future = getDirectCommitFuture(transactionContext, operationCallbackRef, null);
266 return new SingleCommitCohortProxy(txContextFactory.getActorUtils(), future, getIdentifier(),
267 operationCallbackRef);
270 private Future<?> getDirectCommitFuture(final TransactionContext transactionContext,
271 final OperationCallback.Reference operationCallbackRef, final Boolean havePermit) {
272 TransactionRateLimitingCallback rateLimitingCallback = new TransactionRateLimitingCallback(
273 txContextFactory.getActorUtils());
274 operationCallbackRef.set(rateLimitingCallback);
275 rateLimitingCallback.run();
276 return transactionContext.directCommit(havePermit);
279 private AbstractThreePhaseCommitCohort<ActorSelection> createMultiCommitCohort() {
281 final List<ThreePhaseCommitCohortProxy.CohortInfo> cohorts = new ArrayList<>(txContextWrappers.size());
282 final Optional<SortedSet<String>> shardNames = Optional.of(new TreeSet<>(txContextWrappers.keySet()));
283 for (Entry<String, AbstractTransactionContextWrapper> e : txContextWrappers.entrySet()) {
284 LOG.debug("Tx {} Readying transaction for shard {}", getIdentifier(), e.getKey());
286 final AbstractTransactionContextWrapper wrapper = e.getValue();
288 // The remote tx version is obtained the via TransactionContext which may not be available yet so
289 // we pass a Supplier to dynamically obtain it. Once the ready Future is resolved the
290 // TransactionContext is available.
291 cohorts.add(new ThreePhaseCommitCohortProxy.CohortInfo(wrapper.readyTransaction(shardNames),
292 () -> wrapper.getTransactionContext().getTransactionVersion()));
295 return new ThreePhaseCommitCohortProxy(txContextFactory.getActorUtils(), cohorts, getIdentifier());
298 private String shardNameFromIdentifier(final YangInstanceIdentifier path) {
299 return getActorUtils().getShardStrategyFactory().getStrategy(path).findShard(path);
302 private AbstractTransactionContextWrapper wrapperFromRootChild(final PathArgument childId) {
303 return wrapperFromShardName(shardNameFromIdentifier(YangInstanceIdentifier.create(childId)));
306 private AbstractTransactionContextWrapper wrapperFromShardName(final String shardName) {
307 final AbstractTransactionContextWrapper existing = txContextWrappers.get(shardName);
308 if (existing != null) {
312 final AbstractTransactionContextWrapper fresh = txContextFactory.newTransactionContextWrapper(this, shardName);
313 txContextWrappers.put(shardName, fresh);
317 TransactionType getType() {
322 return state != TransactionState.OPEN;
325 final ActorUtils getActorUtils() {
326 return txContextFactory.getActorUtils();