BUG 3019 : Fix Operation throttling for modification batching scenarios
[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 akka.actor.ActorSelection;
12 import akka.dispatch.OnComplete;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.util.concurrent.SettableFuture;
16 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
17 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
18 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
19 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
20 import org.opendaylight.controller.cluster.datastore.messages.DataExistsReply;
21 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
22 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
23 import org.opendaylight.controller.cluster.datastore.messages.SerializableMessage;
24 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
25 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
26 import org.opendaylight.controller.cluster.datastore.modification.Modification;
27 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
28 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
29 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import scala.concurrent.Future;
35
36 /**
37  * Redirects front-end transaction operations to a shard for processing. Instances of this class are used
38  * when the destination shard is remote to the caller.
39  *
40  * @author Thomas Pantelis
41  */
42 public class RemoteTransactionContext extends AbstractTransactionContext {
43     private static final Logger LOG = LoggerFactory.getLogger(RemoteTransactionContext.class);
44
45     private final ActorContext actorContext;
46     private final ActorSelection actor;
47     private final boolean isTxActorLocal;
48     private final short remoteTransactionVersion;
49     private final OperationLimiter limiter;
50
51     private BatchedModifications batchedModifications;
52     private int totalBatchedModificationsSent;
53
54     protected RemoteTransactionContext(TransactionIdentifier identifier, ActorSelection actor,
55             ActorContext actorContext, boolean isTxActorLocal,
56             short remoteTransactionVersion, OperationLimiter limiter) {
57         super(identifier);
58         this.limiter = Preconditions.checkNotNull(limiter);
59         this.actor = actor;
60         this.actorContext = actorContext;
61         this.isTxActorLocal = isTxActorLocal;
62         this.remoteTransactionVersion = remoteTransactionVersion;
63     }
64
65     private Future<Object> completeOperation(Future<Object> operationFuture){
66         operationFuture.onComplete(limiter, actorContext.getClientDispatcher());
67         return operationFuture;
68     }
69
70     private ActorSelection getActor() {
71         return actor;
72     }
73
74     protected ActorContext getActorContext() {
75         return actorContext;
76     }
77
78     protected short getRemoteTransactionVersion() {
79         return remoteTransactionVersion;
80     }
81
82     protected Future<Object> executeOperationAsync(SerializableMessage msg) {
83         return completeOperation(actorContext.executeOperationAsync(getActor(), isTxActorLocal ? msg : msg.toSerializable()));
84     }
85
86     @Override
87     public void closeTransaction() {
88         LOG.debug("Tx {} closeTransaction called", getIdentifier());
89         TransactionContextCleanup.untrack(this);
90
91         actorContext.sendOperationAsync(getActor(), CloseTransaction.INSTANCE.toSerializable());
92     }
93
94     @Override
95     public boolean supportsDirectCommit() {
96         return true;
97     }
98
99     @Override
100     public Future<Object> directCommit() {
101         LOG.debug("Tx {} directCommit called", getIdentifier());
102
103         // Send the remaining batched modifications, if any, with the ready flag set.
104
105         return sendBatchedModifications(true, true);
106     }
107
108     @Override
109     public Future<ActorSelection> readyTransaction() {
110         logModificationCount();
111
112         LOG.debug("Tx {} readyTransaction called", getIdentifier());
113
114         // Send the remaining batched modifications, if any, with the ready flag set.
115
116         Future<Object> lastModificationsFuture = sendBatchedModifications(true, false);
117
118         return transformReadyReply(lastModificationsFuture);
119     }
120
121     protected Future<ActorSelection> transformReadyReply(final Future<Object> readyReplyFuture) {
122         // Transform the last reply Future into a Future that returns the cohort actor path from
123         // the last reply message. That's the end result of the ready operation.
124
125         return TransactionReadyReplyMapper.transform(readyReplyFuture, actorContext, getIdentifier());
126     }
127
128     private BatchedModifications newBatchedModifications() {
129         return new BatchedModifications(getIdentifier().toString(), remoteTransactionVersion, getIdentifier().getChainId());
130     }
131
132     private void batchModification(Modification modification) {
133         incrementModificationCount();
134         if(batchedModifications == null) {
135             batchedModifications = newBatchedModifications();
136         }
137
138         batchedModifications.addModification(modification);
139
140         if(batchedModifications.getModifications().size() >=
141                 actorContext.getDatastoreContext().getShardBatchedModificationCount()) {
142             sendBatchedModifications();
143         }
144     }
145
146     protected Future<Object> sendBatchedModifications() {
147         return sendBatchedModifications(false, false);
148     }
149
150     protected Future<Object> sendBatchedModifications(boolean ready, boolean doCommitOnReady) {
151         Future<Object> sent = null;
152         if(ready || (batchedModifications != null && !batchedModifications.getModifications().isEmpty())) {
153             if(batchedModifications == null) {
154                 batchedModifications = newBatchedModifications();
155             }
156
157             if(LOG.isDebugEnabled()) {
158                 LOG.debug("Tx {} sending {} batched modifications, ready: {}", getIdentifier(),
159                         batchedModifications.getModifications().size(), ready);
160             }
161
162             batchedModifications.setReady(ready);
163             batchedModifications.setDoCommitOnReady(doCommitOnReady);
164             batchedModifications.setTotalMessagesSent(++totalBatchedModificationsSent);
165             sent = executeOperationAsync(batchedModifications);
166
167             if(ready) {
168                 batchedModifications = null;
169             } else {
170                 batchedModifications = newBatchedModifications();
171             }
172         }
173
174         return sent;
175     }
176
177     @Override
178     public void deleteData(YangInstanceIdentifier path) {
179         LOG.debug("Tx {} deleteData called path = {}", getIdentifier(), path);
180
181         acquireOperation();
182         batchModification(new DeleteModification(path));
183     }
184
185     @Override
186     public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
187         LOG.debug("Tx {} mergeData called path = {}", getIdentifier(), path);
188
189         acquireOperation();
190         batchModification(new MergeModification(path, data));
191     }
192
193     @Override
194     public void writeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
195         LOG.debug("Tx {} writeData called path = {}", getIdentifier(), path);
196
197         acquireOperation();
198         batchModification(new WriteModification(path, data));
199     }
200
201     @Override
202     public void readData(final YangInstanceIdentifier path,
203             final SettableFuture<Optional<NormalizedNode<?, ?>>> returnFuture ) {
204
205         LOG.debug("Tx {} readData called path = {}", getIdentifier(), path);
206
207         // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the
208         // public API contract.
209
210         acquireOperation();
211         sendBatchedModifications();
212
213         OnComplete<Object> onComplete = new OnComplete<Object>() {
214             @Override
215             public void onComplete(Throwable failure, Object readResponse) throws Throwable {
216                 if(failure != null) {
217                     LOG.debug("Tx {} read operation failed: {}", getIdentifier(), failure);
218                     returnFuture.setException(new ReadFailedException(
219                             "Error reading data for path " + path, failure));
220
221                 } else {
222                     LOG.debug("Tx {} read operation succeeded", getIdentifier(), failure);
223
224                     if (readResponse instanceof ReadDataReply) {
225                         ReadDataReply reply = (ReadDataReply) readResponse;
226                         returnFuture.set(Optional.<NormalizedNode<?, ?>>fromNullable(reply.getNormalizedNode()));
227
228                     } else if (ReadDataReply.isSerializedType(readResponse)) {
229                         ReadDataReply reply = ReadDataReply.fromSerializable(readResponse);
230                         returnFuture.set(Optional.<NormalizedNode<?, ?>>fromNullable(reply.getNormalizedNode()));
231
232                     } else {
233                         returnFuture.setException(new ReadFailedException(
234                             "Invalid response reading data for path " + path));
235                     }
236                 }
237             }
238         };
239
240         Future<Object> readFuture = executeOperationAsync(new ReadData(path));
241
242         readFuture.onComplete(onComplete, actorContext.getClientDispatcher());
243     }
244
245     @Override
246     public void dataExists(final YangInstanceIdentifier path, final SettableFuture<Boolean> returnFuture) {
247
248         LOG.debug("Tx {} dataExists called path = {}", getIdentifier(), path);
249
250         // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the
251         // public API contract.
252
253         acquireOperation();
254         sendBatchedModifications();
255
256         OnComplete<Object> onComplete = new OnComplete<Object>() {
257             @Override
258             public void onComplete(Throwable failure, Object response) throws Throwable {
259                 if(failure != null) {
260                     LOG.debug("Tx {} dataExists operation failed: {}", getIdentifier(), failure);
261                     returnFuture.setException(new ReadFailedException(
262                             "Error checking data exists for path " + path, failure));
263                 } else {
264                     LOG.debug("Tx {} dataExists operation succeeded", getIdentifier(), failure);
265
266                     if (response instanceof DataExistsReply) {
267                         returnFuture.set(Boolean.valueOf(((DataExistsReply) response).exists()));
268
269                     } else if (response.getClass().equals(DataExistsReply.SERIALIZABLE_CLASS)) {
270                         returnFuture.set(Boolean.valueOf(DataExistsReply.fromSerializable(response).exists()));
271
272                     } else {
273                         returnFuture.setException(new ReadFailedException(
274                                 "Invalid response checking exists for path " + path));
275                     }
276                 }
277             }
278         };
279
280         Future<Object> future = executeOperationAsync(new DataExists(path));
281
282         future.onComplete(onComplete, actorContext.getClientDispatcher());
283     }
284
285     /**
286      * Acquire operation from the limiter if the hand-off has completed. If
287      * the hand-off is still ongoing, this method does nothing.
288      */
289     private final void acquireOperation() {
290         if (isOperationHandOffComplete()) {
291             limiter.acquire();
292         }
293     }
294
295     @Override
296     public boolean usesOperationLimiting() {
297         return true;
298     }
299 }