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.
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
9 package org.opendaylight.controller.cluster.datastore;
11 import akka.actor.ActorSelection;
12 import akka.dispatch.Mapper;
13 import akka.dispatch.OnComplete;
14 import com.google.common.base.Optional;
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.ReadyTransactionReply;
24 import org.opendaylight.controller.cluster.datastore.messages.SerializableMessage;
25 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
26 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
27 import org.opendaylight.controller.cluster.datastore.modification.Modification;
28 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
29 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
30 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import scala.concurrent.Future;
38 public class TransactionContextImpl extends AbstractTransactionContext {
39 private static final Logger LOG = LoggerFactory.getLogger(TransactionContextImpl.class);
41 private final String transactionChainId;
42 private final ActorContext actorContext;
43 private final ActorSelection actor;
44 private final boolean isTxActorLocal;
45 private final short remoteTransactionVersion;
47 private final OperationCompleter operationCompleter;
48 private BatchedModifications batchedModifications;
49 private int totalBatchedModificationsSent;
51 protected TransactionContextImpl(ActorSelection actor, TransactionIdentifier identifier,
52 String transactionChainId, ActorContext actorContext, SchemaContext schemaContext, boolean isTxActorLocal,
53 short remoteTransactionVersion, OperationCompleter operationCompleter) {
56 this.transactionChainId = transactionChainId;
57 this.actorContext = actorContext;
58 this.isTxActorLocal = isTxActorLocal;
59 this.remoteTransactionVersion = remoteTransactionVersion;
60 this.operationCompleter = operationCompleter;
63 private Future<Object> completeOperation(Future<Object> operationFuture){
64 operationFuture.onComplete(this.operationCompleter, actorContext.getClientDispatcher());
65 return operationFuture;
69 private ActorSelection getActor() {
73 protected ActorContext getActorContext() {
77 protected short getRemoteTransactionVersion() {
78 return remoteTransactionVersion;
81 protected Future<Object> executeOperationAsync(SerializableMessage msg) {
82 return completeOperation(actorContext.executeOperationAsync(getActor(), isTxActorLocal ? msg : msg.toSerializable()));
86 public void closeTransaction() {
87 LOG.debug("Tx {} closeTransaction called", getIdentifier());
89 actorContext.sendOperationAsync(getActor(), CloseTransaction.INSTANCE.toSerializable());
93 public Future<ActorSelection> readyTransaction() {
94 LOG.debug("Tx {} readyTransaction called", getIdentifier());
96 // Send the remaining batched modifications, if any, with the ready flag set.
98 Future<Object> lastModificationsFuture = sendBatchedModifications(true);
100 return transformReadyReply(lastModificationsFuture);
103 protected Future<ActorSelection> transformReadyReply(final Future<Object> readyReplyFuture) {
104 // Transform the last reply Future into a Future that returns the cohort actor path from
105 // the last reply message. That's the end result of the ready operation.
107 return readyReplyFuture.transform(new Mapper<Object, ActorSelection>() {
109 public ActorSelection checkedApply(Object serializedReadyReply) {
110 LOG.debug("Tx {} readyTransaction", getIdentifier());
112 // At this point the ready operation succeeded and we need to extract the cohort
113 // actor path from the reply.
114 if(ReadyTransactionReply.isSerializedType(serializedReadyReply)) {
115 ReadyTransactionReply readyTxReply = ReadyTransactionReply.fromSerializable(serializedReadyReply);
116 return actorContext.actorSelection(extractCohortPathFrom(readyTxReply));
119 // Throwing an exception here will fail the Future.
120 throw new IllegalArgumentException(String.format("%s: Invalid reply type %s",
121 getIdentifier(), serializedReadyReply.getClass()));
123 }, TransactionProxy.SAME_FAILURE_TRANSFORMER, actorContext.getClientDispatcher());
126 protected String extractCohortPathFrom(ReadyTransactionReply readyTxReply) {
127 return readyTxReply.getCohortPath();
130 private BatchedModifications newBatchedModifications() {
131 return new BatchedModifications(getIdentifier().toString(), remoteTransactionVersion, transactionChainId);
134 private void batchModification(Modification modification) {
135 if(batchedModifications == null) {
136 batchedModifications = newBatchedModifications();
139 batchedModifications.addModification(modification);
141 if(batchedModifications.getModifications().size() >=
142 actorContext.getDatastoreContext().getShardBatchedModificationCount()) {
143 sendBatchedModifications();
147 protected Future<Object> sendBatchedModifications() {
148 return sendBatchedModifications(false);
151 protected Future<Object> sendBatchedModifications(boolean ready) {
152 Future<Object> sent = null;
153 if(ready || (batchedModifications != null && !batchedModifications.getModifications().isEmpty())) {
154 if(batchedModifications == null) {
155 batchedModifications = newBatchedModifications();
158 if(LOG.isDebugEnabled()) {
159 LOG.debug("Tx {} sending {} batched modifications, ready: {}", getIdentifier(),
160 batchedModifications.getModifications().size(), ready);
163 batchedModifications.setReady(ready);
164 batchedModifications.setTotalMessagesSent(++totalBatchedModificationsSent);
165 sent = executeOperationAsync(batchedModifications);
168 batchedModifications = null;
170 batchedModifications = newBatchedModifications();
178 public void deleteData(YangInstanceIdentifier path) {
179 LOG.debug("Tx {} deleteData called path = {}", getIdentifier(), path);
181 batchModification(new DeleteModification(path));
185 public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
186 LOG.debug("Tx {} mergeData called path = {}", getIdentifier(), path);
188 batchModification(new MergeModification(path, data));
192 public void writeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
193 LOG.debug("Tx {} writeData called path = {}", getIdentifier(), path);
195 batchModification(new WriteModification(path, data));
199 public void readData(final YangInstanceIdentifier path,
200 final SettableFuture<Optional<NormalizedNode<?, ?>>> returnFuture ) {
202 LOG.debug("Tx {} readData called path = {}", getIdentifier(), path);
204 // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the
205 // public API contract.
207 sendBatchedModifications();
209 OnComplete<Object> onComplete = new OnComplete<Object>() {
211 public void onComplete(Throwable failure, Object readResponse) throws Throwable {
212 if(failure != null) {
213 LOG.debug("Tx {} read operation failed: {}", getIdentifier(), failure);
214 returnFuture.setException(new ReadFailedException(
215 "Error reading data for path " + path, failure));
218 LOG.debug("Tx {} read operation succeeded", getIdentifier(), failure);
220 if (readResponse instanceof ReadDataReply) {
221 ReadDataReply reply = (ReadDataReply) readResponse;
222 returnFuture.set(Optional.<NormalizedNode<?, ?>>fromNullable(reply.getNormalizedNode()));
224 } else if (ReadDataReply.isSerializedType(readResponse)) {
225 ReadDataReply reply = ReadDataReply.fromSerializable(readResponse);
226 returnFuture.set(Optional.<NormalizedNode<?, ?>>fromNullable(reply.getNormalizedNode()));
229 returnFuture.setException(new ReadFailedException(
230 "Invalid response reading data for path " + path));
236 Future<Object> readFuture = executeOperationAsync(new ReadData(path));
238 readFuture.onComplete(onComplete, actorContext.getClientDispatcher());
242 public void dataExists(final YangInstanceIdentifier path, final SettableFuture<Boolean> returnFuture) {
244 LOG.debug("Tx {} dataExists called path = {}", getIdentifier(), path);
246 // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the
247 // public API contract.
249 sendBatchedModifications();
251 OnComplete<Object> onComplete = new OnComplete<Object>() {
253 public void onComplete(Throwable failure, Object response) throws Throwable {
254 if(failure != null) {
255 LOG.debug("Tx {} dataExists operation failed: {}", getIdentifier(), failure);
256 returnFuture.setException(new ReadFailedException(
257 "Error checking data exists for path " + path, failure));
259 LOG.debug("Tx {} dataExists operation succeeded", getIdentifier(), failure);
261 if (response instanceof DataExistsReply) {
262 returnFuture.set(Boolean.valueOf(((DataExistsReply) response).exists()));
264 } else if (response.getClass().equals(DataExistsReply.SERIALIZABLE_CLASS)) {
265 returnFuture.set(Boolean.valueOf(DataExistsReply.fromSerializable(response).exists()));
268 returnFuture.setException(new ReadFailedException(
269 "Invalid response checking exists for path " + path));
275 Future<Object> future = executeOperationAsync(new DataExists(path));
277 future.onComplete(onComplete, actorContext.getClientDispatcher());