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 static com.google.common.base.Preconditions.checkState;
12 import com.google.common.annotations.Beta;
13 import com.google.common.util.concurrent.FluentFuture;
14 import java.util.Collection;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursor;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
21 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 * Client-side view of a transaction.
29 * This interface is used by the world outside of the actor system and in the actor system it is manifested via
30 * its client actor. That requires some state transfer with {@link DistributedDataStoreClientBehavior}. In order to
31 * reduce request latency, all messages are carbon-copied (and enqueued first) to the client actor.
34 * It is internally composed of multiple {@link RemoteProxyTransaction}s, each responsible for a component shard.
37 * Implementation is quite a bit complex, and involves cooperation with {@link AbstractClientHistory} for tracking
38 * gaps in transaction identifiers seen by backends.
41 * These gaps need to be accounted for in the transaction setup message sent to a particular backend, so it can verify
42 * that the requested transaction is in-sequence. This is critical in ensuring that transactions (which are independent
43 * entities from message queueing perspective) do not get reodered -- thus allowing multiple in-flight transactions.
46 * Alternative would be to force visibility by sending an abort request to all potential backends, but that would mean
47 * that even empty transactions increase load on all shards -- which would be a scalability issue.
50 * Yet another alternative would be to introduce inter-transaction dependencies to the queueing layer in client actor,
51 * but that would require additional indirection and complexity.
53 * @author Robert Varga
56 public class ClientTransaction extends AbstractClientHandle<AbstractProxyTransaction> {
58 private ClientTransactionCursor cursor;
60 ClientTransaction(final AbstractClientHistory parent, final TransactionIdentifier transactionId) {
61 super(parent, transactionId);
64 private AbstractProxyTransaction ensureTransactionProxy(final YangInstanceIdentifier path) {
65 return ensureProxy(path);
68 public DOMDataTreeWriteCursor openCursor() {
69 checkState(cursor == null, "Transaction %s has open cursor", getIdentifier());
70 cursor = new ClientTransactionCursor(this);
74 public FluentFuture<Boolean> exists(final YangInstanceIdentifier path) {
75 return ensureTransactionProxy(path).exists(path);
78 public FluentFuture<Optional<NormalizedNode<?, ?>>> read(final YangInstanceIdentifier path) {
79 return ensureTransactionProxy(path).read(path);
82 public void delete(final YangInstanceIdentifier path) {
83 ensureTransactionProxy(path).delete(path);
86 public void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
87 ensureTransactionProxy(path).merge(path, data);
90 public void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
91 ensureTransactionProxy(path).write(path, data);
94 public DOMStoreThreePhaseCommitCohort ready() {
95 final Map<Long, AbstractProxyTransaction> participants = ensureClosed();
96 checkState(participants != null, "Attempted to submit a closed transaction %s", this);
98 final Collection<AbstractProxyTransaction> toReady = participants.values();
99 toReady.forEach(AbstractProxyTransaction::seal);
101 final TransactionIdentifier txId = getIdentifier();
102 final AbstractClientHistory parent = parent();
103 parent.onTransactionShardsBound(txId, participants.keySet());
105 final AbstractTransactionCommitCohort cohort;
106 switch (toReady.size()) {
108 cohort = new EmptyTransactionCommitCohort(parent, txId);
111 cohort = new DirectTransactionCommitCohort(parent, txId, toReady.iterator().next());
114 cohort = new ClientTransactionCommitCohort(parent, txId, toReady);
118 return parent.onTransactionReady(this, cohort);
122 final AbstractProxyTransaction createProxy(final Long shard) {
123 return parent().createTransactionProxy(getIdentifier(), shard);
126 void closeCursor(final @NonNull DOMDataTreeCursor cursorToClose) {
127 if (cursorToClose.equals(this.cursor)) {