2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.dom.spi;
10 import static com.google.common.base.Preconditions.checkState;
11 import static com.google.common.base.Verify.verify;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.util.concurrent.FluentFuture;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import com.google.common.util.concurrent.SettableFuture;
19 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20 import java.lang.invoke.MethodHandles;
21 import java.lang.invoke.VarHandle;
23 import java.util.Map.Entry;
24 import java.util.Optional;
25 import java.util.concurrent.CancellationException;
26 import org.checkerframework.checker.lock.qual.GuardedBy;
27 import org.checkerframework.checker.lock.qual.Holding;
28 import org.eclipse.jdt.annotation.NonNull;
29 import org.eclipse.jdt.annotation.Nullable;
30 import org.opendaylight.mdsal.common.api.CommitInfo;
31 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
32 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
33 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
34 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
35 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
36 import org.opendaylight.yangtools.yang.common.Empty;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * The actual implementation of {@link PingPongTransactionChain}. Split out to allow deeper testing while keeping the
44 * externally-visible implementation final.
46 abstract class AbstractPingPongTransactionChain implements DOMTransactionChain {
47 private static final Logger LOG = LoggerFactory.getLogger(AbstractPingPongTransactionChain.class);
49 private final @NonNull SettableFuture<Empty> future = SettableFuture.create();
50 private final @NonNull DOMTransactionChain delegate;
53 private boolean closed;
55 private boolean failed;
57 private PingPongTransaction shutdownTx;
59 private Entry<PingPongTransaction, Throwable> deadTx;
61 // This VarHandle is used to manipulate the "ready" transaction. We perform only atomic get-and-set on it.
62 private static final VarHandle READY_TX;
63 @SuppressWarnings("unused")
64 @SuppressFBWarnings(value = "UUF_UNUSED_FIELD", justification = "https://github.com/spotbugs/spotbugs/issues/2749")
65 private volatile PingPongTransaction readyTx;
68 * This VarHandle is used to manipulate the "locked" transaction. A locked transaction means we know that the user
69 * still holds a transaction and should at some point call us. We perform on compare-and-swap to ensure we properly
70 * detect when a user is attempting to allocated multiple transactions concurrently.
72 private static final VarHandle LOCKED_TX;
73 @SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD",
74 justification = "https://github.com/spotbugs/spotbugs/issues/2749")
75 private volatile PingPongTransaction lockedTx;
78 * This updater is used to manipulate the "inflight" transaction. There can be at most one of these at any given
79 * time. We perform only compare-and-swap on these.
81 private static final VarHandle INFLIGHT_TX;
82 @SuppressFBWarnings(value = "UWF_UNWRITTEN_FIELD",
83 justification = "https://github.com/spotbugs/spotbugs/issues/2749")
84 private volatile PingPongTransaction inflightTx;
87 final var lookup = MethodHandles.lookup();
89 INFLIGHT_TX = lookup.findVarHandle(AbstractPingPongTransactionChain.class, "inflightTx",
90 PingPongTransaction.class);
91 LOCKED_TX = lookup.findVarHandle(AbstractPingPongTransactionChain.class, "lockedTx",
92 PingPongTransaction.class);
93 READY_TX = lookup.findVarHandle(AbstractPingPongTransactionChain.class, "readyTx",
94 PingPongTransaction.class);
95 } catch (NoSuchFieldException | IllegalAccessException e) {
96 throw new ExceptionInInitializerError(e);
100 AbstractPingPongTransactionChain(final DOMTransactionChain delegate) {
101 this.delegate = requireNonNull(delegate);
102 delegate.addCallback(new FutureCallback<>() {
104 public void onSuccess(final Empty result) {
105 delegateSuccessful();
109 public void onFailure(final Throwable cause) {
110 delegateFailed(cause);
116 public final ListenableFuture<Empty> future() {
120 private void delegateSuccessful() {
121 final Entry<PingPongTransaction, Throwable> canceled;
122 synchronized (this) {
123 // This looks weird, but we need not hold the lock while invoking callbacks
127 if (canceled == null) {
128 future.set(Empty.value());
132 // Backend shutdown successful, but we have a batch of transactions we have to report as dead due to the
133 // user calling cancel().
134 final var tx = canceled.getKey();
135 final var cause = canceled.getValue();
136 LOG.debug("Transaction chain {} successful, failing cancelled transaction {}", delegate, tx, cause);
138 future.setException(cause);
142 private void delegateFailed(final Throwable cause) {
143 LOG.debug("Transaction chain {} reported failure", delegate, cause);
145 final var tx = inflightTx;
147 LOG.warn("Transaction chain {} failed with no pending transactions", delegate);
149 future.setException(cause);
151 synchronized (this) {
155 * If we do not have a locked transaction, we need to ensure that the backend transaction is cancelled.
156 * Otherwise we can defer until the user calls us.
158 if (lockedTx == null) {
164 private synchronized @NonNull PingPongTransaction slowAllocateTransaction() {
165 checkState(shutdownTx == null, "Transaction chain %s has been shut down", this);
167 if (deadTx != null) {
168 throw new IllegalStateException(String.format(
169 "Transaction chain %s has failed due to transaction %s being canceled", this, deadTx.getKey()),
173 final DOMDataTreeReadWriteTransaction delegateTx = delegate.newReadWriteTransaction();
174 final PingPongTransaction newTx = new PingPongTransaction(delegateTx);
176 final Object witness = LOCKED_TX.compareAndExchange(this, null, newTx);
177 if (witness != null) {
179 throw new IllegalStateException(
180 String.format("New transaction %s raced with transaction %s", newTx, witness));
186 private @Nullable PingPongTransaction acquireReadyTx() {
187 return (PingPongTransaction) READY_TX.getAndSet(this, null);
190 private @NonNull PingPongTransaction allocateTransaction() {
191 // Step 1: acquire current state
192 final PingPongTransaction oldTx = acquireReadyTx();
194 // Slow path: allocate a delegate transaction
196 return slowAllocateTransaction();
199 // Fast path: reuse current transaction. We will check failures and similar on commit().
200 final Object witness = LOCKED_TX.compareAndExchange(this, null, oldTx);
201 if (witness != null) {
202 // Ouch. Delegate chain has not detected a duplicate transaction allocation. This is the best we can do.
203 oldTx.getTransaction().cancel();
204 throw new IllegalStateException(String.format("Reusable transaction %s raced with transaction %s", oldTx,
212 * This forces allocateTransaction() on a slow path, which has to happen after this method has completed executing.
213 * Also inflightTx may be updated outside the lock, hence we need to re-check.
216 private void processIfReady() {
217 if (inflightTx == null) {
218 final PingPongTransaction tx = acquireReadyTx();
220 processTransaction(tx);
226 * Process a ready transaction. The caller needs to ensure that each transaction is seen only once by this method.
228 * @param tx Transaction which needs processing.
231 private void processTransaction(final @NonNull PingPongTransaction tx) {
233 LOG.debug("Cancelling transaction {}", tx);
234 tx.getTransaction().cancel();
238 LOG.debug("Submitting transaction {}", tx);
239 final Object witness = INFLIGHT_TX.compareAndExchange(this, null, tx);
240 if (witness != null) {
241 LOG.warn("Submitting transaction {} while {} is still running", tx, witness);
244 tx.getTransaction().commit().addCallback(new FutureCallback<CommitInfo>() {
246 public void onSuccess(final CommitInfo result) {
247 transactionSuccessful(tx, result);
251 public void onFailure(final Throwable throwable) {
252 transactionFailed(tx, throwable);
254 }, MoreExecutors.directExecutor());
258 * We got invoked from the data store thread. We need to do two things:
259 * 1) release the in-flight transaction
260 * 2) process the potential next transaction
262 * We have to perform 2) under lock. We could perform 1) without locking, but that means the CAS result may
263 * not be accurate, as a user thread may submit the ready transaction before we acquire the lock -- and checking
264 * for next transaction is not enough, as that may have also be allocated (as a result of a quick
265 * submit/allocate/submit between 1) and 2)). Hence we'd end up doing the following:
266 * 1) CAS of inflightTx
268 * 3) volatile read of inflightTx
270 * Rather than doing that, we keep this method synchronized, hence performing only:
272 * 2) CAS of inflightTx
274 * Since the user thread is barred from submitting the transaction (in processIfReady), we can then proceed with
275 * the knowledge that inflightTx is null -- processTransaction() will still do a CAS, but that is only for
278 private synchronized void processNextTransaction(final PingPongTransaction tx) {
279 final Object witness = INFLIGHT_TX.compareAndExchange(this, tx, null);
280 checkState(witness == tx, "Completed transaction %s while %s was submitted", tx, witness);
282 final PingPongTransaction nextTx = acquireReadyTx();
283 if (nextTx == null) {
284 final PingPongTransaction local = shutdownTx;
286 processTransaction(local);
291 processTransaction(nextTx);
295 private void transactionSuccessful(final PingPongTransaction tx, final CommitInfo result) {
296 LOG.debug("Transaction {} completed successfully", tx);
298 tx.onSuccess(result);
299 processNextTransaction(tx);
302 private void transactionFailed(final PingPongTransaction tx, final Throwable throwable) {
303 LOG.debug("Transaction {} failed", tx, throwable);
305 tx.onFailure(throwable);
306 processNextTransaction(tx);
309 private void readyTransaction(final @NonNull PingPongTransaction tx) {
310 // First mark the transaction as not locked.
311 final Object lockedWitness = LOCKED_TX.compareAndExchange(this, tx, null);
312 checkState(lockedWitness == tx, "Attempted to submit transaction %s while we have %s", tx, lockedWitness);
313 LOG.debug("Transaction {} unlocked", tx);
316 * The transaction is ready. It will then be picked up by either next allocation,
317 * or a background transaction completion callback.
319 final Object readyWitness = READY_TX.compareAndExchange(this, null, tx);
320 checkState(readyWitness == null, "Transaction %s collided on ready state with %s", tx, readyWitness);
321 LOG.debug("Transaction {} readied", tx);
324 * We do not see a transaction being in-flight, so we need to take care of dispatching
325 * the transaction to the backend. We are in the ready case, we cannot short-cut
326 * the checking of readyTx, as an in-flight transaction may have completed between us
327 * setting the field above and us checking.
329 if (inflightTx == null) {
330 synchronized (this) {
337 * Transaction cancellation is a heavyweight operation. We only support cancelation of a locked transaction
338 * and return false for everything else. Cancelling such a transaction will result in all transactions in the
339 * batch to be cancelled.
341 * @param tx Backend shared transaction
342 * @param frontendTx transaction
343 * @return {@code true} if the transaction was cancelled successfully
345 private synchronized boolean cancelTransaction(final PingPongTransaction tx,
346 final DOMDataTreeReadWriteTransaction frontendTx) {
347 // Attempt to unlock the operation.
348 final Object witness = LOCKED_TX.compareAndExchange(this, tx, null);
349 verify(witness == tx, "Cancelling transaction %s collided with locked transaction %s", tx, witness);
351 // Cancel the backend transaction, so we do not end up leaking it.
352 final boolean backendCancelled = tx.getTransaction().cancel();
355 // The transaction has failed, this is probably the user just clearing up the transaction they had. We have
356 // already cancelled the transaction anyway,
360 // We have dealt with cancelling the backend transaction and have unlocked the transaction. Since we are still
361 // inside the synchronized block, any allocations are blocking on the slow path. Now we have to decide the fate
362 // of this transaction chain.
364 // If there are no other frontend transactions in this batch we are aligned with backend state and we can
365 // continue processing.
366 if (frontendTx.equals(tx.getFrontendTransaction())) {
367 if (backendCancelled) {
368 LOG.debug("Cancelled transaction {} was head of the batch, resuming processing", tx);
372 // Backend refused to cancel the transaction. Reinstate it to locked state.
373 final Object reinstateWitness = LOCKED_TX.compareAndExchange(this, null, tx);
374 verify(reinstateWitness == null, "Reinstating transaction %s collided with locked transaction %s", tx,
379 if (!backendCancelled) {
380 LOG.warn("Backend transaction cannot be cancelled during cancellation of {}, attempting to continue", tx);
383 // There are multiple frontend transactions in this batch. We have to report them as failed, which dooms this
384 // transaction chain, too. Since we just came off of a locked transaction, we do not have a ready transaction
385 // at the moment, but there may be some transaction in-flight. So we proceed to shutdown the backend chain
386 // and mark the fact that we should be turning its completion into a failure.
387 deadTx = Map.entry(tx, new CancellationException("Transaction " + frontendTx + " canceled").fillInStackTrace());
393 public final synchronized void close() {
395 LOG.debug("Attempted to close an already-closed chain");
399 // Note: we do not derive from AbstractRegistration due to ordering of this check
400 final var notLocked = lockedTx;
401 if (notLocked != null) {
402 throw new IllegalStateException("Attempted to close chain with outstanding transaction " + notLocked);
406 // This may be a reaction to our failure callback, in that case the backend is already shutdown
407 if (deadTx != null) {
408 LOG.debug("Delegate {} is already closed due to failure {}", delegate, deadTx);
412 // Force allocations on slow path, picking up a potentially-outstanding transaction
413 final var tx = acquireReadyTx();
415 // We have one more transaction, which needs to be processed somewhere. If we do not have
416 // a transaction in-flight, we need to push it down ourselves.
417 // If there is an in-flight transaction we will schedule this last one into a dedicated
418 // slot. Allocation slow path will check its presence and fail, the in-flight path will
419 // pick it up, submit and immediately close the chain.
420 if (inflightTx == null) {
421 processTransaction(tx);
427 // Nothing outstanding, we can safely shutdown
433 public final DOMDataTreeReadTransaction newReadOnlyTransaction() {
434 return new PingPongReadTransaction(allocateTransaction());
438 public final DOMDataTreeReadWriteTransaction newReadWriteTransaction() {
439 final PingPongTransaction tx = allocateTransaction();
440 final DOMDataTreeReadWriteTransaction ret = new PingPongReadWriteTransaction(tx);
441 tx.recordFrontendTransaction(ret);
446 public final DOMDataTreeWriteTransaction newWriteOnlyTransaction() {
447 return newReadWriteTransaction();
450 private final class PingPongReadTransaction implements DOMDataTreeReadTransaction {
451 private final @NonNull PingPongTransaction tx;
453 PingPongReadTransaction(final PingPongTransaction tx) {
454 this.tx = requireNonNull(tx);
458 public FluentFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
459 final YangInstanceIdentifier path) {
460 return tx.getTransaction().read(store, path);
464 public FluentFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
465 return tx.getTransaction().exists(store, path);
469 public Object getIdentifier() {
470 return tx.getTransaction().getIdentifier();
474 public void close() {
475 readyTransaction(tx);
479 private final class PingPongReadWriteTransaction extends ForwardingDOMDataReadWriteTransaction {
480 private final @NonNull PingPongTransaction tx;
482 private boolean isOpen = true;
484 PingPongReadWriteTransaction(final PingPongTransaction tx) {
485 this.tx = requireNonNull(tx);
489 public FluentFuture<? extends CommitInfo> commit() {
490 readyTransaction(tx);
492 return tx.completionFuture();
496 public boolean cancel() {
497 if (isOpen && cancelTransaction(tx, this)) {
505 protected DOMDataTreeReadWriteTransaction delegate() {
506 return tx.getTransaction();