2 * Copyright (c) 2016 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.controller.cluster.databroker.actors.dds;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import java.util.function.Consumer;
13 import java.util.function.Supplier;
14 import javax.annotation.Nullable;
15 import javax.annotation.concurrent.NotThreadSafe;
16 import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
17 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequest;
18 import org.opendaylight.controller.cluster.access.commands.PersistenceProtocol;
19 import org.opendaylight.controller.cluster.access.commands.TransactionAbortRequest;
20 import org.opendaylight.controller.cluster.access.commands.TransactionDelete;
21 import org.opendaylight.controller.cluster.access.commands.TransactionDoCommitRequest;
22 import org.opendaylight.controller.cluster.access.commands.TransactionMerge;
23 import org.opendaylight.controller.cluster.access.commands.TransactionModification;
24 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitRequest;
25 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
26 import org.opendaylight.controller.cluster.access.commands.TransactionWrite;
27 import org.opendaylight.controller.cluster.access.concepts.Response;
28 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
29 import org.opendaylight.controller.cluster.datastore.util.AbstractDataTreeModificationCursor;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeModification;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeSnapshot;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * An {@link AbstractProxyTransaction} for dispatching a transaction towards a shard leader which is co-located with
43 * the client instance.
46 * It requires a {@link DataTreeSnapshot}, which is used to instantiated a new {@link DataTreeModification}. Operations
47 * are then performed on this modification and once the transaction is submitted, the modification is sent to the shard
51 * This class is not thread-safe as usual with transactions. Since it does not interact with the backend until the
52 * transaction is submitted, at which point this class gets out of the picture, this is not a cause for concern.
54 * @author Robert Varga
57 final class LocalReadWriteProxyTransaction extends LocalProxyTransaction {
58 private static final Logger LOG = LoggerFactory.getLogger(LocalReadWriteProxyTransaction.class);
61 * This field needs to be accessed via {@link #getModification()}, which performs state checking to ensure
62 * the modification can actually be accessed.
64 private final CursorAwareDataTreeModification modification;
66 private Supplier<? extends RuntimeException> closedException;
68 private CursorAwareDataTreeModification sealedModification;
71 * Recorded failure from previous operations. Normally we would want to propagate the error directly to the
72 * offending call site, but that exposes inconsistency in behavior during initial connection, when we go through
73 * {@link RemoteProxyTransaction}, which detects this sort of issues at canCommit/directCommit time on the backend.
76 * We therefore do not report incurred exceptions directly, but report them once the user attempts to commit
79 private Exception recordedFailure;
81 LocalReadWriteProxyTransaction(final ProxyHistory parent, final TransactionIdentifier identifier,
82 final DataTreeSnapshot snapshot) {
83 super(parent, identifier);
84 this.modification = (CursorAwareDataTreeModification) snapshot.newModification();
88 boolean isSnapshotOnly() {
93 CursorAwareDataTreeSnapshot readOnlyView() {
94 return getModification();
98 @SuppressWarnings("checkstyle:IllegalCatch")
99 void doDelete(final YangInstanceIdentifier path) {
100 final CursorAwareDataTreeModification mod = getModification();
101 if (recordedFailure != null) {
102 LOG.debug("Transaction {} recorded failure, ignoring delete of {}", getIdentifier(), path);
110 @SuppressWarnings("checkstyle:IllegalCatch")
111 void doMerge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
112 final CursorAwareDataTreeModification mod = getModification();
113 if (recordedFailure != null) {
114 LOG.debug("Transaction {} recorded failure, ignoring merge to {}", getIdentifier(), path);
118 mod.merge(path, data);
122 @SuppressWarnings("checkstyle:IllegalCatch")
123 void doWrite(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
124 final CursorAwareDataTreeModification mod = getModification();
125 if (recordedFailure != null) {
126 LOG.debug("Transaction {} recorded failure, ignoring write to {}", getIdentifier(), path);
130 mod.write(path, data);
133 private RuntimeException abortedException() {
134 return new IllegalStateException("Tracker " + getIdentifier() + " has been aborted");
137 private RuntimeException submittedException() {
138 return new IllegalStateException("Tracker " + getIdentifier() + " has been submitted");
142 CommitLocalTransactionRequest commitRequest(final boolean coordinated) {
143 final CursorAwareDataTreeModification mod = getModification();
144 final CommitLocalTransactionRequest ret = new CommitLocalTransactionRequest(getIdentifier(), nextSequence(),
145 localActor(), mod, coordinated);
146 closedException = this::submittedException;
152 Preconditions.checkState(sealedModification == null, "Transaction %s is already sealed", getIdentifier());
153 final CursorAwareDataTreeModification mod = getModification();
155 sealedModification = mod;
159 void flushState(final AbstractProxyTransaction successor) {
160 sealedModification.applyToCursor(new AbstractDataTreeModificationCursor() {
162 public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
163 successor.write(current().node(child), data);
167 public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
168 successor.merge(current().node(child), data);
172 public void delete(final PathArgument child) {
173 successor.delete(current().node(child));
178 DataTreeSnapshot getSnapshot() {
179 Preconditions.checkState(sealedModification != null, "Proxy %s is not sealed yet", getIdentifier());
180 return sealedModification;
184 void applyModifyTransactionRequest(final ModifyTransactionRequest request,
185 final @Nullable Consumer<Response<?, ?>> callback) {
186 for (final TransactionModification mod : request.getModifications()) {
187 if (mod instanceof TransactionWrite) {
188 getModification().write(mod.getPath(), ((TransactionWrite)mod).getData());
189 } else if (mod instanceof TransactionMerge) {
190 getModification().merge(mod.getPath(), ((TransactionMerge)mod).getData());
191 } else if (mod instanceof TransactionDelete) {
192 getModification().delete(mod.getPath());
194 throw new IllegalArgumentException("Unsupported modification " + mod);
198 final java.util.Optional<PersistenceProtocol> maybeProtocol = request.getPersistenceProtocol();
199 if (maybeProtocol.isPresent()) {
200 Verify.verify(callback != null, "Request {} has null callback", request);
203 switch (maybeProtocol.get()) {
208 // No-op, as we have already issued a seal()
211 sendRequest(commitRequest(false), callback);
214 sendRequest(commitRequest(true), callback);
217 throw new IllegalArgumentException("Unhandled protocol " + maybeProtocol.get());
223 void handleForwardedRemoteRequest(final TransactionRequest<?> request,
224 final @Nullable Consumer<Response<?, ?>> callback) {
225 LOG.debug("Applying forwarded request {}", request);
227 if (request instanceof TransactionPreCommitRequest) {
228 sendRequest(new TransactionPreCommitRequest(getIdentifier(), nextSequence(), localActor()), callback);
229 } else if (request instanceof TransactionDoCommitRequest) {
230 sendRequest(new TransactionDoCommitRequest(getIdentifier(), nextSequence(), localActor()), callback);
231 } else if (request instanceof TransactionAbortRequest) {
234 super.handleForwardedRemoteRequest(request, callback);
239 void forwardToLocal(final LocalProxyTransaction successor, final TransactionRequest<?> request,
240 final Consumer<Response<?, ?>> callback) {
241 if (request instanceof CommitLocalTransactionRequest) {
242 Verify.verify(successor instanceof LocalReadWriteProxyTransaction);
243 ((LocalReadWriteProxyTransaction) successor).sendCommit((CommitLocalTransactionRequest)request, callback);
244 LOG.debug("Forwarded request {} to successor {}", request, successor);
246 super.forwardToLocal(successor, request, callback);
251 void sendAbort(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
252 super.sendAbort(request, callback);
253 closedException = this::abortedException;
256 private CursorAwareDataTreeModification getModification() {
257 if (closedException != null) {
258 throw closedException.get();
264 private void sendCommit(final CommitLocalTransactionRequest request, final Consumer<Response<?, ?>> callback) {
265 // Rebase old modification on new data tree.
266 final CursorAwareDataTreeModification mod = getModification();
268 try (DataTreeModificationCursor cursor = mod.createCursor(YangInstanceIdentifier.EMPTY)) {
269 request.getModification().applyToCursor(cursor);
273 sendRequest(commitRequest(request.isCoordinated()), callback);