BUG-8538: rework transaction abort paths
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / LocalReadWriteProxyTransaction.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.Preconditions;
11 import com.google.common.base.Verify;
12 import java.util.Optional;
13 import java.util.function.BiConsumer;
14 import java.util.function.Consumer;
15 import java.util.function.Supplier;
16 import javax.annotation.Nullable;
17 import javax.annotation.concurrent.NotThreadSafe;
18 import org.opendaylight.controller.cluster.access.commands.AbortLocalTransactionRequest;
19 import org.opendaylight.controller.cluster.access.commands.AbstractLocalTransactionRequest;
20 import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
21 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequest;
22 import org.opendaylight.controller.cluster.access.commands.PersistenceProtocol;
23 import org.opendaylight.controller.cluster.access.commands.TransactionAbortRequest;
24 import org.opendaylight.controller.cluster.access.commands.TransactionDelete;
25 import org.opendaylight.controller.cluster.access.commands.TransactionDoCommitRequest;
26 import org.opendaylight.controller.cluster.access.commands.TransactionMerge;
27 import org.opendaylight.controller.cluster.access.commands.TransactionModification;
28 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitRequest;
29 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
30 import org.opendaylight.controller.cluster.access.commands.TransactionWrite;
31 import org.opendaylight.controller.cluster.access.concepts.Response;
32 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
33 import org.opendaylight.controller.cluster.datastore.util.AbstractDataTreeModificationCursor;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeModification;
38 import org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeSnapshot;
39 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
40 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
41 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * An {@link AbstractProxyTransaction} for dispatching a transaction towards a shard leader which is co-located with
47  * the client instance.
48  *
49  * <p>
50  * It requires a {@link DataTreeSnapshot}, which is used to instantiated a new {@link DataTreeModification}. Operations
51  * are then performed on this modification and once the transaction is submitted, the modification is sent to the shard
52  * leader.
53  *
54  * <p>
55  * This class is not thread-safe as usual with transactions. Since it does not interact with the backend until the
56  * transaction is submitted, at which point this class gets out of the picture, this is not a cause for concern.
57  *
58  * @author Robert Varga
59  */
60 @NotThreadSafe
61 final class LocalReadWriteProxyTransaction extends LocalProxyTransaction {
62     private static final Logger LOG = LoggerFactory.getLogger(LocalReadWriteProxyTransaction.class);
63
64     /**
65      * This field needs to be accessed via {@link #getModification()}, which performs state checking to ensure
66      * the modification can actually be accessed.
67      */
68     private final CursorAwareDataTreeModification modification;
69
70     private Supplier<? extends RuntimeException> closedException;
71
72     private CursorAwareDataTreeModification sealedModification;
73
74     /**
75      * Recorded failure from previous operations. Normally we would want to propagate the error directly to the
76      * offending call site, but that exposes inconsistency in behavior during initial connection, when we go through
77      * {@link RemoteProxyTransaction}, which detects this sort of issues at canCommit/directCommit time on the backend.
78      *
79      * <p>
80      * We therefore do not report incurred exceptions directly, but report them once the user attempts to commit
81      * this transaction.
82      */
83     private Exception recordedFailure;
84
85     LocalReadWriteProxyTransaction(final ProxyHistory parent, final TransactionIdentifier identifier,
86         final DataTreeSnapshot snapshot) {
87         super(parent, identifier);
88         this.modification = (CursorAwareDataTreeModification) snapshot.newModification();
89     }
90
91     @Override
92     boolean isSnapshotOnly() {
93         return false;
94     }
95
96     @Override
97     CursorAwareDataTreeSnapshot readOnlyView() {
98         return getModification();
99     }
100
101     @Override
102     @SuppressWarnings("checkstyle:IllegalCatch")
103     void doDelete(final YangInstanceIdentifier path) {
104         final CursorAwareDataTreeModification mod = getModification();
105         if (recordedFailure != null) {
106             LOG.debug("Transaction {} recorded failure, ignoring delete of {}", getIdentifier(), path);
107             return;
108         }
109
110         try {
111             mod.delete(path);
112         } catch (Exception e) {
113             LOG.debug("Transaction {} delete on {} incurred failure, delaying it until commit", getIdentifier(), path,
114                 e);
115             recordedFailure = e;
116         }
117     }
118
119     @Override
120     @SuppressWarnings("checkstyle:IllegalCatch")
121     void doMerge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
122         final CursorAwareDataTreeModification mod = getModification();
123         if (recordedFailure != null) {
124             LOG.debug("Transaction {} recorded failure, ignoring merge to {}", getIdentifier(), path);
125             return;
126         }
127
128         try {
129             mod.merge(path, data);
130         } catch (Exception e) {
131             LOG.debug("Transaction {} merge to {} incurred failure, delaying it until commit", getIdentifier(), path,
132                 e);
133             recordedFailure = e;
134         }
135     }
136
137     @Override
138     @SuppressWarnings("checkstyle:IllegalCatch")
139     void doWrite(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
140         final CursorAwareDataTreeModification mod = getModification();
141         if (recordedFailure != null) {
142             LOG.debug("Transaction {} recorded failure, ignoring write to {}", getIdentifier(), path);
143             return;
144         }
145
146         try {
147             mod.write(path, data);
148         } catch (Exception e) {
149             LOG.debug("Transaction {} write to {} incurred failure, delaying it until commit", getIdentifier(), path,
150                 e);
151             recordedFailure = e;
152         }
153     }
154
155     private RuntimeException abortedException() {
156         return new IllegalStateException("Tracker " + getIdentifier() + " has been aborted");
157     }
158
159     private RuntimeException submittedException() {
160         return new IllegalStateException("Tracker " + getIdentifier() + " has been submitted");
161     }
162
163     @Override
164     CommitLocalTransactionRequest commitRequest(final boolean coordinated) {
165         final CursorAwareDataTreeModification mod = getModification();
166         final CommitLocalTransactionRequest ret = new CommitLocalTransactionRequest(getIdentifier(), nextSequence(),
167             localActor(), mod, recordedFailure, coordinated);
168         closedException = this::submittedException;
169         return ret;
170     }
171
172     @Override
173     void doSeal() {
174         Preconditions.checkState(sealedModification == null, "Transaction %s is already sealed", getIdentifier());
175         final CursorAwareDataTreeModification mod = getModification();
176         mod.ready();
177         sealedModification = mod;
178     }
179
180     @Override
181     void flushState(final AbstractProxyTransaction successor) {
182         sealedModification.applyToCursor(new AbstractDataTreeModificationCursor() {
183             @Override
184             public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
185                 successor.write(current().node(child), data);
186             }
187
188             @Override
189             public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
190                 successor.merge(current().node(child), data);
191             }
192
193             @Override
194             public void delete(final PathArgument child) {
195                 successor.delete(current().node(child));
196             }
197         });
198     }
199
200     DataTreeSnapshot getSnapshot() {
201         Preconditions.checkState(sealedModification != null, "Proxy %s is not sealed yet", getIdentifier());
202         return sealedModification;
203     }
204
205     @Override
206     void applyForwardedModifyTransactionRequest(final ModifyTransactionRequest request,
207             final @Nullable Consumer<Response<?, ?>> callback) {
208         commonModifyTransactionRequest(request, callback, this::sendRequest);
209     }
210
211     @Override
212     void replayModifyTransactionRequest(final ModifyTransactionRequest request,
213             final @Nullable Consumer<Response<?, ?>> callback, final long enqueuedTicks) {
214         commonModifyTransactionRequest(request, callback, (req, cb) -> enqueueRequest(req, cb, enqueuedTicks));
215     }
216
217     private void commonModifyTransactionRequest(final ModifyTransactionRequest request,
218             final @Nullable Consumer<Response<?, ?>> callback,
219             final BiConsumer<TransactionRequest<?>, Consumer<Response<?, ?>>> sendMethod) {
220         for (final TransactionModification mod : request.getModifications()) {
221             if (mod instanceof TransactionWrite) {
222                 write(mod.getPath(), ((TransactionWrite)mod).getData());
223             } else if (mod instanceof TransactionMerge) {
224                 merge(mod.getPath(), ((TransactionMerge)mod).getData());
225             } else if (mod instanceof TransactionDelete) {
226                 delete(mod.getPath());
227             } else {
228                 throw new IllegalArgumentException("Unsupported modification " + mod);
229             }
230         }
231
232         final Optional<PersistenceProtocol> maybeProtocol = request.getPersistenceProtocol();
233         if (maybeProtocol.isPresent()) {
234             Verify.verify(callback != null, "Request {} has null callback", request);
235             ensureSealed();
236
237             switch (maybeProtocol.get()) {
238                 case ABORT:
239                     sendMethod.accept(new AbortLocalTransactionRequest(getIdentifier(), localActor()), callback);
240                     break;
241                 case READY:
242                     // No-op, as we have already issued a seal()
243                     break;
244                 case SIMPLE:
245                     sendMethod.accept(commitRequest(false), callback);
246                     break;
247                 case THREE_PHASE:
248                     sendMethod.accept(commitRequest(true), callback);
249                     break;
250                 default:
251                     throw new IllegalArgumentException("Unhandled protocol " + maybeProtocol.get());
252             }
253         }
254     }
255
256     @Override
257     void handleReplayedLocalRequest(final AbstractLocalTransactionRequest<?> request,
258             final Consumer<Response<?, ?>> callback, final long now) {
259         if (request instanceof CommitLocalTransactionRequest) {
260             sendCommit((CommitLocalTransactionRequest) request, callback);
261         } else {
262             super.handleReplayedLocalRequest(request, callback, now);
263         }
264     }
265
266     @Override
267     void handleReplayedRemoteRequest(final TransactionRequest<?> request,
268             final @Nullable Consumer<Response<?, ?>> callback, final long enqueuedTicks) {
269         LOG.debug("Applying replayed request {}", request);
270
271         if (request instanceof TransactionPreCommitRequest) {
272             enqueueRequest(new TransactionPreCommitRequest(getIdentifier(), nextSequence(), localActor()), callback,
273                 enqueuedTicks);
274         } else if (request instanceof TransactionDoCommitRequest) {
275             enqueueRequest(new TransactionDoCommitRequest(getIdentifier(), nextSequence(), localActor()), callback,
276                 enqueuedTicks);
277         } else if (request instanceof TransactionAbortRequest) {
278             enqueueDoAbort(callback, enqueuedTicks);
279         } else {
280             super.handleReplayedRemoteRequest(request, callback, enqueuedTicks);
281         }
282     }
283
284     @Override
285     void handleForwardedRemoteRequest(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
286         LOG.debug("Applying forwarded request {}", request);
287
288         if (request instanceof TransactionPreCommitRequest) {
289             sendRequest(new TransactionPreCommitRequest(getIdentifier(), nextSequence(), localActor()), callback);
290         } else if (request instanceof TransactionDoCommitRequest) {
291             sendRequest(new TransactionDoCommitRequest(getIdentifier(), nextSequence(), localActor()), callback);
292         } else if (request instanceof TransactionAbortRequest) {
293             sendDoAbort(callback);
294         } else {
295             super.handleForwardedRemoteRequest(request, callback);
296         }
297     }
298
299     @Override
300     void forwardToLocal(final LocalProxyTransaction successor, final TransactionRequest<?> request,
301             final Consumer<Response<?, ?>> callback) {
302         if (request instanceof CommitLocalTransactionRequest) {
303             Verify.verify(successor instanceof LocalReadWriteProxyTransaction);
304             ((LocalReadWriteProxyTransaction) successor).sendCommit((CommitLocalTransactionRequest)request, callback);
305             LOG.debug("Forwarded request {} to successor {}", request, successor);
306         } else {
307             super.forwardToLocal(successor, request, callback);
308         }
309     }
310
311     @Override
312     void sendAbort(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
313         super.sendAbort(request, callback);
314         closedException = this::abortedException;
315     }
316
317     @Override
318     void enqueueAbort(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback,
319             final long enqueuedTicks) {
320         super.enqueueAbort(request, callback, enqueuedTicks);
321         closedException = this::abortedException;
322     }
323
324     private CursorAwareDataTreeModification getModification() {
325         if (closedException != null) {
326             throw closedException.get();
327         }
328
329         return modification;
330     }
331
332     private void sendCommit(final CommitLocalTransactionRequest request, final Consumer<Response<?, ?>> callback) {
333         // Rebase old modification on new data tree.
334         final CursorAwareDataTreeModification mod = getModification();
335
336         try (DataTreeModificationCursor cursor = mod.createCursor(YangInstanceIdentifier.EMPTY)) {
337             request.getModification().applyToCursor(cursor);
338         }
339
340         ensureSealed();
341         sendRequest(commitRequest(request.isCoordinated()), callback);
342     }
343 }