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 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;
26 import java.util.concurrent.ExecutionException;
27 import java.util.concurrent.TimeoutException;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
30 import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply;
31 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
32 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
33 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
34 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
35 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
36 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
37 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
38 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
39 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
40 import org.opendaylight.controller.cluster.datastore.messages.VersionedExternalizableMessage;
41 import org.opendaylight.controller.cluster.datastore.utils.AbstractBatchedModificationsCursor;
42 import org.opendaylight.yangtools.concepts.Identifier;
43 import org.opendaylight.yangtools.yang.data.api.schema.tree.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 = Preconditions.checkNotNull(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();
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 void handleBatchedModifications(final BatchedModifications batched, final ActorRef sender, final Shard shard) {
134 CohortEntry cohortEntry = cohortCache.get(batched.getTransactionId());
135 if (cohortEntry == null) {
136 cohortEntry = CohortEntry.createOpen(dataTree.newReadWriteTransaction(batched.getTransactionId()),
137 batched.getVersion());
138 cohortCache.put(cohortEntry.getTransactionId(), cohortEntry);
141 if (log.isDebugEnabled()) {
142 log.debug("{}: Applying {} batched modifications for Tx {}", name,
143 batched.getModifications().size(), batched.getTransactionId());
146 cohortEntry.applyModifications(batched.getModifications());
148 if (batched.isReady()) {
149 if (cohortEntry.getLastBatchedModificationsException() != null) {
150 cohortCache.remove(cohortEntry.getTransactionId());
151 throw cohortEntry.getLastBatchedModificationsException();
154 if (cohortEntry.getTotalBatchedModificationsReceived() != batched.getTotalMessagesSent()) {
155 cohortCache.remove(cohortEntry.getTransactionId());
156 throw new IllegalStateException(String.format(
157 "The total number of batched messages received %d does not match the number sent %d",
158 cohortEntry.getTotalBatchedModificationsReceived(), batched.getTotalMessagesSent()));
161 if (log.isDebugEnabled()) {
162 log.debug("{}: Readying Tx {}, client version {}", name,
163 batched.getTransactionId(), batched.getVersion());
166 cohortEntry.setDoImmediateCommit(batched.isDoCommitOnReady());
167 cohortEntry.ready(cohortDecorator);
169 if (batched.isDoCommitOnReady()) {
170 cohortEntry.setReplySender(sender);
171 cohortEntry.setShard(shard);
172 handleCanCommit(cohortEntry);
174 sender.tell(readyTransactionReply(shard.self()), shard.self());
177 sender.tell(new BatchedModificationsReply(batched.getModifications().size()), shard.self());
182 * This method handles {@link ReadyLocalTransaction} message. All transaction modifications have
183 * been prepared beforehand by the sender and we just need to drive them through into the
186 * @param message the ReadyLocalTransaction message to process
187 * @param sender the sender of the message
188 * @param shard the transaction's shard actor
190 void handleReadyLocalTransaction(final ReadyLocalTransaction message, final ActorRef sender, final Shard shard) {
191 final ShardDataTreeCohort cohort = dataTree.createReadyCohort(message.getTransactionId(),
192 message.getModification());
193 final CohortEntry cohortEntry = CohortEntry.createReady(cohort, DataStoreVersions.CURRENT_VERSION);
194 cohortCache.put(cohortEntry.getTransactionId(), cohortEntry);
195 cohortEntry.setDoImmediateCommit(message.isDoCommitOnReady());
197 log.debug("{}: Applying local modifications for Tx {}", name, message.getTransactionId());
199 if (message.isDoCommitOnReady()) {
200 cohortEntry.setReplySender(sender);
201 cohortEntry.setShard(shard);
202 handleCanCommit(cohortEntry);
204 sender.tell(readyTransactionReply(shard.self()), shard.self());
208 Collection<BatchedModifications> createForwardedBatchedModifications(final BatchedModifications from,
209 final int maxModificationsPerBatch) {
210 CohortEntry cohortEntry = cohortCache.remove(from.getTransactionId());
211 if (cohortEntry == null || cohortEntry.getTransaction() == null) {
212 return Collections.singletonList(from);
215 cohortEntry.applyModifications(from.getModifications());
217 final LinkedList<BatchedModifications> newModifications = new LinkedList<>();
218 cohortEntry.getTransaction().getSnapshot().applyToCursor(new AbstractBatchedModificationsCursor() {
220 protected BatchedModifications getModifications() {
221 if (newModifications.isEmpty()
222 || newModifications.getLast().getModifications().size() >= maxModificationsPerBatch) {
223 newModifications.add(new BatchedModifications(from.getTransactionId(), from.getVersion()));
226 return newModifications.getLast();
230 BatchedModifications last = newModifications.getLast();
231 last.setDoCommitOnReady(from.isDoCommitOnReady());
232 last.setReady(from.isReady());
233 last.setTotalMessagesSent(newModifications.size());
234 return newModifications;
237 private void handleCanCommit(final CohortEntry cohortEntry) {
238 cohortEntry.canCommit(new FutureCallback<Void>() {
240 public void onSuccess(final Void result) {
241 log.debug("{}: canCommit for {}: success", name, cohortEntry.getTransactionId());
243 if (cohortEntry.isDoImmediateCommit()) {
244 doCommit(cohortEntry);
246 cohortEntry.getReplySender().tell(
247 CanCommitTransactionReply.yes(cohortEntry.getClientVersion()).toSerializable(),
248 cohortEntry.getShard().self());
253 public void onFailure(final Throwable failure) {
254 log.debug("{}: An exception occurred during canCommit for {}: {}", name,
255 cohortEntry.getTransactionId(), failure);
257 cohortCache.remove(cohortEntry.getTransactionId());
258 cohortEntry.getReplySender().tell(new Failure(failure), cohortEntry.getShard().self());
264 * This method handles the canCommit phase for a transaction.
266 * @param transactionID the ID of the transaction to canCommit
267 * @param sender the actor to which to send the response
268 * @param shard the transaction's shard actor
270 void handleCanCommit(final Identifier transactionID, final ActorRef sender, final Shard shard) {
271 // Lookup the cohort entry that was cached previously (or should have been) by
272 // transactionReady (via the ForwardedReadyTransaction message).
273 final CohortEntry cohortEntry = cohortCache.get(transactionID);
274 if (cohortEntry == null) {
275 // Either canCommit was invoked before ready (shouldn't happen) or a long time passed
276 // between canCommit and ready and the entry was expired from the cache or it was aborted.
277 IllegalStateException ex = new IllegalStateException(
278 String.format("%s: Cannot canCommit transaction %s - no cohort entry found", name, transactionID));
279 log.error(ex.getMessage());
280 sender.tell(new Failure(ex), shard.self());
284 cohortEntry.setReplySender(sender);
285 cohortEntry.setShard(shard);
287 handleCanCommit(cohortEntry);
290 private void doCommit(final CohortEntry cohortEntry) {
291 log.debug("{}: Committing transaction {}", name, cohortEntry.getTransactionId());
293 // We perform the preCommit phase here atomically with the commit phase. This is an
294 // optimization to eliminate the overhead of an extra preCommit message. We lose front-end
295 // coordination of preCommit across shards in case of failure but preCommit should not
296 // normally fail since we ensure only one concurrent 3-phase commit.
297 cohortEntry.preCommit(new FutureCallback<DataTreeCandidate>() {
299 public void onSuccess(final DataTreeCandidate candidate) {
300 finishCommit(cohortEntry.getReplySender(), cohortEntry);
304 public void onFailure(final Throwable failure) {
305 log.error("{} An exception occurred while preCommitting transaction {}", name,
306 cohortEntry.getTransactionId(), failure);
308 cohortCache.remove(cohortEntry.getTransactionId());
309 cohortEntry.getReplySender().tell(new Failure(failure), cohortEntry.getShard().self());
314 private void finishCommit(@Nonnull final ActorRef sender, @Nonnull final CohortEntry cohortEntry) {
315 log.debug("{}: Finishing commit for transaction {}", persistenceId(), cohortEntry.getTransactionId());
317 cohortEntry.commit(new FutureCallback<UnsignedLong>() {
319 public void onSuccess(final UnsignedLong result) {
320 final TransactionIdentifier txId = cohortEntry.getTransactionId();
321 log.debug("{}: Transaction {} committed as {}, sending response to {}", persistenceId(), txId, result,
324 cohortCache.remove(cohortEntry.getTransactionId());
325 sender.tell(CommitTransactionReply.instance(cohortEntry.getClientVersion()).toSerializable(),
326 cohortEntry.getShard().self());
330 public void onFailure(final Throwable failure) {
331 log.error("{}, An exception occurred while committing transaction {}", persistenceId(),
332 cohortEntry.getTransactionId(), failure);
334 cohortCache.remove(cohortEntry.getTransactionId());
335 sender.tell(new Failure(failure), cohortEntry.getShard().self());
341 * This method handles the preCommit and commit phases for a transaction.
343 * @param transactionID the ID of the transaction to commit
344 * @param sender the actor to which to send the response
345 * @param shard the transaction's shard actor
347 void handleCommit(final Identifier transactionID, final ActorRef sender, final Shard shard) {
348 final CohortEntry cohortEntry = cohortCache.get(transactionID);
349 if (cohortEntry == null) {
350 // Either a long time passed between canCommit and commit and the entry was expired from the cache
351 // or it was aborted.
352 IllegalStateException ex = new IllegalStateException(
353 String.format("%s: Cannot commit transaction %s - no cohort entry found", name, transactionID));
354 log.error(ex.getMessage());
355 sender.tell(new Failure(ex), shard.self());
359 cohortEntry.setReplySender(sender);
360 doCommit(cohortEntry);
363 @SuppressWarnings("checkstyle:IllegalCatch")
364 void handleAbort(final Identifier transactionID, final ActorRef sender, final Shard shard) {
365 CohortEntry cohortEntry = cohortCache.remove(transactionID);
366 if (cohortEntry == null) {
370 log.debug("{}: Aborting transaction {}", name, transactionID);
372 final ActorRef self = shard.getSelf();
376 shard.getShardMBean().incrementAbortTransactionsCount();
378 if (sender != null) {
379 sender.tell(AbortTransactionReply.instance(cohortEntry.getClientVersion()).toSerializable(), self);
381 } catch (InterruptedException | ExecutionException | TimeoutException e) {
382 log.error("{}: An exception happened during abort", name, e);
384 if (sender != null) {
385 sender.tell(new Failure(e), self);
390 void checkForExpiredTransactions(final long timeout, final Shard shard) {
391 Iterator<CohortEntry> iter = cohortCache.values().iterator();
392 while (iter.hasNext()) {
393 CohortEntry cohortEntry = iter.next();
394 if (cohortEntry.isFailed()) {
400 void abortPendingTransactions(final String reason, final Shard shard) {
401 final Failure failure = new Failure(new RuntimeException(reason));
402 Collection<ShardDataTreeCohort> pending = dataTree.getAndClearPendingTransactions();
404 log.debug("{}: Aborting {} pending queued transactions", name, pending.size());
406 for (ShardDataTreeCohort cohort : pending) {
407 CohortEntry cohortEntry = cohortCache.remove(cohort.getIdentifier());
408 if (cohortEntry == null) {
412 if (cohortEntry.getReplySender() != null) {
413 cohortEntry.getReplySender().tell(failure, shard.self());
420 Collection<?> convertPendingTransactionsToMessages(final int maxModificationsPerBatch) {
421 final Collection<VersionedExternalizableMessage> messages = new ArrayList<>();
422 for (ShardDataTreeCohort cohort : dataTree.getAndClearPendingTransactions()) {
423 CohortEntry cohortEntry = cohortCache.remove(cohort.getIdentifier());
424 if (cohortEntry == null) {
428 final Deque<BatchedModifications> newMessages = new ArrayDeque<>();
429 cohortEntry.getDataTreeModification().applyToCursor(new AbstractBatchedModificationsCursor() {
431 protected BatchedModifications getModifications() {
432 final BatchedModifications lastBatch = newMessages.peekLast();
434 if (lastBatch != null && lastBatch.getModifications().size() >= maxModificationsPerBatch) {
438 // Allocate a new message
439 final BatchedModifications ret = new BatchedModifications(cohortEntry.getTransactionId(),
440 cohortEntry.getClientVersion());
441 newMessages.add(ret);
446 final BatchedModifications last = newMessages.peekLast();
448 final boolean immediate = cohortEntry.isDoImmediateCommit();
449 last.setDoCommitOnReady(immediate);
451 last.setTotalMessagesSent(newMessages.size());
453 messages.addAll(newMessages);
456 switch (cohort.getState()) {
457 case CAN_COMMIT_COMPLETE:
458 case CAN_COMMIT_PENDING:
459 messages.add(new CanCommitTransaction(cohortEntry.getTransactionId(),
460 cohortEntry.getClientVersion()));
462 case PRE_COMMIT_COMPLETE:
463 case PRE_COMMIT_PENDING:
464 messages.add(new CommitTransaction(cohortEntry.getTransactionId(),
465 cohortEntry.getClientVersion()));
478 void setCohortDecorator(final CohortDecorator cohortDecorator) {
479 this.cohortDecorator = cohortDecorator;