2 * Copyright (c) 2015 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.datastore;
10 import akka.actor.ActorSelection;
11 import akka.dispatch.Mapper;
12 import akka.dispatch.OnComplete;
13 import com.google.common.base.Optional;
14 import com.google.common.collect.Lists;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.util.List;
17 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
18 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
19 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
20 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
22 import org.opendaylight.controller.cluster.datastore.messages.DataExistsReply;
23 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
24 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
25 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
26 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
27 import org.opendaylight.controller.cluster.datastore.messages.SerializableMessage;
28 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
29 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
30 import org.opendaylight.controller.cluster.datastore.modification.Modification;
31 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
32 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
33 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import scala.concurrent.Future;
41 public class TransactionContextImpl extends AbstractTransactionContext {
42 private static final Logger LOG = LoggerFactory.getLogger(TransactionContextImpl.class);
44 private final String transactionChainId;
45 private final ActorContext actorContext;
46 private final ActorSelection actor;
47 private final boolean isTxActorLocal;
48 private final short remoteTransactionVersion;
50 private final OperationCompleter operationCompleter;
51 private BatchedModifications batchedModifications;
53 protected TransactionContextImpl(ActorSelection actor, TransactionIdentifier identifier,
54 String transactionChainId, ActorContext actorContext, SchemaContext schemaContext, boolean isTxActorLocal,
55 short remoteTransactionVersion, OperationCompleter operationCompleter) {
58 this.transactionChainId = transactionChainId;
59 this.actorContext = actorContext;
60 this.isTxActorLocal = isTxActorLocal;
61 this.remoteTransactionVersion = remoteTransactionVersion;
62 this.operationCompleter = operationCompleter;
65 private Future<Object> completeOperation(Future<Object> operationFuture){
66 operationFuture.onComplete(this.operationCompleter, actorContext.getClientDispatcher());
67 return operationFuture;
71 private ActorSelection getActor() {
75 protected ActorContext getActorContext() {
79 protected short getRemoteTransactionVersion() {
80 return remoteTransactionVersion;
83 protected Future<Object> executeOperationAsync(SerializableMessage msg) {
84 return completeOperation(actorContext.executeOperationAsync(getActor(), isTxActorLocal ? msg : msg.toSerializable()));
88 public void closeTransaction() {
89 LOG.debug("Tx {} closeTransaction called", identifier);
91 actorContext.sendOperationAsync(getActor(), CloseTransaction.INSTANCE.toSerializable());
95 public Future<ActorSelection> readyTransaction() {
96 LOG.debug("Tx {} readyTransaction called with {} previous recorded operations pending",
97 identifier, recordedOperationFutures.size());
99 // Send the remaining batched modifications if any.
101 sendAndRecordBatchedModifications();
103 // Send the ReadyTransaction message to the Tx actor.
105 Future<Object> readyReplyFuture = executeOperationAsync(ReadyTransaction.INSTANCE);
107 return combineRecordedOperationsFutures(readyReplyFuture);
110 protected Future<ActorSelection> combineRecordedOperationsFutures(final Future<Object> withLastReplyFuture) {
111 // Combine all the previously recorded put/merge/delete operation reply Futures and the
112 // ReadyTransactionReply Future into one Future. If any one fails then the combined
113 // Future will fail. We need all prior operations and the ready operation to succeed
114 // in order to attempt commit.
116 List<Future<Object>> futureList = Lists.newArrayListWithCapacity(recordedOperationFutures.size() + 1);
117 futureList.addAll(recordedOperationFutures);
118 futureList.add(withLastReplyFuture);
120 Future<Iterable<Object>> combinedFutures = akka.dispatch.Futures.sequence(futureList,
121 actorContext.getClientDispatcher());
123 // Transform the combined Future into a Future that returns the cohort actor path from
124 // the ReadyTransactionReply. That's the end result of the ready operation.
126 return combinedFutures.transform(new Mapper<Iterable<Object>, ActorSelection>() {
128 public ActorSelection checkedApply(Iterable<Object> notUsed) {
129 LOG.debug("Tx {} readyTransaction: pending recorded operations succeeded",
132 // At this point all the Futures succeeded and we need to extract the cohort
133 // actor path from the ReadyTransactionReply. For the recorded operations, they
134 // don't return any data so we're only interested that they completed
135 // successfully. We could be paranoid and verify the correct reply types but
136 // that really should never happen so it's not worth the overhead of
137 // de-serializing each reply.
139 // Note the Future get call here won't block as it's complete.
140 Object serializedReadyReply = withLastReplyFuture.value().get().get();
141 if (serializedReadyReply instanceof ReadyTransactionReply) {
142 return actorContext.actorSelection(((ReadyTransactionReply)serializedReadyReply).getCohortPath());
143 } else if(serializedReadyReply instanceof BatchedModificationsReply) {
144 return actorContext.actorSelection(((BatchedModificationsReply)serializedReadyReply).getCohortPath());
145 } else if(serializedReadyReply.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
146 ReadyTransactionReply reply = ReadyTransactionReply.fromSerializable(serializedReadyReply);
147 String cohortPath = deserializeCohortPath(reply.getCohortPath());
148 return actorContext.actorSelection(cohortPath);
150 // Throwing an exception here will fail the Future.
151 throw new IllegalArgumentException(String.format("%s: Invalid reply type %s",
152 identifier, serializedReadyReply.getClass()));
155 }, TransactionProxy.SAME_FAILURE_TRANSFORMER, actorContext.getClientDispatcher());
158 protected String deserializeCohortPath(String cohortPath) {
162 private void batchModification(Modification modification) {
163 if(batchedModifications == null) {
164 batchedModifications = new BatchedModifications(identifier.toString(), remoteTransactionVersion,
168 batchedModifications.addModification(modification);
170 if(batchedModifications.getModifications().size() >=
171 actorContext.getDatastoreContext().getShardBatchedModificationCount()) {
172 sendAndRecordBatchedModifications();
176 private void sendAndRecordBatchedModifications() {
177 Future<Object> sentFuture = sendBatchedModifications();
178 if(sentFuture != null) {
179 recordedOperationFutures.add(sentFuture);
183 protected Future<Object> sendBatchedModifications() {
184 return sendBatchedModifications(false);
187 protected Future<Object> sendBatchedModifications(boolean ready) {
188 Future<Object> sent = null;
189 if(batchedModifications != null) {
190 if(LOG.isDebugEnabled()) {
191 LOG.debug("Tx {} sending {} batched modifications, ready: {}", identifier,
192 batchedModifications.getModifications().size(), ready);
195 batchedModifications.setReady(ready);
196 sent = executeOperationAsync(batchedModifications);
198 batchedModifications = new BatchedModifications(identifier.toString(), remoteTransactionVersion,
206 public void deleteData(YangInstanceIdentifier path) {
207 LOG.debug("Tx {} deleteData called path = {}", identifier, path);
209 batchModification(new DeleteModification(path));
213 public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
214 LOG.debug("Tx {} mergeData called path = {}", identifier, path);
216 batchModification(new MergeModification(path, data));
220 public void writeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
221 LOG.debug("Tx {} writeData called path = {}", identifier, path);
223 batchModification(new WriteModification(path, data));
227 public void readData(
228 final YangInstanceIdentifier path,final SettableFuture<Optional<NormalizedNode<?, ?>>> returnFuture ) {
230 LOG.debug("Tx {} readData called path = {}", identifier, path);
232 // Send the remaining batched modifications if any.
234 sendAndRecordBatchedModifications();
236 // If there were any previous recorded put/merge/delete operation reply Futures then we
237 // must wait for them to successfully complete. This is necessary to honor the read
238 // uncommitted semantics of the public API contract. If any one fails then fail the read.
240 if(recordedOperationFutures.isEmpty()) {
241 finishReadData(path, returnFuture);
243 LOG.debug("Tx {} readData: verifying {} previous recorded operations",
244 identifier, recordedOperationFutures.size());
246 // Note: we make a copy of recordedOperationFutures to be on the safe side in case
247 // Futures#sequence accesses the passed List on a different thread, as
248 // recordedOperationFutures is not synchronized.
250 Future<Iterable<Object>> combinedFutures = akka.dispatch.Futures.sequence(
251 Lists.newArrayList(recordedOperationFutures),
252 actorContext.getClientDispatcher());
254 OnComplete<Iterable<Object>> onComplete = new OnComplete<Iterable<Object>>() {
256 public void onComplete(Throwable failure, Iterable<Object> notUsed)
258 if(failure != null) {
259 LOG.debug("Tx {} readData: a recorded operation failed: {}",
260 identifier, failure);
261 returnFuture.setException(new ReadFailedException(
262 "The read could not be performed because a previous put, merge,"
263 + "or delete operation failed", failure));
265 finishReadData(path, returnFuture);
270 combinedFutures.onComplete(onComplete, actorContext.getClientDispatcher());
275 private void finishReadData(final YangInstanceIdentifier path,
276 final SettableFuture<Optional<NormalizedNode<?, ?>>> returnFuture) {
278 LOG.debug("Tx {} finishReadData called path = {}", identifier, path);
280 OnComplete<Object> onComplete = new OnComplete<Object>() {
282 public void onComplete(Throwable failure, Object readResponse) throws Throwable {
283 if(failure != null) {
284 LOG.debug("Tx {} read operation failed: {}", identifier, failure);
285 returnFuture.setException(new ReadFailedException(
286 "Error reading data for path " + path, failure));
289 LOG.debug("Tx {} read operation succeeded", identifier, failure);
291 if (readResponse instanceof ReadDataReply) {
292 ReadDataReply reply = (ReadDataReply) readResponse;
293 returnFuture.set(Optional.<NormalizedNode<?, ?>>fromNullable(reply.getNormalizedNode()));
295 } else if (ReadDataReply.isSerializedType(readResponse)) {
296 ReadDataReply reply = ReadDataReply.fromSerializable(readResponse);
297 returnFuture.set(Optional.<NormalizedNode<?, ?>>fromNullable(reply.getNormalizedNode()));
300 returnFuture.setException(new ReadFailedException(
301 "Invalid response reading data for path " + path));
307 Future<Object> readFuture = executeOperationAsync(new ReadData(path));
309 readFuture.onComplete(onComplete, actorContext.getClientDispatcher());
313 public void dataExists(final YangInstanceIdentifier path, final SettableFuture<Boolean> returnFuture) {
315 LOG.debug("Tx {} dataExists called path = {}", identifier, path);
317 // Send the remaining batched modifications if any.
319 sendAndRecordBatchedModifications();
321 // If there were any previous recorded put/merge/delete operation reply Futures then we
322 // must wait for them to successfully complete. This is necessary to honor the read
323 // uncommitted semantics of the public API contract. If any one fails then fail this
326 if(recordedOperationFutures.isEmpty()) {
327 finishDataExists(path, returnFuture);
329 LOG.debug("Tx {} dataExists: verifying {} previous recorded operations",
330 identifier, recordedOperationFutures.size());
332 // Note: we make a copy of recordedOperationFutures to be on the safe side in case
333 // Futures#sequence accesses the passed List on a different thread, as
334 // recordedOperationFutures is not synchronized.
336 Future<Iterable<Object>> combinedFutures = akka.dispatch.Futures.sequence(
337 Lists.newArrayList(recordedOperationFutures),
338 actorContext.getClientDispatcher());
339 OnComplete<Iterable<Object>> onComplete = new OnComplete<Iterable<Object>>() {
341 public void onComplete(Throwable failure, Iterable<Object> notUsed)
343 if(failure != null) {
344 LOG.debug("Tx {} dataExists: a recorded operation failed: {}",
345 identifier, failure);
346 returnFuture.setException(new ReadFailedException(
347 "The data exists could not be performed because a previous "
348 + "put, merge, or delete operation failed", failure));
350 finishDataExists(path, returnFuture);
355 combinedFutures.onComplete(onComplete, actorContext.getClientDispatcher());
359 private void finishDataExists(final YangInstanceIdentifier path,
360 final SettableFuture<Boolean> returnFuture) {
362 LOG.debug("Tx {} finishDataExists called path = {}", identifier, path);
364 OnComplete<Object> onComplete = new OnComplete<Object>() {
366 public void onComplete(Throwable failure, Object response) throws Throwable {
367 if(failure != null) {
368 LOG.debug("Tx {} dataExists operation failed: {}", identifier, failure);
369 returnFuture.setException(new ReadFailedException(
370 "Error checking data exists for path " + path, failure));
372 LOG.debug("Tx {} dataExists operation succeeded", identifier, failure);
374 if (response instanceof DataExistsReply) {
375 returnFuture.set(Boolean.valueOf(((DataExistsReply) response).exists()));
377 } else if (response.getClass().equals(DataExistsReply.SERIALIZABLE_CLASS)) {
378 returnFuture.set(Boolean.valueOf(DataExistsReply.fromSerializable(response).exists()));
381 returnFuture.setException(new ReadFailedException(
382 "Invalid response checking exists for path " + path));
388 Future<Object> future = executeOperationAsync(new DataExists(path));
390 future.onComplete(onComplete, actorContext.getClientDispatcher());