7cdc04aba17b471ea218e3e8060141b9a8a2451e
[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 static com.google.common.base.Preconditions.checkState;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.util.concurrent.FluentFuture;
14 import java.util.Collection;
15 import java.util.Map;
16 import java.util.Optional;
17 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
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;
21
22 /**
23  * Client-side view of a transaction.
24  *
25  * <p>
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.
29  *
30  * <p>
31  * It is internally composed of multiple {@link RemoteProxyTransaction}s, each responsible for a component shard.
32  *
33  * <p>
34  * Implementation is quite a bit complex, and involves cooperation with {@link AbstractClientHistory} for tracking
35  * gaps in transaction identifiers seen by backends.
36  *
37  * <p>
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.
41  *
42  * <p>
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.
45  *
46  * <p>
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.
49  *
50  * @author Robert Varga
51  */
52 @Beta
53 public class ClientTransaction extends AbstractClientHandle<AbstractProxyTransaction> {
54     ClientTransaction(final AbstractClientHistory parent, final TransactionIdentifier transactionId) {
55         super(parent, transactionId);
56     }
57
58     private AbstractProxyTransaction ensureTransactionProxy(final YangInstanceIdentifier path) {
59         return ensureProxy(path);
60     }
61
62     public FluentFuture<Boolean> exists(final YangInstanceIdentifier path) {
63         return ensureTransactionProxy(path).exists(path);
64     }
65
66     public FluentFuture<Optional<NormalizedNode>> read(final YangInstanceIdentifier path) {
67         return ensureTransactionProxy(path).read(path);
68     }
69
70     public void delete(final YangInstanceIdentifier path) {
71         ensureTransactionProxy(path).delete(path);
72     }
73
74     public void merge(final YangInstanceIdentifier path, final NormalizedNode data) {
75         ensureTransactionProxy(path).merge(path, data);
76     }
77
78     public void write(final YangInstanceIdentifier path, final NormalizedNode data) {
79         ensureTransactionProxy(path).write(path, data);
80     }
81
82     public DOMStoreThreePhaseCommitCohort ready() {
83         final Map<Long, AbstractProxyTransaction> participants = ensureClosed();
84         checkState(participants != null, "Attempted to submit a closed transaction %s", this);
85
86         final Collection<AbstractProxyTransaction> toReady = participants.values();
87         toReady.forEach(AbstractProxyTransaction::seal);
88
89         final TransactionIdentifier txId = getIdentifier();
90         final AbstractClientHistory parent = parent();
91         parent.onTransactionShardsBound(txId, participants.keySet());
92
93         final AbstractTransactionCommitCohort cohort;
94         switch (toReady.size()) {
95             case 0:
96                 cohort = new EmptyTransactionCommitCohort(parent, txId);
97                 break;
98             case 1:
99                 cohort = new DirectTransactionCommitCohort(parent, txId, toReady.iterator().next());
100                 break;
101             default:
102                 cohort = new ClientTransactionCommitCohort(parent, txId, toReady);
103                 break;
104         }
105
106         return parent.onTransactionReady(this, cohort);
107     }
108
109     @Override
110     final AbstractProxyTransaction createProxy(final Long shard) {
111         return parent().createTransactionProxy(getIdentifier(), shard);
112     }
113 }