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.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import scala.concurrent.Future;
37 public class TransactionContextImpl extends AbstractTransactionContext {
38 private static final Logger LOG = LoggerFactory.getLogger(TransactionContextImpl.class);
40 private final ActorContext actorContext;
41 private final ActorSelection actor;
42 private final boolean isTxActorLocal;
43 private final short remoteTransactionVersion;
45 private final OperationCompleter operationCompleter;
46 private BatchedModifications batchedModifications;
47 private int totalBatchedModificationsSent;
49 protected TransactionContextImpl(ActorSelection actor, TransactionIdentifier identifier,
50 ActorContext actorContext, boolean isTxActorLocal,
51 short remoteTransactionVersion, OperationCompleter operationCompleter) {
54 this.actorContext = actorContext;
55 this.isTxActorLocal = isTxActorLocal;
56 this.remoteTransactionVersion = remoteTransactionVersion;
57 this.operationCompleter = operationCompleter;
60 private Future<Object> completeOperation(Future<Object> operationFuture){
61 operationFuture.onComplete(this.operationCompleter, actorContext.getClientDispatcher());
62 return operationFuture;
66 private ActorSelection getActor() {
70 protected ActorContext getActorContext() {
74 protected short getRemoteTransactionVersion() {
75 return remoteTransactionVersion;
78 protected Future<Object> executeOperationAsync(SerializableMessage msg) {
79 return completeOperation(actorContext.executeOperationAsync(getActor(), isTxActorLocal ? msg : msg.toSerializable()));
83 public void closeTransaction() {
84 LOG.debug("Tx {} closeTransaction called", getIdentifier());
86 actorContext.sendOperationAsync(getActor(), CloseTransaction.INSTANCE.toSerializable());
90 public boolean supportsDirectCommit() {
95 public Future<Object> directCommit() {
96 LOG.debug("Tx {} directCommit called", getIdentifier());
98 // Send the remaining batched modifications, if any, with the ready flag set.
100 return sendBatchedModifications(true, true);
104 public Future<ActorSelection> readyTransaction() {
105 LOG.debug("Tx {} readyTransaction called", getIdentifier());
107 // Send the remaining batched modifications, if any, with the ready flag set.
109 Future<Object> lastModificationsFuture = sendBatchedModifications(true, false);
111 return transformReadyReply(lastModificationsFuture);
114 protected Future<ActorSelection> transformReadyReply(final Future<Object> readyReplyFuture) {
115 // Transform the last reply Future into a Future that returns the cohort actor path from
116 // the last reply message. That's the end result of the ready operation.
118 return readyReplyFuture.transform(new Mapper<Object, ActorSelection>() {
120 public ActorSelection checkedApply(Object serializedReadyReply) {
121 LOG.debug("Tx {} readyTransaction", getIdentifier());
123 // At this point the ready operation succeeded and we need to extract the cohort
124 // actor path from the reply.
125 if(ReadyTransactionReply.isSerializedType(serializedReadyReply)) {
126 ReadyTransactionReply readyTxReply = ReadyTransactionReply.fromSerializable(serializedReadyReply);
127 return actorContext.actorSelection(extractCohortPathFrom(readyTxReply));
130 // Throwing an exception here will fail the Future.
131 throw new IllegalArgumentException(String.format("%s: Invalid reply type %s",
132 getIdentifier(), serializedReadyReply.getClass()));
134 }, TransactionProxy.SAME_FAILURE_TRANSFORMER, actorContext.getClientDispatcher());
137 protected String extractCohortPathFrom(ReadyTransactionReply readyTxReply) {
138 return readyTxReply.getCohortPath();
141 private BatchedModifications newBatchedModifications() {
142 return new BatchedModifications(getIdentifier().toString(), remoteTransactionVersion, getIdentifier().getChainId());
145 private void batchModification(Modification modification) {
146 if(batchedModifications == null) {
147 batchedModifications = newBatchedModifications();
150 batchedModifications.addModification(modification);
152 if(batchedModifications.getModifications().size() >=
153 actorContext.getDatastoreContext().getShardBatchedModificationCount()) {
154 sendBatchedModifications();
158 protected Future<Object> sendBatchedModifications() {
159 return sendBatchedModifications(false, false);
162 protected Future<Object> sendBatchedModifications(boolean ready, boolean doCommitOnReady) {
163 Future<Object> sent = null;
164 if(ready || (batchedModifications != null && !batchedModifications.getModifications().isEmpty())) {
165 if(batchedModifications == null) {
166 batchedModifications = newBatchedModifications();
169 if(LOG.isDebugEnabled()) {
170 LOG.debug("Tx {} sending {} batched modifications, ready: {}", getIdentifier(),
171 batchedModifications.getModifications().size(), ready);
174 batchedModifications.setReady(ready);
175 batchedModifications.setDoCommitOnReady(doCommitOnReady);
176 batchedModifications.setTotalMessagesSent(++totalBatchedModificationsSent);
177 sent = executeOperationAsync(batchedModifications);
180 batchedModifications = null;
182 batchedModifications = newBatchedModifications();
190 public void deleteData(YangInstanceIdentifier path) {
191 LOG.debug("Tx {} deleteData called path = {}", getIdentifier(), path);
193 batchModification(new DeleteModification(path));
197 public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
198 LOG.debug("Tx {} mergeData called path = {}", getIdentifier(), path);
200 batchModification(new MergeModification(path, data));
204 public void writeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
205 LOG.debug("Tx {} writeData called path = {}", getIdentifier(), path);
207 batchModification(new WriteModification(path, data));
211 public void readData(final YangInstanceIdentifier path,
212 final SettableFuture<Optional<NormalizedNode<?, ?>>> returnFuture ) {
214 LOG.debug("Tx {} readData called path = {}", getIdentifier(), path);
216 // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the
217 // public API contract.
219 sendBatchedModifications();
221 OnComplete<Object> onComplete = new OnComplete<Object>() {
223 public void onComplete(Throwable failure, Object readResponse) throws Throwable {
224 if(failure != null) {
225 LOG.debug("Tx {} read operation failed: {}", getIdentifier(), failure);
226 returnFuture.setException(new ReadFailedException(
227 "Error reading data for path " + path, failure));
230 LOG.debug("Tx {} read operation succeeded", getIdentifier(), failure);
232 if (readResponse instanceof ReadDataReply) {
233 ReadDataReply reply = (ReadDataReply) readResponse;
234 returnFuture.set(Optional.<NormalizedNode<?, ?>>fromNullable(reply.getNormalizedNode()));
236 } else if (ReadDataReply.isSerializedType(readResponse)) {
237 ReadDataReply reply = ReadDataReply.fromSerializable(readResponse);
238 returnFuture.set(Optional.<NormalizedNode<?, ?>>fromNullable(reply.getNormalizedNode()));
241 returnFuture.setException(new ReadFailedException(
242 "Invalid response reading data for path " + path));
248 Future<Object> readFuture = executeOperationAsync(new ReadData(path));
250 readFuture.onComplete(onComplete, actorContext.getClientDispatcher());
254 public void dataExists(final YangInstanceIdentifier path, final SettableFuture<Boolean> returnFuture) {
256 LOG.debug("Tx {} dataExists called path = {}", getIdentifier(), path);
258 // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the
259 // public API contract.
261 sendBatchedModifications();
263 OnComplete<Object> onComplete = new OnComplete<Object>() {
265 public void onComplete(Throwable failure, Object response) throws Throwable {
266 if(failure != null) {
267 LOG.debug("Tx {} dataExists operation failed: {}", getIdentifier(), failure);
268 returnFuture.setException(new ReadFailedException(
269 "Error checking data exists for path " + path, failure));
271 LOG.debug("Tx {} dataExists operation succeeded", getIdentifier(), failure);
273 if (response instanceof DataExistsReply) {
274 returnFuture.set(Boolean.valueOf(((DataExistsReply) response).exists()));
276 } else if (response.getClass().equals(DataExistsReply.SERIALIZABLE_CLASS)) {
277 returnFuture.set(Boolean.valueOf(DataExistsReply.fromSerializable(response).exists()));
280 returnFuture.setException(new ReadFailedException(
281 "Invalid response checking exists for path " + path));
287 Future<Object> future = executeOperationAsync(new DataExists(path));
289 future.onComplete(onComplete, actorContext.getClientDispatcher());