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 static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
14 import akka.actor.ActorSelection;
15 import akka.dispatch.Futures;
16 import akka.dispatch.OnComplete;
17 import com.google.common.util.concurrent.SettableFuture;
18 import java.util.Optional;
19 import java.util.SortedSet;
20 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
21 import org.opendaylight.controller.cluster.datastore.messages.AbstractRead;
22 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
23 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
24 import org.opendaylight.controller.cluster.datastore.modification.AbstractModification;
25 import org.opendaylight.controller.cluster.datastore.modification.Modification;
26 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
27 import org.opendaylight.mdsal.common.api.ReadFailedException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import scala.concurrent.Future;
33 * Redirects front-end transaction operations to a shard for processing. Instances of this class are used
34 * when the destination shard is remote to the caller.
36 * @author Thomas Pantelis
38 public class RemoteTransactionContext extends AbstractTransactionContext {
39 private static final Logger LOG = LoggerFactory.getLogger(RemoteTransactionContext.class);
41 private final ActorUtils actorUtils;
42 private final ActorSelection actor;
43 private final OperationLimiter limiter;
45 private BatchedModifications batchedModifications;
46 private int totalBatchedModificationsSent;
47 private int batchPermits;
50 * We have observed a failed modification batch. This transaction context is effectively doomed, as the backend
51 * does not have a correct view of the world. If this happens, we do not limit operations but rather short-cut them
52 * to a either a no-op (modifications) or a failure (reads). Once the transaction is ready, though, we send the
53 * message to resynchronize with the backend, sharing a 'lost message' failure path.
55 private volatile Throwable failedModification;
57 protected RemoteTransactionContext(final TransactionIdentifier identifier, final ActorSelection actor,
58 final ActorUtils actorUtils, final short remoteTransactionVersion, final OperationLimiter limiter) {
59 super(identifier, remoteTransactionVersion);
60 this.limiter = requireNonNull(limiter);
62 this.actorUtils = actorUtils;
65 private ActorSelection getActor() {
69 protected ActorUtils getActorUtils() {
74 public void closeTransaction() {
75 LOG.debug("Tx {} closeTransaction called", getIdentifier());
76 TransactionContextCleanup.untrack(this);
78 actorUtils.sendOperationAsync(getActor(), new CloseTransaction(getTransactionVersion()).toSerializable());
82 public Future<Object> directCommit(final Boolean havePermit) {
83 LOG.debug("Tx {} directCommit called", getIdentifier());
85 // Send the remaining batched modifications, if any, with the ready flag set.
86 bumpPermits(havePermit);
87 return sendBatchedModifications(true, true, Optional.empty());
91 public Future<ActorSelection> readyTransaction(final Boolean havePermit,
92 final Optional<SortedSet<String>> participatingShardNames) {
93 logModificationCount();
95 LOG.debug("Tx {} readyTransaction called", getIdentifier());
97 // Send the remaining batched modifications, if any, with the ready flag set.
99 bumpPermits(havePermit);
100 Future<Object> lastModificationsFuture = sendBatchedModifications(true, false, participatingShardNames);
102 return transformReadyReply(lastModificationsFuture);
105 private void bumpPermits(final Boolean havePermit) {
106 if (Boolean.TRUE.equals(havePermit)) {
111 protected Future<ActorSelection> transformReadyReply(final Future<Object> readyReplyFuture) {
112 // Transform the last reply Future into a Future that returns the cohort actor path from
113 // the last reply message. That's the end result of the ready operation.
115 return TransactionReadyReplyMapper.transform(readyReplyFuture, actorUtils, getIdentifier());
118 private BatchedModifications newBatchedModifications() {
119 return new BatchedModifications(getIdentifier(), getTransactionVersion());
122 private void batchModification(final Modification modification, final boolean havePermit) {
123 incrementModificationCount();
128 if (batchedModifications == null) {
129 batchedModifications = newBatchedModifications();
132 batchedModifications.addModification(modification);
134 if (batchedModifications.getModifications().size()
135 >= actorUtils.getDatastoreContext().getShardBatchedModificationCount()) {
136 sendBatchedModifications();
140 protected Future<Object> sendBatchedModifications() {
141 return sendBatchedModifications(false, false, Optional.empty());
144 protected Future<Object> sendBatchedModifications(final boolean ready, final boolean doCommitOnReady,
145 final Optional<SortedSet<String>> participatingShardNames) {
146 Future<Object> sent = null;
147 if (ready || batchedModifications != null && !batchedModifications.getModifications().isEmpty()) {
148 if (batchedModifications == null) {
149 batchedModifications = newBatchedModifications();
152 LOG.debug("Tx {} sending {} batched modifications, ready: {}", getIdentifier(),
153 batchedModifications.getModifications().size(), ready);
155 batchedModifications.setDoCommitOnReady(doCommitOnReady);
156 batchedModifications.setTotalMessagesSent(++totalBatchedModificationsSent);
158 final BatchedModifications toSend = batchedModifications;
159 final int permitsToRelease = batchPermits;
163 batchedModifications.setReady(participatingShardNames);
164 batchedModifications.setDoCommitOnReady(doCommitOnReady);
165 batchedModifications = null;
167 batchedModifications = newBatchedModifications();
169 final Throwable failure = failedModification;
170 if (failure != null) {
171 // We have observed a modification failure, it does not make sense to send this batch. This speeds
172 // up the time when the application could be blocked due to messages timing out and operation
173 // limiter kicking in.
174 LOG.debug("Tx {} modifications previously failed, not sending a non-ready batch", getIdentifier());
175 limiter.release(permitsToRelease);
176 return Futures.failed(failure);
180 sent = actorUtils.executeOperationAsync(getActor(), toSend.toSerializable(),
181 actorUtils.getTransactionCommitOperationTimeout());
182 sent.onComplete(new OnComplete<Object>() {
184 public void onComplete(final Throwable failure, final Object success) {
185 if (failure != null) {
186 LOG.debug("Tx {} modifications failed", getIdentifier(), failure);
187 failedModification = failure;
189 LOG.debug("Tx {} modifications completed with {}", getIdentifier(), success);
191 limiter.release(permitsToRelease);
193 }, actorUtils.getClientDispatcher());
200 public void executeModification(final AbstractModification modification, final Boolean havePermit) {
201 LOG.debug("Tx {} executeModification {} called path = {}", getIdentifier(),
202 modification.getClass().getSimpleName(), modification.getPath());
204 final boolean permitToRelease;
205 if (havePermit == null) {
206 permitToRelease = failedModification == null && acquireOperation();
208 permitToRelease = havePermit.booleanValue();
211 batchModification(modification, permitToRelease);
215 public <T> void executeRead(final AbstractRead<T> readCmd, final SettableFuture<T> returnFuture,
216 final Boolean havePermit) {
217 LOG.debug("Tx {} executeRead {} called path = {}", getIdentifier(), readCmd.getClass().getSimpleName(),
220 final Throwable failure = failedModification;
221 if (failure != null) {
222 // If we know there was a previous modification failure, we must not send a read request, as it risks
223 // returning incorrect data. We check this before acquiring an operation simply because we want the app
224 // to complete this transaction as soon as possible.
225 returnFuture.setException(new ReadFailedException("Previous modification failed, cannot "
226 + readCmd.getClass().getSimpleName() + " for path " + readCmd.getPath(), failure));
230 // Send any batched modifications. This is necessary to honor the read uncommitted semantics of the
231 // public API contract.
233 final boolean permitToRelease = havePermit == null ? acquireOperation() : havePermit.booleanValue();
234 sendBatchedModifications();
236 OnComplete<Object> onComplete = new OnComplete<Object>() {
238 public void onComplete(final Throwable failure, final Object response) {
239 // We have previously acquired an operation, now release it, no matter what happened
240 if (permitToRelease) {
244 if (failure != null) {
245 LOG.debug("Tx {} {} operation failed", getIdentifier(), readCmd.getClass().getSimpleName(),
248 returnFuture.setException(new ReadFailedException("Error checking "
249 + readCmd.getClass().getSimpleName() + " for path " + readCmd.getPath(), failure));
251 LOG.debug("Tx {} {} operation succeeded", getIdentifier(), readCmd.getClass().getSimpleName());
252 readCmd.processResponse(response, returnFuture);
257 final Future<Object> future = actorUtils.executeOperationAsync(getActor(),
258 readCmd.asVersion(getTransactionVersion()).toSerializable(), actorUtils.getOperationTimeout());
259 future.onComplete(onComplete, actorUtils.getClientDispatcher());
263 * Acquire operation from the limiter if the hand-off has completed. If the hand-off is still ongoing, this method
266 * @return True if a permit was successfully acquired, false otherwise
268 private boolean acquireOperation() {
269 checkState(isOperationHandOffComplete(),
270 "Attempted to acquire execute operation permit for transaction %s on actor %s during handoff",
271 getIdentifier(), actor);
273 if (limiter.acquire()) {
277 LOG.warn("Failed to acquire execute operation permit for transaction {} on actor {}", getIdentifier(), actor);
282 public boolean usesOperationLimiting() {