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.Optional;
13 import java.util.function.BiConsumer;
14 import java.util.function.Consumer;
15 import java.util.function.Supplier;
16 import javax.annotation.Nullable;
17 import javax.annotation.concurrent.NotThreadSafe;
18 import org.opendaylight.controller.cluster.access.commands.AbortLocalTransactionRequest;
19 import org.opendaylight.controller.cluster.access.commands.AbstractLocalTransactionRequest;
20 import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
21 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequest;
22 import org.opendaylight.controller.cluster.access.commands.PersistenceProtocol;
23 import org.opendaylight.controller.cluster.access.commands.TransactionAbortRequest;
24 import org.opendaylight.controller.cluster.access.commands.TransactionDelete;
25 import org.opendaylight.controller.cluster.access.commands.TransactionDoCommitRequest;
26 import org.opendaylight.controller.cluster.access.commands.TransactionMerge;
27 import org.opendaylight.controller.cluster.access.commands.TransactionModification;
28 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitRequest;
29 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
30 import org.opendaylight.controller.cluster.access.commands.TransactionWrite;
31 import org.opendaylight.controller.cluster.access.concepts.Response;
32 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
33 import org.opendaylight.controller.cluster.datastore.util.AbstractDataTreeModificationCursor;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeModification;
38 import org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeSnapshot;
39 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
40 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
41 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * An {@link AbstractProxyTransaction} for dispatching a transaction towards a shard leader which is co-located with
47 * the client instance.
50 * It requires a {@link DataTreeSnapshot}, which is used to instantiated a new {@link DataTreeModification}. Operations
51 * are then performed on this modification and once the transaction is submitted, the modification is sent to the shard
55 * This class is not thread-safe as usual with transactions. Since it does not interact with the backend until the
56 * transaction is submitted, at which point this class gets out of the picture, this is not a cause for concern.
58 * @author Robert Varga
61 final class LocalReadWriteProxyTransaction extends LocalProxyTransaction {
62 private static final Logger LOG = LoggerFactory.getLogger(LocalReadWriteProxyTransaction.class);
65 * This field needs to be accessed via {@link #getModification()}, which performs state checking to ensure
66 * the modification can actually be accessed.
68 private final CursorAwareDataTreeModification modification;
70 private Supplier<? extends RuntimeException> closedException;
72 private CursorAwareDataTreeModification sealedModification;
75 * Recorded failure from previous operations. Normally we would want to propagate the error directly to the
76 * offending call site, but that exposes inconsistency in behavior during initial connection, when we go through
77 * {@link RemoteProxyTransaction}, which detects this sort of issues at canCommit/directCommit time on the backend.
80 * We therefore do not report incurred exceptions directly, but report them once the user attempts to commit
83 private Exception recordedFailure;
85 LocalReadWriteProxyTransaction(final ProxyHistory parent, final TransactionIdentifier identifier,
86 final DataTreeSnapshot snapshot) {
87 super(parent, identifier);
88 this.modification = (CursorAwareDataTreeModification) snapshot.newModification();
92 boolean isSnapshotOnly() {
97 CursorAwareDataTreeSnapshot readOnlyView() {
98 return getModification();
102 @SuppressWarnings("checkstyle:IllegalCatch")
103 void doDelete(final YangInstanceIdentifier path) {
104 final CursorAwareDataTreeModification mod = getModification();
105 if (recordedFailure != null) {
106 LOG.debug("Transaction {} recorded failure, ignoring delete of {}", getIdentifier(), path);
112 } catch (Exception e) {
113 LOG.debug("Transaction {} delete on {} incurred failure, delaying it until commit", getIdentifier(), path,
120 @SuppressWarnings("checkstyle:IllegalCatch")
121 void doMerge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
122 final CursorAwareDataTreeModification mod = getModification();
123 if (recordedFailure != null) {
124 LOG.debug("Transaction {} recorded failure, ignoring merge to {}", getIdentifier(), path);
129 mod.merge(path, data);
130 } catch (Exception e) {
131 LOG.debug("Transaction {} merge to {} incurred failure, delaying it until commit", getIdentifier(), path,
138 @SuppressWarnings("checkstyle:IllegalCatch")
139 void doWrite(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
140 final CursorAwareDataTreeModification mod = getModification();
141 if (recordedFailure != null) {
142 LOG.debug("Transaction {} recorded failure, ignoring write to {}", getIdentifier(), path);
147 mod.write(path, data);
148 } catch (Exception e) {
149 LOG.debug("Transaction {} write to {} incurred failure, delaying it until commit", getIdentifier(), path,
155 private RuntimeException abortedException() {
156 return new IllegalStateException("Tracker " + getIdentifier() + " has been aborted");
159 private RuntimeException submittedException() {
160 return new IllegalStateException("Tracker " + getIdentifier() + " has been submitted");
164 CommitLocalTransactionRequest commitRequest(final boolean coordinated) {
165 final CursorAwareDataTreeModification mod = getModification();
166 final CommitLocalTransactionRequest ret = new CommitLocalTransactionRequest(getIdentifier(), nextSequence(),
167 localActor(), mod, recordedFailure, coordinated);
168 closedException = this::submittedException;
174 Preconditions.checkState(sealedModification == null, "Transaction %s is already sealed", getIdentifier());
175 final CursorAwareDataTreeModification mod = getModification();
177 sealedModification = mod;
181 void flushState(final AbstractProxyTransaction successor) {
182 sealedModification.applyToCursor(new AbstractDataTreeModificationCursor() {
184 public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
185 successor.write(current().node(child), data);
189 public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
190 successor.merge(current().node(child), data);
194 public void delete(final PathArgument child) {
195 successor.delete(current().node(child));
200 DataTreeSnapshot getSnapshot() {
201 Preconditions.checkState(sealedModification != null, "Proxy %s is not sealed yet", getIdentifier());
202 return sealedModification;
206 void applyForwardedModifyTransactionRequest(final ModifyTransactionRequest request,
207 final @Nullable Consumer<Response<?, ?>> callback) {
208 commonModifyTransactionRequest(request, callback, this::sendRequest);
212 void replayModifyTransactionRequest(final ModifyTransactionRequest request,
213 final @Nullable Consumer<Response<?, ?>> callback, final long enqueuedTicks) {
214 commonModifyTransactionRequest(request, callback, (req, cb) -> enqueueRequest(req, cb, enqueuedTicks));
217 private void commonModifyTransactionRequest(final ModifyTransactionRequest request,
218 final @Nullable Consumer<Response<?, ?>> callback,
219 final BiConsumer<TransactionRequest<?>, Consumer<Response<?, ?>>> sendMethod) {
220 for (final TransactionModification mod : request.getModifications()) {
221 if (mod instanceof TransactionWrite) {
222 write(mod.getPath(), ((TransactionWrite)mod).getData());
223 } else if (mod instanceof TransactionMerge) {
224 merge(mod.getPath(), ((TransactionMerge)mod).getData());
225 } else if (mod instanceof TransactionDelete) {
226 delete(mod.getPath());
228 throw new IllegalArgumentException("Unsupported modification " + mod);
232 final Optional<PersistenceProtocol> maybeProtocol = request.getPersistenceProtocol();
233 if (maybeProtocol.isPresent()) {
234 Verify.verify(callback != null, "Request {} has null callback", request);
237 switch (maybeProtocol.get()) {
239 sendMethod.accept(new AbortLocalTransactionRequest(getIdentifier(), localActor()), callback);
242 // No-op, as we have already issued a seal()
245 sendMethod.accept(commitRequest(false), callback);
248 sendMethod.accept(commitRequest(true), callback);
251 throw new IllegalArgumentException("Unhandled protocol " + maybeProtocol.get());
257 void handleReplayedLocalRequest(final AbstractLocalTransactionRequest<?> request,
258 final Consumer<Response<?, ?>> callback, final long now) {
259 if (request instanceof CommitLocalTransactionRequest) {
260 sendCommit((CommitLocalTransactionRequest) request, callback);
262 super.handleReplayedLocalRequest(request, callback, now);
267 void handleReplayedRemoteRequest(final TransactionRequest<?> request,
268 final @Nullable Consumer<Response<?, ?>> callback, final long enqueuedTicks) {
269 LOG.debug("Applying replayed request {}", request);
271 if (request instanceof TransactionPreCommitRequest) {
272 enqueueRequest(new TransactionPreCommitRequest(getIdentifier(), nextSequence(), localActor()), callback,
274 } else if (request instanceof TransactionDoCommitRequest) {
275 enqueueRequest(new TransactionDoCommitRequest(getIdentifier(), nextSequence(), localActor()), callback,
277 } else if (request instanceof TransactionAbortRequest) {
278 enqueueDoAbort(callback, enqueuedTicks);
280 super.handleReplayedRemoteRequest(request, callback, enqueuedTicks);
285 void handleForwardedRemoteRequest(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
286 LOG.debug("Applying forwarded request {}", request);
288 if (request instanceof TransactionPreCommitRequest) {
289 sendRequest(new TransactionPreCommitRequest(getIdentifier(), nextSequence(), localActor()), callback);
290 } else if (request instanceof TransactionDoCommitRequest) {
291 sendRequest(new TransactionDoCommitRequest(getIdentifier(), nextSequence(), localActor()), callback);
292 } else if (request instanceof TransactionAbortRequest) {
293 sendDoAbort(callback);
295 super.handleForwardedRemoteRequest(request, callback);
300 void forwardToLocal(final LocalProxyTransaction successor, final TransactionRequest<?> request,
301 final Consumer<Response<?, ?>> callback) {
302 if (request instanceof CommitLocalTransactionRequest) {
303 Verify.verify(successor instanceof LocalReadWriteProxyTransaction);
304 ((LocalReadWriteProxyTransaction) successor).sendCommit((CommitLocalTransactionRequest)request, callback);
305 LOG.debug("Forwarded request {} to successor {}", request, successor);
307 super.forwardToLocal(successor, request, callback);
312 void sendAbort(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
313 super.sendAbort(request, callback);
314 closedException = this::abortedException;
318 void enqueueAbort(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback,
319 final long enqueuedTicks) {
320 super.enqueueAbort(request, callback, enqueuedTicks);
321 closedException = this::abortedException;
324 private CursorAwareDataTreeModification getModification() {
325 if (closedException != null) {
326 throw closedException.get();
332 private void sendCommit(final CommitLocalTransactionRequest request, final Consumer<Response<?, ?>> callback) {
333 // Rebase old modification on new data tree.
334 final CursorAwareDataTreeModification mod = getModification();
336 try (DataTreeModificationCursor cursor = mod.createCursor(YangInstanceIdentifier.EMPTY)) {
337 request.getModification().applyToCursor(cursor);
341 sendRequest(commitRequest(request.isCoordinated()), callback);