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.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import java.util.function.Consumer;
15 import javax.annotation.Nonnull;
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.ExistsTransactionRequest;
22 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionSuccess;
23 import org.opendaylight.controller.cluster.access.commands.IncrementTransactionSequenceRequest;
24 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequest;
25 import org.opendaylight.controller.cluster.access.commands.ReadTransactionRequest;
26 import org.opendaylight.controller.cluster.access.commands.ReadTransactionSuccess;
27 import org.opendaylight.controller.cluster.access.commands.TransactionPurgeRequest;
28 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
29 import org.opendaylight.controller.cluster.access.concepts.Response;
30 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
31 import org.opendaylight.controller.cluster.datastore.util.AbstractDataTreeModificationCursor;
32 import org.opendaylight.mdsal.common.api.ReadFailedException;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
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 abstract class LocalProxyTransaction extends AbstractProxyTransaction {
58 private static final Logger LOG = LoggerFactory.getLogger(LocalProxyTransaction.class);
60 private final TransactionIdentifier identifier;
62 LocalProxyTransaction(final ProxyHistory parent, final TransactionIdentifier identifier, final boolean isDone) {
63 super(parent, isDone);
64 this.identifier = Preconditions.checkNotNull(identifier);
68 public final TransactionIdentifier getIdentifier() {
72 abstract @Nonnull DataTreeSnapshot readOnlyView();
74 abstract void applyForwardedModifyTransactionRequest(ModifyTransactionRequest request,
75 @Nullable Consumer<Response<?, ?>> callback);
77 abstract void replayModifyTransactionRequest(ModifyTransactionRequest request,
78 @Nullable Consumer<Response<?, ?>> callback, long enqueuedTicks);
81 final CheckedFuture<Boolean, ReadFailedException> doExists(final YangInstanceIdentifier path) {
82 return Futures.immediateCheckedFuture(readOnlyView().readNode(path).isPresent());
86 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> doRead(final YangInstanceIdentifier path) {
87 return Futures.immediateCheckedFuture(readOnlyView().readNode(path));
91 final AbortLocalTransactionRequest abortRequest() {
92 return new AbortLocalTransactionRequest(identifier, localActor());
96 void handleReplayedLocalRequest(final AbstractLocalTransactionRequest<?> request,
97 final Consumer<Response<?, ?>> callback, final long enqueuedTicks) {
98 if (request instanceof AbortLocalTransactionRequest) {
99 enqueueAbort(request, callback, enqueuedTicks);
101 throw new IllegalArgumentException("Unhandled request" + request);
105 private boolean handleReadRequest(final TransactionRequest<?> request,
106 final @Nullable Consumer<Response<?, ?>> callback) {
107 // Note we delay completion of read requests to limit the scope at which the client can run, as they have
108 // listeners, which we do not want to execute while we are reconnecting.
109 if (request instanceof ReadTransactionRequest) {
110 final YangInstanceIdentifier path = ((ReadTransactionRequest) request).getPath();
111 final Optional<NormalizedNode<?, ?>> result = readOnlyView().readNode(path);
112 if (callback != null) {
113 // XXX: FB does not see that callback is final, on stack and has be check for non-null.
114 final Consumer<Response<?, ?>> fbIsStupid = Preconditions.checkNotNull(callback);
115 executeInActor(() -> fbIsStupid.accept(new ReadTransactionSuccess(request.getTarget(),
116 request.getSequence(), result)));
119 } else if (request instanceof ExistsTransactionRequest) {
120 final YangInstanceIdentifier path = ((ExistsTransactionRequest) request).getPath();
121 final boolean result = readOnlyView().readNode(path).isPresent();
122 if (callback != null) {
123 // XXX: FB does not see that callback is final, on stack and has be check for non-null.
124 final Consumer<Response<?, ?>> fbIsStupid = Preconditions.checkNotNull(callback);
125 executeInActor(() -> fbIsStupid.accept(new ExistsTransactionSuccess(request.getTarget(),
126 request.getSequence(), result)));
135 void handleReplayedRemoteRequest(final TransactionRequest<?> request,
136 final @Nullable Consumer<Response<?, ?>> callback, final long enqueuedTicks) {
137 if (request instanceof ModifyTransactionRequest) {
138 replayModifyTransactionRequest((ModifyTransactionRequest) request, callback, enqueuedTicks);
139 } else if (handleReadRequest(request, callback)) {
141 } else if (request instanceof TransactionPurgeRequest) {
142 enqueuePurge(callback, enqueuedTicks);
143 } else if (request instanceof IncrementTransactionSequenceRequest) {
144 // Local transactions do not have non-replayable requests which would be visible to the backend,
145 // hence we can skip sequence increments.
146 LOG.debug("Not replaying {}", request);
148 throw new IllegalArgumentException("Unhandled request " + request);
153 * Remote-to-local equivalent of {@link #handleReplayedRemoteRequest(TransactionRequest, Consumer, long)},
154 * except it is invoked in the forwarding path from
155 * {@link RemoteProxyTransaction#forwardToLocal(LocalProxyTransaction, TransactionRequest, Consumer)}.
157 * @param request Forwarded request
158 * @param callback Callback to be invoked once the request completes
160 void handleForwardedRemoteRequest(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
161 if (request instanceof ModifyTransactionRequest) {
162 applyForwardedModifyTransactionRequest((ModifyTransactionRequest) request, callback);
163 } else if (handleReadRequest(request, callback)) {
165 } else if (request instanceof TransactionPurgeRequest) {
166 enqueuePurge(callback);
168 throw new IllegalArgumentException("Unhandled request " + request);
173 final void forwardToRemote(final RemoteProxyTransaction successor, final TransactionRequest<?> request,
174 final Consumer<Response<?, ?>> callback) {
175 if (request instanceof CommitLocalTransactionRequest) {
176 final CommitLocalTransactionRequest req = (CommitLocalTransactionRequest) request;
177 final DataTreeModification mod = req.getModification();
179 LOG.debug("Applying modification {} to successor {}", mod, successor);
180 mod.applyToCursor(new AbstractDataTreeModificationCursor() {
182 public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
183 successor.write(current().node(child), data);
187 public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
188 successor.merge(current().node(child), data);
192 public void delete(final PathArgument child) {
193 successor.delete(current().node(child));
197 successor.ensureSealed();
199 final ModifyTransactionRequest successorReq = successor.commitRequest(req.isCoordinated());
200 successor.sendRequest(successorReq, callback);
201 } else if (request instanceof AbortLocalTransactionRequest) {
202 LOG.debug("Forwarding abort {} to successor {}", request, successor);
204 } else if (request instanceof TransactionPurgeRequest) {
205 LOG.debug("Forwarding purge {} to successor {}", request, successor);
206 successor.enqueuePurge(callback);
208 throw new IllegalArgumentException("Unhandled request" + request);
213 void forwardToLocal(final LocalProxyTransaction successor, final TransactionRequest<?> request,
214 final Consumer<Response<?, ?>> callback) {
215 if (request instanceof AbortLocalTransactionRequest) {
216 successor.sendAbort(request, callback);
217 } else if (request instanceof TransactionPurgeRequest) {
218 successor.enqueuePurge(callback);
220 throw new IllegalArgumentException("Unhandled request" + request);
223 LOG.debug("Forwarded request {} to successor {}", request, successor);
226 void sendAbort(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
227 sendRequest(request, callback);
230 void enqueueAbort(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback,
231 final long enqueuedTicks) {
232 enqueueRequest(request, callback, enqueuedTicks);