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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19 import java.util.ArrayDeque;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Deque;
24 import java.util.HashMap;
25 import java.util.LinkedList;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
29 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
30 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
31 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
32 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
33 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
34 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
35 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
36 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
37 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
38 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
39 import org.opendaylight.controller.cluster.datastore.messages.VersionedExternalizableMessage;
40 import org.opendaylight.controller.cluster.datastore.utils.AbstractBatchedModificationsCursor;
41 import org.opendaylight.yangtools.concepts.Identifier;
42 import org.opendaylight.yangtools.yang.common.Empty;
43 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
44 import org.slf4j.Logger;
47 * Coordinates commits for a shard ensuring only one concurrent 3-phase commit.
49 * @author Thomas Pantelis
51 final class ShardCommitCoordinator {
53 // Interface hook for unit tests to replace or decorate the ShardDataTreeCohorts.
55 public interface CohortDecorator {
56 ShardDataTreeCohort decorate(Identifier transactionID, ShardDataTreeCohort actual);
59 private final Map<Identifier, CohortEntry> cohortCache = new HashMap<>();
61 private final ShardDataTree dataTree;
63 private final Logger log;
65 private final String name;
67 // This is a hook for unit tests to replace or decorate the ShardDataTreeCohorts.
69 private CohortDecorator cohortDecorator;
71 private ReadyTransactionReply readyTransactionReply;
73 ShardCommitCoordinator(final ShardDataTree dataTree, final Logger log, final String name) {
76 this.dataTree = requireNonNull(dataTree);
79 int getCohortCacheSize() {
80 return cohortCache.size();
83 private String persistenceId() {
84 return dataTree.logContext();
87 private ReadyTransactionReply readyTransactionReply(final ActorRef cohort) {
88 if (readyTransactionReply == null) {
89 readyTransactionReply = new ReadyTransactionReply(Serialization.serializedActorPath(cohort));
92 return readyTransactionReply;
96 * This method is called to ready a transaction that was prepared by ShardTransaction actor. It caches
97 * the prepared cohort entry for the given transactions ID in preparation for the subsequent 3-phase commit.
99 * @param ready the ForwardedReadyTransaction message to process
100 * @param sender the sender of the message
101 * @param shard the transaction's shard actor
103 void handleForwardedReadyTransaction(final ForwardedReadyTransaction ready, final ActorRef sender,
105 log.debug("{}: Readying transaction {}, client version {}", name,
106 ready.getTransactionId(), ready.getTxnClientVersion());
108 final ShardDataTreeCohort cohort = ready.getTransaction().ready(ready.getParticipatingShardNames());
109 final CohortEntry cohortEntry = CohortEntry.createReady(cohort, ready.getTxnClientVersion());
110 cohortCache.put(cohortEntry.getTransactionId(), cohortEntry);
112 if (ready.isDoImmediateCommit()) {
113 cohortEntry.setDoImmediateCommit(true);
114 cohortEntry.setReplySender(sender);
115 cohortEntry.setShard(shard);
116 handleCanCommit(cohortEntry);
118 // The caller does not want immediate commit - the 3-phase commit will be coordinated by the
119 // front-end so send back a ReadyTransactionReply with our actor path.
120 sender.tell(readyTransactionReply(shard.self()), shard.self());
125 * This method handles a BatchedModifications message for a transaction being prepared directly on the
126 * Shard actor instead of via a ShardTransaction actor. If there's no currently cached
127 * DOMStoreWriteTransaction, one is created. The batched modifications are applied to the write Tx. If
128 * the BatchedModifications is ready to commit then a DOMStoreThreePhaseCommitCohort is created.
130 * @param batched the BatchedModifications message to process
131 * @param sender the sender of the message
133 @SuppressFBWarnings(value = "THROWS_METHOD_THROWS_RUNTIMEEXCEPTION", justification = "Replay of captured failure")
134 void handleBatchedModifications(final BatchedModifications batched, final ActorRef sender, final Shard shard) {
135 CohortEntry cohortEntry = cohortCache.get(batched.getTransactionId());
136 if (cohortEntry == null || cohortEntry.isSealed()) {
137 cohortEntry = CohortEntry.createOpen(dataTree.newReadWriteTransaction(batched.getTransactionId()),
138 batched.getVersion());
139 cohortCache.put(cohortEntry.getTransactionId(), cohortEntry);
142 if (log.isDebugEnabled()) {
143 log.debug("{}: Applying {} batched modifications for Tx {}", name,
144 batched.getModifications().size(), batched.getTransactionId());
147 cohortEntry.applyModifications(batched.getModifications());
149 if (batched.isReady()) {
150 if (cohortEntry.getLastBatchedModificationsException() != null) {
151 cohortCache.remove(cohortEntry.getTransactionId());
152 throw cohortEntry.getLastBatchedModificationsException();
155 if (cohortEntry.getTotalBatchedModificationsReceived() != batched.getTotalMessagesSent()) {
156 cohortCache.remove(cohortEntry.getTransactionId());
157 throw new IllegalStateException(String.format(
158 "The total number of batched messages received %d does not match the number sent %d",
159 cohortEntry.getTotalBatchedModificationsReceived(), batched.getTotalMessagesSent()));
162 if (log.isDebugEnabled()) {
163 log.debug("{}: Readying Tx {} of {} operations, client version {}", name,
164 batched.getTransactionId(), cohortEntry.getTotalOperationsProcessed(), batched.getVersion());
167 cohortEntry.setDoImmediateCommit(batched.isDoCommitOnReady());
168 cohortEntry.ready(batched.getParticipatingShardNames(), cohortDecorator);
170 if (batched.isDoCommitOnReady()) {
171 cohortEntry.setReplySender(sender);
172 cohortEntry.setShard(shard);
173 handleCanCommit(cohortEntry);
175 sender.tell(readyTransactionReply(shard.self()), shard.self());
178 sender.tell(new BatchedModificationsReply(batched.getModifications().size()), shard.self());
183 * This method handles {@link ReadyLocalTransaction} message. All transaction modifications have
184 * been prepared beforehand by the sender and we just need to drive them through into the
187 * @param message the ReadyLocalTransaction message to process
188 * @param sender the sender of the message
189 * @param shard the transaction's shard actor
191 void handleReadyLocalTransaction(final ReadyLocalTransaction message, final ActorRef sender, final Shard shard) {
192 final TransactionIdentifier txId = message.getTransactionId();
193 final ShardDataTreeCohort cohort = dataTree.newReadyCohort(txId, message.getModification(),
194 message.getParticipatingShardNames());
195 final CohortEntry cohortEntry = CohortEntry.createReady(cohort, DataStoreVersions.CURRENT_VERSION);
196 cohortCache.put(cohortEntry.getTransactionId(), cohortEntry);
197 cohortEntry.setDoImmediateCommit(message.isDoCommitOnReady());
199 log.debug("{}: Applying local modifications for Tx {}", name, txId);
201 if (message.isDoCommitOnReady()) {
202 cohortEntry.setReplySender(sender);
203 cohortEntry.setShard(shard);
204 handleCanCommit(cohortEntry);
206 sender.tell(readyTransactionReply(shard.self()), shard.self());
210 Collection<BatchedModifications> createForwardedBatchedModifications(final BatchedModifications from,
211 final int maxModificationsPerBatch) {
212 CohortEntry cohortEntry = cohortCache.remove(from.getTransactionId());
213 if (cohortEntry == null || cohortEntry.getTransaction() == null) {
214 return Collections.singletonList(from);
217 cohortEntry.applyModifications(from.getModifications());
219 final LinkedList<BatchedModifications> newModifications = new LinkedList<>();
220 cohortEntry.getTransaction().getSnapshot().applyToCursor(new AbstractBatchedModificationsCursor() {
222 protected BatchedModifications getModifications() {
223 if (newModifications.isEmpty()
224 || newModifications.getLast().getModifications().size() >= maxModificationsPerBatch) {
225 newModifications.add(new BatchedModifications(from.getTransactionId(), from.getVersion()));
228 return newModifications.getLast();
232 BatchedModifications last = newModifications.getLast();
233 last.setDoCommitOnReady(from.isDoCommitOnReady());
234 if (from.isReady()) {
235 last.setReady(from.getParticipatingShardNames());
237 last.setTotalMessagesSent(newModifications.size());
238 return newModifications;
241 private void handleCanCommit(final CohortEntry cohortEntry) {
242 cohortEntry.canCommit(new FutureCallback<>() {
244 public void onSuccess(final Empty result) {
245 log.debug("{}: canCommit for {}: success", name, cohortEntry.getTransactionId());
247 if (cohortEntry.isDoImmediateCommit()) {
248 doCommit(cohortEntry);
250 cohortEntry.getReplySender().tell(
251 CanCommitTransactionReply.yes(cohortEntry.getClientVersion()).toSerializable(),
252 cohortEntry.getShard().self());
257 public void onFailure(final Throwable failure) {
258 log.debug("{}: An exception occurred during canCommit for {}", name, cohortEntry.getTransactionId(),
261 cohortCache.remove(cohortEntry.getTransactionId());
262 cohortEntry.getReplySender().tell(new Failure(failure), cohortEntry.getShard().self());
268 * This method handles the canCommit phase for a transaction.
270 * @param transactionID the ID of the transaction to canCommit
271 * @param sender the actor to which to send the response
272 * @param shard the transaction's shard actor
274 void handleCanCommit(final Identifier transactionID, final ActorRef sender, final Shard shard) {
275 // Lookup the cohort entry that was cached previously (or should have been) by
276 // transactionReady (via the ForwardedReadyTransaction message).
277 final CohortEntry cohortEntry = cohortCache.get(transactionID);
278 if (cohortEntry == null) {
279 // Either canCommit was invoked before ready (shouldn't happen) or a long time passed
280 // between canCommit and ready and the entry was expired from the cache or it was aborted.
281 IllegalStateException ex = new IllegalStateException(
282 String.format("%s: Cannot canCommit transaction %s - no cohort entry found", name, transactionID));
283 log.error("{}: Inconsistency during transaction {} canCommit", name, transactionID, ex);
284 sender.tell(new Failure(ex), shard.self());
288 cohortEntry.setReplySender(sender);
289 cohortEntry.setShard(shard);
291 handleCanCommit(cohortEntry);
294 void doCommit(final CohortEntry cohortEntry) {
295 log.debug("{}: Committing transaction {}", name, cohortEntry.getTransactionId());
297 // We perform the preCommit phase here atomically with the commit phase. This is an
298 // optimization to eliminate the overhead of an extra preCommit message. We lose front-end
299 // coordination of preCommit across shards in case of failure but preCommit should not
300 // normally fail since we ensure only one concurrent 3-phase commit.
301 cohortEntry.preCommit(new FutureCallback<DataTreeCandidate>() {
303 public void onSuccess(final DataTreeCandidate candidate) {
304 finishCommit(cohortEntry.getReplySender(), cohortEntry);
308 public void onFailure(final Throwable failure) {
309 log.error("{} An exception occurred while preCommitting transaction {}", name,
310 cohortEntry.getTransactionId(), failure);
312 cohortCache.remove(cohortEntry.getTransactionId());
313 cohortEntry.getReplySender().tell(new Failure(failure), cohortEntry.getShard().self());
318 void finishCommit(final @NonNull ActorRef sender, final @NonNull CohortEntry cohortEntry) {
319 log.debug("{}: Finishing commit for transaction {}", persistenceId(), cohortEntry.getTransactionId());
321 cohortEntry.commit(new FutureCallback<UnsignedLong>() {
323 public void onSuccess(final UnsignedLong result) {
324 final TransactionIdentifier txId = cohortEntry.getTransactionId();
325 log.debug("{}: Transaction {} committed as {}, sending response to {}", persistenceId(), txId, result,
328 cohortCache.remove(cohortEntry.getTransactionId());
329 sender.tell(CommitTransactionReply.instance(cohortEntry.getClientVersion()).toSerializable(),
330 cohortEntry.getShard().self());
334 public void onFailure(final Throwable failure) {
335 final TransactionIdentifier txId = cohortEntry.getTransactionId();
336 log.error("{}, An exception occurred while committing transaction {}", persistenceId(), txId, failure);
338 cohortCache.remove(cohortEntry.getTransactionId());
339 sender.tell(new Failure(failure), cohortEntry.getShard().self());
345 * This method handles the preCommit and commit phases for a transaction.
347 * @param transactionID the ID of the transaction to commit
348 * @param sender the actor to which to send the response
349 * @param shard the transaction's shard actor
351 void handleCommit(final Identifier transactionID, final ActorRef sender, final Shard shard) {
352 final CohortEntry cohortEntry = cohortCache.get(transactionID);
353 if (cohortEntry == null) {
354 // Either a long time passed between canCommit and commit and the entry was expired from the cache
355 // or it was aborted.
356 IllegalStateException ex = new IllegalStateException(
357 String.format("%s: Cannot commit transaction %s - no cohort entry found", name, transactionID));
358 log.error("{}: Inconsistency during transaction {} commit", name, transactionID, ex);
359 sender.tell(new Failure(ex), shard.self());
363 cohortEntry.setReplySender(sender);
364 doCommit(cohortEntry);
367 @SuppressWarnings("checkstyle:IllegalCatch")
368 void handleAbort(final Identifier transactionID, final ActorRef sender, final Shard shard) {
369 CohortEntry cohortEntry = cohortCache.remove(transactionID);
370 if (cohortEntry == null) {
374 log.debug("{}: Aborting transaction {}", name, transactionID);
376 final ActorRef self = shard.getSelf();
377 cohortEntry.abort(new FutureCallback<>() {
379 public void onSuccess(final Empty result) {
380 if (sender != null) {
381 sender.tell(AbortTransactionReply.instance(cohortEntry.getClientVersion()).toSerializable(), self);
386 public void onFailure(final Throwable failure) {
387 log.error("{}: An exception happened during abort", name, failure);
389 if (sender != null) {
390 sender.tell(new Failure(failure), self);
395 shard.getShardMBean().incrementAbortTransactionsCount();
398 void checkForExpiredTransactions(final long timeout, final Shard shard) {
399 cohortCache.values().removeIf(CohortEntry::isFailed);
402 void abortPendingTransactions(final String reason, final Shard shard) {
403 final Failure failure = new Failure(new RuntimeException(reason));
404 Collection<ShardDataTreeCohort> pending = dataTree.getAndClearPendingTransactions();
406 log.debug("{}: Aborting {} pending queued transactions", name, pending.size());
408 for (ShardDataTreeCohort cohort : pending) {
409 CohortEntry cohortEntry = cohortCache.remove(cohort.getIdentifier());
410 if (cohortEntry == null) {
414 if (cohortEntry.getReplySender() != null) {
415 cohortEntry.getReplySender().tell(failure, shard.self());
422 Collection<?> convertPendingTransactionsToMessages(final int maxModificationsPerBatch) {
423 final Collection<VersionedExternalizableMessage> messages = new ArrayList<>();
424 for (ShardDataTreeCohort cohort : dataTree.getAndClearPendingTransactions()) {
425 CohortEntry cohortEntry = cohortCache.remove(cohort.getIdentifier());
426 if (cohortEntry == null) {
430 final Deque<BatchedModifications> newMessages = new ArrayDeque<>();
431 cohortEntry.getDataTreeModification().applyToCursor(new AbstractBatchedModificationsCursor() {
433 protected BatchedModifications getModifications() {
434 final BatchedModifications lastBatch = newMessages.peekLast();
436 if (lastBatch != null && lastBatch.getModifications().size() >= maxModificationsPerBatch) {
440 // Allocate a new message
441 final BatchedModifications ret = new BatchedModifications(cohortEntry.getTransactionId(),
442 cohortEntry.getClientVersion());
443 newMessages.add(ret);
448 final BatchedModifications last = newMessages.peekLast();
450 final boolean immediate = cohortEntry.isDoImmediateCommit();
451 last.setDoCommitOnReady(immediate);
452 last.setReady(cohortEntry.getParticipatingShardNames());
453 last.setTotalMessagesSent(newMessages.size());
455 messages.addAll(newMessages);
458 switch (cohort.getState()) {
459 case CAN_COMMIT_COMPLETE:
460 case CAN_COMMIT_PENDING:
461 messages.add(new CanCommitTransaction(cohortEntry.getTransactionId(),
462 cohortEntry.getClientVersion()));
464 case PRE_COMMIT_COMPLETE:
465 case PRE_COMMIT_PENDING:
466 messages.add(new CommitTransaction(cohortEntry.getTransactionId(),
467 cohortEntry.getClientVersion()));
480 void setCohortDecorator(final CohortDecorator cohortDecorator) {
481 this.cohortDecorator = cohortDecorator;