Convert DatastoreSnapshotRestore to OSGi DS
[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 static java.util.Objects.requireNonNull;
11
12 import akka.actor.ActorRef;
13 import akka.actor.Status.Failure;
14 import akka.serialization.Serialization;
15 import com.google.common.annotations.VisibleForTesting;
16 import com.google.common.primitives.UnsignedLong;
17 import com.google.common.util.concurrent.FutureCallback;
18 import java.util.ArrayDeque;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.Deque;
23 import java.util.HashMap;
24 import java.util.LinkedList;
25 import java.util.Map;
26 import org.eclipse.jdt.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 = requireNonNull(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      */
101     void handleForwardedReadyTransaction(final ForwardedReadyTransaction ready, final ActorRef sender,
102             final Shard shard) {
103         log.debug("{}: Readying transaction {}, client version {}", name,
104                 ready.getTransactionId(), ready.getTxnClientVersion());
105
106         final ShardDataTreeCohort cohort = ready.getTransaction().ready(ready.getParticipatingShardNames());
107         final CohortEntry cohortEntry = CohortEntry.createReady(cohort, ready.getTxnClientVersion());
108         cohortCache.put(cohortEntry.getTransactionId(), cohortEntry);
109
110         if (ready.isDoImmediateCommit()) {
111             cohortEntry.setDoImmediateCommit(true);
112             cohortEntry.setReplySender(sender);
113             cohortEntry.setShard(shard);
114             handleCanCommit(cohortEntry);
115         } else {
116             // The caller does not want immediate commit - the 3-phase commit will be coordinated by the
117             // front-end so send back a ReadyTransactionReply with our actor path.
118             sender.tell(readyTransactionReply(shard.self()), shard.self());
119         }
120     }
121
122     /**
123      * This method handles a BatchedModifications message for a transaction being prepared directly on the
124      * Shard actor instead of via a ShardTransaction actor. If there's no currently cached
125      * DOMStoreWriteTransaction, one is created. The batched modifications are applied to the write Tx. If
126      * the BatchedModifications is ready to commit then a DOMStoreThreePhaseCommitCohort is created.
127      *
128      * @param batched the BatchedModifications message to process
129      * @param sender the sender of the message
130      */
131     void handleBatchedModifications(final BatchedModifications batched, final ActorRef sender, final Shard shard) {
132         CohortEntry cohortEntry = cohortCache.get(batched.getTransactionId());
133         if (cohortEntry == null || cohortEntry.isSealed()) {
134             cohortEntry = CohortEntry.createOpen(dataTree.newReadWriteTransaction(batched.getTransactionId()),
135                 batched.getVersion());
136             cohortCache.put(cohortEntry.getTransactionId(), cohortEntry);
137         }
138
139         if (log.isDebugEnabled()) {
140             log.debug("{}: Applying {} batched modifications for Tx {}", name,
141                     batched.getModifications().size(), batched.getTransactionId());
142         }
143
144         cohortEntry.applyModifications(batched.getModifications());
145
146         if (batched.isReady()) {
147             if (cohortEntry.getLastBatchedModificationsException() != null) {
148                 cohortCache.remove(cohortEntry.getTransactionId());
149                 throw cohortEntry.getLastBatchedModificationsException();
150             }
151
152             if (cohortEntry.getTotalBatchedModificationsReceived() != batched.getTotalMessagesSent()) {
153                 cohortCache.remove(cohortEntry.getTransactionId());
154                 throw new IllegalStateException(String.format(
155                         "The total number of batched messages received %d does not match the number sent %d",
156                         cohortEntry.getTotalBatchedModificationsReceived(), batched.getTotalMessagesSent()));
157             }
158
159             if (log.isDebugEnabled()) {
160                 log.debug("{}: Readying Tx {} of {} operations, client version {}", name,
161                         batched.getTransactionId(), cohortEntry.getTotalOperationsProcessed(), batched.getVersion());
162             }
163
164             cohortEntry.setDoImmediateCommit(batched.isDoCommitOnReady());
165             cohortEntry.ready(batched.getParticipatingShardNames(), cohortDecorator);
166
167             if (batched.isDoCommitOnReady()) {
168                 cohortEntry.setReplySender(sender);
169                 cohortEntry.setShard(shard);
170                 handleCanCommit(cohortEntry);
171             } else {
172                 sender.tell(readyTransactionReply(shard.self()), shard.self());
173             }
174         } else {
175             sender.tell(new BatchedModificationsReply(batched.getModifications().size()), shard.self());
176         }
177     }
178
179     /**
180      * This method handles {@link ReadyLocalTransaction} message. All transaction modifications have
181      * been prepared beforehand by the sender and we just need to drive them through into the
182      * dataTree.
183      *
184      * @param message the ReadyLocalTransaction message to process
185      * @param sender the sender of the message
186      * @param shard the transaction's shard actor
187      */
188     void handleReadyLocalTransaction(final ReadyLocalTransaction message, final ActorRef sender, final Shard shard) {
189         final TransactionIdentifier txId = message.getTransactionId();
190         final ShardDataTreeCohort cohort = dataTree.newReadyCohort(txId, message.getModification(),
191                 message.getParticipatingShardNames());
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, txId);
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         if (from.isReady()) {
232             last.setReady(from.getParticipatingShardNames());
233         }
234         last.setTotalMessagesSent(newModifications.size());
235         return newModifications;
236     }
237
238     private void handleCanCommit(final CohortEntry cohortEntry) {
239         cohortEntry.canCommit(new FutureCallback<Void>() {
240             @Override
241             public void onSuccess(final Void result) {
242                 log.debug("{}: canCommit for {}: success", name, cohortEntry.getTransactionId());
243
244                 if (cohortEntry.isDoImmediateCommit()) {
245                     doCommit(cohortEntry);
246                 } else {
247                     cohortEntry.getReplySender().tell(
248                         CanCommitTransactionReply.yes(cohortEntry.getClientVersion()).toSerializable(),
249                         cohortEntry.getShard().self());
250                 }
251             }
252
253             @Override
254             public void onFailure(final Throwable failure) {
255                 log.debug("{}: An exception occurred during canCommit for {}", name, cohortEntry.getTransactionId(),
256                     failure);
257
258                 cohortCache.remove(cohortEntry.getTransactionId());
259                 cohortEntry.getReplySender().tell(new Failure(failure), cohortEntry.getShard().self());
260             }
261         });
262     }
263
264     /**
265      * This method handles the canCommit phase for a transaction.
266      *
267      * @param transactionID the ID of the transaction to canCommit
268      * @param sender the actor to which to send the response
269      * @param shard the transaction's shard actor
270      */
271     void handleCanCommit(final Identifier transactionID, final ActorRef sender, final Shard shard) {
272         // Lookup the cohort entry that was cached previously (or should have been) by
273         // transactionReady (via the ForwardedReadyTransaction message).
274         final CohortEntry cohortEntry = cohortCache.get(transactionID);
275         if (cohortEntry == null) {
276             // Either canCommit was invoked before ready (shouldn't happen) or a long time passed
277             // between canCommit and ready and the entry was expired from the cache or it was aborted.
278             IllegalStateException ex = new IllegalStateException(
279                     String.format("%s: Cannot canCommit transaction %s - no cohort entry found", name, transactionID));
280             log.error("{}: Inconsistency during transaction {} canCommit", name, transactionID, ex);
281             sender.tell(new Failure(ex), shard.self());
282             return;
283         }
284
285         cohortEntry.setReplySender(sender);
286         cohortEntry.setShard(shard);
287
288         handleCanCommit(cohortEntry);
289     }
290
291     void doCommit(final CohortEntry cohortEntry) {
292         log.debug("{}: Committing transaction {}", name, cohortEntry.getTransactionId());
293
294         // We perform the preCommit phase here atomically with the commit phase. This is an
295         // optimization to eliminate the overhead of an extra preCommit message. We lose front-end
296         // coordination of preCommit across shards in case of failure but preCommit should not
297         // normally fail since we ensure only one concurrent 3-phase commit.
298         cohortEntry.preCommit(new FutureCallback<DataTreeCandidate>() {
299             @Override
300             public void onSuccess(final DataTreeCandidate candidate) {
301                 finishCommit(cohortEntry.getReplySender(), cohortEntry);
302             }
303
304             @Override
305             public void onFailure(final Throwable failure) {
306                 log.error("{} An exception occurred while preCommitting transaction {}", name,
307                         cohortEntry.getTransactionId(), failure);
308
309                 cohortCache.remove(cohortEntry.getTransactionId());
310                 cohortEntry.getReplySender().tell(new Failure(failure), cohortEntry.getShard().self());
311             }
312         });
313     }
314
315     void finishCommit(final @NonNull ActorRef sender, final @NonNull CohortEntry cohortEntry) {
316         log.debug("{}: Finishing commit for transaction {}", persistenceId(), cohortEntry.getTransactionId());
317
318         cohortEntry.commit(new FutureCallback<UnsignedLong>() {
319             @Override
320             public void onSuccess(final UnsignedLong result) {
321                 final TransactionIdentifier txId = cohortEntry.getTransactionId();
322                 log.debug("{}: Transaction {} committed as {}, sending response to {}", persistenceId(), txId, result,
323                     sender);
324
325                 cohortCache.remove(cohortEntry.getTransactionId());
326                 sender.tell(CommitTransactionReply.instance(cohortEntry.getClientVersion()).toSerializable(),
327                     cohortEntry.getShard().self());
328             }
329
330             @Override
331             public void onFailure(final Throwable failure) {
332                 final TransactionIdentifier txId = cohortEntry.getTransactionId();
333                 log.error("{}, An exception occurred while committing transaction {}", persistenceId(), txId, failure);
334
335                 cohortCache.remove(cohortEntry.getTransactionId());
336                 sender.tell(new Failure(failure), cohortEntry.getShard().self());
337             }
338         });
339     }
340
341     /**
342      * This method handles the preCommit and commit phases for a transaction.
343      *
344      * @param transactionID the ID of the transaction to commit
345      * @param sender the actor to which to send the response
346      * @param shard the transaction's shard actor
347      */
348     void handleCommit(final Identifier transactionID, final ActorRef sender, final Shard shard) {
349         final CohortEntry cohortEntry = cohortCache.get(transactionID);
350         if (cohortEntry == null) {
351             // Either a long time passed between canCommit and commit and the entry was expired from the cache
352             // or it was aborted.
353             IllegalStateException ex = new IllegalStateException(
354                     String.format("%s: Cannot commit transaction %s - no cohort entry found", name, transactionID));
355             log.error("{}: Inconsistency during transaction {} commit", name, transactionID, ex);
356             sender.tell(new Failure(ex), shard.self());
357             return;
358         }
359
360         cohortEntry.setReplySender(sender);
361         doCommit(cohortEntry);
362     }
363
364     @SuppressWarnings("checkstyle:IllegalCatch")
365     void handleAbort(final Identifier transactionID, final ActorRef sender, final Shard shard) {
366         CohortEntry cohortEntry = cohortCache.remove(transactionID);
367         if (cohortEntry == null) {
368             return;
369         }
370
371         log.debug("{}: Aborting transaction {}", name, transactionID);
372
373         final ActorRef self = shard.getSelf();
374         cohortEntry.abort(new FutureCallback<Void>() {
375             @Override
376             public void onSuccess(final Void result) {
377                 if (sender != null) {
378                     sender.tell(AbortTransactionReply.instance(cohortEntry.getClientVersion()).toSerializable(), self);
379                 }
380             }
381
382             @Override
383             public void onFailure(final Throwable failure) {
384                 log.error("{}: An exception happened during abort", name, failure);
385
386                 if (sender != null) {
387                     sender.tell(new Failure(failure), self);
388                 }
389             }
390         });
391
392         shard.getShardMBean().incrementAbortTransactionsCount();
393     }
394
395     void checkForExpiredTransactions(final long timeout, final Shard shard) {
396         cohortCache.values().removeIf(CohortEntry::isFailed);
397     }
398
399     void abortPendingTransactions(final String reason, final Shard shard) {
400         final Failure failure = new Failure(new RuntimeException(reason));
401         Collection<ShardDataTreeCohort> pending = dataTree.getAndClearPendingTransactions();
402
403         log.debug("{}: Aborting {} pending queued transactions", name, pending.size());
404
405         for (ShardDataTreeCohort cohort : pending) {
406             CohortEntry cohortEntry = cohortCache.remove(cohort.getIdentifier());
407             if (cohortEntry == null) {
408                 continue;
409             }
410
411             if (cohortEntry.getReplySender() != null) {
412                 cohortEntry.getReplySender().tell(failure, shard.self());
413             }
414         }
415
416         cohortCache.clear();
417     }
418
419     Collection<?> convertPendingTransactionsToMessages(final int maxModificationsPerBatch) {
420         final Collection<VersionedExternalizableMessage> messages = new ArrayList<>();
421         for (ShardDataTreeCohort cohort : dataTree.getAndClearPendingTransactions()) {
422             CohortEntry cohortEntry = cohortCache.remove(cohort.getIdentifier());
423             if (cohortEntry == null) {
424                 continue;
425             }
426
427             final Deque<BatchedModifications> newMessages = new ArrayDeque<>();
428             cohortEntry.getDataTreeModification().applyToCursor(new AbstractBatchedModificationsCursor() {
429                 @Override
430                 protected BatchedModifications getModifications() {
431                     final BatchedModifications lastBatch = newMessages.peekLast();
432
433                     if (lastBatch != null && lastBatch.getModifications().size() >= maxModificationsPerBatch) {
434                         return lastBatch;
435                     }
436
437                     // Allocate a new message
438                     final BatchedModifications ret = new BatchedModifications(cohortEntry.getTransactionId(),
439                         cohortEntry.getClientVersion());
440                     newMessages.add(ret);
441                     return ret;
442                 }
443             });
444
445             final BatchedModifications last = newMessages.peekLast();
446             if (last != null) {
447                 final boolean immediate = cohortEntry.isDoImmediateCommit();
448                 last.setDoCommitOnReady(immediate);
449                 last.setReady(cohortEntry.getParticipatingShardNames());
450                 last.setTotalMessagesSent(newMessages.size());
451
452                 messages.addAll(newMessages);
453
454                 if (!immediate) {
455                     switch (cohort.getState()) {
456                         case CAN_COMMIT_COMPLETE:
457                         case CAN_COMMIT_PENDING:
458                             messages.add(new CanCommitTransaction(cohortEntry.getTransactionId(),
459                                 cohortEntry.getClientVersion()));
460                             break;
461                         case PRE_COMMIT_COMPLETE:
462                         case PRE_COMMIT_PENDING:
463                             messages.add(new CommitTransaction(cohortEntry.getTransactionId(),
464                                 cohortEntry.getClientVersion()));
465                             break;
466                         default:
467                             break;
468                     }
469                 }
470             }
471         }
472
473         return messages;
474     }
475
476     @VisibleForTesting
477     void setCohortDecorator(final CohortDecorator cohortDecorator) {
478         this.cohortDecorator = cohortDecorator;
479     }
480 }