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