BUG-5280: separate request sequence and transmit sequence
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / AbstractProxyTransaction.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.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Verify;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.SettableFuture;
16 import org.opendaylight.controller.cluster.access.commands.TransactionAbortRequest;
17 import org.opendaylight.controller.cluster.access.commands.TransactionAbortSuccess;
18 import org.opendaylight.controller.cluster.access.commands.TransactionCanCommitSuccess;
19 import org.opendaylight.controller.cluster.access.commands.TransactionCommitSuccess;
20 import org.opendaylight.controller.cluster.access.commands.TransactionDoCommitRequest;
21 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitRequest;
22 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitSuccess;
23 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
24 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
25 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
26 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
27 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
28 import org.opendaylight.yangtools.concepts.Identifiable;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
32
33 /**
34  * Class translating transaction operations towards a particular backend shard.
35  *
36  * This class is not safe to access from multiple application threads, as is usual for transactions. Internal state
37  * transitions coming from interactions with backend are expected to be thread-safe.
38  *
39  * This class interacts with the queueing mechanism in ClientActorBehavior, hence once we arrive at a decision
40  * to use either a local or remote implementation, we are stuck with it. We can re-evaluate on the next transaction.
41  *
42  * @author Robert Varga
43  */
44 abstract class AbstractProxyTransaction implements Identifiable<TransactionIdentifier> {
45     private final DistributedDataStoreClientBehavior client;
46
47     private long sequence;
48     private boolean sealed;
49
50     AbstractProxyTransaction(final DistributedDataStoreClientBehavior client) {
51         this.client = Preconditions.checkNotNull(client);
52     }
53
54     /**
55      * Instantiate a new tracker for a transaction. This method bases its decision on which implementation to use
56      * based on provided {@link ShardBackendInfo}. If no information is present, it will choose the remote
57      * implementation, which is fine, as the queueing logic in ClientActorBehavior will hold on to the requests until
58      * the backend is located.
59      *
60      * @param client Client behavior
61      * @param historyId Local history identifier
62      * @param transactionId Transaction identifier
63      * @param backend Optional backend identifier
64      * @return A new state tracker
65      */
66     static AbstractProxyTransaction create(final DistributedDataStoreClientBehavior client,
67             final LocalHistoryIdentifier historyId, final long transactionId,
68             final java.util.Optional<ShardBackendInfo> backend) {
69
70         final java.util.Optional<DataTree> dataTree = backend.flatMap(ShardBackendInfo::getDataTree);
71         final TransactionIdentifier identifier = new TransactionIdentifier(historyId, transactionId);
72         if (dataTree.isPresent()) {
73             return new LocalProxyTransaction(client, identifier, dataTree.get().takeSnapshot());
74         } else {
75             return new RemoteProxyTransaction(client, identifier);
76         }
77     }
78
79     final DistributedDataStoreClientBehavior client() {
80         return client;
81     }
82
83     final long nextSequence() {
84         return sequence++;
85     }
86
87     final void delete(final YangInstanceIdentifier path) {
88         checkSealed();
89         doDelete(path);
90     }
91
92     final void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
93         checkSealed();
94         doMerge(path, data);
95     }
96
97     final void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
98         checkSealed();
99         doWrite(path, data);
100     }
101
102     final CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
103         checkSealed();
104         return doExists(path);
105     }
106
107     final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
108         checkSealed();
109         return doRead(path);
110     }
111
112     /**
113      * Seal this transaction before it is either
114      */
115     final void seal() {
116         checkSealed();
117         doSeal();
118         sealed = true;
119     }
120
121     private void checkSealed() {
122         Preconditions.checkState(sealed, "Transaction %s has not been sealed yet", getIdentifier());
123     }
124
125     /**
126      * Abort this transaction. This is invoked only for read-only transactions and will result in an explicit message
127      * being sent to the backend.
128      */
129     final void abort() {
130         checkSealed();
131         doAbort();
132     }
133
134     /**
135      * Commit this transaction, possibly in a coordinated fashion.
136      *
137      * @param coordinated True if this transaction should be coordinated across multiple participants.
138      * @return Future completion
139      */
140     final ListenableFuture<Boolean> directCommit() {
141         checkSealed();
142
143         final SettableFuture<Boolean> ret = SettableFuture.create();
144         client().sendRequest(Verify.verifyNotNull(doCommit(false)), t -> {
145             if (t instanceof TransactionCommitSuccess) {
146                 ret.set(Boolean.TRUE);
147             } else if (t instanceof RequestFailure) {
148                 ret.setException(((RequestFailure<?, ?>) t).getCause());
149             } else {
150                 ret.setException(new IllegalStateException("Unhandled response " + t.getClass()));
151             }
152         });
153         return ret;
154     }
155
156     void abort(final VotingFuture<Void> ret) {
157         checkSealed();
158
159         client.sendRequest(new TransactionAbortRequest(getIdentifier(), nextSequence(), client().self()), t -> {
160             if (t instanceof TransactionAbortSuccess) {
161                 ret.voteYes();
162             } else if (t instanceof RequestFailure) {
163                 ret.voteNo(((RequestFailure<?, ?>) t).getCause());
164             } else {
165                 ret.voteNo(new IllegalStateException("Unhandled response " + t.getClass()));
166             }
167         });
168     }
169
170     void canCommit(final VotingFuture<?> ret) {
171         checkSealed();
172
173         client.sendRequest(Verify.verifyNotNull(doCommit(true)), t -> {
174             if (t instanceof TransactionCanCommitSuccess) {
175                 ret.voteYes();
176             } else if (t instanceof RequestFailure) {
177                 ret.voteNo(((RequestFailure<?, ?>) t).getCause());
178             } else {
179                 ret.voteNo(new IllegalStateException("Unhandled response " + t.getClass()));
180             }
181         });
182     }
183
184     void preCommit(final VotingFuture<?> ret) {
185         checkSealed();
186
187         client.sendRequest(new TransactionPreCommitRequest(getIdentifier(), nextSequence(), client().self()), t-> {
188             if (t instanceof TransactionPreCommitSuccess) {
189                 ret.voteYes();
190             } else if (t instanceof RequestFailure) {
191                 ret.voteNo(((RequestFailure<?, ?>) t).getCause());
192             } else {
193                 ret.voteNo(new IllegalStateException("Unhandled response " + t.getClass()));
194             }
195         });
196     }
197
198     void doCommit(final VotingFuture<?> ret) {
199         checkSealed();
200
201         client.sendRequest(new TransactionDoCommitRequest(getIdentifier(), nextSequence(), client().self()), t-> {
202             if (t instanceof TransactionCommitSuccess) {
203                 ret.voteYes();
204             } else if (t instanceof RequestFailure) {
205                 ret.voteNo(((RequestFailure<?, ?>) t).getCause());
206             } else {
207                 ret.voteNo(new IllegalStateException("Unhandled response " + t.getClass()));
208             }
209         });
210     }
211
212     abstract void doDelete(final YangInstanceIdentifier path);
213
214     abstract void doMerge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data);
215
216     abstract void doWrite(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data);
217
218     abstract CheckedFuture<Boolean, ReadFailedException> doExists(final YangInstanceIdentifier path);
219
220     abstract CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> doRead(final YangInstanceIdentifier path);
221
222     abstract void doSeal();
223
224     abstract void doAbort();
225
226     abstract TransactionRequest<?> doCommit(boolean coordinated);
227 }