BUG-5280: switch transactionIdentifier
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / RemoteTransactionContextSupport.java
1 /*
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.
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 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.access.concepts.TransactionIdentifier;
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.messages.CreateTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
22 import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
23 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
24 import org.opendaylight.controller.cluster.datastore.utils.TransactionIdentifierUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import scala.concurrent.Future;
28 import scala.concurrent.duration.FiniteDuration;
29
30 /**
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.
34  * <p>
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.
39  */
40 final class RemoteTransactionContextSupport {
41     private static final Logger LOG = LoggerFactory.getLogger(RemoteTransactionContextSupport.class);
42
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;
45
46     private final TransactionProxy parent;
47     private final String shardName;
48
49     /**
50      * The target primary shard.
51      */
52     private volatile PrimaryShardInfo primaryShardInfo;
53
54     /**
55      * The total timeout for creating a tx on the primary shard.
56      */
57     private volatile long totalCreateTxTimeout;
58
59     private final Timeout createTxMessageTimeout;
60
61     private final TransactionContextWrapper transactionContextWrapper;
62
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;
68
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;
73
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);
80     }
81
82     String getShardName() {
83         return shardName;
84     }
85
86     private TransactionType getTransactionType() {
87         return parent.getType();
88     }
89
90     private ActorContext getActorContext() {
91         return parent.getActorContext();
92     }
93
94     private TransactionIdentifier getIdentifier() {
95         return parent.getIdentifier();
96     }
97
98     /**
99      * Sets the target primary shard and initiates a CreateTransaction try.
100      */
101     void setPrimaryShard(PrimaryShardInfo primaryShardInfo) {
102         this.primaryShardInfo = primaryShardInfo;
103
104         if (getTransactionType() == TransactionType.WRITE_ONLY  &&
105                 getActorContext().getDatastoreContext().isWriteOnlyTransactionOptimizationsEnabled()) {
106             ActorSelection primaryShard = primaryShardInfo.getPrimaryShardActor();
107
108             LOG.debug("Tx {} Primary shard {} found - creating WRITE_ONLY transaction context",
109                 getIdentifier(), primaryShard);
110
111             // For write-only Tx's we prepare the transaction modifications directly on the shard actor
112             // to avoid the overhead of creating a separate transaction actor.
113             transactionContextWrapper.executePriorTransactionOperations(createValidTransactionContext(
114                     primaryShard, String.valueOf(primaryShard.path()), primaryShardInfo.getPrimaryShardVersion()));
115         } else {
116             tryCreateTransaction();
117         }
118     }
119
120     /**
121      * @deprecated Temporary utility for extracting transaction chain ID from a {@link TransactionIdentifier}
122      */
123     @Deprecated
124     static String compatTransactionChainId(final TransactionIdentifier txId) {
125         final long historyId = txId.getHistoryId().getHistoryId();
126         return historyId == 0 ? "" : Long.toUnsignedString(historyId);
127     }
128
129     /**
130      * Performs a CreateTransaction try async.
131      */
132     private void tryCreateTransaction() {
133         if(LOG.isDebugEnabled()) {
134             LOG.debug("Tx {} Primary shard {} found - trying create transaction", getIdentifier(),
135                     primaryShardInfo.getPrimaryShardActor());
136         }
137
138         Object serializedCreateMessage = new CreateTransaction(TransactionIdentifierUtils.actorNameFor(getIdentifier()),
139                 getTransactionType().ordinal(), compatTransactionChainId(getIdentifier()),
140                     primaryShardInfo.getPrimaryShardVersion()).toSerializable();
141
142         Future<Object> createTxFuture = getActorContext().executeOperationAsync(
143                 primaryShardInfo.getPrimaryShardActor(), serializedCreateMessage, createTxMessageTimeout);
144
145         createTxFuture.onComplete(new OnComplete<Object>() {
146             @Override
147             public void onComplete(Throwable failure, Object response) {
148                 onCreateTransactionComplete(failure, response);
149             }
150         }, getActorContext().getClientDispatcher());
151     }
152
153     private void tryFindPrimaryShard() {
154         LOG.debug("Tx {} Retrying findPrimaryShardAsync for shard {}", getIdentifier(), shardName);
155
156         this.primaryShardInfo = null;
157         Future<PrimaryShardInfo> findPrimaryFuture = getActorContext().findPrimaryShardAsync(shardName);
158         findPrimaryFuture.onComplete(new OnComplete<PrimaryShardInfo>() {
159             @Override
160             public void onComplete(final Throwable failure, final PrimaryShardInfo primaryShardInfo) {
161                 onFindPrimaryShardComplete(failure, primaryShardInfo);
162             }
163         }, getActorContext().getClientDispatcher());
164     }
165
166     private void onFindPrimaryShardComplete(final Throwable failure, final PrimaryShardInfo primaryShardInfo) {
167         if (failure == null) {
168             this.primaryShardInfo = primaryShardInfo;
169             tryCreateTransaction();
170         } else {
171             LOG.debug("Tx {}: Find primary for shard {} failed", getIdentifier(), shardName, failure);
172
173             onCreateTransactionComplete(failure, null);
174         }
175     }
176
177     private void onCreateTransactionComplete(Throwable failure, Object response) {
178         // An AskTimeoutException will occur if the local shard forwards to an unavailable remote leader or
179         // the cached remote leader actor is no longer available.
180         boolean retryCreateTransaction = primaryShardInfo != null &&
181                 (failure instanceof NoShardLeaderException || failure instanceof AskTimeoutException);
182         if(retryCreateTransaction) {
183             // Schedule a retry unless we're out of retries. Note: totalCreateTxTimeout is volatile as it may
184             // be written by different threads however not concurrently, therefore decrementing it
185             // non-atomically here is ok.
186             if(totalCreateTxTimeout > 0) {
187                 long scheduleInterval = CREATE_TX_TRY_INTERVAL_IN_MS;
188                 if(failure instanceof AskTimeoutException) {
189                     // Since we use the createTxMessageTimeout for the CreateTransaction request and it timed
190                     // out, subtract it from the total timeout. Also since the createTxMessageTimeout period
191                     // has already elapsed, we can immediately schedule the retry (10 ms is virtually immediate).
192                     totalCreateTxTimeout -= createTxMessageTimeout.duration().toMillis();
193                     scheduleInterval = 10;
194                 }
195
196                 totalCreateTxTimeout -= scheduleInterval;
197
198                 LOG.debug("Tx {}: create tx on shard {} failed with exception \"{}\" - scheduling retry in {} ms",
199                         getIdentifier(), shardName, failure, scheduleInterval);
200
201                 getActorContext().getActorSystem().scheduler().scheduleOnce(
202                         FiniteDuration.create(scheduleInterval, TimeUnit.MILLISECONDS),
203                         new Runnable() {
204                             @Override
205                             public void run() {
206                                 tryFindPrimaryShard();
207                             }
208                         }, getActorContext().getClientDispatcher());
209                 return;
210             }
211         }
212
213         createTransactionContext(failure, response);
214     }
215
216     private void createTransactionContext(Throwable failure, Object response) {
217         // Create the TransactionContext from the response or failure. Store the new
218         // TransactionContext locally until we've completed invoking the
219         // TransactionOperations. This avoids thread timing issues which could cause
220         // out-of-order TransactionOperations. Eg, on a modification operation, if the
221         // TransactionContext is non-null, then we directly call the TransactionContext.
222         // However, at the same time, the code may be executing the cached
223         // TransactionOperations. So to avoid thus timing, we don't publish the
224         // TransactionContext until after we've executed all cached TransactionOperations.
225         TransactionContext localTransactionContext;
226         if(failure != null) {
227             LOG.debug("Tx {} Creating NoOpTransaction because of error", getIdentifier(), failure);
228
229             Throwable resultingEx = failure;
230             if(failure instanceof AskTimeoutException) {
231                 resultingEx = new ShardLeaderNotRespondingException(String.format(
232                         "Could not create a %s transaction on shard %s. The shard leader isn't responding.",
233                         parent.getType(), shardName), failure);
234             } else if(!(failure instanceof NoShardLeaderException)) {
235                 resultingEx = new Exception(String.format(
236                     "Error creating %s transaction on shard %s", parent.getType(), shardName), failure);
237             }
238
239             localTransactionContext = new NoOpTransactionContext(resultingEx, getIdentifier());
240         } else if (CreateTransactionReply.isSerializedType(response)) {
241             localTransactionContext = createValidTransactionContext(
242                     CreateTransactionReply.fromSerializable(response));
243         } else {
244             IllegalArgumentException exception = new IllegalArgumentException(String.format(
245                     "Invalid reply type %s for CreateTransaction", response.getClass()));
246
247             localTransactionContext = new NoOpTransactionContext(exception, getIdentifier());
248         }
249
250         transactionContextWrapper.executePriorTransactionOperations(localTransactionContext);
251     }
252
253     private TransactionContext createValidTransactionContext(CreateTransactionReply reply) {
254         LOG.debug("Tx {} Received {}", getIdentifier(), reply);
255
256         return createValidTransactionContext(getActorContext().actorSelection(reply.getTransactionPath()),
257                 reply.getTransactionPath(), primaryShardInfo.getPrimaryShardVersion());
258     }
259
260     private TransactionContext createValidTransactionContext(ActorSelection transactionActor, String transactionPath,
261             short remoteTransactionVersion) {
262         final TransactionContext ret = new RemoteTransactionContext(transactionContextWrapper.getIdentifier(),
263                 transactionActor, getActorContext(), remoteTransactionVersion, transactionContextWrapper.getLimiter());
264
265         if(parent.getType() == TransactionType.READ_ONLY) {
266             TransactionContextCleanup.track(parent, ret);
267         }
268
269         return ret;
270     }
271 }
272