Bug 8342: Add info logging to ConfigManagerActivator
[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
75     RemoteProxyTransaction(final ProxyHistory parent, final TransactionIdentifier identifier,
76             final boolean snapshotOnly, final boolean sendReadyOnSeal) {
77         super(parent);
78         this.snapshotOnly = snapshotOnly;
79         this.sendReadyOnSeal = sendReadyOnSeal;
80         builder = new ModifyTransactionRequestBuilder(identifier, localActor());
81     }
82
83     @Override
84     boolean isSnapshotOnly() {
85         return snapshotOnly;
86     }
87
88     @Override
89     public TransactionIdentifier getIdentifier() {
90         return builder.getIdentifier();
91     }
92
93     @Override
94     void doDelete(final YangInstanceIdentifier path) {
95         appendModification(new TransactionDelete(path));
96     }
97
98     @Override
99     void doMerge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
100         appendModification(new TransactionMerge(path, data));
101     }
102
103     @Override
104     void doWrite(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
105         appendModification(new TransactionWrite(path, data));
106     }
107
108     private <T> CheckedFuture<T, ReadFailedException> sendReadRequest(final AbstractReadTransactionRequest<?> request,
109             final Consumer<Response<?, ?>> completer, final ListenableFuture<T> future) {
110         // Check if a previous operation failed. If it has, do not bother sending anything and report a failure
111         final Exception local = operationFailure;
112         if (local != null) {
113             return Futures.immediateFailedCheckedFuture(new ReadFailedException("Previous operation failed", local));
114         }
115
116         // Make sure we send any modifications before issuing a read
117         ensureFlushedBuider();
118         sendRequest(request, completer);
119         return MappingCheckedFuture.create(future, ReadFailedException.MAPPER);
120     }
121
122     @Override
123     CheckedFuture<Boolean, ReadFailedException> doExists(final YangInstanceIdentifier path) {
124         final SettableFuture<Boolean> future = SettableFuture.create();
125         return sendReadRequest(new ExistsTransactionRequest(getIdentifier(), nextSequence(), localActor(), path,
126             isSnapshotOnly()), t -> completeExists(future, t), future);
127     }
128
129     @Override
130     CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> doRead(final YangInstanceIdentifier path) {
131         final SettableFuture<Optional<NormalizedNode<?, ?>>> future = SettableFuture.create();
132         return sendReadRequest(new ReadTransactionRequest(getIdentifier(), nextSequence(), localActor(), path,
133             isSnapshotOnly()), t -> completeRead(future, t), future);
134     }
135
136     @Override
137     void doAbort() {
138         ensureInitializedBuilder();
139         builder.setAbort();
140         flushBuilder();
141     }
142
143     private void ensureInitializedBuilder() {
144         if (!builderBusy) {
145             builder.setSequence(nextSequence());
146             builderBusy = true;
147         }
148     }
149
150     private void ensureFlushedBuider() {
151         if (builderBusy) {
152             flushBuilder();
153         }
154     }
155
156     private void flushBuilder() {
157         final ModifyTransactionRequest request = builder.build();
158         builderBusy = false;
159
160         sendModification(request);
161     }
162
163     private void sendModification(final TransactionRequest<?> request) {
164         sendRequest(request, response -> completeModify(request, response));
165     }
166
167     @Override
168     void handleForwardedRemoteRequest(final TransactionRequest<?> request,
169             final @Nullable Consumer<Response<?, ?>> callback) {
170         nextSequence();
171
172         if (callback == null) {
173             sendModification(request);
174             return;
175         }
176
177         /*
178          * FindBugs is utterly stupid, as it does not recognize the fact that we have checked for null
179          * and reports NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE in the lambda below.
180          */
181         final Consumer<Response<?, ?>> findBugsIsStupid = callback;
182
183         // FIXME: do not use sendRequest() once we have throttling in place, as we have already waited the
184         //        period required to get into the queue.
185         sendRequest(request, response -> {
186             findBugsIsStupid.accept(Preconditions.checkNotNull(response));
187             completeModify(request, response);
188         });
189     }
190
191     private void appendModification(final TransactionModification modification) {
192         if (operationFailure == null) {
193             ensureInitializedBuilder();
194
195             builder.addModification(modification);
196             if (builder.size() >= REQUEST_MAX_MODIFICATIONS) {
197                 flushBuilder();
198             }
199         } else {
200             LOG.debug("Transaction {} failed, not attempting further transactions", getIdentifier());
201         }
202     }
203
204     private void completeModify(final TransactionRequest<?> request, final Response<?, ?> response) {
205         LOG.debug("Modification request {} completed with {}", request, response);
206
207         if (response instanceof TransactionSuccess) {
208             // Happy path
209             recordSuccessfulRequest(request);
210         } else {
211             recordFailedResponse(response);
212         }
213     }
214
215     private Exception recordFailedResponse(final Response<?, ?> response) {
216         final Exception failure;
217         if (response instanceof RequestFailure) {
218             failure = ((RequestFailure<?, ?>) response).getCause();
219         } else {
220             LOG.warn("Unhandled response {}", response);
221             failure = new IllegalArgumentException("Unhandled response " + response.getClass());
222         }
223
224         if (operationFailure == null) {
225             LOG.debug("Transaction {} failed", getIdentifier(), failure);
226             operationFailure = failure;
227         }
228         return failure;
229     }
230
231     private void failFuture(final SettableFuture<?> future, final Response<?, ?> response) {
232         future.setException(recordFailedResponse(response));
233     }
234
235     private void completeExists(final SettableFuture<Boolean> future, final Response<?, ?> response) {
236         LOG.debug("Exists request completed with {}", response);
237
238         if (response instanceof ExistsTransactionSuccess) {
239             future.set(((ExistsTransactionSuccess) response).getExists());
240         } else {
241             failFuture(future, response);
242         }
243
244         recordFinishedRequest();
245     }
246
247     private void completeRead(final SettableFuture<Optional<NormalizedNode<?, ?>>> future,
248             final Response<?, ?> response) {
249         LOG.debug("Read request completed with {}", response);
250
251         if (response instanceof ReadTransactionSuccess) {
252             future.set(((ReadTransactionSuccess) response).getData());
253         } else {
254             failFuture(future, response);
255         }
256
257         recordFinishedRequest();
258     }
259
260     @Override
261     ModifyTransactionRequest commitRequest(final boolean coordinated) {
262         ensureInitializedBuilder();
263         builder.setCommit(coordinated);
264
265         final ModifyTransactionRequest ret = builder.build();
266         builderBusy = false;
267         return ret;
268     }
269
270     @Override
271     void doSeal() {
272         if (sendReadyOnSeal) {
273             ensureInitializedBuilder();
274             builder.setReady();
275             flushBuilder();
276         }
277     }
278
279     @Override
280     void flushState(final AbstractProxyTransaction successor) {
281         if (builderBusy) {
282             final ModifyTransactionRequest request = builder.build();
283             builderBusy = false;
284             successor.handleForwardedRemoteRequest(request, null);
285         }
286     }
287
288     @Override
289     void forwardToRemote(final RemoteProxyTransaction successor, final TransactionRequest<?> request,
290             final Consumer<Response<?, ?>> callback) {
291         successor.handleForwardedRequest(request, callback);
292     }
293
294     private void handleForwardedRequest(final TransactionRequest<?> request, final Consumer<Response<?, ?>> callback) {
295         if (request instanceof ModifyTransactionRequest) {
296             final ModifyTransactionRequest req = (ModifyTransactionRequest) request;
297
298             req.getModifications().forEach(this::appendModification);
299
300             final java.util.Optional<PersistenceProtocol> maybeProto = req.getPersistenceProtocol();
301             if (maybeProto.isPresent()) {
302                 ensureSealed();
303
304                 switch (maybeProto.get()) {
305                     case ABORT:
306                         sendAbort(callback);
307                         break;
308                     case SIMPLE:
309                         sendRequest(commitRequest(false), callback);
310                         break;
311                     case THREE_PHASE:
312                         sendRequest(commitRequest(true), callback);
313                         break;
314                     case READY:
315                         //no op
316                         break;
317                     default:
318                         throw new IllegalArgumentException("Unhandled protocol " + maybeProto.get());
319                 }
320             }
321         } else if (request instanceof ReadTransactionRequest) {
322             ensureFlushedBuider();
323             sendRequest(new ReadTransactionRequest(getIdentifier(), nextSequence(), localActor(),
324                 ((ReadTransactionRequest) request).getPath(), isSnapshotOnly()), callback);
325         } else if (request instanceof ExistsTransactionRequest) {
326             ensureFlushedBuider();
327             sendRequest(new ExistsTransactionRequest(getIdentifier(), nextSequence(), localActor(),
328                 ((ExistsTransactionRequest) request).getPath(), isSnapshotOnly()), callback);
329         } else if (request instanceof TransactionPreCommitRequest) {
330             ensureFlushedBuider();
331             sendRequest(new TransactionPreCommitRequest(getIdentifier(), nextSequence(), localActor()), callback);
332         } else if (request instanceof TransactionDoCommitRequest) {
333             ensureFlushedBuider();
334             sendRequest(new TransactionDoCommitRequest(getIdentifier(), nextSequence(), localActor()), callback);
335         } else if (request instanceof TransactionAbortRequest) {
336             ensureFlushedBuider();
337             sendAbort(callback);
338         } else if (request instanceof TransactionPurgeRequest) {
339             purge();
340         } else {
341             throw new IllegalArgumentException("Unhandled request {}" + request);
342         }
343     }
344
345     @Override
346     void forwardToLocal(final LocalProxyTransaction successor, final TransactionRequest<?> request,
347             final Consumer<Response<?, ?>> callback) {
348         successor.handleForwardedRemoteRequest(request, callback);
349     }
350 }

©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.