BUG-5280: fix a thinko around sealed transactions
[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 akka.actor.ActorRef;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Verify;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.SettableFuture;
17 import java.util.function.Consumer;
18 import org.opendaylight.controller.cluster.access.commands.TransactionAbortRequest;
19 import org.opendaylight.controller.cluster.access.commands.TransactionAbortSuccess;
20 import org.opendaylight.controller.cluster.access.commands.TransactionCanCommitSuccess;
21 import org.opendaylight.controller.cluster.access.commands.TransactionCommitSuccess;
22 import org.opendaylight.controller.cluster.access.commands.TransactionDoCommitRequest;
23 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitRequest;
24 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitSuccess;
25 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
26 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
27 import org.opendaylight.controller.cluster.access.concepts.Response;
28 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
29 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
30 import org.opendaylight.yangtools.concepts.Identifiable;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33
34 /**
35  * Class translating transaction operations towards a particular backend shard.
36  *
37  * <p>
38  * This class is not safe to access from multiple application threads, as is usual for transactions. Internal state
39  * transitions coming from interactions with backend are expected to be thread-safe.
40  *
41  * <p>
42  * This class interacts with the queueing mechanism in ClientActorBehavior, hence once we arrive at a decision
43  * to use either a local or remote implementation, we are stuck with it. We can re-evaluate on the next transaction.
44  *
45  * @author Robert Varga
46  */
47 abstract class AbstractProxyTransaction implements Identifiable<TransactionIdentifier> {
48     private final DistributedDataStoreClientBehavior client;
49
50     private long sequence;
51     private boolean sealed;
52
53     AbstractProxyTransaction(final DistributedDataStoreClientBehavior client) {
54         this.client = Preconditions.checkNotNull(client);
55     }
56
57     final ActorRef localActor() {
58         return client.self();
59     }
60
61     final long nextSequence() {
62         return sequence++;
63     }
64
65     final void delete(final YangInstanceIdentifier path) {
66         checkNotSealed();
67         doDelete(path);
68     }
69
70     final void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
71         checkNotSealed();
72         doMerge(path, data);
73     }
74
75     final void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
76         checkNotSealed();
77         doWrite(path, data);
78     }
79
80     final CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
81         checkNotSealed();
82         return doExists(path);
83     }
84
85     final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
86         checkNotSealed();
87         return doRead(path);
88     }
89
90     final void sendRequest(final TransactionRequest<?> request, final Consumer<Response<?, ?>> completer) {
91         client.sendRequest(request, completer);
92     }
93
94     /**
95      * Seals this transaction when ready.
96      */
97     final void seal() {
98         checkNotSealed();
99         doSeal();
100         sealed = true;
101     }
102
103     private void checkNotSealed() {
104         Preconditions.checkState(!sealed, "Transaction %s has already been sealed", getIdentifier());
105     }
106
107     private void checkSealed() {
108         Preconditions.checkState(sealed, "Transaction %s has not been sealed yet", getIdentifier());
109     }
110
111     /**
112      * Abort this transaction. This is invoked only for read-only transactions and will result in an explicit message
113      * being sent to the backend.
114      */
115     final void abort() {
116         checkNotSealed();
117         doAbort();
118     }
119
120     void abort(final VotingFuture<Void> ret) {
121         checkSealed();
122
123         sendRequest(new TransactionAbortRequest(getIdentifier(), nextSequence(), localActor()), t -> {
124             if (t instanceof TransactionAbortSuccess) {
125                 ret.voteYes();
126             } else if (t instanceof RequestFailure) {
127                 ret.voteNo(((RequestFailure<?, ?>) t).getCause());
128             } else {
129                 ret.voteNo(new IllegalStateException("Unhandled response " + t.getClass()));
130             }
131         });
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         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 canCommit(final VotingFuture<?> ret) {
157         checkSealed();
158
159         sendRequest(Verify.verifyNotNull(doCommit(true)), t -> {
160             if (t instanceof TransactionCanCommitSuccess) {
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 preCommit(final VotingFuture<?> ret) {
171         checkSealed();
172
173         sendRequest(new TransactionPreCommitRequest(getIdentifier(), nextSequence(), localActor()), t -> {
174             if (t instanceof TransactionPreCommitSuccess) {
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 doCommit(final VotingFuture<?> ret) {
185         checkSealed();
186
187         sendRequest(new TransactionDoCommitRequest(getIdentifier(), nextSequence(), localActor()), t -> {
188             if (t instanceof TransactionCommitSuccess) {
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     abstract TransactionRequest<?> doCommit(boolean coordinated);
199
200     abstract void doDelete(final YangInstanceIdentifier path);
201
202     abstract void doMerge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data);
203
204     abstract void doWrite(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data);
205
206     abstract CheckedFuture<Boolean, ReadFailedException> doExists(final YangInstanceIdentifier path);
207
208     abstract CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> doRead(
209             final YangInstanceIdentifier path);
210
211     abstract void doSeal();
212
213     abstract void doAbort();
214 }