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.Nullable;
16 import javax.annotation.concurrent.NotThreadSafe;
17 import org.opendaylight.controller.cluster.access.commands.AbortLocalTransactionRequest;
18 import org.opendaylight.controller.cluster.access.commands.AbstractLocalTransactionRequest;
19 import org.opendaylight.controller.cluster.access.commands.CommitLocalTransactionRequest;
20 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionRequest;
21 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionSuccess;
22 import org.opendaylight.controller.cluster.access.commands.IncrementTransactionSequenceRequest;
23 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequest;
24 import org.opendaylight.controller.cluster.access.commands.ReadTransactionRequest;
25 import org.opendaylight.controller.cluster.access.commands.ReadTransactionSuccess;
26 import org.opendaylight.controller.cluster.access.commands.TransactionPurgeRequest;
27 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
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.mdsal.common.api.ReadFailedException;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * An {@link AbstractProxyTransaction} for dispatching a transaction towards a shard leader which is co-located with
42 * the client instance.
45 * It requires a {@link DataTreeSnapshot}, which is used to instantiated a new {@link DataTreeModification}. Operations
46 * are then performed on this modification and once the transaction is submitted, the modification is sent to the shard
50 * This class is not thread-safe as usual with transactions. Since it does not interact with the backend until the
51 * transaction is submitted, at which point this class gets out of the picture, this is not a cause for concern.
53 * @author Robert Varga
56 abstract class LocalProxyTransaction extends AbstractProxyTransaction {
57 private static final Logger LOG = LoggerFactory.getLogger(LocalProxyTransaction.class);
59 private final TransactionIdentifier identifier;
61 LocalProxyTransaction(final ProxyHistory parent, final TransactionIdentifier identifier) {
63 this.identifier = Preconditions.checkNotNull(identifier);
67 public final TransactionIdentifier getIdentifier() {
71 abstract DataTreeSnapshot readOnlyView();
73 abstract void applyForwardedModifyTransactionRequest(ModifyTransactionRequest request,
74 @Nullable Consumer<Response<?, ?>> callback);
76 abstract void replayModifyTransactionRequest(ModifyTransactionRequest request,
77 @Nullable Consumer<Response<?, ?>> callback, long enqueuedTicks);
80 final CheckedFuture<Boolean, ReadFailedException> doExists(final YangInstanceIdentifier path) {
81 return Futures.immediateCheckedFuture(readOnlyView().readNode(path).isPresent());
85 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> doRead(final YangInstanceIdentifier path) {
86 return Futures.immediateCheckedFuture(readOnlyView().readNode(path));
90 final AbortLocalTransactionRequest abortRequest() {
91 return new AbortLocalTransactionRequest(identifier, localActor());
95 void handleReplayedLocalRequest(final AbstractLocalTransactionRequest<?> request,
96 final Consumer<Response<?, ?>> callback, final long enqueuedTicks) {
97 if (request instanceof AbortLocalTransactionRequest) {
98 enqueueAbort(request, callback, enqueuedTicks);
100 throw new IllegalArgumentException("Unhandled request" + request);
104 private boolean handleReadRequest(final TransactionRequest<?> request,
105 final @Nullable Consumer<Response<?, ?>> callback) {
106 // Note we delay completion of read requests to limit the scope at which the client can run, as they have
107 // listeners, which we do not want to execute while we are reconnecting.
108 if (request instanceof ReadTransactionRequest) {
109 final YangInstanceIdentifier path = ((ReadTransactionRequest) request).getPath();
110 final Optional<NormalizedNode<?, ?>> result = readOnlyView().readNode(path);
111 if (callback != null) {
112 // XXX: FB does not see that callback is final, on stack and has be check for non-null.
113 final Consumer<Response<?, ?>> fbIsStupid = Preconditions.checkNotNull(callback);
114 executeInActor(() -> fbIsStupid.accept(new ReadTransactionSuccess(request.getTarget(),
115 request.getSequence(), result)));
118 } else if (request instanceof ExistsTransactionRequest) {
119 final YangInstanceIdentifier path = ((ExistsTransactionRequest) request).getPath();
120 final boolean result = readOnlyView().readNode(path).isPresent();
121 if (callback != null) {
122 // XXX: FB does not see that callback is final, on stack and has be check for non-null.
123 final Consumer<Response<?, ?>> fbIsStupid = Preconditions.checkNotNull(callback);
124 executeInActor(() -> fbIsStupid.accept(new ExistsTransactionSuccess(request.getTarget(),
125 request.getSequence(), result)));
134 void handleReplayedRemoteRequest(final TransactionRequest<?> request,
135 final @Nullable Consumer<Response<?, ?>> callback, final long enqueuedTicks) {
136 if (request instanceof ModifyTransactionRequest) {
137 replayModifyTransactionRequest((ModifyTransactionRequest) request, callback, enqueuedTicks);
138 } else if (handleReadRequest(request, callback)) {
140 } else if (request instanceof TransactionPurgeRequest) {
141 enqueuePurge(callback, enqueuedTicks);
142 } else if (request instanceof IncrementTransactionSequenceRequest) {
143 // Local transactions do not have non-replayable requests which would be visible to the backend,
144 // hence we can skip sequence increments.
145 LOG.debug("Not replaying {}", request);
147 throw new IllegalArgumentException("Unhandled request " + request);
152 * Remote-to-local equivalent of {@link #handleReplayedRemoteRequest(TransactionRequest, Consumer, long)},
153 * except it is invoked in the forwarding path from
154 * {@link RemoteProxyTransaction#forwardToLocal(LocalProxyTransaction, TransactionRequest, Consumer)}.
156 * @param request Forwarded request
157 * @param callback Callback to be invoked once the request completes
159 void handleForwardedRemoteRequest(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
160 if (request instanceof ModifyTransactionRequest) {
161 applyForwardedModifyTransactionRequest((ModifyTransactionRequest) request, callback);
162 } else if (handleReadRequest(request, callback)) {
164 } else if (request instanceof TransactionPurgeRequest) {
165 enqueuePurge(callback);
167 throw new IllegalArgumentException("Unhandled request " + request);
172 final void forwardToRemote(final RemoteProxyTransaction successor, final TransactionRequest<?> request,
173 final Consumer<Response<?, ?>> callback) {
174 if (request instanceof CommitLocalTransactionRequest) {
175 final CommitLocalTransactionRequest req = (CommitLocalTransactionRequest) request;
176 final DataTreeModification mod = req.getModification();
178 LOG.debug("Applying modification {} to successor {}", mod, successor);
179 mod.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));
196 successor.ensureSealed();
198 final ModifyTransactionRequest successorReq = successor.commitRequest(req.isCoordinated());
199 successor.sendRequest(successorReq, callback);
200 } else if (request instanceof AbortLocalTransactionRequest) {
201 LOG.debug("Forwarding abort {} to successor {}", request, successor);
203 } else if (request instanceof TransactionPurgeRequest) {
204 LOG.debug("Forwarding purge {} to successor {}", request, successor);
205 successor.enqueuePurge(callback);
207 throw new IllegalArgumentException("Unhandled request" + request);
212 void forwardToLocal(final LocalProxyTransaction successor, final TransactionRequest<?> request,
213 final Consumer<Response<?, ?>> callback) {
214 if (request instanceof AbortLocalTransactionRequest) {
215 successor.sendAbort(request, callback);
216 } else if (request instanceof TransactionPurgeRequest) {
217 successor.enqueuePurge(callback);
219 throw new IllegalArgumentException("Unhandled request" + request);
222 LOG.debug("Forwarded request {} to successor {}", request, successor);
225 void sendAbort(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
226 sendRequest(request, callback);
229 void enqueueAbort(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback,
230 final long enqueuedTicks) {
231 enqueueRequest(request, callback, enqueuedTicks);