Add UnsignedLongBitmap
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / RemoteTransactionContext.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.controller.cluster.datastore;
10
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
13
14 import akka.actor.ActorSelection;
15 import akka.dispatch.Futures;
16 import akka.dispatch.OnComplete;
17 import com.google.common.annotations.VisibleForTesting;
18 import com.google.common.util.concurrent.SettableFuture;
19 import java.util.Optional;
20 import java.util.SortedSet;
21 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
22 import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
23 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
24 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
25 import org.opendaylight.controller.cluster.datastore.modification.AbstractModification;
26 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
27 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
28 import org.opendaylight.controller.cluster.datastore.modification.Modification;
29 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
30 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
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.schema.NormalizedNode;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import scala.concurrent.Future;
37
38 /**
39  * Redirects front-end transaction operations to a shard for processing. Instances of this class are used
40  * when the destination shard is remote to the caller.
41  *
42  * @author Thomas Pantelis
43  */
44 final class RemoteTransactionContext extends TransactionContext {
45     private static final Logger LOG = LoggerFactory.getLogger(RemoteTransactionContext.class);
46
47     private final ActorUtils actorUtils;
48     private final ActorSelection actor;
49     private final OperationLimiter limiter;
50
51     private BatchedModifications batchedModifications;
52     private int totalBatchedModificationsSent;
53     private int batchPermits;
54
55     /**
56      * We have observed a failed modification batch. This transaction context is effectively doomed, as the backend
57      * does not have a correct view of the world. If this happens, we do not limit operations but rather short-cut them
58      * to a either a no-op (modifications) or a failure (reads). Once the transaction is ready, though, we send the
59      * message to resynchronize with the backend, sharing a 'lost message' failure path.
60      */
61     private volatile Throwable failedModification;
62
63     RemoteTransactionContext(final TransactionIdentifier identifier, final ActorSelection actor,
64             final ActorUtils actorUtils, final short remoteTransactionVersion, final OperationLimiter limiter) {
65         super(identifier, remoteTransactionVersion);
66         this.limiter = requireNonNull(limiter);
67         this.actor = actor;
68         this.actorUtils = actorUtils;
69     }
70
71     private ActorSelection getActor() {
72         return actor;
73     }
74
75     protected ActorUtils getActorUtils() {
76         return actorUtils;
77     }
78
79     @Override
80     void closeTransaction() {
81         LOG.debug("Tx {} closeTransaction called", getIdentifier());
82         TransactionContextCleanup.untrack(this);
83
84         actorUtils.sendOperationAsync(getActor(), new CloseTransaction(getTransactionVersion()).toSerializable());
85     }
86
87     @Override
88     Future<Object> directCommit(final Boolean havePermit) {
89         LOG.debug("Tx {} directCommit called", getIdentifier());
90
91         // Send the remaining batched modifications, if any, with the ready flag set.
92         bumpPermits(havePermit);
93         return sendBatchedModifications(true, true, Optional.empty());
94     }
95
96     @Override
97     Future<ActorSelection> readyTransaction(final Boolean havePermit,
98             final Optional<SortedSet<String>> participatingShardNames) {
99         logModificationCount();
100
101         LOG.debug("Tx {} readyTransaction called", getIdentifier());
102
103         // Send the remaining batched modifications, if any, with the ready flag set.
104
105         bumpPermits(havePermit);
106         Future<Object> lastModificationsFuture = sendBatchedModifications(true, false, participatingShardNames);
107
108         // Transform the last reply Future into a Future that returns the cohort actor path from
109         // the last reply message. That's the end result of the ready operation.
110         return TransactionReadyReplyMapper.transform(lastModificationsFuture, actorUtils, getIdentifier());
111     }
112
113     private void bumpPermits(final Boolean havePermit) {
114         if (Boolean.TRUE.equals(havePermit)) {
115             ++batchPermits;
116         }
117     }
118
119     private BatchedModifications newBatchedModifications() {
120         return new BatchedModifications(getIdentifier(), getTransactionVersion());
121     }
122
123     private void batchModification(final Modification modification, final boolean havePermit) {
124         incrementModificationCount();
125         if (havePermit) {
126             ++batchPermits;
127         }
128
129         if (batchedModifications == null) {
130             batchedModifications = newBatchedModifications();
131         }
132
133         batchedModifications.addModification(modification);
134
135         if (batchedModifications.getModifications().size()
136                 >= actorUtils.getDatastoreContext().getShardBatchedModificationCount()) {
137             sendBatchedModifications();
138         }
139     }
140
141     @VisibleForTesting
142     Future<Object> sendBatchedModifications() {
143         return sendBatchedModifications(false, false, Optional.empty());
144     }
145
146     private Future<Object> sendBatchedModifications(final boolean ready, final boolean doCommitOnReady,
147             final Optional<SortedSet<String>> participatingShardNames) {
148         Future<Object> sent = null;
149         if (ready || batchedModifications != null && !batchedModifications.getModifications().isEmpty()) {
150             if (batchedModifications == null) {
151                 batchedModifications = newBatchedModifications();
152             }
153
154             LOG.debug("Tx {} sending {} batched modifications, ready: {}", getIdentifier(),
155                     batchedModifications.getModifications().size(), ready);
156
157             batchedModifications.setDoCommitOnReady(doCommitOnReady);
158             batchedModifications.setTotalMessagesSent(++totalBatchedModificationsSent);
159
160             final BatchedModifications toSend = batchedModifications;
161             final int permitsToRelease = batchPermits;
162             batchPermits = 0;
163
164             if (ready) {
165                 batchedModifications.setReady(participatingShardNames);
166                 batchedModifications.setDoCommitOnReady(doCommitOnReady);
167                 batchedModifications = null;
168             } else {
169                 batchedModifications = newBatchedModifications();
170
171                 final Throwable failure = failedModification;
172                 if (failure != null) {
173                     // We have observed a modification failure, it does not make sense to send this batch. This speeds
174                     // up the time when the application could be blocked due to messages timing out and operation
175                     // limiter kicking in.
176                     LOG.debug("Tx {} modifications previously failed, not sending a non-ready batch", getIdentifier());
177                     limiter.release(permitsToRelease);
178                     return Futures.failed(failure);
179                 }
180             }
181
182             sent = actorUtils.executeOperationAsync(getActor(), toSend.toSerializable(),
183                 actorUtils.getTransactionCommitOperationTimeout());
184             sent.onComplete(new OnComplete<>() {
185                 @Override
186                 public void onComplete(final Throwable failure, final Object success) {
187                     if (failure != null) {
188                         LOG.debug("Tx {} modifications failed", getIdentifier(), failure);
189                         failedModification = failure;
190                     } else {
191                         LOG.debug("Tx {} modifications completed with {}", getIdentifier(), success);
192                     }
193                     limiter.release(permitsToRelease);
194                 }
195             }, actorUtils.getClientDispatcher());
196         }
197
198         return sent;
199     }
200
201     @Override
202     void executeDelete(final YangInstanceIdentifier path, final Boolean havePermit) {
203         LOG.debug("Tx {} executeDelete called path = {}", getIdentifier(), path);
204         executeModification(new DeleteModification(path), havePermit);
205     }
206
207     @Override
208     void executeMerge(final YangInstanceIdentifier path, final NormalizedNode data, final Boolean havePermit) {
209         LOG.debug("Tx {} executeMerge called path = {}", getIdentifier(), path);
210         executeModification(new MergeModification(path, data), havePermit);
211     }
212
213     @Override
214     void executeWrite(final YangInstanceIdentifier path, final NormalizedNode data, final Boolean havePermit) {
215         LOG.debug("Tx {} executeWrite called path = {}", getIdentifier(), path);
216         executeModification(new WriteModification(path, data), havePermit);
217     }
218
219     private void executeModification(final AbstractModification modification, final Boolean havePermit) {
220         final boolean permitToRelease;
221         if (havePermit == null) {
222             permitToRelease = failedModification == null && acquireOperation();
223         } else {
224             permitToRelease = havePermit;
225         }
226
227         batchModification(modification, permitToRelease);
228     }
229
230     @Override
231     <T> void executeRead(final AbstractRead<T> readCmd, final SettableFuture<T> returnFuture,
232             final Boolean havePermit) {
233         LOG.debug("Tx {} executeRead {} called path = {}", getIdentifier(), readCmd.getClass().getSimpleName(),
234                 readCmd.getPath());
235
236         final Throwable failure = failedModification;
237         if (failure != null) {
238             // If we know there was a previous modification failure, we must not send a read request, as it risks
239             // returning incorrect data. We check this before acquiring an operation simply because we want the app
240             // to complete this transaction as soon as possible.
241             returnFuture.setException(new ReadFailedException("Previous modification failed, cannot "
242                     + readCmd.getClass().getSimpleName() + " for path " + readCmd.getPath(), failure));
243             return;
244         }
245
246         // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the
247         // public API contract.
248
249         final boolean permitToRelease = havePermit == null ? acquireOperation() : havePermit;
250         sendBatchedModifications();
251
252         OnComplete<Object> onComplete = new OnComplete<>() {
253             @Override
254             public void onComplete(final Throwable failure, final Object response) {
255                 // We have previously acquired an operation, now release it, no matter what happened
256                 if (permitToRelease) {
257                     limiter.release();
258                 }
259
260                 if (failure != null) {
261                     LOG.debug("Tx {} {} operation failed", getIdentifier(), readCmd.getClass().getSimpleName(),
262                         failure);
263
264                     returnFuture.setException(new ReadFailedException("Error checking "
265                         + readCmd.getClass().getSimpleName() + " for path " + readCmd.getPath(), failure));
266                 } else {
267                     LOG.debug("Tx {} {} operation succeeded", getIdentifier(), readCmd.getClass().getSimpleName());
268                     readCmd.processResponse(response, returnFuture);
269                 }
270             }
271         };
272
273         final Future<Object> future = actorUtils.executeOperationAsync(getActor(),
274             readCmd.asVersion(getTransactionVersion()).toSerializable(), actorUtils.getOperationTimeout());
275         future.onComplete(onComplete, actorUtils.getClientDispatcher());
276     }
277
278     /**
279      * Acquire operation from the limiter if the hand-off has completed. If the hand-off is still ongoing, this method
280      * does nothing.
281      *
282      * @return True if a permit was successfully acquired, false otherwise
283      */
284     private boolean acquireOperation() {
285         checkState(isOperationHandOffComplete(),
286             "Attempted to acquire execute operation permit for transaction %s on actor %s during handoff",
287             getIdentifier(), actor);
288
289         if (limiter.acquire()) {
290             return true;
291         }
292
293         LOG.warn("Failed to acquire execute operation permit for transaction {} on actor {}", getIdentifier(), actor);
294         return false;
295     }
296
297     @Override
298     boolean usesOperationLimiting() {
299         return true;
300     }
301 }