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.OnComplete;
13 import com.google.common.base.Optional;
14 import com.google.common.util.concurrent.SettableFuture;
15 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
16 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
17 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
18 import org.opendaylight.controller.cluster.datastore.messages.DataExistsReply;
19 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
20 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
21 import org.opendaylight.controller.cluster.datastore.messages.SerializableMessage;
22 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
23 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
24 import org.opendaylight.controller.cluster.datastore.modification.Modification;
25 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
26 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
27 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import scala.concurrent.Future;
35 * Redirects front-end transaction operations to a shard for processing. Instances of this class are used
36 * when the destination shard is remote to the caller.
38 * @author Thomas Pantelis
40 public class RemoteTransactionContext extends AbstractTransactionContext {
41 private static final Logger LOG = LoggerFactory.getLogger(RemoteTransactionContext.class);
43 private final ActorContext actorContext;
44 private final ActorSelection actor;
45 private final boolean isTxActorLocal;
46 private final short remoteTransactionVersion;
48 private BatchedModifications batchedModifications;
49 private int totalBatchedModificationsSent;
51 protected RemoteTransactionContext(ActorSelection actor,
52 ActorContext actorContext, boolean isTxActorLocal,
53 short remoteTransactionVersion, OperationLimiter limiter) {
56 this.actorContext = actorContext;
57 this.isTxActorLocal = isTxActorLocal;
58 this.remoteTransactionVersion = remoteTransactionVersion;
61 private Future<Object> completeOperation(Future<Object> operationFuture){
62 operationFuture.onComplete(getLimiter(), actorContext.getClientDispatcher());
63 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());
85 TransactionContextCleanup.untrack(this);
87 actorContext.sendOperationAsync(getActor(), CloseTransaction.INSTANCE.toSerializable());
91 public boolean supportsDirectCommit() {
96 public Future<Object> directCommit() {
97 LOG.debug("Tx {} directCommit called", getIdentifier());
99 // Send the remaining batched modifications, if any, with the ready flag set.
101 return sendBatchedModifications(true, true);
105 public Future<ActorSelection> readyTransaction() {
106 logModificationCount();
108 LOG.debug("Tx {} readyTransaction called", getIdentifier());
110 // Send the remaining batched modifications, if any, with the ready flag set.
112 Future<Object> lastModificationsFuture = sendBatchedModifications(true, false);
114 return transformReadyReply(lastModificationsFuture);
117 protected Future<ActorSelection> transformReadyReply(final Future<Object> readyReplyFuture) {
118 // Transform the last reply Future into a Future that returns the cohort actor path from
119 // the last reply message. That's the end result of the ready operation.
121 return TransactionReadyReplyMapper.transform(readyReplyFuture, actorContext, getIdentifier());
124 private BatchedModifications newBatchedModifications() {
125 return new BatchedModifications(getIdentifier().toString(), remoteTransactionVersion, getIdentifier().getChainId());
128 private void batchModification(Modification modification) {
129 incrementModificationCount();
130 if(batchedModifications == null) {
131 batchedModifications = newBatchedModifications();
134 batchedModifications.addModification(modification);
136 if(batchedModifications.getModifications().size() >=
137 actorContext.getDatastoreContext().getShardBatchedModificationCount()) {
138 sendBatchedModifications();
142 protected Future<Object> sendBatchedModifications() {
143 return sendBatchedModifications(false, false);
146 protected Future<Object> sendBatchedModifications(boolean ready, boolean doCommitOnReady) {
147 Future<Object> sent = null;
148 if(ready || (batchedModifications != null && !batchedModifications.getModifications().isEmpty())) {
149 if(batchedModifications == null) {
150 batchedModifications = newBatchedModifications();
153 if(LOG.isDebugEnabled()) {
154 LOG.debug("Tx {} sending {} batched modifications, ready: {}", getIdentifier(),
155 batchedModifications.getModifications().size(), ready);
158 batchedModifications.setReady(ready);
159 batchedModifications.setDoCommitOnReady(doCommitOnReady);
160 batchedModifications.setTotalMessagesSent(++totalBatchedModificationsSent);
161 sent = executeOperationAsync(batchedModifications);
164 batchedModifications = null;
166 batchedModifications = newBatchedModifications();
174 public void deleteData(YangInstanceIdentifier path) {
175 LOG.debug("Tx {} deleteData called path = {}", getIdentifier(), path);
178 batchModification(new DeleteModification(path));
182 public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
183 LOG.debug("Tx {} mergeData called path = {}", getIdentifier(), path);
186 batchModification(new MergeModification(path, data));
190 public void writeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
191 LOG.debug("Tx {} writeData called path = {}", getIdentifier(), path);
194 batchModification(new WriteModification(path, data));
198 public void readData(final YangInstanceIdentifier path,
199 final SettableFuture<Optional<NormalizedNode<?, ?>>> returnFuture ) {
201 LOG.debug("Tx {} readData called path = {}", getIdentifier(), path);
203 // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the
204 // 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.
250 sendBatchedModifications();
252 OnComplete<Object> onComplete = new OnComplete<Object>() {
254 public void onComplete(Throwable failure, Object response) throws Throwable {
255 if(failure != null) {
256 LOG.debug("Tx {} dataExists operation failed: {}", getIdentifier(), failure);
257 returnFuture.setException(new ReadFailedException(
258 "Error checking data exists for path " + path, failure));
260 LOG.debug("Tx {} dataExists operation succeeded", getIdentifier(), failure);
262 if (response instanceof DataExistsReply) {
263 returnFuture.set(Boolean.valueOf(((DataExistsReply) response).exists()));
265 } else if (response.getClass().equals(DataExistsReply.SERIALIZABLE_CLASS)) {
266 returnFuture.set(Boolean.valueOf(DataExistsReply.fromSerializable(response).exists()));
269 returnFuture.setException(new ReadFailedException(
270 "Invalid response checking exists for path " + path));
276 Future<Object> future = executeOperationAsync(new DataExists(path));
278 future.onComplete(onComplete, actorContext.getClientDispatcher());