d14b7bda666790f7268875c4dead378d77e70b31
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / databroker / actors / dds / RemoteProxyTransaction.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.databroker.actors.dds;
9
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 com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.util.function.Consumer;
17 import javax.annotation.Nullable;
18 import org.opendaylight.controller.cluster.access.commands.AbstractReadTransactionRequest;
19 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionRequest;
20 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionSuccess;
21 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequest;
22 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequestBuilder;
23 import org.opendaylight.controller.cluster.access.commands.PersistenceProtocol;
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.TransactionAbortRequest;
27 import org.opendaylight.controller.cluster.access.commands.TransactionDelete;
28 import org.opendaylight.controller.cluster.access.commands.TransactionDoCommitRequest;
29 import org.opendaylight.controller.cluster.access.commands.TransactionMerge;
30 import org.opendaylight.controller.cluster.access.commands.TransactionModification;
31 import org.opendaylight.controller.cluster.access.commands.TransactionPreCommitRequest;
32 import org.opendaylight.controller.cluster.access.commands.TransactionPurgeRequest;
33 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
34 import org.opendaylight.controller.cluster.access.commands.TransactionSuccess;
35 import org.opendaylight.controller.cluster.access.commands.TransactionWrite;
36 import org.opendaylight.controller.cluster.access.concepts.RequestFailure;
37 import org.opendaylight.controller.cluster.access.concepts.Response;
38 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
39 import org.opendaylight.mdsal.common.api.ReadFailedException;
40 import org.opendaylight.yangtools.util.concurrent.MappingCheckedFuture;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * An {@link AbstractProxyTransaction} for dispatching a transaction towards a shard leader whose location is currently
48  * not known or is known to be not co-located with the client.
49  *
50  * <p>
51  * It packages operations and sends them via the client actor queue to the shard leader. That queue is responsible for
52  * maintaining any submitted operations until the leader is discovered.
53  *
54  * <p>
55  * This class is not safe to access from multiple application threads, as is usual for transactions. Its internal state
56  * transitions based on backend responses are thread-safe.
57  *
58  * @author Robert Varga
59  */
60 final class RemoteProxyTransaction extends AbstractProxyTransaction {
61     private static final Logger LOG = LoggerFactory.getLogger(RemoteProxyTransaction.class);
62
63     // FIXME: make this tuneable
64     private static final int REQUEST_MAX_MODIFICATIONS = 1000;
65
66     private final ModifyTransactionRequestBuilder builder;
67     private final boolean sendReadyOnSeal;
68     private final boolean snapshotOnly;
69
70     private boolean builderBusy;
71
72     private volatile Exception operationFailure;
73
74     RemoteProxyTransaction(final ProxyHistory parent, final TransactionIdentifier identifier,
75             final boolean snapshotOnly, final boolean sendReadyOnSeal) {
76         super(parent);
77         this.snapshotOnly = snapshotOnly;
78         this.sendReadyOnSeal = sendReadyOnSeal;
79         builder = new ModifyTransactionRequestBuilder(identifier, localActor());
80     }
81
82     @Override
83     boolean isSnapshotOnly() {
84         return snapshotOnly;
85     }
86
87     @Override
88     public TransactionIdentifier getIdentifier() {
89         return builder.getIdentifier();
90     }
91
92     @Override
93     void doDelete(final YangInstanceIdentifier path) {
94         appendModification(new TransactionDelete(path));
95     }
96
97     @Override
98     void doMerge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
99         appendModification(new TransactionMerge(path, data));
100     }
101
102     @Override
103     void doWrite(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
104         appendModification(new TransactionWrite(path, data));
105     }
106
107     private <T> CheckedFuture<T, ReadFailedException> sendReadRequest(final AbstractReadTransactionRequest<?> request,
108             final Consumer<Response<?, ?>> completer, final ListenableFuture<T> future) {
109         // Check if a previous operation failed. If it has, do not bother sending anything and report a failure
110         final Exception local = operationFailure;
111         if (local != null) {
112             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Previous operation failed", local));
113         }
114
115         // Make sure we send any modifications before issuing a read
116         ensureFlushedBuider();
117         sendRequest(request, completer);
118         return MappingCheckedFuture.create(future, ReadFailedException.MAPPER);
119     }
120
121     @Override
122     CheckedFuture<Boolean, ReadFailedException> doExists(final YangInstanceIdentifier path) {
123         final SettableFuture<Boolean> future = SettableFuture.create();
124         return sendReadRequest(new ExistsTransactionRequest(getIdentifier(), nextSequence(), localActor(), path,
125             isSnapshotOnly()), t -> completeExists(future, t), future);
126     }
127
128     @Override
129     CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> doRead(final YangInstanceIdentifier path) {
130         final SettableFuture<Optional<NormalizedNode<?, ?>>> future = SettableFuture.create();
131         return sendReadRequest(new ReadTransactionRequest(getIdentifier(), nextSequence(), localActor(), path,
132             isSnapshotOnly()), t -> completeRead(future, t), future);
133     }
134
135     @Override
136     void doAbort() {
137         ensureInitializedBuilder();
138         builder.setAbort();
139         flushBuilder();
140     }
141
142     private void ensureInitializedBuilder() {
143         if (!builderBusy) {
144             builder.setSequence(nextSequence());
145             builderBusy = true;
146         }
147     }
148
149     private void ensureFlushedBuider() {
150         if (builderBusy) {
151             flushBuilder();
152         }
153     }
154
155     private void flushBuilder() {
156         final ModifyTransactionRequest request = builder.build();
157         builderBusy = false;
158
159         sendModification(request);
160     }
161
162     private void sendModification(final TransactionRequest<?> request) {
163         sendRequest(request, response -> completeModify(request, response));
164     }
165
166     @Override
167     void handleForwardedRemoteRequest(final TransactionRequest<?> request,
168             final @Nullable Consumer<Response<?, ?>> callback) {
169         nextSequence();
170
171         if (callback == null) {
172             sendModification(request);
173             return;
174         }
175
176         /*
177          * FindBugs is utterly stupid, as it does not recognize the fact that we have checked for null
178          * and reports NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE in the lambda below.
179          */
180         final Consumer<Response<?, ?>> findBugsIsStupid = callback;
181
182         // FIXME: do not use sendRequest() once we have throttling in place, as we have already waited the
183         //        period required to get into the queue.
184         sendRequest(request, response -> {
185             findBugsIsStupid.accept(Preconditions.checkNotNull(response));
186             completeModify(request, response);
187         });
188     }
189
190     private void appendModification(final TransactionModification modification) {
191         if (operationFailure == null) {
192             ensureInitializedBuilder();
193
194             builder.addModification(modification);
195             if (builder.size() >= REQUEST_MAX_MODIFICATIONS) {
196                 flushBuilder();
197             }
198         } else {
199             LOG.debug("Transaction {} failed, not attempting further transactions", getIdentifier());
200         }
201     }
202
203     private void completeModify(final TransactionRequest<?> request, final Response<?, ?> response) {
204         LOG.debug("Modification request {} completed with {}", request, response);
205
206         if (response instanceof TransactionSuccess) {
207             // Happy path
208             recordSuccessfulRequest(request);
209         } else {
210             recordFailedResponse(response);
211         }
212     }
213
214     private Exception recordFailedResponse(final Response<?, ?> response) {
215         final Exception failure;
216         if (response instanceof RequestFailure) {
217             failure = ((RequestFailure<?, ?>) response).getCause();
218         } else {
219             LOG.warn("Unhandled response {}", response);
220             failure = new IllegalArgumentException("Unhandled response " + response.getClass());
221         }
222
223         if (operationFailure == null) {
224             LOG.debug("Transaction {} failed", getIdentifier(), failure);
225             operationFailure = failure;
226         }
227         return failure;
228     }
229
230     private void failFuture(final SettableFuture<?> future, final Response<?, ?> response) {
231         future.setException(recordFailedResponse(response));
232     }
233
234     private void completeExists(final SettableFuture<Boolean> future, final Response<?, ?> response) {
235         LOG.debug("Exists request completed with {}", response);
236
237         if (response instanceof ExistsTransactionSuccess) {
238             future.set(((ExistsTransactionSuccess) response).getExists());
239         } else {
240             failFuture(future, response);
241         }
242
243         recordFinishedRequest();
244     }
245
246     private void completeRead(final SettableFuture<Optional<NormalizedNode<?, ?>>> future,
247             final Response<?, ?> response) {
248         LOG.debug("Read request completed with {}", response);
249
250         if (response instanceof ReadTransactionSuccess) {
251             future.set(((ReadTransactionSuccess) response).getData());
252         } else {
253             failFuture(future, response);
254         }
255
256         recordFinishedRequest();
257     }
258
259     @Override
260     ModifyTransactionRequest commitRequest(final boolean coordinated) {
261         ensureInitializedBuilder();
262         builder.setCommit(coordinated);
263
264         final ModifyTransactionRequest ret = builder.build();
265         builderBusy = false;
266         return ret;
267     }
268
269     @Override
270     void doSeal() {
271         if (sendReadyOnSeal) {
272             ensureInitializedBuilder();
273             builder.setReady();
274             flushBuilder();
275         }
276     }
277
278     @Override
279     void flushState(final AbstractProxyTransaction successor) {
280         if (builderBusy) {
281             final ModifyTransactionRequest request = builder.build();
282             builderBusy = false;
283             successor.handleForwardedRemoteRequest(request, null);
284         }
285     }
286
287     @Override
288     void forwardToRemote(final RemoteProxyTransaction successor, final TransactionRequest<?> request,
289             final Consumer<Response<?, ?>> callback) {
290         successor.handleForwardedRequest(request, callback);
291     }
292
293     private void handleForwardedRequest(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
294         if (request instanceof ModifyTransactionRequest) {
295             final ModifyTransactionRequest req = (ModifyTransactionRequest) request;
296
297             req.getModifications().forEach(this::appendModification);
298
299             final java.util.Optional<PersistenceProtocol> maybeProto = req.getPersistenceProtocol();
300             if (maybeProto.isPresent()) {
301                 ensureSealed();
302
303                 switch (maybeProto.get()) {
304                     case ABORT:
305                         ensureInitializedBuilder();
306                         builder.setAbort();
307                         final ModifyTransactionRequest newReq = builder.build();
308                         builderBusy = false;
309                         sendRequest(newReq, callback);
310                         break;
311                     case SIMPLE:
312                         sendRequest(commitRequest(false), callback);
313                         break;
314                     case THREE_PHASE:
315                         sendRequest(commitRequest(true), callback);
316                         break;
317                     case READY:
318                         //no op
319                         break;
320                     default:
321                         throw new IllegalArgumentException("Unhandled protocol " + maybeProto.get());
322                 }
323             }
324         } else if (request instanceof ReadTransactionRequest) {
325             ensureFlushedBuider();
326             sendRequest(new ReadTransactionRequest(getIdentifier(), nextSequence(), localActor(),
327                 ((ReadTransactionRequest) request).getPath(), isSnapshotOnly()), callback);
328         } else if (request instanceof ExistsTransactionRequest) {
329             ensureFlushedBuider();
330             sendRequest(new ExistsTransactionRequest(getIdentifier(), nextSequence(), localActor(),
331                 ((ExistsTransactionRequest) request).getPath(), isSnapshotOnly()), callback);
332         } else if (request instanceof TransactionPreCommitRequest) {
333             ensureFlushedBuider();
334             sendRequest(new TransactionPreCommitRequest(getIdentifier(), nextSequence(), localActor()), callback);
335         } else if (request instanceof TransactionDoCommitRequest) {
336             ensureFlushedBuider();
337             sendRequest(new TransactionDoCommitRequest(getIdentifier(), nextSequence(), localActor()), callback);
338         } else if (request instanceof TransactionAbortRequest) {
339             ensureFlushedBuider();
340             sendAbort(callback);
341         } else if (request instanceof TransactionPurgeRequest) {
342             purge();
343         } else {
344             throw new IllegalArgumentException("Unhandled request {}" + request);
345         }
346     }
347
348     @Override
349     void forwardToLocal(final LocalProxyTransaction successor, final TransactionRequest<?> request,
350             final Consumer<Response<?, ?>> callback) {
351         successor.handleForwardedRemoteRequest(request, callback);
352     }
353 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.