Remove TransactionContext.supportsDirectCommit method
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / RemoteTransactionContext.java
1 /*
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.
4  *
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
8  */
9 package org.opendaylight.controller.cluster.datastore;
10
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;
27
28 /**
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.
31  *
32  * @author Thomas Pantelis
33  */
34 public class RemoteTransactionContext extends AbstractTransactionContext {
35     private static final Logger LOG = LoggerFactory.getLogger(RemoteTransactionContext.class);
36
37     private final ActorContext actorContext;
38     private final ActorSelection actor;
39     private final OperationLimiter limiter;
40
41     private BatchedModifications batchedModifications;
42     private int totalBatchedModificationsSent;
43
44     protected RemoteTransactionContext(TransactionIdentifier identifier, ActorSelection actor,
45             ActorContext actorContext, short remoteTransactionVersion, OperationLimiter limiter) {
46         super(identifier, remoteTransactionVersion);
47         this.limiter = Preconditions.checkNotNull(limiter);
48         this.actor = actor;
49         this.actorContext = actorContext;
50     }
51
52     private Future<Object> completeOperation(Future<Object> operationFuture){
53         operationFuture.onComplete(limiter, actorContext.getClientDispatcher());
54         return operationFuture;
55     }
56
57     private ActorSelection getActor() {
58         return actor;
59     }
60
61     protected ActorContext getActorContext() {
62         return actorContext;
63     }
64
65     protected Future<Object> executeOperationAsync(SerializableMessage msg) {
66         return completeOperation(actorContext.executeOperationAsync(getActor(), msg.toSerializable()));
67     }
68
69     @Override
70     public void closeTransaction() {
71         LOG.debug("Tx {} closeTransaction called", getIdentifier());
72         TransactionContextCleanup.untrack(this);
73
74         actorContext.sendOperationAsync(getActor(), new CloseTransaction(getTransactionVersion()).toSerializable());
75     }
76
77     @Override
78     public Future<Object> directCommit() {
79         LOG.debug("Tx {} directCommit called", getIdentifier());
80
81         // Send the remaining batched modifications, if any, with the ready flag set.
82
83         return sendBatchedModifications(true, true);
84     }
85
86     @Override
87     public Future<ActorSelection> readyTransaction() {
88         logModificationCount();
89
90         LOG.debug("Tx {} readyTransaction called", getIdentifier());
91
92         // Send the remaining batched modifications, if any, with the ready flag set.
93
94         Future<Object> lastModificationsFuture = sendBatchedModifications(true, false);
95
96         return transformReadyReply(lastModificationsFuture);
97     }
98
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.
102
103         return TransactionReadyReplyMapper.transform(readyReplyFuture, actorContext, getIdentifier());
104     }
105
106     private BatchedModifications newBatchedModifications() {
107         return new BatchedModifications(getIdentifier().toString(), getTransactionVersion(),
108                 getIdentifier().getChainId());
109     }
110
111     private void batchModification(Modification modification) {
112         incrementModificationCount();
113         if(batchedModifications == null) {
114             batchedModifications = newBatchedModifications();
115         }
116
117         batchedModifications.addModification(modification);
118
119         if(batchedModifications.getModifications().size() >=
120                 actorContext.getDatastoreContext().getShardBatchedModificationCount()) {
121             sendBatchedModifications();
122         }
123     }
124
125     protected Future<Object> sendBatchedModifications() {
126         return sendBatchedModifications(false, false);
127     }
128
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();
134             }
135
136             if(LOG.isDebugEnabled()) {
137                 LOG.debug("Tx {} sending {} batched modifications, ready: {}", getIdentifier(),
138                         batchedModifications.getModifications().size(), ready);
139             }
140
141             batchedModifications.setReady(ready);
142             batchedModifications.setDoCommitOnReady(doCommitOnReady);
143             batchedModifications.setTotalMessagesSent(++totalBatchedModificationsSent);
144             sent = executeOperationAsync(batchedModifications);
145
146             if(ready) {
147                 batchedModifications = null;
148             } else {
149                 batchedModifications = newBatchedModifications();
150             }
151         }
152
153         return sent;
154     }
155
156     @Override
157     public void executeModification(AbstractModification modification) {
158         if(LOG.isDebugEnabled()) {
159             LOG.debug("Tx {} executeModification {} called path = {}", getIdentifier(), modification.getClass()
160                     .getSimpleName(), modification.getPath());
161         }
162
163         acquireOperation();
164         batchModification(modification);
165     }
166
167     @Override
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(),
171                     readCmd.getPath());
172         }
173
174         // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the
175         // public API contract.
176
177         acquireOperation();
178         sendBatchedModifications();
179
180         OnComplete<Object> onComplete = new OnComplete<Object>() {
181             @Override
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(),
186                                 failure);
187                     }
188                     returnFuture.setException(new ReadFailedException("Error checking " + readCmd.getClass().getSimpleName()
189                             + " for path " + readCmd.getPath(), failure));
190                 } else {
191                     if(LOG.isDebugEnabled()) {
192                         LOG.debug("Tx {} {} operation succeeded", getIdentifier(), readCmd.getClass().getSimpleName());
193                     }
194                     readCmd.processResponse(response, returnFuture);
195                 }
196             }
197         };
198
199         Future<Object> future = executeOperationAsync(readCmd.asVersion(getTransactionVersion()));
200
201         future.onComplete(onComplete, actorContext.getClientDispatcher());
202     }
203
204     /**
205      * Acquire operation from the limiter if the hand-off has completed. If
206      * the hand-off is still ongoing, this method does nothing.
207      */
208     private final void acquireOperation() {
209         if (isOperationHandOffComplete()) {
210             limiter.acquire();
211         }
212     }
213
214     @Override
215     public boolean usesOperationLimiting() {
216         return true;
217     }
218 }