829be478f00a6210c88a027547d1fe6a76ec135a
[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) {
82         super(parent);
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     @Override
142     void doAbort() {
143         ensureInitializedBuilder();
144         builder.setAbort();
145         flushBuilder();
146     }
147
148     private void ensureInitializedBuilder() {
149         if (!builderBusy) {
150             builder.setSequence(nextSequence());
151             builderBusy = true;
152         }
153     }
154
155     private void ensureFlushedBuider() {
156         if (builderBusy) {
157             flushBuilder();
158         }
159     }
160
161     private void ensureFlushedBuider(final Optional<Long> enqueuedTicks) {
162         if (builderBusy) {
163             flushBuilder(enqueuedTicks);
164         }
165     }
166
167     private void flushBuilder() {
168         flushBuilder(Optional.absent());
169     }
170
171     private void flushBuilder(final Optional<Long> enqueuedTicks) {
172         final ModifyTransactionRequest request = builder.build();
173         builderBusy = false;
174
175         sendModification(request, enqueuedTicks);
176     }
177
178     private void sendModification(final TransactionRequest<?> request, final Optional<Long> enqueuedTicks) {
179         if (enqueuedTicks.isPresent()) {
180             enqueueRequest(request, response -> completeModify(request, response), enqueuedTicks.get().longValue());
181         } else {
182             sendRequest(request, response -> completeModify(request, response));
183         }
184     }
185
186     private void appendModification(final TransactionModification modification) {
187         appendModification(modification, Optional.absent());
188     }
189
190     private void appendModification(final TransactionModification modification, final Optional<Long> enqueuedTicks) {
191         if (operationFailure == null) {
192             ensureInitializedBuilder();
193
194             builder.addModification(modification);
195             if (builder.size() >= REQUEST_MAX_MODIFICATIONS) {
196                 flushBuilder(enqueuedTicks);
197             }
198         } else {
199             LOG.debug("Transaction {} failed, not attempting further transactions", getIdentifier());
200         }
201     }
202
203     private void completeModify(final TransactionRequest<?> request, final Response<?, ?> response) {
204         LOG.debug("Modification request {} completed with {}", request, response);
205
206         if (response instanceof TransactionSuccess) {
207             // Happy path
208             recordSuccessfulRequest(request);
209         } else {
210             recordFailedResponse(response);
211         }
212     }
213
214     private Exception recordFailedResponse(final Response<?, ?> response) {
215         final Exception failure;
216         if (response instanceof RequestFailure) {
217             failure = ((RequestFailure<?, ?>) response).getCause();
218         } else {
219             LOG.warn("Unhandled response {}", response);
220             failure = new IllegalArgumentException("Unhandled response " + response.getClass());
221         }
222
223         if (operationFailure == null) {
224             LOG.debug("Transaction {} failed", getIdentifier(), failure);
225             operationFailure = failure;
226         }
227         return failure;
228     }
229
230     private void failFuture(final SettableFuture<?> future, final Response<?, ?> response) {
231         future.setException(recordFailedResponse(response));
232     }
233
234     private void completeExists(final SettableFuture<Boolean> future, final Response<?, ?> response) {
235         LOG.debug("Exists request completed with {}", response);
236
237         if (response instanceof ExistsTransactionSuccess) {
238             future.set(((ExistsTransactionSuccess) response).getExists());
239         } else {
240             failFuture(future, response);
241         }
242
243         recordFinishedRequest(response);
244     }
245
246     private void completeRead(final SettableFuture<Optional<NormalizedNode<?, ?>>> future,
247             final Response<?, ?> response) {
248         LOG.debug("Read request completed with {}", response);
249
250         if (response instanceof ReadTransactionSuccess) {
251             future.set(((ReadTransactionSuccess) response).getData());
252         } else {
253             failFuture(future, response);
254         }
255
256         recordFinishedRequest(response);
257     }
258
259     private ModifyTransactionRequest abortRequest() {
260         ensureInitializedBuilder();
261         builder.setAbort();
262         final ModifyTransactionRequest ret = builder.build();
263         builderBusy = false;
264         return ret;
265     }
266
267     @Override
268     ModifyTransactionRequest commitRequest(final boolean coordinated) {
269         ensureInitializedBuilder();
270         builder.setCommit(coordinated);
271
272         final ModifyTransactionRequest ret = builder.build();
273         builderBusy = false;
274         return ret;
275     }
276
277     @Override
278     void doSeal() {
279         if (sendReadyOnSeal) {
280             ensureInitializedBuilder();
281             builder.setReady();
282             flushBuilder();
283         }
284     }
285
286     @Override
287     void flushState(final AbstractProxyTransaction successor) {
288         if (builderBusy) {
289             final ModifyTransactionRequest request = builder.build();
290             builderBusy = false;
291             forwardToSuccessor(successor, request, null);
292         }
293     }
294
295     @Override
296     void forwardToRemote(final RemoteProxyTransaction successor, final TransactionRequest<?> request,
297             final Consumer<Response<?, ?>> callback) {
298         successor.handleForwardedRequest(request, callback);
299     }
300
301     private void handleForwardedRequest(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
302         if (request instanceof ModifyTransactionRequest) {
303             final ModifyTransactionRequest req = (ModifyTransactionRequest) request;
304
305             req.getModifications().forEach(this::appendModification);
306
307             final java.util.Optional<PersistenceProtocol> maybeProto = req.getPersistenceProtocol();
308             if (maybeProto.isPresent()) {
309                 ensureSealed();
310
311                 final TransactionRequest<?> tmp;
312                 switch (maybeProto.get()) {
313                     case ABORT:
314                         tmp = abortRequest();
315                         sendRequest(tmp, resp -> {
316                             completeModify(tmp, resp);
317                             callback.accept(resp);
318                         });
319                         break;
320                     case SIMPLE:
321                         tmp = commitRequest(false);
322                         sendRequest(tmp, resp -> {
323                             completeModify(tmp, resp);
324                             callback.accept(resp);
325                         });
326                         break;
327                     case THREE_PHASE:
328                         tmp = commitRequest(true);
329                         sendRequest(tmp, resp -> {
330                             recordSuccessfulRequest(tmp);
331                             callback.accept(resp);
332                         });
333                         break;
334                     case READY:
335                         //no op
336                         break;
337                     default:
338                         throw new IllegalArgumentException("Unhandled protocol " + maybeProto.get());
339                 }
340             }
341         } else if (request instanceof ReadTransactionRequest) {
342             ensureFlushedBuider();
343             sendRequest(new ReadTransactionRequest(getIdentifier(), nextSequence(), localActor(),
344                 ((ReadTransactionRequest) request).getPath(), isSnapshotOnly()), resp -> {
345                     recordFinishedRequest(resp);
346                     callback.accept(resp);
347                 });
348         } else if (request instanceof ExistsTransactionRequest) {
349             ensureFlushedBuider();
350             sendRequest(new ExistsTransactionRequest(getIdentifier(), nextSequence(), localActor(),
351                 ((ExistsTransactionRequest) request).getPath(), isSnapshotOnly()), resp -> {
352                     recordFinishedRequest(resp);
353                     callback.accept(resp);
354                 });
355         } else if (request instanceof TransactionPreCommitRequest) {
356             ensureFlushedBuider();
357             final TransactionRequest<?> tmp = new TransactionPreCommitRequest(getIdentifier(), nextSequence(),
358                 localActor());
359             sendRequest(tmp, resp -> {
360                 recordSuccessfulRequest(tmp);
361                 callback.accept(resp);
362             });
363         } else if (request instanceof TransactionDoCommitRequest) {
364             ensureFlushedBuider();
365             sendRequest(new TransactionDoCommitRequest(getIdentifier(), nextSequence(), localActor()), callback);
366         } else if (request instanceof TransactionAbortRequest) {
367             ensureFlushedBuider();
368             sendAbort(callback);
369         } else if (request instanceof TransactionPurgeRequest) {
370             sendPurge();
371         } else {
372             throw new IllegalArgumentException("Unhandled request {}" + request);
373         }
374     }
375
376     @Override
377     void forwardToLocal(final LocalProxyTransaction successor, final TransactionRequest<?> request,
378             final Consumer<Response<?, ?>> callback) {
379         successor.handleForwardedRemoteRequest(request, callback);
380     }
381
382     @Override
383     void handleReplayedLocalRequest(final AbstractLocalTransactionRequest<?> request,
384             final Consumer<Response<?, ?>> callback, final long enqueuedTicks) {
385         if (request instanceof CommitLocalTransactionRequest) {
386             replayLocalCommitRequest((CommitLocalTransactionRequest) request, callback, enqueuedTicks);
387         } else if (request instanceof AbortLocalTransactionRequest) {
388             enqueueRequest(abortRequest(), callback, enqueuedTicks);
389         } else {
390             throw new IllegalStateException("Unhandled request " + request);
391         }
392     }
393
394     private void replayLocalCommitRequest(final CommitLocalTransactionRequest request,
395             final Consumer<Response<?, ?>> callback, final long enqueuedTicks) {
396         final DataTreeModification mod = request.getModification();
397         final Optional<Long> optTicks = Optional.of(Long.valueOf(enqueuedTicks));
398
399         mod.applyToCursor(new AbstractDataTreeModificationCursor() {
400             @Override
401             public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
402                 appendModification(new TransactionWrite(current().node(child), data), optTicks);
403             }
404
405             @Override
406             public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
407                 appendModification(new TransactionMerge(current().node(child), data), optTicks);
408             }
409
410             @Override
411             public void delete(final PathArgument child) {
412                 appendModification(new TransactionDelete(current().node(child)), optTicks);
413             }
414         });
415
416         enqueueRequest(commitRequest(request.isCoordinated()), callback, enqueuedTicks);
417     }
418
419     @Override
420     void handleReplayedRemoteRequest(final TransactionRequest<?> request,
421             final @Nullable Consumer<Response<?, ?>> callback, final long enqueuedTicks) {
422         final Consumer<Response<?, ?>> cb = callback != null ? callback : resp -> { };
423         final Optional<Long> optTicks = Optional.of(Long.valueOf(enqueuedTicks));
424
425         if (request instanceof ModifyTransactionRequest) {
426             final ModifyTransactionRequest req = (ModifyTransactionRequest) request;
427             for (TransactionModification mod : req.getModifications()) {
428                 appendModification(mod, optTicks);
429             }
430
431             final java.util.Optional<PersistenceProtocol> maybeProto = req.getPersistenceProtocol();
432             if (maybeProto.isPresent()) {
433                 ensureSealed();
434
435                 final TransactionRequest<?> tmp;
436                 switch (maybeProto.get()) {
437                     case ABORT:
438                         tmp = abortRequest();
439                         enqueueRequest(tmp, resp -> {
440                             completeModify(tmp, resp);
441                             cb.accept(resp);
442                         }, enqueuedTicks);
443                         break;
444                     case SIMPLE:
445                         tmp = commitRequest(false);
446                         enqueueRequest(tmp, resp -> {
447                             completeModify(tmp, resp);
448                             cb.accept(resp);
449                         }, enqueuedTicks);
450                         break;
451                     case THREE_PHASE:
452                         tmp = commitRequest(true);
453                         enqueueRequest(tmp, resp -> {
454                             recordSuccessfulRequest(tmp);
455                             cb.accept(resp);
456                         }, enqueuedTicks);
457                         break;
458                     case READY:
459                         //no op
460                         break;
461                     default:
462                         throw new IllegalArgumentException("Unhandled protocol " + maybeProto.get());
463                 }
464             }
465         } else if (request instanceof ReadTransactionRequest) {
466             ensureFlushedBuider(optTicks);
467             enqueueRequest(new ReadTransactionRequest(getIdentifier(), nextSequence(), localActor(),
468                 ((ReadTransactionRequest) request).getPath(), isSnapshotOnly()), resp -> {
469                     recordFinishedRequest(resp);
470                     cb.accept(resp);
471                 }, enqueuedTicks);
472         } else if (request instanceof ExistsTransactionRequest) {
473             ensureFlushedBuider(optTicks);
474             enqueueRequest(new ExistsTransactionRequest(getIdentifier(), nextSequence(), localActor(),
475                 ((ExistsTransactionRequest) request).getPath(), isSnapshotOnly()), resp -> {
476                     recordFinishedRequest(resp);
477                     cb.accept(resp);
478                 }, enqueuedTicks);
479         } else if (request instanceof TransactionPreCommitRequest) {
480             ensureFlushedBuider(optTicks);
481             final TransactionRequest<?> tmp = new TransactionPreCommitRequest(getIdentifier(), nextSequence(),
482                 localActor());
483             enqueueRequest(tmp, resp -> {
484                 recordSuccessfulRequest(tmp);
485                 cb.accept(resp);
486             }, enqueuedTicks);
487         } else if (request instanceof TransactionDoCommitRequest) {
488             ensureFlushedBuider(optTicks);
489             enqueueRequest(new TransactionDoCommitRequest(getIdentifier(), nextSequence(), localActor()), callback,
490                 enqueuedTicks);
491         } else if (request instanceof TransactionAbortRequest) {
492             ensureFlushedBuider(optTicks);
493             enqueueAbort(callback, enqueuedTicks);
494         } else if (request instanceof TransactionPurgeRequest) {
495             enqueuePurge(enqueuedTicks);
496         } else if (request instanceof IncrementTransactionSequenceRequest) {
497             final IncrementTransactionSequenceRequest req = (IncrementTransactionSequenceRequest) request;
498             ensureFlushedBuider(optTicks);
499             enqueueRequest(new IncrementTransactionSequenceRequest(getIdentifier(), nextSequence(), localActor(),
500                 snapshotOnly, req.getIncrement()), callback, enqueuedTicks);
501             incrementSequence(req.getIncrement());
502         } else {
503             throw new IllegalArgumentException("Unhandled request {}" + request);
504         }
505     }
506 }

©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.