X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatabroker%2Factors%2Fdds%2FLocalReadWriteProxyTransaction.java;h=9eba0bf4e94d17c369b9e8319826f2d6650b6a9a;hp=db24e3c73c867020ab46d210ce52d1b633cf2715;hb=047566574ea74d1dfe24fa8075f8ba137faa698c;hpb=d6ed0a044d591d65847714451d97d80345154089 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/LocalReadWriteProxyTransaction.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/LocalReadWriteProxyTransaction.java index db24e3c73c..9eba0bf4e9 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/LocalReadWriteProxyTransaction.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/LocalReadWriteProxyTransaction.java @@ -10,6 +10,7 @@ package org.opendaylight.controller.cluster.databroker.actors.dds; import com.google.common.base.Preconditions; import com.google.common.base.Verify; import java.util.function.Consumer; +import java.util.function.Supplier; import javax.annotation.Nullable; import javax.annotation.concurrent.NotThreadSafe; import org.opendaylight.controller.cluster.access.commands.AbortLocalTransactionRequest; @@ -57,9 +58,27 @@ import org.slf4j.LoggerFactory; final class LocalReadWriteProxyTransaction extends LocalProxyTransaction { private static final Logger LOG = LoggerFactory.getLogger(LocalReadWriteProxyTransaction.class); - private CursorAwareDataTreeModification modification; + /** + * This field needs to be accessed via {@link #getModification()}, which performs state checking to ensure + * the modification can actually be accessed. + */ + private final CursorAwareDataTreeModification modification; + + private Supplier closedException; + private CursorAwareDataTreeModification sealedModification; + /** + * Recorded failure from previous operations. Normally we would want to propagate the error directly to the + * offending call site, but that exposes inconsistency in behavior during initial connection, when we go through + * {@link RemoteProxyTransaction}, which detects this sort of issues at canCommit/directCommit time on the backend. + * + *

+ * We therefore do not report incurred exceptions directly, but report them once the user attempts to commit + * this transaction. + */ + private Exception recordedFailure; + LocalReadWriteProxyTransaction(final ProxyHistory parent, final TransactionIdentifier identifier, final DataTreeSnapshot snapshot) { super(parent, identifier); @@ -73,22 +92,61 @@ final class LocalReadWriteProxyTransaction extends LocalProxyTransaction { @Override CursorAwareDataTreeSnapshot readOnlyView() { - return modification; + return getModification(); } @Override + @SuppressWarnings("checkstyle:IllegalCatch") void doDelete(final YangInstanceIdentifier path) { - modification.delete(path); + final CursorAwareDataTreeModification mod = getModification(); + if (recordedFailure != null) { + LOG.debug("Transaction {} recorded failure, ignoring delete of {}", getIdentifier(), path); + return; + } + + try { + mod.delete(path); + } catch (Exception e) { + LOG.debug("Transaction {} delete on {} incurred failure, delaying it until commit", getIdentifier(), path, + e); + recordedFailure = e; + } } @Override + @SuppressWarnings("checkstyle:IllegalCatch") void doMerge(final YangInstanceIdentifier path, final NormalizedNode data) { - modification.merge(path, data); + final CursorAwareDataTreeModification mod = getModification(); + if (recordedFailure != null) { + LOG.debug("Transaction {} recorded failure, ignoring merge to {}", getIdentifier(), path); + return; + } + + try { + mod.merge(path, data); + } catch (Exception e) { + LOG.debug("Transaction {} merge to {} incurred failure, delaying it until commit", getIdentifier(), path, + e); + recordedFailure = e; + } } @Override + @SuppressWarnings("checkstyle:IllegalCatch") void doWrite(final YangInstanceIdentifier path, final NormalizedNode data) { - modification.write(path, data); + final CursorAwareDataTreeModification mod = getModification(); + if (recordedFailure != null) { + LOG.debug("Transaction {} recorded failure, ignoring write to {}", getIdentifier(), path); + return; + } + + try { + mod.write(path, data); + } catch (Exception e) { + LOG.debug("Transaction {} write to {} incurred failure, delaying it until commit", getIdentifier(), path, + e); + recordedFailure = e; + } } private RuntimeException abortedException() { @@ -101,16 +159,19 @@ final class LocalReadWriteProxyTransaction extends LocalProxyTransaction { @Override CommitLocalTransactionRequest commitRequest(final boolean coordinated) { + final CursorAwareDataTreeModification mod = getModification(); final CommitLocalTransactionRequest ret = new CommitLocalTransactionRequest(getIdentifier(), nextSequence(), - localActor(), modification, coordinated); - modification = new FailedDataTreeModification(this::submittedException); + localActor(), mod, recordedFailure, coordinated); + closedException = this::submittedException; return ret; } @Override void doSeal() { - modification.ready(); - sealedModification = modification; + Preconditions.checkState(sealedModification == null, "Transaction %s is already sealed", getIdentifier()); + final CursorAwareDataTreeModification mod = getModification(); + mod.ready(); + sealedModification = mod; } @Override @@ -141,13 +202,13 @@ final class LocalReadWriteProxyTransaction extends LocalProxyTransaction { @Override void applyModifyTransactionRequest(final ModifyTransactionRequest request, final @Nullable Consumer> callback) { - for (TransactionModification mod : request.getModifications()) { + for (final TransactionModification mod : request.getModifications()) { if (mod instanceof TransactionWrite) { - modification.write(mod.getPath(), ((TransactionWrite)mod).getData()); + write(mod.getPath(), ((TransactionWrite)mod).getData()); } else if (mod instanceof TransactionMerge) { - modification.merge(mod.getPath(), ((TransactionMerge)mod).getData()); + merge(mod.getPath(), ((TransactionMerge)mod).getData()); } else if (mod instanceof TransactionDelete) { - modification.delete(mod.getPath()); + delete(mod.getPath()); } else { throw new IllegalArgumentException("Unsupported modification " + mod); } @@ -160,7 +221,7 @@ final class LocalReadWriteProxyTransaction extends LocalProxyTransaction { switch (maybeProtocol.get()) { case ABORT: - sendAbort(callback); + sendRequest(new AbortLocalTransactionRequest(getIdentifier(), localActor()), callback); break; case READY: // No-op, as we have already issued a seal() @@ -193,43 +254,6 @@ final class LocalReadWriteProxyTransaction extends LocalProxyTransaction { } } - @Override - void forwardToRemote(final RemoteProxyTransaction successor, final TransactionRequest request, - final Consumer> callback) { - if (request instanceof CommitLocalTransactionRequest) { - final CommitLocalTransactionRequest req = (CommitLocalTransactionRequest) request; - final DataTreeModification mod = req.getModification(); - - LOG.debug("Applying modification {} to successor {}", mod, successor); - mod.applyToCursor(new AbstractDataTreeModificationCursor() { - @Override - public void write(final PathArgument child, final NormalizedNode data) { - successor.write(current().node(child), data); - } - - @Override - public void merge(final PathArgument child, final NormalizedNode data) { - successor.merge(current().node(child), data); - } - - @Override - public void delete(final PathArgument child) { - successor.delete(current().node(child)); - } - }); - - successor.ensureSealed(); - - final ModifyTransactionRequest successorReq = successor.commitRequest(req.isCoordinated()); - successor.sendRequest(successorReq, callback); - } else if (request instanceof AbortLocalTransactionRequest) { - LOG.debug("Forwarding abort {} to successor {}", request, successor); - successor.abort(); - } else { - throw new IllegalArgumentException("Unhandled request" + request); - } - } - @Override void forwardToLocal(final LocalProxyTransaction successor, final TransactionRequest request, final Consumer> callback) { @@ -245,12 +269,22 @@ final class LocalReadWriteProxyTransaction extends LocalProxyTransaction { @Override void sendAbort(final TransactionRequest request, final Consumer> callback) { super.sendAbort(request, callback); - modification = new FailedDataTreeModification(this::abortedException); + closedException = this::abortedException; + } + + private CursorAwareDataTreeModification getModification() { + if (closedException != null) { + throw closedException.get(); + } + + return modification; } private void sendCommit(final CommitLocalTransactionRequest request, final Consumer> callback) { // Rebase old modification on new data tree. - try (DataTreeModificationCursor cursor = modification.createCursor(YangInstanceIdentifier.EMPTY)) { + final CursorAwareDataTreeModification mod = getModification(); + + try (DataTreeModificationCursor cursor = mod.createCursor(YangInstanceIdentifier.EMPTY)) { request.getModification().applyToCursor(cursor); }