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