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