BUG-8538: rework transaction abort paths
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / AbstractProxyTransaction.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 akka.actor.ActorRef;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.base.Throwables;
15 import com.google.common.base.Verify;
16 import com.google.common.collect.Iterables;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import com.google.common.util.concurrent.SettableFuture;
20 import java.util.ArrayDeque;
21 import java.util.Deque;
22 import java.util.Iterator;
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
25 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
26 import java.util.function.Consumer;
27 import javax.annotation.Nonnull;
28 import javax.annotation.Nullable;
29 import javax.annotation.concurrent.GuardedBy;
30 import javax.annotation.concurrent.NotThreadSafe;
31 import org.opendaylight.controller.cluster.access.client.ConnectionEntry;
32 import org.opendaylight.controller.cluster.access.commands.AbstractLocalTransactionRequest;
33 import org.opendaylight.controller.cluster.access.commands.IncrementTransactionSequenceRequest;
34 import org.opendaylight.controller.cluster.access.commands.TransactionAbortRequest;
35 import org.opendaylight.controller.cluster.access.commands.TransactionAbortSuccess;
36 import org.opendaylight.controller.cluster.access.commands.TransactionCanCommitSuccess;
37 import org.opendaylight.controller.cluster.access.commands.TransactionCommitSuccess;
38 import org.opendaylight.controller.cluster.access.commands.TransactionDoCommitRequest;
39 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitRequest;
40 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitSuccess;
41 import org.opendaylight.controller.cluster.access.commands.TransactionPurgeRequest;
42 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
43 import org.opendaylight.controller.cluster.access.concepts.Request;
44 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
45 import org.opendaylight.controller.cluster.access.concepts.Response;
46 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
47 import org.opendaylight.mdsal.common.api.ReadFailedException;
48 import org.opendaylight.yangtools.concepts.Identifiable;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 /**
55  * Class translating transaction operations towards a particular backend shard.
56  *
57  * <p>
58  * This class is not safe to access from multiple application threads, as is usual for transactions. Internal state
59  * transitions coming from interactions with backend are expected to be thread-safe.
60  *
61  * <p>
62  * This class interacts with the queueing mechanism in ClientActorBehavior, hence once we arrive at a decision
63  * to use either a local or remote implementation, we are stuck with it. We can re-evaluate on the next transaction.
64  *
65  * @author Robert Varga
66  */
67 abstract class AbstractProxyTransaction implements Identifiable<TransactionIdentifier> {
68     /**
69      * Marker object used instead of read-type of requests, which are satisfied only once. This has a lower footprint
70      * and allows compressing multiple requests into a single entry.
71      */
72     @NotThreadSafe
73     private static final class IncrementSequence {
74         private final long sequence;
75         private long delta = 0;
76
77         IncrementSequence(final long sequence) {
78             this.sequence = sequence;
79         }
80
81         long getDelta() {
82             return delta;
83         }
84
85         long getSequence() {
86             return sequence;
87         }
88
89         void incrementDelta() {
90             delta++;
91         }
92     }
93
94     // Generic state base class. Direct instances are used for fast paths, sub-class is used for successor transitions
95     private static class State {
96         private final String string;
97
98         State(final String string) {
99             this.string = Preconditions.checkNotNull(string);
100         }
101
102         @Override
103         public final String toString() {
104             return string;
105         }
106     }
107
108     // State class used when a successor has interfered. Contains coordinator latch, the successor and previous state
109     private static final class SuccessorState extends State {
110         private final CountDownLatch latch = new CountDownLatch(1);
111         private AbstractProxyTransaction successor;
112         private State prevState;
113
114         SuccessorState() {
115             super("successor");
116         }
117
118         // Synchronize with succession process and return the successor
119         AbstractProxyTransaction await() {
120             try {
121                 latch.await();
122             } catch (InterruptedException e) {
123                 LOG.warn("Interrupted while waiting for latch of {}", successor);
124                 throw Throwables.propagate(e);
125             }
126             return successor;
127         }
128
129         void finish() {
130             latch.countDown();
131         }
132
133         State getPrevState() {
134             return prevState;
135         }
136
137         void setPrevState(final State prevState) {
138             Verify.verify(this.prevState == null);
139             this.prevState = Preconditions.checkNotNull(prevState);
140         }
141
142         // To be called from safe contexts, where successor is known to be completed
143         AbstractProxyTransaction getSuccessor() {
144             return Verify.verifyNotNull(successor);
145         }
146
147         void setSuccessor(final AbstractProxyTransaction successor) {
148             Verify.verify(this.successor == null);
149             this.successor = Preconditions.checkNotNull(successor);
150         }
151     }
152
153     private static final Logger LOG = LoggerFactory.getLogger(AbstractProxyTransaction.class);
154     private static final AtomicIntegerFieldUpdater<AbstractProxyTransaction> SEALED_UPDATER =
155             AtomicIntegerFieldUpdater.newUpdater(AbstractProxyTransaction.class, "sealed");
156     private static final AtomicReferenceFieldUpdater<AbstractProxyTransaction, State> STATE_UPDATER =
157             AtomicReferenceFieldUpdater.newUpdater(AbstractProxyTransaction.class, State.class, "state");
158     private static final State OPEN = new State("open");
159     private static final State SEALED = new State("sealed");
160     private static final State FLUSHED = new State("flushed");
161
162     // Touched from client actor thread only
163     private final Deque<Object> successfulRequests = new ArrayDeque<>();
164     private final ProxyHistory parent;
165
166     // Accessed from user thread only, which may not access this object concurrently
167     private long sequence;
168
169     /*
170      * Atomic state-keeping is required to synchronize the process of propagating completed transaction state towards
171      * the backend -- which may include a successor.
172      *
173      * Successor, unlike {@link AbstractProxyTransaction#seal()} is triggered from the client actor thread, which means
174      * the successor placement needs to be atomic with regard to the application thread.
175      *
176      * In the common case, the application thread performs performs the seal operations and then "immediately" sends
177      * the corresponding message. The uncommon case is when the seal and send operations race with a connect completion
178      * or timeout, when a successor is injected.
179      *
180      * This leaves the problem of needing to completely transferring state just after all queued messages are replayed
181      * after a successor was injected, so that it can be properly sealed if we are racing. Further complication comes
182      * from lock ordering, where the successor injection works with a locked queue and locks proxy objects -- leading
183      * to a potential AB-BA deadlock in case of a naive implementation.
184      *
185      * For tracking user-visible state we use a single volatile int, which is flipped atomically from 0 to 1 exactly
186      * once in {@link AbstractProxyTransaction#seal()}. That keeps common operations fast, as they need to perform
187      * only a single volatile read to assert state correctness.
188      *
189      * For synchronizing client actor (successor-injecting) and user (commit-driving) thread, we keep a separate state
190      * variable. It uses pre-allocated objects for fast paths (i.e. no successor present) and a per-transition object
191      * for slow paths (when successor is injected/present).
192      */
193     private volatile int sealed = 0;
194     private volatile State state = OPEN;
195
196     AbstractProxyTransaction(final ProxyHistory parent) {
197         this.parent = Preconditions.checkNotNull(parent);
198     }
199
200     final void executeInActor(final Runnable command) {
201         parent.context().executeInActor(behavior -> {
202             command.run();
203             return behavior;
204         });
205     }
206
207     final ActorRef localActor() {
208         return parent.localActor();
209     }
210
211     final void incrementSequence(final long delta) {
212         sequence += delta;
213         LOG.debug("Transaction {} incremented sequence to {}", this, sequence);
214     }
215
216     final long nextSequence() {
217         final long ret = sequence++;
218         LOG.debug("Transaction {} allocated sequence {}", this, ret);
219         return ret;
220     }
221
222     final void delete(final YangInstanceIdentifier path) {
223         checkReadWrite();
224         checkNotSealed();
225         doDelete(path);
226     }
227
228     final void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
229         checkReadWrite();
230         checkNotSealed();
231         doMerge(path, data);
232     }
233
234     final void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
235         checkReadWrite();
236         checkNotSealed();
237         doWrite(path, data);
238     }
239
240     final CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
241         checkNotSealed();
242         return doExists(path);
243     }
244
245     final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
246         checkNotSealed();
247         return doRead(path);
248     }
249
250     final void enqueueRequest(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback,
251             final long enqueuedTicks) {
252         LOG.debug("Transaction proxy {} enqueing request {} callback {}", this, request, callback);
253         parent.enqueueRequest(request, callback, enqueuedTicks);
254     }
255
256     final void sendRequest(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
257         LOG.debug("Transaction proxy {} sending request {} callback {}", this, request, callback);
258         parent.sendRequest(request, callback);
259     }
260
261     /**
262      * Seal this transaction before it is either committed or aborted.
263      */
264     final void seal() {
265         // Transition user-visible state first
266         final boolean success = SEALED_UPDATER.compareAndSet(this, 0, 1);
267         Preconditions.checkState(success, "Proxy %s was already sealed", getIdentifier());
268         internalSeal();
269     }
270
271     final void ensureSealed() {
272         if (SEALED_UPDATER.compareAndSet(this, 0, 1)) {
273             internalSeal();
274         }
275     }
276
277     private void internalSeal() {
278         doSeal();
279         parent.onTransactionSealed(this);
280
281         // Now deal with state transfer, which can occur via successor or a follow-up canCommit() or directCommit().
282         if (!STATE_UPDATER.compareAndSet(this, OPEN, SEALED)) {
283             // Slow path: wait for the successor to complete
284             final AbstractProxyTransaction successor = awaitSuccessor();
285
286             // At this point the successor has completed transition and is possibly visible by the user thread, which is
287             // still stuck here. The successor has not seen final part of our state, nor the fact it is sealed.
288             // Propagate state and seal the successor.
289             flushState(successor);
290             successor.ensureSealed();
291         }
292     }
293
294     private void checkNotSealed() {
295         Preconditions.checkState(sealed == 0, "Transaction %s has already been sealed", getIdentifier());
296     }
297
298     private void checkSealed() {
299         Preconditions.checkState(sealed != 0, "Transaction %s has not been sealed yet", getIdentifier());
300     }
301
302     private SuccessorState getSuccessorState() {
303         final State local = state;
304         Verify.verify(local instanceof SuccessorState, "State %s has unexpected class", local);
305         return (SuccessorState) local;
306     }
307
308     private void checkReadWrite() {
309         if (isSnapshotOnly()) {
310             throw new UnsupportedOperationException("Transaction " + getIdentifier() + " is a read-only snapshot");
311         }
312     }
313
314     final void recordSuccessfulRequest(final @Nonnull TransactionRequest<?> req) {
315         successfulRequests.add(Verify.verifyNotNull(req));
316     }
317
318     final void recordFinishedRequest(final Response<?, ?> response) {
319         final Object last = successfulRequests.peekLast();
320         if (last instanceof IncrementSequence) {
321             ((IncrementSequence) last).incrementDelta();
322         } else {
323             successfulRequests.addLast(new IncrementSequence(response.getSequence()));
324         }
325     }
326
327     /**
328      * Abort this transaction. This is invoked only for read-only transactions and will result in an explicit message
329      * being sent to the backend.
330      */
331     final void abort() {
332         checkNotSealed();
333         parent.abortTransaction(this);
334
335         sendRequest(abortRequest(), resp -> {
336             LOG.debug("Transaction {} abort completed with {}", getIdentifier(), resp);
337             sendPurge();
338         });
339     }
340
341     final void abort(final VotingFuture<Void> ret) {
342         checkSealed();
343
344         sendDoAbort(t -> {
345             if (t instanceof TransactionAbortSuccess) {
346                 ret.voteYes();
347             } else if (t instanceof RequestFailure) {
348                 ret.voteNo(((RequestFailure<?, ?>) t).getCause().unwrap());
349             } else {
350                 ret.voteNo(new IllegalStateException("Unhandled response " + t.getClass()));
351             }
352
353             // This is a terminal request, hence we do not need to record it
354             LOG.debug("Transaction {} abort completed", this);
355             sendPurge();
356         });
357     }
358
359     final void enqueueAbort(final Consumer<Response<?, ?>> callback, final long enqueuedTicks) {
360         checkNotSealed();
361         parent.abortTransaction(this);
362
363         enqueueRequest(abortRequest(), resp -> {
364             LOG.debug("Transaction {} abort completed with {}", getIdentifier(), resp);
365             // Purge will be sent by the predecessor's callback
366             if (callback != null) {
367                 callback.accept(resp);
368             }
369         }, enqueuedTicks);
370     }
371
372     final void enqueueDoAbort(final Consumer<Response<?, ?>> callback, final long enqueuedTicks) {
373         enqueueRequest(new TransactionAbortRequest(getIdentifier(), nextSequence(), localActor()), callback,
374             enqueuedTicks);
375     }
376
377     final void sendDoAbort(final Consumer<Response<?, ?>> callback) {
378         sendRequest(new TransactionAbortRequest(getIdentifier(), nextSequence(), localActor()), callback);
379     }
380
381     /**
382      * Commit this transaction, possibly in a coordinated fashion.
383      *
384      * @param coordinated True if this transaction should be coordinated across multiple participants.
385      * @return Future completion
386      */
387     final ListenableFuture<Boolean> directCommit() {
388         checkReadWrite();
389         checkSealed();
390
391         // Precludes startReconnect() from interfering with the fast path
392         synchronized (this) {
393             if (STATE_UPDATER.compareAndSet(this, SEALED, FLUSHED)) {
394                 final SettableFuture<Boolean> ret = SettableFuture.create();
395                 sendRequest(Verify.verifyNotNull(commitRequest(false)), t -> {
396                     if (t instanceof TransactionCommitSuccess) {
397                         ret.set(Boolean.TRUE);
398                     } else if (t instanceof RequestFailure) {
399                         ret.setException(((RequestFailure<?, ?>) t).getCause().unwrap());
400                     } else {
401                         ret.setException(new IllegalStateException("Unhandled response " + t.getClass()));
402                     }
403
404                     // This is a terminal request, hence we do not need to record it
405                     LOG.debug("Transaction {} directCommit completed", this);
406                     sendPurge();
407                 });
408
409                 return ret;
410             }
411         }
412
413         // We have had some interference with successor injection, wait for it to complete and defer to the successor.
414         return awaitSuccessor().directCommit();
415     }
416
417     final void canCommit(final VotingFuture<?> ret) {
418         checkReadWrite();
419         checkSealed();
420
421         // Precludes startReconnect() from interfering with the fast path
422         synchronized (this) {
423             if (STATE_UPDATER.compareAndSet(this, SEALED, FLUSHED)) {
424                 final TransactionRequest<?> req = Verify.verifyNotNull(commitRequest(true));
425
426                 sendRequest(req, t -> {
427                     if (t instanceof TransactionCanCommitSuccess) {
428                         ret.voteYes();
429                     } else if (t instanceof RequestFailure) {
430                         ret.voteNo(((RequestFailure<?, ?>) t).getCause().unwrap());
431                     } else {
432                         ret.voteNo(new IllegalStateException("Unhandled response " + t.getClass()));
433                     }
434
435                     recordSuccessfulRequest(req);
436                     LOG.debug("Transaction {} canCommit completed", this);
437                 });
438
439                 return;
440             }
441         }
442
443         // We have had some interference with successor injection, wait for it to complete and defer to the successor.
444         awaitSuccessor().canCommit(ret);
445     }
446
447     private AbstractProxyTransaction awaitSuccessor() {
448         return getSuccessorState().await();
449     }
450
451     final void preCommit(final VotingFuture<?> ret) {
452         checkReadWrite();
453         checkSealed();
454
455         final TransactionRequest<?> req = new TransactionPreCommitRequest(getIdentifier(), nextSequence(),
456             localActor());
457         sendRequest(req, t -> {
458             if (t instanceof TransactionPreCommitSuccess) {
459                 ret.voteYes();
460             } else if (t instanceof RequestFailure) {
461                 ret.voteNo(((RequestFailure<?, ?>) t).getCause().unwrap());
462             } else {
463                 ret.voteNo(new IllegalStateException("Unhandled response " + t.getClass()));
464             }
465
466             onPreCommitComplete(req);
467         });
468     }
469
470     private void onPreCommitComplete(final TransactionRequest<?> req) {
471         /*
472          * The backend has agreed that the transaction has entered PRE_COMMIT phase, meaning it will be committed
473          * to storage after the timeout completes.
474          *
475          * All state has been replicated to the backend, hence we do not need to keep it around. Retain only
476          * the precommit request, so we know which request to use for resync.
477          */
478         LOG.debug("Transaction {} preCommit completed, clearing successfulRequests", this);
479         successfulRequests.clear();
480
481         // TODO: this works, but can contain some useless state (like batched operations). Create an empty
482         //       equivalent of this request and store that.
483         recordSuccessfulRequest(req);
484     }
485
486     final void doCommit(final VotingFuture<?> ret) {
487         checkReadWrite();
488         checkSealed();
489
490         sendRequest(new TransactionDoCommitRequest(getIdentifier(), nextSequence(), localActor()), t -> {
491             if (t instanceof TransactionCommitSuccess) {
492                 ret.voteYes();
493             } else if (t instanceof RequestFailure) {
494                 ret.voteNo(((RequestFailure<?, ?>) t).getCause().unwrap());
495             } else {
496                 ret.voteNo(new IllegalStateException("Unhandled response " + t.getClass()));
497             }
498
499             LOG.debug("Transaction {} doCommit completed", this);
500             sendPurge();
501         });
502     }
503
504     private void sendPurge() {
505         sendPurge(null);
506     }
507
508     final void sendPurge(final Consumer<Response<?, ?>> callback) {
509         sendRequest(purgeRequest(), resp -> completePurge(resp, callback));
510     }
511
512     final void enqueuePurge(final Consumer<Response<?, ?>> callback, final long enqueuedTicks) {
513         enqueueRequest(purgeRequest(), resp -> completePurge(resp, callback), enqueuedTicks);
514     }
515
516     private TransactionPurgeRequest purgeRequest() {
517         successfulRequests.clear();
518         return new TransactionPurgeRequest(getIdentifier(), nextSequence(), localActor());
519     }
520
521     private void completePurge(final Response<?, ?> resp, final Consumer<Response<?, ?>> callback) {
522         LOG.debug("Transaction {} purge completed", this);
523         parent.completeTransaction(this);
524         if (callback != null) {
525             callback.accept(resp);
526         }
527     }
528
529     // Called with the connection unlocked
530     final synchronized void startReconnect() {
531         // At this point canCommit/directCommit are blocked, we assert a new successor state, retrieving the previous
532         // state. This method is called with the queue still unlocked.
533         final SuccessorState nextState = new SuccessorState();
534         final State prevState = STATE_UPDATER.getAndSet(this, nextState);
535
536         LOG.debug("Start reconnect of proxy {} previous state {}", this, prevState);
537         Verify.verify(!(prevState instanceof SuccessorState), "Proxy %s duplicate reconnect attempt after %s", this,
538             prevState);
539
540         // We have asserted a slow-path state, seal(), canCommit(), directCommit() are forced to slow paths, which will
541         // wait until we unblock nextState's latch before accessing state. Now we record prevState for later use and we
542         // are done.
543         nextState.setPrevState(prevState);
544     }
545
546     // Called with the connection locked
547     final void replayMessages(final AbstractProxyTransaction successor,
548             final Iterable<ConnectionEntry> enqueuedEntries) {
549         final SuccessorState local = getSuccessorState();
550         local.setSuccessor(successor);
551
552         // Replay successful requests first
553         if (!successfulRequests.isEmpty()) {
554             // We need to find a good timestamp to use for successful requests, as we do not want to time them out
555             // nor create timing inconsistencies in the queue -- requests are expected to be ordered by their enqueue
556             // time. We will pick the time of the first entry available. If there is none, we will just use current
557             // time, as all other requests will get enqueued afterwards.
558             final ConnectionEntry firstInQueue = Iterables.getFirst(enqueuedEntries, null);
559             final long now = firstInQueue != null ? firstInQueue.getEnqueuedTicks() : parent.currentTime();
560
561             for (Object obj : successfulRequests) {
562                 if (obj instanceof TransactionRequest) {
563                     LOG.debug("Forwarding successful request {} to successor {}", obj, successor);
564                     successor.replayRequest((TransactionRequest<?>) obj, resp -> { }, now);
565                 } else {
566                     Verify.verify(obj instanceof IncrementSequence);
567                     final IncrementSequence increment = (IncrementSequence) obj;
568                     successor.replayRequest(new IncrementTransactionSequenceRequest(getIdentifier(),
569                         increment.getSequence(), localActor(), isSnapshotOnly(), increment.getDelta()), resp -> { },
570                         now);
571                     LOG.debug("Incrementing sequence {} to successor {}", obj, successor);
572                 }
573             }
574             LOG.debug("{} replayed {} successful requests", getIdentifier(), successfulRequests.size());
575             successfulRequests.clear();
576         }
577
578         // Now replay whatever is in the connection
579         final Iterator<ConnectionEntry> it = enqueuedEntries.iterator();
580         while (it.hasNext()) {
581             final ConnectionEntry e = it.next();
582             final Request<?, ?> req = e.getRequest();
583
584             if (getIdentifier().equals(req.getTarget())) {
585                 Verify.verify(req instanceof TransactionRequest, "Unhandled request %s", req);
586                 LOG.debug("Replaying queued request {} to successor {}", req, successor);
587                 successor.replayRequest((TransactionRequest<?>) req, e.getCallback(), e.getEnqueuedTicks());
588                 it.remove();
589             }
590         }
591
592         /*
593          * Check the state at which we have started the reconnect attempt. State transitions triggered while we were
594          * reconnecting have been forced to slow paths, which will be unlocked once we unblock the state latch
595          * at the end of this method.
596          */
597         final State prevState = local.getPrevState();
598         if (SEALED.equals(prevState)) {
599             LOG.debug("Proxy {} reconnected while being sealed, propagating state to successor {}", this, successor);
600             flushState(successor);
601             successor.ensureSealed();
602         }
603     }
604
605     /**
606      * Invoked from {@link #replayMessages(AbstractProxyTransaction, Iterable)} to have successor adopt an in-flight
607      * request.
608      *
609      * <p>
610      * Note: this method is invoked by the predecessor on the successor.
611      *
612      * @param request Request which needs to be forwarded
613      * @param callback Callback to be invoked once the request completes
614      * @param enqueuedTicks ticker-based time stamp when the request was enqueued
615      */
616     private void replayRequest(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback,
617             final long enqueuedTicks) {
618         if (request instanceof AbstractLocalTransactionRequest) {
619             handleReplayedLocalRequest((AbstractLocalTransactionRequest<?>) request, callback, enqueuedTicks);
620         } else {
621             handleReplayedRemoteRequest(request, callback, enqueuedTicks);
622         }
623     }
624
625     // Called with the connection locked
626     final void finishReconnect() {
627         final SuccessorState local = getSuccessorState();
628         LOG.debug("Finishing reconnect of proxy {}", this);
629
630         // All done, release the latch, unblocking seal() and canCommit() slow paths
631         local.finish();
632     }
633
634     /**
635      * Invoked from a retired connection for requests which have been in-flight and need to be re-adjusted
636      * and forwarded to the successor connection.
637      *
638      * @param request Request to be forwarded
639      * @param callback Original callback
640      */
641     final void forwardRequest(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
642         forwardToSuccessor(getSuccessorState().getSuccessor(), request, callback);
643     }
644
645     final void forwardToSuccessor(final AbstractProxyTransaction successor, final TransactionRequest<?> request,
646             final Consumer<Response<?, ?>> callback) {
647         if (successor instanceof LocalProxyTransaction) {
648             forwardToLocal((LocalProxyTransaction)successor, request, callback);
649         } else if (successor instanceof RemoteProxyTransaction) {
650             forwardToRemote((RemoteProxyTransaction)successor, request, callback);
651         } else {
652             throw new IllegalStateException("Unhandled successor " + successor);
653         }
654     }
655
656     abstract boolean isSnapshotOnly();
657
658     abstract void doDelete(YangInstanceIdentifier path);
659
660     abstract void doMerge(YangInstanceIdentifier path, NormalizedNode<?, ?> data);
661
662     abstract void doWrite(YangInstanceIdentifier path, NormalizedNode<?, ?> data);
663
664     abstract CheckedFuture<Boolean, ReadFailedException> doExists(YangInstanceIdentifier path);
665
666     abstract CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> doRead(YangInstanceIdentifier path);
667
668     abstract void doSeal();
669
670     @GuardedBy("this")
671     abstract void flushState(AbstractProxyTransaction successor);
672
673     abstract TransactionRequest<?> abortRequest();
674
675     abstract TransactionRequest<?> commitRequest(boolean coordinated);
676
677     /**
678      * Replay a request originating in this proxy to a successor remote proxy.
679      */
680     abstract void forwardToRemote(RemoteProxyTransaction successor, TransactionRequest<?> request,
681             Consumer<Response<?, ?>> callback);
682
683     /**
684      * Replay a request originating in this proxy to a successor local proxy.
685      */
686     abstract void forwardToLocal(LocalProxyTransaction successor, TransactionRequest<?> request,
687             Consumer<Response<?, ?>> callback);
688
689     /**
690      * Invoked from {@link LocalProxyTransaction} when it replays its successful requests to its successor.
691      *
692      * <p>
693      * Note: this method is invoked by the predecessor on the successor.
694      *
695      * @param request Request which needs to be forwarded
696      * @param callback Callback to be invoked once the request completes
697      * @param enqueuedTicks Time stamp to use for enqueue time
698      */
699     abstract void handleReplayedLocalRequest(AbstractLocalTransactionRequest<?> request,
700             @Nullable Consumer<Response<?, ?>> callback, long enqueuedTicks);
701
702     /**
703      * Invoked from {@link RemoteProxyTransaction} when it replays its successful requests to its successor.
704      *
705      * <p>
706      * Note: this method is invoked by the predecessor on the successor.
707      *
708      * @param request Request which needs to be forwarded
709      * @param callback Callback to be invoked once the request completes
710      * @param enqueuedTicks Time stamp to use for enqueue time
711      */
712     abstract void handleReplayedRemoteRequest(TransactionRequest<?> request,
713             @Nullable Consumer<Response<?, ?>> callback, long enqueuedTicks);
714
715     @Override
716     public final String toString() {
717         return MoreObjects.toStringHelper(this).add("identifier", getIdentifier()).add("state", state).toString();
718     }
719 }

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