BUG-5280: add FrontendMetadata
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardCommitCoordinator.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore;
9
10 import akka.actor.ActorRef;
11 import akka.actor.Status.Failure;
12 import akka.serialization.Serialization;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.base.Preconditions;
15 import com.google.common.primitives.UnsignedLong;
16 import com.google.common.util.concurrent.FutureCallback;
17 import java.util.ArrayDeque;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Deque;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.LinkedList;
25 import java.util.Map;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
28 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
29 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
30 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
31 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
32 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
33 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
34 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
35 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
36 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
37 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
38 import org.opendaylight.controller.cluster.datastore.messages.VersionedExternalizableMessage;
39 import org.opendaylight.controller.cluster.datastore.utils.AbstractBatchedModificationsCursor;
40 import org.opendaylight.yangtools.concepts.Identifier;
41 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
42 import org.slf4j.Logger;
43
44 /**
45  * Coordinates commits for a shard ensuring only one concurrent 3-phase commit.
46  *
47  * @author Thomas Pantelis
48  */
49 final class ShardCommitCoordinator {
50
51     // Interface hook for unit tests to replace or decorate the ShardDataTreeCohorts.
52     @VisibleForTesting
53     public interface CohortDecorator {
54         ShardDataTreeCohort decorate(Identifier transactionID, ShardDataTreeCohort actual);
55     }
56
57     private final Map<Identifier, CohortEntry> cohortCache = new HashMap<>();
58
59     private final ShardDataTree dataTree;
60
61     private final Logger log;
62
63     private final String name;
64
65     // This is a hook for unit tests to replace or decorate the ShardDataTreeCohorts.
66     @VisibleForTesting
67     private CohortDecorator cohortDecorator;
68
69     private ReadyTransactionReply readyTransactionReply;
70
71     ShardCommitCoordinator(final ShardDataTree dataTree, final Logger log, final String name) {
72         this.log = log;
73         this.name = name;
74         this.dataTree = Preconditions.checkNotNull(dataTree);
75     }
76
77     int getCohortCacheSize() {
78         return cohortCache.size();
79     }
80
81     private String persistenceId() {
82         return dataTree.logContext();
83     }
84
85     private ReadyTransactionReply readyTransactionReply(final ActorRef cohort) {
86         if (readyTransactionReply == null) {
87             readyTransactionReply = new ReadyTransactionReply(Serialization.serializedActorPath(cohort));
88         }
89
90         return readyTransactionReply;
91     }
92
93     /**
94      * This method is called to ready a transaction that was prepared by ShardTransaction actor. It caches
95      * the prepared cohort entry for the given transactions ID in preparation for the subsequent 3-phase commit.
96      *
97      * @param ready the ForwardedReadyTransaction message to process
98      * @param sender the sender of the message
99      * @param shard the transaction's shard actor
100      * @param schema
101      */
102     void handleForwardedReadyTransaction(final ForwardedReadyTransaction ready, final ActorRef sender,
103             final Shard shard) {
104         log.debug("{}: Readying transaction {}, client version {}", name,
105                 ready.getTransactionID(), ready.getTxnClientVersion());
106
107         final ShardDataTreeCohort cohort = ready.getTransaction().ready();
108         final CohortEntry cohortEntry = CohortEntry.createReady(cohort, ready.getTxnClientVersion());
109         cohortCache.put(cohortEntry.getTransactionID(), cohortEntry);
110
111         if (ready.isDoImmediateCommit()) {
112             cohortEntry.setDoImmediateCommit(true);
113             cohortEntry.setReplySender(sender);
114             cohortEntry.setShard(shard);
115             handleCanCommit(cohortEntry);
116         } else {
117             // The caller does not want immediate commit - the 3-phase commit will be coordinated by the
118             // front-end so send back a ReadyTransactionReply with our actor path.
119             sender.tell(readyTransactionReply(shard.self()), shard.self());
120         }
121     }
122
123     /**
124      * This method handles a BatchedModifications message for a transaction being prepared directly on the
125      * Shard actor instead of via a ShardTransaction actor. If there's no currently cached
126      * DOMStoreWriteTransaction, one is created. The batched modifications are applied to the write Tx. If
127      * the BatchedModifications is ready to commit then a DOMStoreThreePhaseCommitCohort is created.
128      *
129      * @param batched the BatchedModifications message to process
130      * @param sender the sender of the message
131      */
132     void handleBatchedModifications(final BatchedModifications batched, final ActorRef sender, final Shard shard) {
133         CohortEntry cohortEntry = cohortCache.get(batched.getTransactionID());
134         if (cohortEntry == null) {
135             cohortEntry = CohortEntry.createOpen(dataTree.newReadWriteTransaction(batched.getTransactionID()),
136                 batched.getVersion());
137             cohortCache.put(cohortEntry.getTransactionID(), cohortEntry);
138         }
139
140         if (log.isDebugEnabled()) {
141             log.debug("{}: Applying {} batched modifications for Tx {}", name,
142                     batched.getModifications().size(), batched.getTransactionID());
143         }
144
145         cohortEntry.applyModifications(batched.getModifications());
146
147         if (batched.isReady()) {
148             if (cohortEntry.getLastBatchedModificationsException() != null) {
149                 cohortCache.remove(cohortEntry.getTransactionID());
150                 throw cohortEntry.getLastBatchedModificationsException();
151             }
152
153             if (cohortEntry.getTotalBatchedModificationsReceived() != batched.getTotalMessagesSent()) {
154                 cohortCache.remove(cohortEntry.getTransactionID());
155                 throw new IllegalStateException(String.format(
156                         "The total number of batched messages received %d does not match the number sent %d",
157                         cohortEntry.getTotalBatchedModificationsReceived(), batched.getTotalMessagesSent()));
158             }
159
160             if (log.isDebugEnabled()) {
161                 log.debug("{}: Readying Tx {}, client version {}", name,
162                         batched.getTransactionID(), batched.getVersion());
163             }
164
165             cohortEntry.setDoImmediateCommit(batched.isDoCommitOnReady());
166             cohortEntry.ready(cohortDecorator);
167
168             if (batched.isDoCommitOnReady()) {
169                 cohortEntry.setReplySender(sender);
170                 cohortEntry.setShard(shard);
171                 handleCanCommit(cohortEntry);
172             } else {
173                 sender.tell(readyTransactionReply(shard.self()), shard.self());
174             }
175         } else {
176             sender.tell(new BatchedModificationsReply(batched.getModifications().size()), shard.self());
177         }
178     }
179
180     /**
181      * This method handles {@link ReadyLocalTransaction} message. All transaction modifications have
182      * been prepared beforehand by the sender and we just need to drive them through into the
183      * dataTree.
184      *
185      * @param message the ReadyLocalTransaction message to process
186      * @param sender the sender of the message
187      * @param shard the transaction's shard actor
188      */
189     void handleReadyLocalTransaction(final ReadyLocalTransaction message, final ActorRef sender, final Shard shard) {
190         final ShardDataTreeCohort cohort = dataTree.createReadyCohort(message.getTransactionID(),
191             message.getModification());
192         final CohortEntry cohortEntry = CohortEntry.createReady(cohort, DataStoreVersions.CURRENT_VERSION);
193         cohortCache.put(cohortEntry.getTransactionID(), cohortEntry);
194         cohortEntry.setDoImmediateCommit(message.isDoCommitOnReady());
195
196         log.debug("{}: Applying local modifications for Tx {}", name, message.getTransactionID());
197
198         if (message.isDoCommitOnReady()) {
199             cohortEntry.setReplySender(sender);
200             cohortEntry.setShard(shard);
201             handleCanCommit(cohortEntry);
202         } else {
203             sender.tell(readyTransactionReply(shard.self()), shard.self());
204         }
205     }
206
207     Collection<BatchedModifications> createForwardedBatchedModifications(final BatchedModifications from,
208             final int maxModificationsPerBatch) {
209         CohortEntry cohortEntry = cohortCache.remove(from.getTransactionID());
210         if (cohortEntry == null || cohortEntry.getTransaction() == null) {
211             return Collections.singletonList(from);
212         }
213
214         cohortEntry.applyModifications(from.getModifications());
215
216         final LinkedList<BatchedModifications> newModifications = new LinkedList<>();
217         cohortEntry.getTransaction().getSnapshot().applyToCursor(new AbstractBatchedModificationsCursor() {
218             @Override
219             protected BatchedModifications getModifications() {
220                 if (newModifications.isEmpty() ||
221                         newModifications.getLast().getModifications().size() >= maxModificationsPerBatch) {
222                     newModifications.add(new BatchedModifications(from.getTransactionID(), from.getVersion()));
223                 }
224
225                 return newModifications.getLast();
226             }
227         });
228
229         BatchedModifications last = newModifications.getLast();
230         last.setDoCommitOnReady(from.isDoCommitOnReady());
231         last.setReady(from.isReady());
232         last.setTotalMessagesSent(newModifications.size());
233         return newModifications;
234     }
235
236     private void handleCanCommit(final CohortEntry cohortEntry) {
237         cohortEntry.canCommit(new FutureCallback<Void>() {
238             @Override
239             public void onSuccess(final Void result) {
240                 log.debug("{}: canCommit for {}: success", name, cohortEntry.getTransactionID());
241
242                 if (cohortEntry.isDoImmediateCommit()) {
243                     doCommit(cohortEntry);
244                 } else {
245                     cohortEntry.getReplySender().tell(
246                         CanCommitTransactionReply.yes(cohortEntry.getClientVersion()).toSerializable(),
247                         cohortEntry.getShard().self());
248                 }
249             }
250
251             @Override
252             public void onFailure(final Throwable t) {
253                 log.debug("{}: An exception occurred during canCommit for {}: {}", name,
254                         cohortEntry.getTransactionID(), t);
255
256                 cohortCache.remove(cohortEntry.getTransactionID());
257                 cohortEntry.getReplySender().tell(new Failure(t), cohortEntry.getShard().self());
258             }
259         });
260     }
261
262     /**
263      * This method handles the canCommit phase for a transaction.
264      *
265      * @param transactionID the ID of the transaction to canCommit
266      * @param sender the actor to which to send the response
267      * @param shard the transaction's shard actor
268      */
269     void handleCanCommit(final Identifier transactionID, final ActorRef sender, final Shard shard) {
270         // Lookup the cohort entry that was cached previously (or should have been) by
271         // transactionReady (via the ForwardedReadyTransaction message).
272         final CohortEntry cohortEntry = cohortCache.get(transactionID);
273         if (cohortEntry == null) {
274             // Either canCommit was invoked before ready (shouldn't happen) or a long time passed
275             // between canCommit and ready and the entry was expired from the cache or it was aborted.
276             IllegalStateException ex = new IllegalStateException(
277                     String.format("%s: Cannot canCommit transaction %s - no cohort entry found", name, transactionID));
278             log.error(ex.getMessage());
279             sender.tell(new Failure(ex), shard.self());
280             return;
281         }
282
283         cohortEntry.setReplySender(sender);
284         cohortEntry.setShard(shard);
285
286         handleCanCommit(cohortEntry);
287     }
288
289     private void doCommit(final CohortEntry cohortEntry) {
290         log.debug("{}: Committing transaction {}", name, cohortEntry.getTransactionID());
291
292         // We perform the preCommit phase here atomically with the commit phase. This is an
293         // optimization to eliminate the overhead of an extra preCommit message. We lose front-end
294         // coordination of preCommit across shards in case of failure but preCommit should not
295         // normally fail since we ensure only one concurrent 3-phase commit.
296         cohortEntry.preCommit(new FutureCallback<DataTreeCandidate>() {
297             @Override
298             public void onSuccess(final DataTreeCandidate candidate) {
299                 finishCommit(cohortEntry.getReplySender(), cohortEntry);
300             }
301
302             @Override
303             public void onFailure(final Throwable t) {
304                 log.error("{} An exception occurred while preCommitting transaction {}", name,
305                         cohortEntry.getTransactionID(), t);
306
307                 cohortCache.remove(cohortEntry.getTransactionID());
308                 cohortEntry.getReplySender().tell(new Failure(t), cohortEntry.getShard().self());
309             }
310         });
311     }
312
313     private void finishCommit(@Nonnull final ActorRef sender, @Nonnull final CohortEntry cohortEntry) {
314         log.debug("{}: Finishing commit for transaction {}", persistenceId(), cohortEntry.getTransactionID());
315
316         cohortEntry.commit(new FutureCallback<UnsignedLong>() {
317             @Override
318             public void onSuccess(final UnsignedLong result) {
319                 final TransactionIdentifier txId = cohortEntry.getTransactionID();
320                 log.debug("{}: Transaction {} committed as {}, sending response to {}", persistenceId(), txId, result,
321                     sender);
322
323                 cohortCache.remove(cohortEntry.getTransactionID());
324                 sender.tell(CommitTransactionReply.instance(cohortEntry.getClientVersion()).toSerializable(),
325                     cohortEntry.getShard().self());
326             }
327
328             @Override
329             public void onFailure(final Throwable t) {
330                 log.error("{}, An exception occurred while committing transaction {}", persistenceId(),
331                         cohortEntry.getTransactionID(), t);
332
333                 cohortCache.remove(cohortEntry.getTransactionID());
334                 sender.tell(new Failure(t), cohortEntry.getShard().self());
335             }
336         });
337     }
338
339     /**
340      * This method handles the preCommit and commit phases for a transaction.
341      *
342      * @param transactionID the ID of the transaction to commit
343      * @param sender the actor to which to send the response
344      * @param shard the transaction's shard actor
345      */
346     void handleCommit(final Identifier transactionID, final ActorRef sender, final Shard shard) {
347         final CohortEntry cohortEntry = cohortCache.get(transactionID);
348         if (cohortEntry == null) {
349             // Either a long time passed between canCommit and commit and the entry was expired from the cache
350             // or it was aborted.
351             IllegalStateException ex = new IllegalStateException(
352                     String.format("%s: Cannot commit transaction %s - no cohort entry found", name, transactionID));
353             log.error(ex.getMessage());
354             sender.tell(new Failure(ex), shard.self());
355             return;
356         }
357
358         cohortEntry.setReplySender(sender);
359         doCommit(cohortEntry);
360     }
361
362     void handleAbort(final Identifier transactionID, final ActorRef sender, final Shard shard) {
363         CohortEntry cohortEntry = cohortCache.remove(transactionID);
364         if (cohortEntry == null) {
365             return;
366         }
367
368         log.debug("{}: Aborting transaction {}", name, transactionID);
369
370         final ActorRef self = shard.getSelf();
371         try {
372             cohortEntry.abort();
373
374             shard.getShardMBean().incrementAbortTransactionsCount();
375
376             if (sender != null) {
377                 sender.tell(AbortTransactionReply.instance(cohortEntry.getClientVersion()).toSerializable(), self);
378             }
379         } catch (Exception e) {
380             log.error("{}: An exception happened during abort", name, e);
381
382             if (sender != null) {
383                 sender.tell(new Failure(e), self);
384             }
385         }
386     }
387
388     void checkForExpiredTransactions(final long timeout, final Shard shard) {
389         Iterator<CohortEntry> iter = cohortCache.values().iterator();
390         while (iter.hasNext()) {
391             CohortEntry cohortEntry = iter.next();
392             if(cohortEntry.isFailed()) {
393                 iter.remove();
394             }
395         }
396     }
397
398     void abortPendingTransactions(final String reason, final Shard shard) {
399         final Failure failure = new Failure(new RuntimeException(reason));
400         Collection<ShardDataTreeCohort> pending = dataTree.getAndClearPendingTransactions();
401
402         log.debug("{}: Aborting {} pending queued transactions", name, pending.size());
403
404         for (ShardDataTreeCohort cohort : pending) {
405             CohortEntry cohortEntry = cohortCache.remove(cohort.getIdentifier());
406             if (cohortEntry == null) {
407                 continue;
408             }
409
410             if (cohortEntry.getReplySender() != null) {
411                 cohortEntry.getReplySender().tell(failure, shard.self());
412             }
413         }
414
415         cohortCache.clear();
416     }
417
418     Collection<?> convertPendingTransactionsToMessages(final int maxModificationsPerBatch) {
419         final Collection<VersionedExternalizableMessage> messages = new ArrayList<>();
420         for (ShardDataTreeCohort cohort : dataTree.getAndClearPendingTransactions()) {
421             CohortEntry cohortEntry = cohortCache.remove(cohort.getIdentifier());
422             if (cohortEntry == null) {
423                 continue;
424             }
425
426             final Deque<BatchedModifications> newMessages = new ArrayDeque<>();
427             cohortEntry.getDataTreeModification().applyToCursor(new AbstractBatchedModificationsCursor() {
428                 @Override
429                 protected BatchedModifications getModifications() {
430                     final BatchedModifications lastBatch = newMessages.peekLast();
431
432                     if (lastBatch != null && lastBatch.getModifications().size() >= maxModificationsPerBatch) {
433                         return lastBatch;
434                     }
435
436                     // Allocate a new message
437                     final BatchedModifications ret = new BatchedModifications(cohortEntry.getTransactionID(),
438                         cohortEntry.getClientVersion());
439                     newMessages.add(ret);
440                     return ret;
441                 }
442             });
443
444             final BatchedModifications last = newMessages.peekLast();
445             if (last != null) {
446                 final boolean immediate = cohortEntry.isDoImmediateCommit();
447                 last.setDoCommitOnReady(immediate);
448                 last.setReady(true);
449                 last.setTotalMessagesSent(newMessages.size());
450
451                 messages.addAll(newMessages);
452
453                 if (!immediate) {
454                     switch (cohort.getState()) {
455                         case CAN_COMMIT_COMPLETE:
456                         case CAN_COMMIT_PENDING:
457                             messages.add(new CanCommitTransaction(cohortEntry.getTransactionID(),
458                                 cohortEntry.getClientVersion()));
459                             break;
460                         case PRE_COMMIT_COMPLETE:
461                         case PRE_COMMIT_PENDING:
462                             messages.add(new CommitTransaction(cohortEntry.getTransactionID(),
463                                 cohortEntry.getClientVersion()));
464                             break;
465                         default:
466                             break;
467                     }
468                 }
469             }
470         }
471
472         return messages;
473     }
474
475     @VisibleForTesting
476     void setCohortDecorator(final CohortDecorator cohortDecorator) {
477         this.cohortDecorator = cohortDecorator;
478     }
479 }