2 * Copyright (c) 2016 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.databroker.actors.dds;
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Iterables;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import java.util.Collection;
16 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
17 import org.opendaylight.mdsal.common.api.ReadFailedException;
18 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 * Client-side view of a transaction.
26 * This interface is used by the world outside of the actor system and in the actor system it is manifested via
27 * its client actor. That requires some state transfer with {@link DistributedDataStoreClientBehavior}. In order to
28 * reduce request latency, all messages are carbon-copied (and enqueued first) to the client actor.
31 * It is internally composed of multiple {@link RemoteProxyTransaction}s, each responsible for a component shard.
34 * Implementation is quite a bit complex, and involves cooperation with {@link AbstractClientHistory} for tracking
35 * gaps in transaction identifiers seen by backends.
38 * These gaps need to be accounted for in the transaction setup message sent to a particular backend, so it can verify
39 * that the requested transaction is in-sequence. This is critical in ensuring that transactions (which are independent
40 * entities from message queueing perspective) do not get reodered -- thus allowing multiple in-flight transactions.
43 * Alternative would be to force visibility by sending an abort request to all potential backends, but that would mean
44 * that even empty transactions increase load on all shards -- which would be a scalability issue.
47 * Yet another alternative would be to introduce inter-transaction dependencies to the queueing layer in client actor,
48 * but that would require additional indirection and complexity.
50 * @author Robert Varga
53 public final class ClientTransaction extends AbstractClientHandle<AbstractProxyTransaction> {
55 ClientTransaction(final AbstractClientHistory parent, final TransactionIdentifier transactionId) {
56 super(parent, transactionId);
60 private AbstractProxyTransaction createProxy(final Long shard) {
61 return parent().createTransactionProxy(getIdentifier(), shard);
64 private AbstractProxyTransaction ensureTransactionProxy(final YangInstanceIdentifier path) {
65 return ensureProxy(path, this::createProxy);
68 public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
69 return ensureTransactionProxy(path).exists(path);
72 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(
73 final YangInstanceIdentifier path) {
74 return ensureTransactionProxy(path).read(path);
77 public void delete(final YangInstanceIdentifier path) {
78 ensureTransactionProxy(path).delete(path);
81 public void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
82 ensureTransactionProxy(path).merge(path, data);
85 public void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
86 ensureTransactionProxy(path).write(path, data);
89 public DOMStoreThreePhaseCommitCohort ready() {
90 final Collection<AbstractProxyTransaction> toReady = ensureClosed();
91 Preconditions.checkState(toReady != null, "Attempted to submit a closed transaction %s", this);
93 toReady.forEach(AbstractProxyTransaction::seal);
94 final AbstractTransactionCommitCohort cohort;
95 switch (toReady.size()) {
97 cohort = new EmptyTransactionCommitCohort(parent(), getIdentifier());
100 cohort = new DirectTransactionCommitCohort(parent(), getIdentifier(),
101 Iterables.getOnlyElement(toReady));
104 cohort = new ClientTransactionCommitCohort(parent(), getIdentifier(), toReady);
108 return parent().onTransactionReady(this, cohort);