BUG-8704: rework seal mechanics to not wait during replay
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / RemoteProxyTransaction.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.Optional;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.util.function.Consumer;
16 import javax.annotation.Nullable;
17 import org.opendaylight.controller.cluster.access.commands.AbortLocalTransactionRequest;
18 import org.opendaylight.controller.cluster.access.commands.AbstractLocalTransactionRequest;
19 import org.opendaylight.controller.cluster.access.commands.AbstractReadTransactionRequest;
20 import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
21 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionRequest;
22 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionSuccess;
23 import org.opendaylight.controller.cluster.access.commands.IncrementTransactionSequenceRequest;
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.ReadTransactionRequest;
28 import org.opendaylight.controller.cluster.access.commands.ReadTransactionSuccess;
29 import org.opendaylight.controller.cluster.access.commands.TransactionAbortRequest;
30 import org.opendaylight.controller.cluster.access.commands.TransactionDelete;
31 import org.opendaylight.controller.cluster.access.commands.TransactionDoCommitRequest;
32 import org.opendaylight.controller.cluster.access.commands.TransactionMerge;
33 import org.opendaylight.controller.cluster.access.commands.TransactionModification;
34 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitRequest;
35 import org.opendaylight.controller.cluster.access.commands.TransactionPurgeRequest;
36 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
37 import org.opendaylight.controller.cluster.access.commands.TransactionSuccess;
38 import org.opendaylight.controller.cluster.access.commands.TransactionWrite;
39 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
40 import org.opendaylight.controller.cluster.access.concepts.Response;
41 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
42 import org.opendaylight.controller.cluster.datastore.util.AbstractDataTreeModificationCursor;
43 import org.opendaylight.mdsal.common.api.ReadFailedException;
44 import org.opendaylight.yangtools.util.concurrent.MappingCheckedFuture;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
47 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 /**
53  * An {@link AbstractProxyTransaction} for dispatching a transaction towards a shard leader whose location is currently
54  * not known or is known to be not co-located with the client.
55  *
56  * <p>
57  * It packages operations and sends them via the client actor queue to the shard leader. That queue is responsible for
58  * maintaining any submitted operations until the leader is discovered.
59  *
60  * <p>
61  * This class is not safe to access from multiple application threads, as is usual for transactions. Its internal state
62  * transitions based on backend responses are thread-safe.
63  *
64  * @author Robert Varga
65  */
66 final class RemoteProxyTransaction extends AbstractProxyTransaction {
67     private static final Logger LOG = LoggerFactory.getLogger(RemoteProxyTransaction.class);
68
69     // FIXME: make this tuneable
70     private static final int REQUEST_MAX_MODIFICATIONS = 1000;
71
72     private final ModifyTransactionRequestBuilder builder;
73     private final boolean sendReadyOnSeal;
74     private final boolean snapshotOnly;
75
76     private boolean builderBusy;
77
78     private volatile Exception operationFailure;
79
80     RemoteProxyTransaction(final ProxyHistory parent, final TransactionIdentifier identifier,
81             final boolean snapshotOnly, final boolean sendReadyOnSeal, final boolean isDone) {
82         super(parent, isDone);
83         this.snapshotOnly = snapshotOnly;
84         this.sendReadyOnSeal = sendReadyOnSeal;
85         builder = new ModifyTransactionRequestBuilder(identifier, localActor());
86     }
87
88     @Override
89     boolean isSnapshotOnly() {
90         return snapshotOnly;
91     }
92
93     @Override
94     public TransactionIdentifier getIdentifier() {
95         return builder.getIdentifier();
96     }
97
98     @Override
99     void doDelete(final YangInstanceIdentifier path) {
100         appendModification(new TransactionDelete(path), Optional.absent());
101     }
102
103     @Override
104     void doMerge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
105         appendModification(new TransactionMerge(path, data), Optional.absent());
106     }
107
108     @Override
109     void doWrite(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
110         appendModification(new TransactionWrite(path, data), Optional.absent());
111     }
112
113     private <T> CheckedFuture<T, ReadFailedException> sendReadRequest(final AbstractReadTransactionRequest<?> request,
114             final Consumer<Response<?, ?>> completer, final ListenableFuture<T> future) {
115         // Check if a previous operation failed. If it has, do not bother sending anything and report a failure
116         final Exception local = operationFailure;
117         if (local != null) {
118             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Previous operation failed", local));
119         }
120
121         // Make sure we send any modifications before issuing a read
122         ensureFlushedBuider();
123         sendRequest(request, completer);
124         return MappingCheckedFuture.create(future, ReadFailedException.MAPPER);
125     }
126
127     @Override
128     CheckedFuture<Boolean, ReadFailedException> doExists(final YangInstanceIdentifier path) {
129         final SettableFuture<Boolean> future = SettableFuture.create();
130         return sendReadRequest(new ExistsTransactionRequest(getIdentifier(), nextSequence(), localActor(), path,
131             isSnapshotOnly()), t -> completeExists(future, t), future);
132     }
133
134     @Override
135     CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> doRead(final YangInstanceIdentifier path) {
136         final SettableFuture<Optional<NormalizedNode<?, ?>>> future = SettableFuture.create();
137         return sendReadRequest(new ReadTransactionRequest(getIdentifier(), nextSequence(), localActor(), path,
138             isSnapshotOnly()), t -> completeRead(future, t), future);
139     }
140
141     private void ensureInitializedBuilder() {
142         if (!builderBusy) {
143             builder.setSequence(nextSequence());
144             builderBusy = true;
145         }
146     }
147
148     private void ensureFlushedBuider() {
149         ensureFlushedBuider(Optional.absent());
150     }
151
152     private void ensureFlushedBuider(final Optional<Long> enqueuedTicks) {
153         if (builderBusy) {
154             flushBuilder(enqueuedTicks);
155         }
156     }
157
158     private void flushBuilder(final Optional<Long> enqueuedTicks) {
159         final ModifyTransactionRequest request = builder.build();
160         builderBusy = false;
161
162         sendModification(request, enqueuedTicks);
163     }
164
165     private void sendModification(final TransactionRequest<?> request, final Optional<Long> enqueuedTicks) {
166         if (enqueuedTicks.isPresent()) {
167             enqueueRequest(request, response -> completeModify(request, response), enqueuedTicks.get().longValue());
168         } else {
169             sendRequest(request, response -> completeModify(request, response));
170         }
171     }
172
173     private void appendModification(final TransactionModification modification) {
174         appendModification(modification, Optional.absent());
175     }
176
177     private void appendModification(final TransactionModification modification, final Optional<Long> enqueuedTicks) {
178         if (operationFailure == null) {
179             ensureInitializedBuilder();
180
181             builder.addModification(modification);
182             if (builder.size() >= REQUEST_MAX_MODIFICATIONS) {
183                 flushBuilder(enqueuedTicks);
184             }
185         } else {
186             LOG.debug("Transaction {} failed, not attempting further transactions", getIdentifier());
187         }
188     }
189
190     private void completeModify(final TransactionRequest<?> request, final Response<?, ?> response) {
191         LOG.debug("Modification request {} completed with {}", request, response);
192
193         if (response instanceof TransactionSuccess) {
194             // Happy path
195             recordSuccessfulRequest(request);
196         } else {
197             recordFailedResponse(response);
198         }
199     }
200
201     private Exception recordFailedResponse(final Response<?, ?> response) {
202         final Exception failure;
203         if (response instanceof RequestFailure) {
204             failure = ((RequestFailure<?, ?>) response).getCause();
205         } else {
206             LOG.warn("Unhandled response {}", response);
207             failure = new IllegalArgumentException("Unhandled response " + response.getClass());
208         }
209
210         if (operationFailure == null) {
211             LOG.debug("Transaction {} failed", getIdentifier(), failure);
212             operationFailure = failure;
213         }
214         return failure;
215     }
216
217     private void failFuture(final SettableFuture<?> future, final Response<?, ?> response) {
218         future.setException(recordFailedResponse(response));
219     }
220
221     private void completeExists(final SettableFuture<Boolean> future, final Response<?, ?> response) {
222         LOG.debug("Exists request completed with {}", response);
223
224         if (response instanceof ExistsTransactionSuccess) {
225             future.set(((ExistsTransactionSuccess) response).getExists());
226         } else {
227             failFuture(future, response);
228         }
229
230         recordFinishedRequest(response);
231     }
232
233     private void completeRead(final SettableFuture<Optional<NormalizedNode<?, ?>>> future,
234             final Response<?, ?> response) {
235         LOG.debug("Read request completed with {}", response);
236
237         if (response instanceof ReadTransactionSuccess) {
238             future.set(((ReadTransactionSuccess) response).getData());
239         } else {
240             failFuture(future, response);
241         }
242
243         recordFinishedRequest(response);
244     }
245
246     @Override
247     ModifyTransactionRequest abortRequest() {
248         ensureInitializedBuilder();
249         builder.setAbort();
250         builderBusy = false;
251         return builder.build();
252     }
253
254     @Override
255     ModifyTransactionRequest commitRequest(final boolean coordinated) {
256         ensureInitializedBuilder();
257         builder.setCommit(coordinated);
258         builderBusy = false;
259         return builder.build();
260     }
261
262     private ModifyTransactionRequest readyRequest() {
263         ensureInitializedBuilder();
264         builder.setReady();
265         builderBusy = false;
266         return builder.build();
267     }
268
269     @Override
270     boolean sealAndSend(final Optional<Long> enqueuedTicks) {
271         if (sendReadyOnSeal) {
272             ensureInitializedBuilder();
273             builder.setReady();
274             flushBuilder(enqueuedTicks);
275         }
276         return super.sealAndSend(enqueuedTicks);
277     }
278
279     @Override
280     void flushState(final AbstractProxyTransaction successor) {
281         if (builderBusy) {
282             final ModifyTransactionRequest request = builder.build();
283             builderBusy = false;
284             forwardToSuccessor(successor, request, null);
285         }
286     }
287
288     @Override
289     void forwardToRemote(final RemoteProxyTransaction successor, final TransactionRequest<?> request,
290             final Consumer<Response<?, ?>> callback) {
291         successor.handleForwardedRequest(request, callback);
292     }
293
294     private void handleForwardedRequest(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
295         if (request instanceof ModifyTransactionRequest) {
296             final ModifyTransactionRequest req = (ModifyTransactionRequest) request;
297
298             req.getModifications().forEach(this::appendModification);
299
300             final java.util.Optional<PersistenceProtocol> maybeProto = req.getPersistenceProtocol();
301             if (maybeProto.isPresent()) {
302                 // Persistence protocol implies we are sealed, propagate the marker, but hold off doing other actions
303                 // until we know what we are going to do.
304                 if (markSealed()) {
305                     sealOnly();
306                 }
307
308                 final TransactionRequest<?> tmp;
309                 switch (maybeProto.get()) {
310                     case ABORT:
311                         tmp = abortRequest();
312                         sendRequest(tmp, resp -> {
313                             completeModify(tmp, resp);
314                             callback.accept(resp);
315                         });
316                         break;
317                     case SIMPLE:
318                         tmp = commitRequest(false);
319                         sendRequest(tmp, resp -> {
320                             completeModify(tmp, resp);
321                             callback.accept(resp);
322                         });
323                         break;
324                     case THREE_PHASE:
325                         tmp = commitRequest(true);
326                         sendRequest(tmp, resp -> {
327                             recordSuccessfulRequest(tmp);
328                             callback.accept(resp);
329                         });
330                         break;
331                     case READY:
332                         tmp = readyRequest();
333                         sendRequest(tmp, resp -> {
334                             recordSuccessfulRequest(tmp);
335                             callback.accept(resp);
336                         });
337                         break;
338                     default:
339                         throw new IllegalArgumentException("Unhandled protocol " + maybeProto.get());
340                 }
341             }
342         } else if (request instanceof ReadTransactionRequest) {
343             ensureFlushedBuider();
344             sendRequest(new ReadTransactionRequest(getIdentifier(), nextSequence(), localActor(),
345                 ((ReadTransactionRequest) request).getPath(), isSnapshotOnly()), resp -> {
346                     recordFinishedRequest(resp);
347                     callback.accept(resp);
348                 });
349         } else if (request instanceof ExistsTransactionRequest) {
350             ensureFlushedBuider();
351             sendRequest(new ExistsTransactionRequest(getIdentifier(), nextSequence(), localActor(),
352                 ((ExistsTransactionRequest) request).getPath(), isSnapshotOnly()), resp -> {
353                     recordFinishedRequest(resp);
354                     callback.accept(resp);
355                 });
356         } else if (request instanceof TransactionPreCommitRequest) {
357             ensureFlushedBuider();
358             final TransactionRequest<?> tmp = new TransactionPreCommitRequest(getIdentifier(), nextSequence(),
359                 localActor());
360             sendRequest(tmp, resp -> {
361                 recordSuccessfulRequest(tmp);
362                 callback.accept(resp);
363             });
364         } else if (request instanceof TransactionDoCommitRequest) {
365             ensureFlushedBuider();
366             sendRequest(new TransactionDoCommitRequest(getIdentifier(), nextSequence(), localActor()), callback);
367         } else if (request instanceof TransactionAbortRequest) {
368             ensureFlushedBuider();
369             sendDoAbort(callback);
370         } else if (request instanceof TransactionPurgeRequest) {
371             enqueuePurge(callback);
372         } else {
373             throw new IllegalArgumentException("Unhandled request {}" + request);
374         }
375     }
376
377     @Override
378     void forwardToLocal(final LocalProxyTransaction successor, final TransactionRequest<?> request,
379             final Consumer<Response<?, ?>> callback) {
380         successor.handleForwardedRemoteRequest(request, callback);
381     }
382
383     @Override
384     void handleReplayedLocalRequest(final AbstractLocalTransactionRequest<?> request,
385             final Consumer<Response<?, ?>> callback, final long enqueuedTicks) {
386         if (request instanceof CommitLocalTransactionRequest) {
387             replayLocalCommitRequest((CommitLocalTransactionRequest) request, callback, enqueuedTicks);
388         } else if (request instanceof AbortLocalTransactionRequest) {
389             enqueueRequest(abortRequest(), callback, enqueuedTicks);
390         } else {
391             throw new IllegalStateException("Unhandled request " + request);
392         }
393     }
394
395     private void replayLocalCommitRequest(final CommitLocalTransactionRequest request,
396             final Consumer<Response<?, ?>> callback, final long enqueuedTicks) {
397         final DataTreeModification mod = request.getModification();
398         final Optional<Long> optTicks = Optional.of(Long.valueOf(enqueuedTicks));
399
400         mod.applyToCursor(new AbstractDataTreeModificationCursor() {
401             @Override
402             public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
403                 appendModification(new TransactionWrite(current().node(child), data), optTicks);
404             }
405
406             @Override
407             public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
408                 appendModification(new TransactionMerge(current().node(child), data), optTicks);
409             }
410
411             @Override
412             public void delete(final PathArgument child) {
413                 appendModification(new TransactionDelete(current().node(child)), optTicks);
414             }
415         });
416
417         enqueueRequest(commitRequest(request.isCoordinated()), callback, enqueuedTicks);
418     }
419
420     @Override
421     void handleReplayedRemoteRequest(final TransactionRequest<?> request,
422             final @Nullable Consumer<Response<?, ?>> callback, final long enqueuedTicks) {
423         final Consumer<Response<?, ?>> cb = callback != null ? callback : resp -> { };
424         final Optional<Long> optTicks = Optional.of(Long.valueOf(enqueuedTicks));
425
426         if (request instanceof ModifyTransactionRequest) {
427             final ModifyTransactionRequest req = (ModifyTransactionRequest) request;
428             for (TransactionModification mod : req.getModifications()) {
429                 appendModification(mod, optTicks);
430             }
431
432             final java.util.Optional<PersistenceProtocol> maybeProto = req.getPersistenceProtocol();
433             if (maybeProto.isPresent()) {
434                 // Persistence protocol implies we are sealed, propagate the marker, but hold off doing other actions
435                 // until we know what we are going to do.
436                 if (markSealed()) {
437                     sealOnly();
438                 }
439
440                 final TransactionRequest<?> tmp;
441                 switch (maybeProto.get()) {
442                     case ABORT:
443                         tmp = abortRequest();
444                         enqueueRequest(tmp, resp -> {
445                             completeModify(tmp, resp);
446                             cb.accept(resp);
447                         }, enqueuedTicks);
448                         break;
449                     case SIMPLE:
450                         tmp = commitRequest(false);
451                         enqueueRequest(tmp, resp -> {
452                             completeModify(tmp, resp);
453                             cb.accept(resp);
454                         }, enqueuedTicks);
455                         break;
456                     case THREE_PHASE:
457                         tmp = commitRequest(true);
458                         enqueueRequest(tmp, resp -> {
459                             recordSuccessfulRequest(tmp);
460                             cb.accept(resp);
461                         }, enqueuedTicks);
462                         break;
463                     case READY:
464                         tmp = readyRequest();
465                         enqueueRequest(tmp, resp -> {
466                             recordSuccessfulRequest(tmp);
467                             cb.accept(resp);
468                         }, enqueuedTicks);
469                         break;
470                     default:
471                         throw new IllegalArgumentException("Unhandled protocol " + maybeProto.get());
472                 }
473             }
474         } else if (request instanceof ReadTransactionRequest) {
475             ensureFlushedBuider(optTicks);
476             enqueueRequest(new ReadTransactionRequest(getIdentifier(), nextSequence(), localActor(),
477                 ((ReadTransactionRequest) request).getPath(), isSnapshotOnly()), resp -> {
478                     recordFinishedRequest(resp);
479                     cb.accept(resp);
480                 }, enqueuedTicks);
481         } else if (request instanceof ExistsTransactionRequest) {
482             ensureFlushedBuider(optTicks);
483             enqueueRequest(new ExistsTransactionRequest(getIdentifier(), nextSequence(), localActor(),
484                 ((ExistsTransactionRequest) request).getPath(), isSnapshotOnly()), resp -> {
485                     recordFinishedRequest(resp);
486                     cb.accept(resp);
487                 }, enqueuedTicks);
488         } else if (request instanceof TransactionPreCommitRequest) {
489             ensureFlushedBuider(optTicks);
490             final TransactionRequest<?> tmp = new TransactionPreCommitRequest(getIdentifier(), nextSequence(),
491                 localActor());
492             enqueueRequest(tmp, resp -> {
493                 recordSuccessfulRequest(tmp);
494                 cb.accept(resp);
495             }, enqueuedTicks);
496         } else if (request instanceof TransactionDoCommitRequest) {
497             ensureFlushedBuider(optTicks);
498             enqueueRequest(new TransactionDoCommitRequest(getIdentifier(), nextSequence(), localActor()), callback,
499                 enqueuedTicks);
500         } else if (request instanceof TransactionAbortRequest) {
501             ensureFlushedBuider(optTicks);
502             enqueueDoAbort(callback, enqueuedTicks);
503         } else if (request instanceof TransactionPurgeRequest) {
504             enqueuePurge(callback, enqueuedTicks);
505         } else if (request instanceof IncrementTransactionSequenceRequest) {
506             final IncrementTransactionSequenceRequest req = (IncrementTransactionSequenceRequest) request;
507             ensureFlushedBuider(optTicks);
508             enqueueRequest(new IncrementTransactionSequenceRequest(getIdentifier(), nextSequence(), localActor(),
509                 snapshotOnly, req.getIncrement()), callback, enqueuedTicks);
510             incrementSequence(req.getIncrement());
511         } else {
512             throw new IllegalArgumentException("Unhandled request {}" + request);
513         }
514     }
515 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.