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 boolean isTxActorLocal;
40 private final short remoteTransactionVersion;
41 private final OperationLimiter limiter;
43 private BatchedModifications batchedModifications;
44 private int totalBatchedModificationsSent;
46 protected RemoteTransactionContext(TransactionIdentifier identifier, ActorSelection actor,
47 ActorContext actorContext, boolean isTxActorLocal,
48 short remoteTransactionVersion, OperationLimiter limiter) {
50 this.limiter = Preconditions.checkNotNull(limiter);
52 this.actorContext = actorContext;
53 this.isTxActorLocal = isTxActorLocal;
54 this.remoteTransactionVersion = remoteTransactionVersion;
57 private Future<Object> completeOperation(Future<Object> operationFuture){
58 operationFuture.onComplete(limiter, actorContext.getClientDispatcher());
59 return operationFuture;
62 private ActorSelection getActor() {
66 protected ActorContext getActorContext() {
70 protected short getRemoteTransactionVersion() {
71 return remoteTransactionVersion;
74 protected Future<Object> executeOperationAsync(SerializableMessage msg) {
75 return completeOperation(actorContext.executeOperationAsync(getActor(), isTxActorLocal ? msg : msg.toSerializable()));
79 public void closeTransaction() {
80 LOG.debug("Tx {} closeTransaction called", getIdentifier());
81 TransactionContextCleanup.untrack(this);
83 actorContext.sendOperationAsync(getActor(), CloseTransaction.INSTANCE.toSerializable());
87 public boolean supportsDirectCommit() {
92 public Future<Object> directCommit() {
93 LOG.debug("Tx {} directCommit called", getIdentifier());
95 // Send the remaining batched modifications, if any, with the ready flag set.
97 return sendBatchedModifications(true, true);
101 public Future<ActorSelection> readyTransaction() {
102 logModificationCount();
104 LOG.debug("Tx {} readyTransaction called", getIdentifier());
106 // Send the remaining batched modifications, if any, with the ready flag set.
108 Future<Object> lastModificationsFuture = sendBatchedModifications(true, false);
110 return transformReadyReply(lastModificationsFuture);
113 protected Future<ActorSelection> transformReadyReply(final Future<Object> readyReplyFuture) {
114 // Transform the last reply Future into a Future that returns the cohort actor path from
115 // the last reply message. That's the end result of the ready operation.
117 return TransactionReadyReplyMapper.transform(readyReplyFuture, actorContext, getIdentifier());
120 private BatchedModifications newBatchedModifications() {
121 return new BatchedModifications(getIdentifier().toString(), remoteTransactionVersion, getIdentifier().getChainId());
124 private void batchModification(Modification modification) {
125 incrementModificationCount();
126 if(batchedModifications == null) {
127 batchedModifications = newBatchedModifications();
130 batchedModifications.addModification(modification);
132 if(batchedModifications.getModifications().size() >=
133 actorContext.getDatastoreContext().getShardBatchedModificationCount()) {
134 sendBatchedModifications();
138 protected Future<Object> sendBatchedModifications() {
139 return sendBatchedModifications(false, false);
142 protected Future<Object> sendBatchedModifications(boolean ready, boolean doCommitOnReady) {
143 Future<Object> sent = null;
144 if(ready || (batchedModifications != null && !batchedModifications.getModifications().isEmpty())) {
145 if(batchedModifications == null) {
146 batchedModifications = newBatchedModifications();
149 if(LOG.isDebugEnabled()) {
150 LOG.debug("Tx {} sending {} batched modifications, ready: {}", getIdentifier(),
151 batchedModifications.getModifications().size(), ready);
154 batchedModifications.setReady(ready);
155 batchedModifications.setDoCommitOnReady(doCommitOnReady);
156 batchedModifications.setTotalMessagesSent(++totalBatchedModificationsSent);
157 sent = executeOperationAsync(batchedModifications);
160 batchedModifications = null;
162 batchedModifications = newBatchedModifications();
170 public void executeModification(AbstractModification modification) {
171 if(LOG.isDebugEnabled()) {
172 LOG.debug("Tx {} executeModification {} called path = {}", getIdentifier(), modification.getClass()
173 .getSimpleName(), modification.getPath());
177 batchModification(modification);
181 public <T> void executeRead(final AbstractRead<T> readCmd, final SettableFuture<T> returnFuture) {
182 if(LOG.isDebugEnabled()) {
183 LOG.debug("Tx {} executeRead {} called path = {}", getIdentifier(), readCmd.getClass().getSimpleName(),
187 // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the
188 // public API contract.
191 sendBatchedModifications();
193 OnComplete<Object> onComplete = new OnComplete<Object>() {
195 public void onComplete(Throwable failure, Object response) throws Throwable {
196 if(failure != null) {
197 if(LOG.isDebugEnabled()) {
198 LOG.debug("Tx {} {} operation failed: {}", getIdentifier(), readCmd.getClass().getSimpleName(),
201 returnFuture.setException(new ReadFailedException("Error checking " + readCmd.getClass().getSimpleName()
202 + " for path " + readCmd.getPath(), failure));
204 if(LOG.isDebugEnabled()) {
205 LOG.debug("Tx {} {} operation succeeded", getIdentifier(), readCmd.getClass().getSimpleName());
207 readCmd.processResponse(response, returnFuture);
212 Future<Object> future = executeOperationAsync(readCmd);
214 future.onComplete(onComplete, actorContext.getClientDispatcher());
218 * Acquire operation from the limiter if the hand-off has completed. If
219 * the hand-off is still ongoing, this method does nothing.
221 private final void acquireOperation() {
222 if (isOperationHandOffComplete()) {
228 public boolean usesOperationLimiting() {