2 * Copyright (c) 2014 Brocade Communications 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 static java.util.Objects.requireNonNull;
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;
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;
45 * Coordinates commits for a shard ensuring only one concurrent 3-phase commit.
47 * @author Thomas Pantelis
49 final class ShardCommitCoordinator {
51 // Interface hook for unit tests to replace or decorate the ShardDataTreeCohorts.
53 public interface CohortDecorator {
54 ShardDataTreeCohort decorate(Identifier transactionID, ShardDataTreeCohort actual);
57 private final Map<Identifier, CohortEntry> cohortCache = new HashMap<>();
59 private final ShardDataTree dataTree;
61 private final Logger log;
63 private final String name;
65 // This is a hook for unit tests to replace or decorate the ShardDataTreeCohorts.
67 private CohortDecorator cohortDecorator;
69 private ReadyTransactionReply readyTransactionReply;
71 ShardCommitCoordinator(final ShardDataTree dataTree, final Logger log, final String name) {
74 this.dataTree = requireNonNull(dataTree);
77 int getCohortCacheSize() {
78 return cohortCache.size();
81 private String persistenceId() {
82 return dataTree.logContext();
85 private ReadyTransactionReply readyTransactionReply(final ActorRef cohort) {
86 if (readyTransactionReply == null) {
87 readyTransactionReply = new ReadyTransactionReply(Serialization.serializedActorPath(cohort));
90 return readyTransactionReply;
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.
97 * @param ready the ForwardedReadyTransaction message to process
98 * @param sender the sender of the message
99 * @param shard the transaction's shard actor
101 void handleForwardedReadyTransaction(final ForwardedReadyTransaction ready, final ActorRef sender,
103 log.debug("{}: Readying transaction {}, client version {}", name,
104 ready.getTransactionId(), ready.getTxnClientVersion());
106 final ShardDataTreeCohort cohort = ready.getTransaction().ready(ready.getParticipatingShardNames());
107 final CohortEntry cohortEntry = CohortEntry.createReady(cohort, ready.getTxnClientVersion());
108 cohortCache.put(cohortEntry.getTransactionId(), cohortEntry);
110 if (ready.isDoImmediateCommit()) {
111 cohortEntry.setDoImmediateCommit(true);
112 cohortEntry.setReplySender(sender);
113 cohortEntry.setShard(shard);
114 handleCanCommit(cohortEntry);
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());
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.
128 * @param batched the BatchedModifications message to process
129 * @param sender the sender of the message
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);
139 if (log.isDebugEnabled()) {
140 log.debug("{}: Applying {} batched modifications for Tx {}", name,
141 batched.getModifications().size(), batched.getTransactionId());
144 cohortEntry.applyModifications(batched.getModifications());
146 if (batched.isReady()) {
147 if (cohortEntry.getLastBatchedModificationsException() != null) {
148 cohortCache.remove(cohortEntry.getTransactionId());
149 throw cohortEntry.getLastBatchedModificationsException();
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()));
159 if (log.isDebugEnabled()) {
160 log.debug("{}: Readying Tx {} of {} operations, client version {}", name,
161 batched.getTransactionId(), cohortEntry.getTotalOperationsProcessed(), batched.getVersion());
164 cohortEntry.setDoImmediateCommit(batched.isDoCommitOnReady());
165 cohortEntry.ready(batched.getParticipatingShardNames(), cohortDecorator);
167 if (batched.isDoCommitOnReady()) {
168 cohortEntry.setReplySender(sender);
169 cohortEntry.setShard(shard);
170 handleCanCommit(cohortEntry);
172 sender.tell(readyTransactionReply(shard.self()), shard.self());
175 sender.tell(new BatchedModificationsReply(batched.getModifications().size()), shard.self());
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
184 * @param message the ReadyLocalTransaction message to process
185 * @param sender the sender of the message
186 * @param shard the transaction's shard actor
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());
196 log.debug("{}: Applying local modifications for Tx {}", name, txId);
198 if (message.isDoCommitOnReady()) {
199 cohortEntry.setReplySender(sender);
200 cohortEntry.setShard(shard);
201 handleCanCommit(cohortEntry);
203 sender.tell(readyTransactionReply(shard.self()), shard.self());
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);
214 cohortEntry.applyModifications(from.getModifications());
216 final LinkedList<BatchedModifications> newModifications = new LinkedList<>();
217 cohortEntry.getTransaction().getSnapshot().applyToCursor(new AbstractBatchedModificationsCursor() {
219 protected BatchedModifications getModifications() {
220 if (newModifications.isEmpty()
221 || newModifications.getLast().getModifications().size() >= maxModificationsPerBatch) {
222 newModifications.add(new BatchedModifications(from.getTransactionId(), from.getVersion()));
225 return newModifications.getLast();
229 BatchedModifications last = newModifications.getLast();
230 last.setDoCommitOnReady(from.isDoCommitOnReady());
231 if (from.isReady()) {
232 last.setReady(from.getParticipatingShardNames());
234 last.setTotalMessagesSent(newModifications.size());
235 return newModifications;
238 private void handleCanCommit(final CohortEntry cohortEntry) {
239 cohortEntry.canCommit(new FutureCallback<Void>() {
241 public void onSuccess(final Void result) {
242 log.debug("{}: canCommit for {}: success", name, cohortEntry.getTransactionId());
244 if (cohortEntry.isDoImmediateCommit()) {
245 doCommit(cohortEntry);
247 cohortEntry.getReplySender().tell(
248 CanCommitTransactionReply.yes(cohortEntry.getClientVersion()).toSerializable(),
249 cohortEntry.getShard().self());
254 public void onFailure(final Throwable failure) {
255 log.debug("{}: An exception occurred during canCommit for {}", name, cohortEntry.getTransactionId(),
258 cohortCache.remove(cohortEntry.getTransactionId());
259 cohortEntry.getReplySender().tell(new Failure(failure), cohortEntry.getShard().self());
265 * This method handles the canCommit phase for a transaction.
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
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());
285 cohortEntry.setReplySender(sender);
286 cohortEntry.setShard(shard);
288 handleCanCommit(cohortEntry);
291 void doCommit(final CohortEntry cohortEntry) {
292 log.debug("{}: Committing transaction {}", name, cohortEntry.getTransactionId());
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>() {
300 public void onSuccess(final DataTreeCandidate candidate) {
301 finishCommit(cohortEntry.getReplySender(), cohortEntry);
305 public void onFailure(final Throwable failure) {
306 log.error("{} An exception occurred while preCommitting transaction {}", name,
307 cohortEntry.getTransactionId(), failure);
309 cohortCache.remove(cohortEntry.getTransactionId());
310 cohortEntry.getReplySender().tell(new Failure(failure), cohortEntry.getShard().self());
315 void finishCommit(final @NonNull ActorRef sender, final @NonNull CohortEntry cohortEntry) {
316 log.debug("{}: Finishing commit for transaction {}", persistenceId(), cohortEntry.getTransactionId());
318 cohortEntry.commit(new FutureCallback<UnsignedLong>() {
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,
325 cohortCache.remove(cohortEntry.getTransactionId());
326 sender.tell(CommitTransactionReply.instance(cohortEntry.getClientVersion()).toSerializable(),
327 cohortEntry.getShard().self());
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);
335 cohortCache.remove(cohortEntry.getTransactionId());
336 sender.tell(new Failure(failure), cohortEntry.getShard().self());
342 * This method handles the preCommit and commit phases for a transaction.
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
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());
360 cohortEntry.setReplySender(sender);
361 doCommit(cohortEntry);
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) {
371 log.debug("{}: Aborting transaction {}", name, transactionID);
373 final ActorRef self = shard.getSelf();
374 cohortEntry.abort(new FutureCallback<Void>() {
376 public void onSuccess(final Void result) {
377 if (sender != null) {
378 sender.tell(AbortTransactionReply.instance(cohortEntry.getClientVersion()).toSerializable(), self);
383 public void onFailure(final Throwable failure) {
384 log.error("{}: An exception happened during abort", name, failure);
386 if (sender != null) {
387 sender.tell(new Failure(failure), self);
392 shard.getShardMBean().incrementAbortTransactionsCount();
395 void checkForExpiredTransactions(final long timeout, final Shard shard) {
396 cohortCache.values().removeIf(CohortEntry::isFailed);
399 void abortPendingTransactions(final String reason, final Shard shard) {
400 final Failure failure = new Failure(new RuntimeException(reason));
401 Collection<ShardDataTreeCohort> pending = dataTree.getAndClearPendingTransactions();
403 log.debug("{}: Aborting {} pending queued transactions", name, pending.size());
405 for (ShardDataTreeCohort cohort : pending) {
406 CohortEntry cohortEntry = cohortCache.remove(cohort.getIdentifier());
407 if (cohortEntry == null) {
411 if (cohortEntry.getReplySender() != null) {
412 cohortEntry.getReplySender().tell(failure, shard.self());
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) {
427 final Deque<BatchedModifications> newMessages = new ArrayDeque<>();
428 cohortEntry.getDataTreeModification().applyToCursor(new AbstractBatchedModificationsCursor() {
430 protected BatchedModifications getModifications() {
431 final BatchedModifications lastBatch = newMessages.peekLast();
433 if (lastBatch != null && lastBatch.getModifications().size() >= maxModificationsPerBatch) {
437 // Allocate a new message
438 final BatchedModifications ret = new BatchedModifications(cohortEntry.getTransactionId(),
439 cohortEntry.getClientVersion());
440 newMessages.add(ret);
445 final BatchedModifications last = newMessages.peekLast();
447 final boolean immediate = cohortEntry.isDoImmediateCommit();
448 last.setDoCommitOnReady(immediate);
449 last.setReady(cohortEntry.getParticipatingShardNames());
450 last.setTotalMessagesSent(newMessages.size());
452 messages.addAll(newMessages);
455 switch (cohort.getState()) {
456 case CAN_COMMIT_COMPLETE:
457 case CAN_COMMIT_PENDING:
458 messages.add(new CanCommitTransaction(cohortEntry.getTransactionId(),
459 cohortEntry.getClientVersion()));
461 case PRE_COMMIT_COMPLETE:
462 case PRE_COMMIT_PENDING:
463 messages.add(new CommitTransaction(cohortEntry.getTransactionId(),
464 cohortEntry.getClientVersion()));
477 void setCohortDecorator(final CohortDecorator cohortDecorator) {
478 this.cohortDecorator = cohortDecorator;