2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.datastore;
10 import akka.actor.ActorSelection;
11 import akka.dispatch.OnComplete;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Lists;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.concurrent.Semaphore;
18 import java.util.concurrent.TimeUnit;
19 import javax.annotation.concurrent.GuardedBy;
20 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
21 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
22 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
23 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
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 * Implements a Future OnComplete callback for a CreateTransaction message. This class handles
32 * retries, up to a limit, if the shard doesn't have a leader yet. This is done by scheduling a
33 * 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 and executed once the CreateTransaction completes,
38 * successfully or not.
40 final class TransactionFutureCallback extends OnComplete<Object> {
41 private static final Logger LOG = LoggerFactory.getLogger(TransactionFutureCallback.class);
44 * Time interval in between transaction create retries.
46 private static final FiniteDuration CREATE_TX_TRY_INTERVAL = FiniteDuration.create(1, TimeUnit.SECONDS);
49 * The list of transaction operations to execute once the CreateTransaction completes.
51 @GuardedBy("txOperationsOnComplete")
52 private final List<TransactionOperation> txOperationsOnComplete = Lists.newArrayList();
53 private final TransactionProxy proxy;
54 private final String shardName;
57 * The TransactionContext resulting from the CreateTransaction reply.
59 private volatile TransactionContext transactionContext;
62 * The target primary shard.
64 private volatile ActorSelection primaryShard;
65 private volatile int createTxTries;
67 TransactionFutureCallback(final TransactionProxy proxy, final String shardName) {
68 this.proxy = Preconditions.checkNotNull(proxy);
69 this.shardName = shardName;
70 createTxTries = (int) (proxy.getActorContext().getDatastoreContext().
71 getShardLeaderElectionTimeout().duration().toMillis() /
72 CREATE_TX_TRY_INTERVAL.toMillis());
75 String getShardName() {
79 TransactionContext getTransactionContext() {
80 return transactionContext;
83 private TransactionType getTransactionType() {
84 return proxy.getTransactionType();
87 private TransactionIdentifier getIdentifier() {
88 return proxy.getIdentifier();
91 private ActorContext getActorContext() {
92 return proxy.getActorContext();
95 private Semaphore getOperationLimiter() {
96 return proxy.getOperationLimiter();
100 * Sets the target primary shard and initiates a CreateTransaction try.
102 void setPrimaryShard(ActorSelection primaryShard) {
103 this.primaryShard = primaryShard;
105 if (getTransactionType() == TransactionType.WRITE_ONLY &&
106 getActorContext().getDatastoreContext().isWriteOnlyTransactionOptimizationsEnabled()) {
107 LOG.debug("Tx {} Primary shard {} found - creating WRITE_ONLY transaction context",
108 getIdentifier(), primaryShard);
110 // For write-only Tx's we prepare the transaction modifications directly on the shard actor
111 // to avoid the overhead of creating a separate transaction actor.
112 // FIXME: can't assume the shard version is LITHIUM_VERSION - need to obtain it somehow.
113 executeTxOperatonsOnComplete(proxy.createValidTransactionContext(this.primaryShard,
114 this.primaryShard.path().toString(), DataStoreVersions.LITHIUM_VERSION));
116 tryCreateTransaction();
121 * Adds a TransactionOperation to be executed after the CreateTransaction completes.
123 private void addTxOperationOnComplete(TransactionOperation operation) {
124 boolean invokeOperation = true;
125 synchronized(txOperationsOnComplete) {
126 if(transactionContext == null) {
127 LOG.debug("Tx {} Adding operation on complete", getIdentifier());
129 invokeOperation = false;
130 txOperationsOnComplete.add(operation);
134 if(invokeOperation) {
135 operation.invoke(transactionContext);
139 void enqueueTransactionOperation(final TransactionOperation op) {
141 if (transactionContext != null) {
142 op.invoke(transactionContext);
144 // The shard Tx hasn't been created yet so add the Tx operation to the Tx Future
145 // callback to be executed after the Tx is created.
146 addTxOperationOnComplete(op);
151 * Performs a CreateTransaction try async.
153 private void tryCreateTransaction() {
154 if(LOG.isDebugEnabled()) {
155 LOG.debug("Tx {} Primary shard {} found - trying create transaction", getIdentifier(), primaryShard);
158 Object serializedCreateMessage = new CreateTransaction(getIdentifier().toString(),
159 getTransactionType().ordinal(), proxy.getTransactionChainId()).toSerializable();
161 Future<Object> createTxFuture = getActorContext().executeOperationAsync(primaryShard, serializedCreateMessage);
163 createTxFuture.onComplete(this, getActorContext().getClientDispatcher());
167 public void onComplete(Throwable failure, Object response) {
168 if(failure instanceof NoShardLeaderException) {
169 // There's no leader for the shard yet - schedule and try again, unless we're out
170 // of retries. Note: createTxTries is volatile as it may be written by different
171 // threads however not concurrently, therefore decrementing it non-atomically here
173 if(--createTxTries > 0) {
174 LOG.debug("Tx {} Shard {} has no leader yet - scheduling create Tx retry",
175 getIdentifier(), shardName);
177 getActorContext().getActorSystem().scheduler().scheduleOnce(CREATE_TX_TRY_INTERVAL,
181 tryCreateTransaction();
183 }, getActorContext().getClientDispatcher());
188 createTransactionContext(failure, response);
191 void createTransactionContext(Throwable failure, Object response) {
192 // Mainly checking for state violation here to perform a volatile read of "initialized" to
193 // ensure updates to operationLimter et al are visible to this thread (ie we're doing
194 // "piggy-back" synchronization here).
195 proxy.ensureInitializied();
197 // Create the TransactionContext from the response or failure. Store the new
198 // TransactionContext locally until we've completed invoking the
199 // TransactionOperations. This avoids thread timing issues which could cause
200 // out-of-order TransactionOperations. Eg, on a modification operation, if the
201 // TransactionContext is non-null, then we directly call the TransactionContext.
202 // However, at the same time, the code may be executing the cached
203 // TransactionOperations. So to avoid thus timing, we don't publish the
204 // TransactionContext until after we've executed all cached TransactionOperations.
205 TransactionContext localTransactionContext;
206 if(failure != null) {
207 LOG.debug("Tx {} Creating NoOpTransaction because of error", getIdentifier(), failure);
209 localTransactionContext = new NoOpTransactionContext(failure, getIdentifier(), getOperationLimiter());
210 } else if (CreateTransactionReply.SERIALIZABLE_CLASS.equals(response.getClass())) {
211 localTransactionContext = createValidTransactionContext(
212 CreateTransactionReply.fromSerializable(response));
214 IllegalArgumentException exception = new IllegalArgumentException(String.format(
215 "Invalid reply type %s for CreateTransaction", response.getClass()));
217 localTransactionContext = new NoOpTransactionContext(exception, getIdentifier(), getOperationLimiter());
220 executeTxOperatonsOnComplete(localTransactionContext);
223 private void executeTxOperatonsOnComplete(TransactionContext localTransactionContext) {
225 // Access to txOperationsOnComplete and transactionContext must be protected and atomic
226 // (ie synchronized) with respect to #addTxOperationOnComplete to handle timing
227 // issues and ensure no TransactionOperation is missed and that they are processed
228 // in the order they occurred.
230 // We'll make a local copy of the txOperationsOnComplete list to handle re-entrancy
231 // in case a TransactionOperation results in another transaction operation being
232 // queued (eg a put operation from a client read Future callback that is notified
234 Collection<TransactionOperation> operationsBatch = null;
235 synchronized(txOperationsOnComplete) {
236 if(txOperationsOnComplete.isEmpty()) {
237 // We're done invoking the TransactionOperations so we can now publish the
238 // TransactionContext.
239 transactionContext = localTransactionContext;
243 operationsBatch = new ArrayList<>(txOperationsOnComplete);
244 txOperationsOnComplete.clear();
247 // Invoke TransactionOperations outside the sync block to avoid unnecessary blocking.
248 // A slight down-side is that we need to re-acquire the lock below but this should
250 for(TransactionOperation oper: operationsBatch) {
251 oper.invoke(localTransactionContext);
256 private TransactionContext createValidTransactionContext(CreateTransactionReply reply) {
257 LOG.debug("Tx {} Received {}", getIdentifier(), reply);
259 return proxy.createValidTransactionContext(getActorContext().actorSelection(reply.getTransactionPath()),
260 reply.getTransactionPath(), reply.getVersion());