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.AbortLocalTransactionRequest;
17 import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
18 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequest;
19 import org.opendaylight.controller.cluster.access.commands.PersistenceProtocol;
20 import org.opendaylight.controller.cluster.access.commands.TransactionAbortRequest;
21 import org.opendaylight.controller.cluster.access.commands.TransactionDelete;
22 import org.opendaylight.controller.cluster.access.commands.TransactionDoCommitRequest;
23 import org.opendaylight.controller.cluster.access.commands.TransactionMerge;
24 import org.opendaylight.controller.cluster.access.commands.TransactionModification;
25 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitRequest;
26 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
27 import org.opendaylight.controller.cluster.access.commands.TransactionWrite;
28 import org.opendaylight.controller.cluster.access.concepts.Response;
29 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
30 import org.opendaylight.controller.cluster.datastore.util.AbstractDataTreeModificationCursor;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeModification;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeSnapshot;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
38 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * An {@link AbstractProxyTransaction} for dispatching a transaction towards a shard leader which is co-located with
44 * the client instance.
47 * It requires a {@link DataTreeSnapshot}, which is used to instantiated a new {@link DataTreeModification}. Operations
48 * are then performed on this modification and once the transaction is submitted, the modification is sent to the shard
52 * This class is not thread-safe as usual with transactions. Since it does not interact with the backend until the
53 * transaction is submitted, at which point this class gets out of the picture, this is not a cause for concern.
55 * @author Robert Varga
58 final class LocalReadWriteProxyTransaction extends LocalProxyTransaction {
59 private static final Logger LOG = LoggerFactory.getLogger(LocalReadWriteProxyTransaction.class);
62 * This field needs to be accessed via {@link #getModification()}, which performs state checking to ensure
63 * the modification can actually be accessed.
65 private final CursorAwareDataTreeModification modification;
67 private Supplier<? extends RuntimeException> closedException;
69 private CursorAwareDataTreeModification sealedModification;
72 * Recorded failure from previous operations. Normally we would want to propagate the error directly to the
73 * offending call site, but that exposes inconsistency in behavior during initial connection, when we go through
74 * {@link RemoteProxyTransaction}, which detects this sort of issues at canCommit/directCommit time on the backend.
77 * We therefore do not report incurred exceptions directly, but report them once the user attempts to commit
80 private Exception recordedFailure;
82 LocalReadWriteProxyTransaction(final ProxyHistory parent, final TransactionIdentifier identifier,
83 final DataTreeSnapshot snapshot) {
84 super(parent, identifier);
85 this.modification = (CursorAwareDataTreeModification) snapshot.newModification();
89 boolean isSnapshotOnly() {
94 CursorAwareDataTreeSnapshot readOnlyView() {
95 return getModification();
99 @SuppressWarnings("checkstyle:IllegalCatch")
100 void doDelete(final YangInstanceIdentifier path) {
101 final CursorAwareDataTreeModification mod = getModification();
102 if (recordedFailure != null) {
103 LOG.debug("Transaction {} recorded failure, ignoring delete of {}", getIdentifier(), path);
109 } catch (Exception e) {
110 LOG.debug("Transaction {} delete on {} incurred failure, delaying it until commit", getIdentifier(), path,
117 @SuppressWarnings("checkstyle:IllegalCatch")
118 void doMerge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
119 final CursorAwareDataTreeModification mod = getModification();
120 if (recordedFailure != null) {
121 LOG.debug("Transaction {} recorded failure, ignoring merge to {}", getIdentifier(), path);
126 mod.merge(path, data);
127 } catch (Exception e) {
128 LOG.debug("Transaction {} merge to {} incurred failure, delaying it until commit", getIdentifier(), path,
135 @SuppressWarnings("checkstyle:IllegalCatch")
136 void doWrite(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
137 final CursorAwareDataTreeModification mod = getModification();
138 if (recordedFailure != null) {
139 LOG.debug("Transaction {} recorded failure, ignoring write to {}", getIdentifier(), path);
144 mod.write(path, data);
145 } catch (Exception e) {
146 LOG.debug("Transaction {} write to {} incurred failure, delaying it until commit", getIdentifier(), path,
152 private RuntimeException abortedException() {
153 return new IllegalStateException("Tracker " + getIdentifier() + " has been aborted");
156 private RuntimeException submittedException() {
157 return new IllegalStateException("Tracker " + getIdentifier() + " has been submitted");
161 CommitLocalTransactionRequest commitRequest(final boolean coordinated) {
162 final CursorAwareDataTreeModification mod = getModification();
163 final CommitLocalTransactionRequest ret = new CommitLocalTransactionRequest(getIdentifier(), nextSequence(),
164 localActor(), mod, recordedFailure, coordinated);
165 closedException = this::submittedException;
171 Preconditions.checkState(sealedModification == null, "Transaction %s is already sealed", getIdentifier());
172 final CursorAwareDataTreeModification mod = getModification();
174 sealedModification = mod;
178 void flushState(final AbstractProxyTransaction successor) {
179 sealedModification.applyToCursor(new AbstractDataTreeModificationCursor() {
181 public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
182 successor.write(current().node(child), data);
186 public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
187 successor.merge(current().node(child), data);
191 public void delete(final PathArgument child) {
192 successor.delete(current().node(child));
197 DataTreeSnapshot getSnapshot() {
198 Preconditions.checkState(sealedModification != null, "Proxy %s is not sealed yet", getIdentifier());
199 return sealedModification;
203 void applyModifyTransactionRequest(final ModifyTransactionRequest request,
204 final @Nullable Consumer<Response<?, ?>> callback) {
205 for (final TransactionModification mod : request.getModifications()) {
206 if (mod instanceof TransactionWrite) {
207 write(mod.getPath(), ((TransactionWrite)mod).getData());
208 } else if (mod instanceof TransactionMerge) {
209 merge(mod.getPath(), ((TransactionMerge)mod).getData());
210 } else if (mod instanceof TransactionDelete) {
211 delete(mod.getPath());
213 throw new IllegalArgumentException("Unsupported modification " + mod);
217 final java.util.Optional<PersistenceProtocol> maybeProtocol = request.getPersistenceProtocol();
218 if (maybeProtocol.isPresent()) {
219 Verify.verify(callback != null, "Request {} has null callback", request);
222 switch (maybeProtocol.get()) {
224 sendRequest(new AbortLocalTransactionRequest(getIdentifier(), localActor()), callback);
227 // No-op, as we have already issued a seal()
230 sendRequest(commitRequest(false), callback);
233 sendRequest(commitRequest(true), callback);
236 throw new IllegalArgumentException("Unhandled protocol " + maybeProtocol.get());
242 void handleForwardedRemoteRequest(final TransactionRequest<?> request,
243 final @Nullable Consumer<Response<?, ?>> callback) {
244 LOG.debug("Applying forwarded request {}", request);
246 if (request instanceof TransactionPreCommitRequest) {
247 sendRequest(new TransactionPreCommitRequest(getIdentifier(), nextSequence(), localActor()), callback);
248 } else if (request instanceof TransactionDoCommitRequest) {
249 sendRequest(new TransactionDoCommitRequest(getIdentifier(), nextSequence(), localActor()), callback);
250 } else if (request instanceof TransactionAbortRequest) {
253 super.handleForwardedRemoteRequest(request, callback);
258 void forwardToLocal(final LocalProxyTransaction successor, final TransactionRequest<?> request,
259 final Consumer<Response<?, ?>> callback) {
260 if (request instanceof CommitLocalTransactionRequest) {
261 Verify.verify(successor instanceof LocalReadWriteProxyTransaction);
262 ((LocalReadWriteProxyTransaction) successor).sendCommit((CommitLocalTransactionRequest)request, callback);
263 LOG.debug("Forwarded request {} to successor {}", request, successor);
265 super.forwardToLocal(successor, request, callback);
270 void sendAbort(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
271 super.sendAbort(request, callback);
272 closedException = this::abortedException;
275 private CursorAwareDataTreeModification getModification() {
276 if (closedException != null) {
277 throw closedException.get();
283 private void sendCommit(final CommitLocalTransactionRequest request, final Consumer<Response<?, ?>> callback) {
284 // Rebase old modification on new data tree.
285 final CursorAwareDataTreeModification mod = getModification();
287 try (DataTreeModificationCursor cursor = mod.createCursor(YangInstanceIdentifier.EMPTY)) {
288 request.getModification().applyToCursor(cursor);
292 sendRequest(commitRequest(request.isCoordinated()), callback);