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.Preconditions;
14 import com.google.common.util.concurrent.SettableFuture;
15 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
16 import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
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.SerializableMessage;
20 import org.opendaylight.controller.cluster.datastore.modification.AbstractModification;
21 import org.opendaylight.controller.cluster.datastore.modification.Modification;
22 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
23 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import scala.concurrent.Future;
29 * Redirects front-end transaction operations to a shard for processing. Instances of this class are used
30 * when the destination shard is remote to the caller.
32 * @author Thomas Pantelis
34 public class RemoteTransactionContext extends AbstractTransactionContext {
35 private static final Logger LOG = LoggerFactory.getLogger(RemoteTransactionContext.class);
37 private final ActorContext actorContext;
38 private final ActorSelection actor;
39 private final OperationLimiter limiter;
41 private BatchedModifications batchedModifications;
42 private int totalBatchedModificationsSent;
44 protected RemoteTransactionContext(TransactionIdentifier identifier, ActorSelection actor,
45 ActorContext actorContext, short remoteTransactionVersion, OperationLimiter limiter) {
46 super(identifier, remoteTransactionVersion);
47 this.limiter = Preconditions.checkNotNull(limiter);
49 this.actorContext = actorContext;
52 private Future<Object> completeOperation(Future<Object> operationFuture){
53 operationFuture.onComplete(limiter, actorContext.getClientDispatcher());
54 return operationFuture;
57 private ActorSelection getActor() {
61 protected ActorContext getActorContext() {
65 protected Future<Object> executeOperationAsync(SerializableMessage msg) {
66 return completeOperation(actorContext.executeOperationAsync(getActor(), msg.toSerializable()));
70 public void closeTransaction() {
71 LOG.debug("Tx {} closeTransaction called", getIdentifier());
72 TransactionContextCleanup.untrack(this);
74 actorContext.sendOperationAsync(getActor(), new CloseTransaction(getTransactionVersion()).toSerializable());
78 public Future<Object> directCommit() {
79 LOG.debug("Tx {} directCommit called", getIdentifier());
81 // Send the remaining batched modifications, if any, with the ready flag set.
83 return sendBatchedModifications(true, true);
87 public Future<ActorSelection> readyTransaction() {
88 logModificationCount();
90 LOG.debug("Tx {} readyTransaction called", getIdentifier());
92 // Send the remaining batched modifications, if any, with the ready flag set.
94 Future<Object> lastModificationsFuture = sendBatchedModifications(true, false);
96 return transformReadyReply(lastModificationsFuture);
99 protected Future<ActorSelection> transformReadyReply(final Future<Object> readyReplyFuture) {
100 // Transform the last reply Future into a Future that returns the cohort actor path from
101 // the last reply message. That's the end result of the ready operation.
103 return TransactionReadyReplyMapper.transform(readyReplyFuture, actorContext, getIdentifier());
106 private BatchedModifications newBatchedModifications() {
107 return new BatchedModifications(getIdentifier().toString(), getTransactionVersion(),
108 getIdentifier().getChainId());
111 private void batchModification(Modification modification) {
112 incrementModificationCount();
113 if(batchedModifications == null) {
114 batchedModifications = newBatchedModifications();
117 batchedModifications.addModification(modification);
119 if(batchedModifications.getModifications().size() >=
120 actorContext.getDatastoreContext().getShardBatchedModificationCount()) {
121 sendBatchedModifications();
125 protected Future<Object> sendBatchedModifications() {
126 return sendBatchedModifications(false, false);
129 protected Future<Object> sendBatchedModifications(boolean ready, boolean doCommitOnReady) {
130 Future<Object> sent = null;
131 if(ready || (batchedModifications != null && !batchedModifications.getModifications().isEmpty())) {
132 if(batchedModifications == null) {
133 batchedModifications = newBatchedModifications();
136 if(LOG.isDebugEnabled()) {
137 LOG.debug("Tx {} sending {} batched modifications, ready: {}", getIdentifier(),
138 batchedModifications.getModifications().size(), ready);
141 batchedModifications.setReady(ready);
142 batchedModifications.setDoCommitOnReady(doCommitOnReady);
143 batchedModifications.setTotalMessagesSent(++totalBatchedModificationsSent);
144 sent = executeOperationAsync(batchedModifications);
147 batchedModifications = null;
149 batchedModifications = newBatchedModifications();
157 public void executeModification(AbstractModification modification) {
158 if(LOG.isDebugEnabled()) {
159 LOG.debug("Tx {} executeModification {} called path = {}", getIdentifier(), modification.getClass()
160 .getSimpleName(), modification.getPath());
164 batchModification(modification);
168 public <T> void executeRead(final AbstractRead<T> readCmd, final SettableFuture<T> returnFuture) {
169 if(LOG.isDebugEnabled()) {
170 LOG.debug("Tx {} executeRead {} called path = {}", getIdentifier(), readCmd.getClass().getSimpleName(),
174 // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the
175 // public API contract.
178 sendBatchedModifications();
180 OnComplete<Object> onComplete = new OnComplete<Object>() {
182 public void onComplete(Throwable failure, Object response) throws Throwable {
183 if(failure != null) {
184 if(LOG.isDebugEnabled()) {
185 LOG.debug("Tx {} {} operation failed: {}", getIdentifier(), readCmd.getClass().getSimpleName(),
188 returnFuture.setException(new ReadFailedException("Error checking " + readCmd.getClass().getSimpleName()
189 + " for path " + readCmd.getPath(), failure));
191 if(LOG.isDebugEnabled()) {
192 LOG.debug("Tx {} {} operation succeeded", getIdentifier(), readCmd.getClass().getSimpleName());
194 readCmd.processResponse(response, returnFuture);
199 Future<Object> future = executeOperationAsync(readCmd.asVersion(getTransactionVersion()));
201 future.onComplete(onComplete, actorContext.getClientDispatcher());
205 * Acquire operation from the limiter if the hand-off has completed. If
206 * the hand-off is still ongoing, this method does nothing.
208 private final void acquireOperation() {
209 if (isOperationHandOffComplete()) {
215 public boolean usesOperationLimiting() {