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.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;
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.
40 * @author Thomas Pantelis
42 public class RemoteTransactionContext extends AbstractTransactionContext {
43 private static final Logger LOG = LoggerFactory.getLogger(RemoteTransactionContext.class);
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;
51 private BatchedModifications batchedModifications;
52 private int totalBatchedModificationsSent;
54 protected RemoteTransactionContext(TransactionIdentifier identifier, ActorSelection actor,
55 ActorContext actorContext, boolean isTxActorLocal,
56 short remoteTransactionVersion, OperationLimiter limiter) {
58 this.limiter = Preconditions.checkNotNull(limiter);
60 this.actorContext = actorContext;
61 this.isTxActorLocal = isTxActorLocal;
62 this.remoteTransactionVersion = remoteTransactionVersion;
65 private Future<Object> completeOperation(Future<Object> operationFuture){
66 operationFuture.onComplete(limiter, actorContext.getClientDispatcher());
67 return operationFuture;
70 private ActorSelection getActor() {
74 protected ActorContext getActorContext() {
78 protected short getRemoteTransactionVersion() {
79 return remoteTransactionVersion;
82 protected Future<Object> executeOperationAsync(SerializableMessage msg) {
83 return completeOperation(actorContext.executeOperationAsync(getActor(), isTxActorLocal ? msg : msg.toSerializable()));
87 public void closeTransaction() {
88 LOG.debug("Tx {} closeTransaction called", getIdentifier());
89 TransactionContextCleanup.untrack(this);
91 actorContext.sendOperationAsync(getActor(), CloseTransaction.INSTANCE.toSerializable());
95 public boolean supportsDirectCommit() {
100 public Future<Object> directCommit() {
101 LOG.debug("Tx {} directCommit called", getIdentifier());
103 // Send the remaining batched modifications, if any, with the ready flag set.
105 return sendBatchedModifications(true, true);
109 public Future<ActorSelection> readyTransaction() {
110 logModificationCount();
112 LOG.debug("Tx {} readyTransaction called", getIdentifier());
114 // Send the remaining batched modifications, if any, with the ready flag set.
116 Future<Object> lastModificationsFuture = sendBatchedModifications(true, false);
118 return transformReadyReply(lastModificationsFuture);
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.
125 return TransactionReadyReplyMapper.transform(readyReplyFuture, actorContext, getIdentifier());
128 private BatchedModifications newBatchedModifications() {
129 return new BatchedModifications(getIdentifier().toString(), remoteTransactionVersion, getIdentifier().getChainId());
132 private void batchModification(Modification modification) {
133 incrementModificationCount();
134 if(batchedModifications == null) {
135 batchedModifications = newBatchedModifications();
138 batchedModifications.addModification(modification);
140 if(batchedModifications.getModifications().size() >=
141 actorContext.getDatastoreContext().getShardBatchedModificationCount()) {
142 sendBatchedModifications();
146 protected Future<Object> sendBatchedModifications() {
147 return sendBatchedModifications(false, false);
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();
157 if(LOG.isDebugEnabled()) {
158 LOG.debug("Tx {} sending {} batched modifications, ready: {}", getIdentifier(),
159 batchedModifications.getModifications().size(), ready);
162 batchedModifications.setReady(ready);
163 batchedModifications.setDoCommitOnReady(doCommitOnReady);
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);
182 batchModification(new DeleteModification(path));
186 public void mergeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
187 LOG.debug("Tx {} mergeData called path = {}", getIdentifier(), path);
190 batchModification(new MergeModification(path, data));
194 public void writeData(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
195 LOG.debug("Tx {} writeData called path = {}", getIdentifier(), path);
198 batchModification(new WriteModification(path, data));
202 public void readData(final YangInstanceIdentifier path,
203 final SettableFuture<Optional<NormalizedNode<?, ?>>> returnFuture ) {
205 LOG.debug("Tx {} readData called path = {}", getIdentifier(), path);
207 // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the
208 // public API contract.
211 sendBatchedModifications();
213 OnComplete<Object> onComplete = new OnComplete<Object>() {
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));
222 LOG.debug("Tx {} read operation succeeded", getIdentifier(), failure);
224 if (readResponse instanceof ReadDataReply) {
225 ReadDataReply reply = (ReadDataReply) readResponse;
226 returnFuture.set(Optional.<NormalizedNode<?, ?>>fromNullable(reply.getNormalizedNode()));
228 } else if (ReadDataReply.isSerializedType(readResponse)) {
229 ReadDataReply reply = ReadDataReply.fromSerializable(readResponse);
230 returnFuture.set(Optional.<NormalizedNode<?, ?>>fromNullable(reply.getNormalizedNode()));
233 returnFuture.setException(new ReadFailedException(
234 "Invalid response reading data for path " + path));
240 Future<Object> readFuture = executeOperationAsync(new ReadData(path));
242 readFuture.onComplete(onComplete, actorContext.getClientDispatcher());
246 public void dataExists(final YangInstanceIdentifier path, final SettableFuture<Boolean> returnFuture) {
248 LOG.debug("Tx {} dataExists called path = {}", getIdentifier(), path);
250 // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the
251 // public API contract.
254 sendBatchedModifications();
256 OnComplete<Object> onComplete = new OnComplete<Object>() {
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));
264 LOG.debug("Tx {} dataExists operation succeeded", getIdentifier(), failure);
266 if (response instanceof DataExistsReply) {
267 returnFuture.set(Boolean.valueOf(((DataExistsReply) response).exists()));
269 } else if (response.getClass().equals(DataExistsReply.SERIALIZABLE_CLASS)) {
270 returnFuture.set(Boolean.valueOf(DataExistsReply.fromSerializable(response).exists()));
273 returnFuture.setException(new ReadFailedException(
274 "Invalid response checking exists for path " + path));
280 Future<Object> future = executeOperationAsync(new DataExists(path));
282 future.onComplete(onComplete, actorContext.getClientDispatcher());
286 * Acquire operation from the limiter if the hand-off has completed. If
287 * the hand-off is still ongoing, this method does nothing.
289 private final void acquireOperation() {
290 if (isOperationHandOffComplete()) {
296 public boolean usesOperationLimiting() {