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.Futures;
13 import akka.dispatch.OnComplete;
14 import com.google.common.base.Preconditions;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.util.Optional;
17 import java.util.SortedSet;
18 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
19 import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
20 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
21 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
22 import org.opendaylight.controller.cluster.datastore.modification.AbstractModification;
23 import org.opendaylight.controller.cluster.datastore.modification.Modification;
24 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import scala.concurrent.Future;
31 * Redirects front-end transaction operations to a shard for processing. Instances of this class are used
32 * when the destination shard is remote to the caller.
34 * @author Thomas Pantelis
36 public class RemoteTransactionContext extends AbstractTransactionContext {
37 private static final Logger LOG = LoggerFactory.getLogger(RemoteTransactionContext.class);
39 private final ActorContext actorContext;
40 private final ActorSelection actor;
41 private final OperationLimiter limiter;
43 private BatchedModifications batchedModifications;
44 private int totalBatchedModificationsSent;
45 private int batchPermits;
48 * We have observed a failed modification batch. This transaction context is effectively doomed, as the backend
49 * does not have a correct view of the world. If this happens, we do not limit operations but rather short-cut them
50 * to a either a no-op (modifications) or a failure (reads). Once the transaction is ready, though, we send the
51 * message to resynchronize with the backend, sharing a 'lost message' failure path.
53 private volatile Throwable failedModification;
55 protected RemoteTransactionContext(final TransactionIdentifier identifier, final ActorSelection actor,
56 final ActorContext actorContext, final short remoteTransactionVersion, final OperationLimiter limiter) {
57 super(identifier, remoteTransactionVersion);
58 this.limiter = Preconditions.checkNotNull(limiter);
60 this.actorContext = actorContext;
63 private ActorSelection getActor() {
67 protected ActorContext getActorContext() {
72 public void closeTransaction() {
73 LOG.debug("Tx {} closeTransaction called", getIdentifier());
74 TransactionContextCleanup.untrack(this);
76 actorContext.sendOperationAsync(getActor(), new CloseTransaction(getTransactionVersion()).toSerializable());
80 public Future<Object> directCommit(final Boolean havePermit) {
81 LOG.debug("Tx {} directCommit called", getIdentifier());
83 // Send the remaining batched modifications, if any, with the ready flag set.
84 bumpPermits(havePermit);
85 return sendBatchedModifications(true, true, Optional.empty());
89 public Future<ActorSelection> readyTransaction(final Boolean havePermit,
90 final Optional<SortedSet<String>> participatingShardNames) {
91 logModificationCount();
93 LOG.debug("Tx {} readyTransaction called", getIdentifier());
95 // Send the remaining batched modifications, if any, with the ready flag set.
97 bumpPermits(havePermit);
98 Future<Object> lastModificationsFuture = sendBatchedModifications(true, false, participatingShardNames);
100 return transformReadyReply(lastModificationsFuture);
103 private void bumpPermits(final Boolean havePermit) {
104 if (Boolean.TRUE.equals(havePermit)) {
109 protected Future<ActorSelection> transformReadyReply(final Future<Object> readyReplyFuture) {
110 // Transform the last reply Future into a Future that returns the cohort actor path from
111 // the last reply message. That's the end result of the ready operation.
113 return TransactionReadyReplyMapper.transform(readyReplyFuture, actorContext, getIdentifier());
116 private BatchedModifications newBatchedModifications() {
117 return new BatchedModifications(getIdentifier(), getTransactionVersion());
120 private void batchModification(final Modification modification, final boolean havePermit) {
121 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, Optional.empty());
142 protected Future<Object> sendBatchedModifications(final boolean ready, final boolean doCommitOnReady,
143 final Optional<SortedSet<String>> participatingShardNames) {
144 Future<Object> sent = null;
145 if (ready || batchedModifications != null && !batchedModifications.getModifications().isEmpty()) {
146 if (batchedModifications == null) {
147 batchedModifications = newBatchedModifications();
150 LOG.debug("Tx {} sending {} batched modifications, ready: {}", getIdentifier(),
151 batchedModifications.getModifications().size(), ready);
153 batchedModifications.setDoCommitOnReady(doCommitOnReady);
154 batchedModifications.setTotalMessagesSent(++totalBatchedModificationsSent);
156 final BatchedModifications toSend = batchedModifications;
157 final int permitsToRelease = batchPermits;
161 batchedModifications.setReady(participatingShardNames);
162 batchedModifications.setDoCommitOnReady(doCommitOnReady);
163 batchedModifications = null;
165 batchedModifications = newBatchedModifications();
167 final Throwable failure = failedModification;
168 if (failure != null) {
169 // We have observed a modification failure, it does not make sense to send this batch. This speeds
170 // up the time when the application could be blocked due to messages timing out and operation
171 // limiter kicking in.
172 LOG.debug("Tx {} modifications previously failed, not sending a non-ready batch", getIdentifier());
173 limiter.release(permitsToRelease);
174 return Futures.failed(failure);
178 sent = actorContext.executeOperationAsync(getActor(), toSend.toSerializable(),
179 actorContext.getTransactionCommitOperationTimeout());
180 sent.onComplete(new OnComplete<Object>() {
182 public void onComplete(final Throwable failure, final Object success) {
183 if (failure != null) {
184 LOG.debug("Tx {} modifications failed", getIdentifier(), failure);
185 failedModification = failure;
187 LOG.debug("Tx {} modifications completed with {}", getIdentifier(), success);
189 limiter.release(permitsToRelease);
191 }, actorContext.getClientDispatcher());
198 public void executeModification(final AbstractModification modification, final Boolean havePermit) {
199 LOG.debug("Tx {} executeModification {} called path = {}", getIdentifier(),
200 modification.getClass().getSimpleName(), modification.getPath());
202 final boolean permitToRelease;
203 if (havePermit == null) {
204 permitToRelease = failedModification == null && acquireOperation();
206 permitToRelease = havePermit.booleanValue();
209 batchModification(modification, permitToRelease);
213 public <T> void executeRead(final AbstractRead<T> readCmd, final SettableFuture<T> returnFuture,
214 final Boolean havePermit) {
215 LOG.debug("Tx {} executeRead {} called path = {}", getIdentifier(), readCmd.getClass().getSimpleName(),
218 final Throwable failure = failedModification;
219 if (failure != null) {
220 // If we know there was a previous modification failure, we must not send a read request, as it risks
221 // returning incorrect data. We check this before acquiring an operation simply because we want the app
222 // to complete this transaction as soon as possible.
223 returnFuture.setException(new ReadFailedException("Previous modification failed, cannot "
224 + readCmd.getClass().getSimpleName() + " for path " + readCmd.getPath(), failure));
228 // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the
229 // public API contract.
231 final boolean permitToRelease = havePermit == null ? acquireOperation() : havePermit.booleanValue();
232 sendBatchedModifications();
234 OnComplete<Object> onComplete = new OnComplete<Object>() {
236 public void onComplete(final Throwable failure, final Object response) {
237 // We have previously acquired an operation, now release it, no matter what happened
238 if (permitToRelease) {
242 if (failure != null) {
243 LOG.debug("Tx {} {} operation failed: {}", getIdentifier(), readCmd.getClass().getSimpleName(),
246 returnFuture.setException(new ReadFailedException("Error checking "
247 + readCmd.getClass().getSimpleName() + " for path " + readCmd.getPath(), failure));
249 LOG.debug("Tx {} {} operation succeeded", getIdentifier(), readCmd.getClass().getSimpleName());
250 readCmd.processResponse(response, returnFuture);
255 final Future<Object> future = actorContext.executeOperationAsync(getActor(),
256 readCmd.asVersion(getTransactionVersion()).toSerializable(), actorContext.getOperationTimeout());
257 future.onComplete(onComplete, actorContext.getClientDispatcher());
261 * Acquire operation from the limiter if the hand-off has completed. If the hand-off is still ongoing, this method
264 * @return True if a permit was successfully acquired, false otherwise
266 private boolean acquireOperation() {
267 Preconditions.checkState(isOperationHandOffComplete(),
268 "Attempted to acquire execute operation permit for transaction %s on actor %s during handoff",
269 getIdentifier(), actor);
271 if (limiter.acquire()) {
275 LOG.warn("Failed to acquire execute operation permit for transaction {} on actor {}", getIdentifier(), actor);
280 public boolean usesOperationLimiting() {