BUG 3019 : Fix Operation throttling for modification batching scenarios
[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 com.google.common.base.Preconditions;
14 import java.util.concurrent.TimeUnit;
15 import org.opendaylight.controller.cluster.datastore.compat.PreLithiumTransactionContextImpl;
16 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
17 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
18 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
19 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
20 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import scala.concurrent.Future;
24 import scala.concurrent.duration.FiniteDuration;
25
26 /**
27  * Handles creation of TransactionContext instances for remote transactions. This class creates
28  * remote transactions, if necessary, by sending CreateTransaction messages with retries, up to a limit,
29  * if the shard doesn't have a leader yet. This is done by scheduling a retry task after a short delay.
30  * <p>
31  * The end result from a completed CreateTransaction message is a TransactionContext that is
32  * used to perform transaction operations. Transaction operations that occur before the
33  * CreateTransaction completes are cache via a TransactionContextWrapper and executed once the
34  * CreateTransaction completes, successfully or not.
35  */
36 final class RemoteTransactionContextSupport {
37     private static final Logger LOG = LoggerFactory.getLogger(RemoteTransactionContextSupport.class);
38
39     /**
40      * Time interval in between transaction create retries.
41      */
42     private static final FiniteDuration CREATE_TX_TRY_INTERVAL = FiniteDuration.create(1, TimeUnit.SECONDS);
43
44     private final TransactionProxy parent;
45     private final String shardName;
46
47     /**
48      * The target primary shard.
49      */
50     private volatile ActorSelection primaryShard;
51     private volatile int createTxTries;
52
53     private final TransactionContextWrapper transactionContextAdapter;
54
55     RemoteTransactionContextSupport(final TransactionContextWrapper transactionContextAdapter, final TransactionProxy parent,
56             final String shardName) {
57         this.parent = Preconditions.checkNotNull(parent);
58         this.shardName = shardName;
59         this.transactionContextAdapter = transactionContextAdapter;
60         createTxTries = (int) (parent.getActorContext().getDatastoreContext().
61                 getShardLeaderElectionTimeout().duration().toMillis() /
62                 CREATE_TX_TRY_INTERVAL.toMillis());
63     }
64
65     String getShardName() {
66         return shardName;
67     }
68
69     private TransactionType getTransactionType() {
70         return parent.getType();
71     }
72
73     private ActorContext getActorContext() {
74         return parent.getActorContext();
75     }
76
77     private OperationLimiter getOperationLimiter() {
78         return transactionContextAdapter.getLimiter();
79     }
80
81     private TransactionIdentifier getIdentifier() {
82         return parent.getIdentifier();
83     }
84
85     /**
86      * Sets the target primary shard and initiates a CreateTransaction try.
87      */
88     void setPrimaryShard(ActorSelection primaryShard, short primaryVersion) {
89         this.primaryShard = primaryShard;
90
91         if (getTransactionType() == TransactionType.WRITE_ONLY && primaryVersion >= DataStoreVersions.LITHIUM_VERSION &&
92                 getActorContext().getDatastoreContext().isWriteOnlyTransactionOptimizationsEnabled()) {
93             LOG.debug("Tx {} Primary shard {} found - creating WRITE_ONLY transaction context",
94                 getIdentifier(), primaryShard);
95
96             // For write-only Tx's we prepare the transaction modifications directly on the shard actor
97             // to avoid the overhead of creating a separate transaction actor.
98             transactionContextAdapter.executePriorTransactionOperations(createValidTransactionContext(this.primaryShard,
99                     this.primaryShard.path().toString(), primaryVersion));
100         } else {
101             tryCreateTransaction();
102         }
103     }
104
105     /**
106      * Performs a CreateTransaction try async.
107      */
108     private void tryCreateTransaction() {
109         if(LOG.isDebugEnabled()) {
110             LOG.debug("Tx {} Primary shard {} found - trying create transaction", getIdentifier(), primaryShard);
111         }
112
113         Object serializedCreateMessage = new CreateTransaction(getIdentifier().toString(),
114             getTransactionType().ordinal(), getIdentifier().getChainId()).toSerializable();
115
116         Future<Object> createTxFuture = getActorContext().executeOperationAsync(primaryShard, serializedCreateMessage);
117
118         createTxFuture.onComplete(new OnComplete<Object>() {
119             @Override
120             public void onComplete(Throwable failure, Object response) {
121                 onCreateTransactionComplete(failure, response);
122             }
123         }, getActorContext().getClientDispatcher());
124     }
125
126     private void onCreateTransactionComplete(Throwable failure, Object response) {
127         if(failure instanceof NoShardLeaderException) {
128             // There's no leader for the shard yet - schedule and try again, unless we're out
129             // of retries. Note: createTxTries is volatile as it may be written by different
130             // threads however not concurrently, therefore decrementing it non-atomically here
131             // is ok.
132             if(--createTxTries > 0) {
133                 LOG.debug("Tx {} Shard {} has no leader yet - scheduling create Tx retry",
134                     getIdentifier(), shardName);
135
136                 getActorContext().getActorSystem().scheduler().scheduleOnce(CREATE_TX_TRY_INTERVAL,
137                         new Runnable() {
138                             @Override
139                             public void run() {
140                                 tryCreateTransaction();
141                             }
142                         }, getActorContext().getClientDispatcher());
143                 return;
144             }
145         }
146
147         createTransactionContext(failure, response);
148     }
149
150     private void createTransactionContext(Throwable failure, Object response) {
151         // Create the TransactionContext from the response or failure. Store the new
152         // TransactionContext locally until we've completed invoking the
153         // TransactionOperations. This avoids thread timing issues which could cause
154         // out-of-order TransactionOperations. Eg, on a modification operation, if the
155         // TransactionContext is non-null, then we directly call the TransactionContext.
156         // However, at the same time, the code may be executing the cached
157         // TransactionOperations. So to avoid thus timing, we don't publish the
158         // TransactionContext until after we've executed all cached TransactionOperations.
159         TransactionContext localTransactionContext;
160         if(failure != null) {
161             LOG.debug("Tx {} Creating NoOpTransaction because of error", getIdentifier(), failure);
162
163             localTransactionContext = new NoOpTransactionContext(failure, getIdentifier());
164         } else if (CreateTransactionReply.SERIALIZABLE_CLASS.equals(response.getClass())) {
165             localTransactionContext = createValidTransactionContext(
166                     CreateTransactionReply.fromSerializable(response));
167         } else {
168             IllegalArgumentException exception = new IllegalArgumentException(String.format(
169                     "Invalid reply type %s for CreateTransaction", response.getClass()));
170
171             localTransactionContext = new NoOpTransactionContext(exception, getIdentifier());
172         }
173
174         transactionContextAdapter.executePriorTransactionOperations(localTransactionContext);
175     }
176
177     private TransactionContext createValidTransactionContext(CreateTransactionReply reply) {
178         LOG.debug("Tx {} Received {}", getIdentifier(), reply);
179
180         return createValidTransactionContext(getActorContext().actorSelection(reply.getTransactionPath()),
181                 reply.getTransactionPath(), reply.getVersion());
182     }
183
184     private TransactionContext createValidTransactionContext(ActorSelection transactionActor, String transactionPath,
185             short remoteTransactionVersion) {
186         // TxActor is always created where the leader of the shard is.
187         // Check if TxActor is created in the same node
188         boolean isTxActorLocal = getActorContext().isPathLocal(transactionPath);
189         final TransactionContext ret;
190
191         if (remoteTransactionVersion < DataStoreVersions.LITHIUM_VERSION) {
192             ret = new PreLithiumTransactionContextImpl(transactionContextAdapter.getIdentifier(), transactionPath, transactionActor,
193                 getActorContext(), isTxActorLocal, remoteTransactionVersion, transactionContextAdapter.getLimiter());
194         } else {
195             ret = new RemoteTransactionContext(transactionContextAdapter.getIdentifier(), transactionActor, getActorContext(),
196                 isTxActorLocal, remoteTransactionVersion, transactionContextAdapter.getLimiter());
197         }
198
199         if(parent.getType() == TransactionType.READ_ONLY) {
200             TransactionContextCleanup.track(this, ret);
201         }
202
203         return ret;
204     }
205 }
206