2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
3 * Copyright (c) 2015 Cisco 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 akka.pattern.AskTimeoutException;
14 import akka.util.Timeout;
15 import com.google.common.base.Preconditions;
16 import java.util.concurrent.TimeUnit;
17 import org.opendaylight.controller.cluster.datastore.compat.PreLithiumTransactionContextImpl;
18 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
19 import org.opendaylight.controller.cluster.datastore.exceptions.ShardLeaderNotRespondingException;
20 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
21 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
22 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
23 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
24 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import scala.concurrent.Future;
28 import scala.concurrent.duration.FiniteDuration;
31 * Handles creation of TransactionContext instances for remote transactions. This class creates
32 * remote transactions, if necessary, by sending CreateTransaction messages with retries, up to a limit,
33 * if the shard doesn't have a leader yet. This is done by scheduling a retry task after a short delay.
35 * The end result from a completed CreateTransaction message is a TransactionContext that is
36 * used to perform transaction operations. Transaction operations that occur before the
37 * CreateTransaction completes are cache via a TransactionContextWrapper and executed once the
38 * CreateTransaction completes, successfully or not.
40 final class RemoteTransactionContextSupport {
41 private static final Logger LOG = LoggerFactory.getLogger(RemoteTransactionContextSupport.class);
43 private static final long CREATE_TX_TRY_INTERVAL_IN_MS = 1000;
44 private static final long MAX_CREATE_TX_MSG_TIMEOUT_IN_MS = 5000;
46 private final TransactionProxy parent;
47 private final String shardName;
50 * The target primary shard.
52 private volatile ActorSelection primaryShard;
55 * The total timeout for creating a tx on the primary shard.
57 private volatile long totalCreateTxTimeout;
59 private final Timeout createTxMessageTimeout;
61 private final TransactionContextWrapper transactionContextWrapper;
63 RemoteTransactionContextSupport(final TransactionContextWrapper transactionContextWrapper, final TransactionProxy parent,
64 final String shardName) {
65 this.parent = Preconditions.checkNotNull(parent);
66 this.shardName = shardName;
67 this.transactionContextWrapper = transactionContextWrapper;
69 // For the total create tx timeout, use 2 times the election timeout. This should be enough time for
70 // a leader re-election to occur if we happen to hit it in transition.
71 totalCreateTxTimeout = parent.getActorContext().getDatastoreContext().getShardRaftConfig()
72 .getElectionTimeOutInterval().toMillis() * 2;
74 // We'll use the operationTimeout for the the create Tx message timeout so it can be set appropriately
75 // for unit tests but cap it at MAX_CREATE_TX_MSG_TIMEOUT_IN_MS. The operationTimeout could be set
76 // larger than the totalCreateTxTimeout in production which we don't want.
77 long operationTimeout = parent.getActorContext().getOperationTimeout().duration().toMillis();
78 createTxMessageTimeout = new Timeout(Math.min(operationTimeout, MAX_CREATE_TX_MSG_TIMEOUT_IN_MS),
79 TimeUnit.MILLISECONDS);
82 String getShardName() {
86 private TransactionType getTransactionType() {
87 return parent.getType();
90 private ActorContext getActorContext() {
91 return parent.getActorContext();
94 private TransactionIdentifier getIdentifier() {
95 return parent.getIdentifier();
99 * Sets the target primary shard and initiates a CreateTransaction try.
101 void setPrimaryShard(ActorSelection primaryShard, short primaryVersion) {
102 this.primaryShard = primaryShard;
104 if (getTransactionType() == TransactionType.WRITE_ONLY && primaryVersion >= DataStoreVersions.LITHIUM_VERSION &&
105 getActorContext().getDatastoreContext().isWriteOnlyTransactionOptimizationsEnabled()) {
106 LOG.debug("Tx {} Primary shard {} found - creating WRITE_ONLY transaction context",
107 getIdentifier(), primaryShard);
109 // For write-only Tx's we prepare the transaction modifications directly on the shard actor
110 // to avoid the overhead of creating a separate transaction actor.
111 transactionContextWrapper.executePriorTransactionOperations(createValidTransactionContext(this.primaryShard,
112 this.primaryShard.path().toString(), primaryVersion));
114 tryCreateTransaction();
119 * Performs a CreateTransaction try async.
121 private void tryCreateTransaction() {
122 if(LOG.isDebugEnabled()) {
123 LOG.debug("Tx {} Primary shard {} found - trying create transaction", getIdentifier(), primaryShard);
126 Object serializedCreateMessage = new CreateTransaction(getIdentifier().toString(),
127 getTransactionType().ordinal(), getIdentifier().getChainId()).toSerializable();
129 Future<Object> createTxFuture = getActorContext().executeOperationAsync(primaryShard,
130 serializedCreateMessage, createTxMessageTimeout);
132 createTxFuture.onComplete(new OnComplete<Object>() {
134 public void onComplete(Throwable failure, Object response) {
135 onCreateTransactionComplete(failure, response);
137 }, getActorContext().getClientDispatcher());
140 private void tryFindPrimaryShard() {
141 LOG.debug("Tx {} Retrying findPrimaryShardAsync for shard {}", getIdentifier(), shardName);
143 this.primaryShard = null;
144 Future<PrimaryShardInfo> findPrimaryFuture = getActorContext().findPrimaryShardAsync(shardName);
145 findPrimaryFuture.onComplete(new OnComplete<PrimaryShardInfo>() {
147 public void onComplete(final Throwable failure, final PrimaryShardInfo primaryShardInfo) {
148 onFindPrimaryShardComplete(failure, primaryShardInfo);
150 }, getActorContext().getClientDispatcher());
153 private void onFindPrimaryShardComplete(final Throwable failure, final PrimaryShardInfo primaryShardInfo) {
154 if (failure == null) {
155 this.primaryShard = primaryShardInfo.getPrimaryShardActor();
156 tryCreateTransaction();
158 LOG.debug("Tx {}: Find primary for shard {} failed", getIdentifier(), shardName, failure);
160 onCreateTransactionComplete(failure, null);
164 private void onCreateTransactionComplete(Throwable failure, Object response) {
165 // An AskTimeoutException will occur if the local shard forwards to an unavailable remote leader or
166 // the cached remote leader actor is no longer available.
167 boolean retryCreateTransaction = this.primaryShard != null &&
168 (failure instanceof NoShardLeaderException || failure instanceof AskTimeoutException);
169 if(retryCreateTransaction) {
170 // Schedule a retry unless we're out of retries. Note: totalCreateTxTimeout is volatile as it may
171 // be written by different threads however not concurrently, therefore decrementing it
172 // non-atomically here is ok.
173 if(totalCreateTxTimeout > 0) {
174 long scheduleInterval = CREATE_TX_TRY_INTERVAL_IN_MS;
175 if(failure instanceof AskTimeoutException) {
176 // Since we use the createTxMessageTimeout for the CreateTransaction request and it timed
177 // out, subtract it from the total timeout. Also since the createTxMessageTimeout period
178 // has already elapsed, we can immediately schedule the retry (10 ms is virtually immediate).
179 totalCreateTxTimeout -= createTxMessageTimeout.duration().toMillis();
180 scheduleInterval = 10;
183 totalCreateTxTimeout -= scheduleInterval;
185 LOG.debug("Tx {}: create tx on shard {} failed with exception \"{}\" - scheduling retry in {} ms",
186 getIdentifier(), shardName, failure, scheduleInterval);
188 getActorContext().getActorSystem().scheduler().scheduleOnce(
189 FiniteDuration.create(scheduleInterval, TimeUnit.MILLISECONDS),
193 tryFindPrimaryShard();
195 }, getActorContext().getClientDispatcher());
200 createTransactionContext(failure, response);
203 private void createTransactionContext(Throwable failure, Object response) {
204 // Create the TransactionContext from the response or failure. Store the new
205 // TransactionContext locally until we've completed invoking the
206 // TransactionOperations. This avoids thread timing issues which could cause
207 // out-of-order TransactionOperations. Eg, on a modification operation, if the
208 // TransactionContext is non-null, then we directly call the TransactionContext.
209 // However, at the same time, the code may be executing the cached
210 // TransactionOperations. So to avoid thus timing, we don't publish the
211 // TransactionContext until after we've executed all cached TransactionOperations.
212 TransactionContext localTransactionContext;
213 if(failure != null) {
214 LOG.debug("Tx {} Creating NoOpTransaction because of error", getIdentifier(), failure);
216 Throwable resultingEx = failure;
217 if(failure instanceof AskTimeoutException) {
218 resultingEx = new ShardLeaderNotRespondingException(String.format(
219 "Could not create a %s transaction on shard %s. The shard leader isn't responding.",
220 parent.getType(), shardName), failure);
221 } else if(!(failure instanceof NoShardLeaderException)) {
222 resultingEx = new Exception(String.format(
223 "Error creating %s transaction on shard %s", parent.getType(), shardName), failure);
226 localTransactionContext = new NoOpTransactionContext(resultingEx, getIdentifier());
227 } else if (CreateTransactionReply.SERIALIZABLE_CLASS.equals(response.getClass())) {
228 localTransactionContext = createValidTransactionContext(
229 CreateTransactionReply.fromSerializable(response));
231 IllegalArgumentException exception = new IllegalArgumentException(String.format(
232 "Invalid reply type %s for CreateTransaction", response.getClass()));
234 localTransactionContext = new NoOpTransactionContext(exception, getIdentifier());
237 transactionContextWrapper.executePriorTransactionOperations(localTransactionContext);
240 private TransactionContext createValidTransactionContext(CreateTransactionReply reply) {
241 LOG.debug("Tx {} Received {}", getIdentifier(), reply);
243 return createValidTransactionContext(getActorContext().actorSelection(reply.getTransactionPath()),
244 reply.getTransactionPath(), reply.getVersion());
247 private TransactionContext createValidTransactionContext(ActorSelection transactionActor, String transactionPath,
248 short remoteTransactionVersion) {
249 // TxActor is always created where the leader of the shard is.
250 // Check if TxActor is created in the same node
251 boolean isTxActorLocal = getActorContext().isPathLocal(transactionPath);
252 final TransactionContext ret;
254 if (remoteTransactionVersion < DataStoreVersions.LITHIUM_VERSION) {
255 ret = new PreLithiumTransactionContextImpl(transactionContextWrapper.getIdentifier(), transactionPath, transactionActor,
256 getActorContext(), isTxActorLocal, remoteTransactionVersion, transactionContextWrapper.getLimiter());
258 ret = new RemoteTransactionContext(transactionContextWrapper.getIdentifier(), transactionActor, getActorContext(),
259 isTxActorLocal, remoteTransactionVersion, transactionContextWrapper.getLimiter());
262 if(parent.getType() == TransactionType.READ_ONLY) {
263 TransactionContextCleanup.track(this, ret);