46e208b0b83a4396ce5cacdb1df1755f85d2b39d
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / ClientTransaction.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.databroker.actors.dds;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Iterables;
13 import com.google.common.util.concurrent.FluentFuture;
14 import java.util.Collection;
15 import java.util.Optional;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursor;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
20 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23
24 /**
25  * Client-side view of a transaction.
26  *
27  * <p>
28  * This interface is used by the world outside of the actor system and in the actor system it is manifested via
29  * its client actor. That requires some state transfer with {@link DistributedDataStoreClientBehavior}. In order to
30  * reduce request latency, all messages are carbon-copied (and enqueued first) to the client actor.
31  *
32  * <p>
33  * It is internally composed of multiple {@link RemoteProxyTransaction}s, each responsible for a component shard.
34  *
35  * <p>
36  * Implementation is quite a bit complex, and involves cooperation with {@link AbstractClientHistory} for tracking
37  * gaps in transaction identifiers seen by backends.
38  *
39  * <p>
40  * These gaps need to be accounted for in the transaction setup message sent to a particular backend, so it can verify
41  * that the requested transaction is in-sequence. This is critical in ensuring that transactions (which are independent
42  * entities from message queueing perspective) do not get reodered -- thus allowing multiple in-flight transactions.
43  *
44  * <p>
45  * Alternative would be to force visibility by sending an abort request to all potential backends, but that would mean
46  * that even empty transactions increase load on all shards -- which would be a scalability issue.
47  *
48  * <p>
49  * Yet another alternative would be to introduce inter-transaction dependencies to the queueing layer in client actor,
50  * but that would require additional indirection and complexity.
51  *
52  * @author Robert Varga
53  */
54 @Beta
55 public class ClientTransaction extends AbstractClientHandle<AbstractProxyTransaction> {
56
57     private ClientTransactionCursor cursor;
58
59     ClientTransaction(final AbstractClientHistory parent, final TransactionIdentifier transactionId) {
60         super(parent, transactionId);
61     }
62
63     private AbstractProxyTransaction createProxy(final Long shard) {
64         return parent().createTransactionProxy(getIdentifier(), shard);
65     }
66
67     private AbstractProxyTransaction ensureTransactionProxy(final YangInstanceIdentifier path) {
68         return ensureProxy(path, this::createProxy);
69     }
70
71     public DOMDataTreeWriteCursor openCursor() {
72         Preconditions.checkState(cursor == null, "Transaction %s has open cursor", getIdentifier());
73         cursor = new ClientTransactionCursor(this);
74         return cursor;
75     }
76
77     public FluentFuture<Boolean> exists(final YangInstanceIdentifier path) {
78         return ensureTransactionProxy(path).exists(path);
79     }
80
81     public FluentFuture<Optional<NormalizedNode<?, ?>>> read(final YangInstanceIdentifier path) {
82         return ensureTransactionProxy(path).read(path);
83     }
84
85     public void delete(final YangInstanceIdentifier path) {
86         ensureTransactionProxy(path).delete(path);
87     }
88
89     public void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
90         ensureTransactionProxy(path).merge(path, data);
91     }
92
93     public void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
94         ensureTransactionProxy(path).write(path, data);
95     }
96
97     public DOMStoreThreePhaseCommitCohort ready() {
98         final Collection<AbstractProxyTransaction> toReady = ensureClosed();
99         Preconditions.checkState(toReady != null, "Attempted to submit a closed transaction %s", this);
100
101         toReady.forEach(AbstractProxyTransaction::seal);
102         final AbstractTransactionCommitCohort cohort;
103         switch (toReady.size()) {
104             case 0:
105                 cohort = new EmptyTransactionCommitCohort(parent(), getIdentifier());
106                 break;
107             case 1:
108                 cohort = new DirectTransactionCommitCohort(parent(), getIdentifier(),
109                     Iterables.getOnlyElement(toReady));
110                 break;
111             default:
112                 cohort = new ClientTransactionCommitCohort(parent(), getIdentifier(), toReady);
113                 break;
114         }
115
116         return parent().onTransactionReady(this, cohort);
117     }
118
119     void closeCursor(final @NonNull DOMDataTreeCursor cursorToClose) {
120         if (cursorToClose.equals(this.cursor)) {
121             this.cursor = null;
122         }
123     }
124 }