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 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;
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 = Preconditions.checkNotNull(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
102 void handleForwardedReadyTransaction(final ForwardedReadyTransaction ready, final ActorRef sender,
104 log.debug("{}: Readying transaction {}, client version {}", name,
105 ready.getTransactionID(), ready.getTxnClientVersion());
107 final ShardDataTreeCohort cohort = ready.getTransaction().ready();
108 final CohortEntry cohortEntry = CohortEntry.createReady(cohort, ready.getTxnClientVersion());
109 cohortCache.put(cohortEntry.getTransactionID(), cohortEntry);
111 if (ready.isDoImmediateCommit()) {
112 cohortEntry.setDoImmediateCommit(true);
113 cohortEntry.setReplySender(sender);
114 cohortEntry.setShard(shard);
115 handleCanCommit(cohortEntry);
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());
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.
129 * @param batched the BatchedModifications message to process
130 * @param sender the sender of the message
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);
140 if (log.isDebugEnabled()) {
141 log.debug("{}: Applying {} batched modifications for Tx {}", name,
142 batched.getModifications().size(), batched.getTransactionID());
145 cohortEntry.applyModifications(batched.getModifications());
147 if (batched.isReady()) {
148 if (cohortEntry.getLastBatchedModificationsException() != null) {
149 cohortCache.remove(cohortEntry.getTransactionID());
150 throw cohortEntry.getLastBatchedModificationsException();
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()));
160 if (log.isDebugEnabled()) {
161 log.debug("{}: Readying Tx {}, client version {}", name,
162 batched.getTransactionID(), batched.getVersion());
165 cohortEntry.setDoImmediateCommit(batched.isDoCommitOnReady());
166 cohortEntry.ready(cohortDecorator);
168 if (batched.isDoCommitOnReady()) {
169 cohortEntry.setReplySender(sender);
170 cohortEntry.setShard(shard);
171 handleCanCommit(cohortEntry);
173 sender.tell(readyTransactionReply(shard.self()), shard.self());
176 sender.tell(new BatchedModificationsReply(batched.getModifications().size()), shard.self());
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
185 * @param message the ReadyLocalTransaction message to process
186 * @param sender the sender of the message
187 * @param shard the transaction's shard actor
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());
196 log.debug("{}: Applying local modifications for Tx {}", name, message.getTransactionID());
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 last.setReady(from.isReady());
232 last.setTotalMessagesSent(newModifications.size());
233 return newModifications;
236 private void handleCanCommit(final CohortEntry cohortEntry) {
237 cohortEntry.canCommit(new FutureCallback<Void>() {
239 public void onSuccess(final Void result) {
240 log.debug("{}: canCommit for {}: success", name, cohortEntry.getTransactionID());
242 if (cohortEntry.isDoImmediateCommit()) {
243 doCommit(cohortEntry);
245 cohortEntry.getReplySender().tell(
246 CanCommitTransactionReply.yes(cohortEntry.getClientVersion()).toSerializable(),
247 cohortEntry.getShard().self());
252 public void onFailure(final Throwable t) {
253 log.debug("{}: An exception occurred during canCommit for {}: {}", name,
254 cohortEntry.getTransactionID(), t);
256 cohortCache.remove(cohortEntry.getTransactionID());
257 cohortEntry.getReplySender().tell(new Failure(t), cohortEntry.getShard().self());
263 * This method handles the canCommit phase for a transaction.
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
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());
283 cohortEntry.setReplySender(sender);
284 cohortEntry.setShard(shard);
286 handleCanCommit(cohortEntry);
289 private void doCommit(final CohortEntry cohortEntry) {
290 log.debug("{}: Committing transaction {}", name, cohortEntry.getTransactionID());
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>() {
298 public void onSuccess(final DataTreeCandidate candidate) {
299 finishCommit(cohortEntry.getReplySender(), cohortEntry);
303 public void onFailure(final Throwable t) {
304 log.error("{} An exception occurred while preCommitting transaction {}", name,
305 cohortEntry.getTransactionID(), t);
307 cohortCache.remove(cohortEntry.getTransactionID());
308 cohortEntry.getReplySender().tell(new Failure(t), cohortEntry.getShard().self());
313 private void finishCommit(@Nonnull final ActorRef sender, @Nonnull final CohortEntry cohortEntry) {
314 log.debug("{}: Finishing commit for transaction {}", persistenceId(), cohortEntry.getTransactionID());
316 cohortEntry.commit(new FutureCallback<UnsignedLong>() {
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,
323 cohortCache.remove(cohortEntry.getTransactionID());
324 sender.tell(CommitTransactionReply.instance(cohortEntry.getClientVersion()).toSerializable(),
325 cohortEntry.getShard().self());
329 public void onFailure(final Throwable t) {
330 log.error("{}, An exception occurred while committing transaction {}", persistenceId(),
331 cohortEntry.getTransactionID(), t);
333 cohortCache.remove(cohortEntry.getTransactionID());
334 sender.tell(new Failure(t), cohortEntry.getShard().self());
340 * This method handles the preCommit and commit phases for a transaction.
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
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());
358 cohortEntry.setReplySender(sender);
359 doCommit(cohortEntry);
362 void handleAbort(final Identifier transactionID, final ActorRef sender, final Shard shard) {
363 CohortEntry cohortEntry = cohortCache.remove(transactionID);
364 if (cohortEntry == null) {
368 log.debug("{}: Aborting transaction {}", name, transactionID);
370 final ActorRef self = shard.getSelf();
374 shard.getShardMBean().incrementAbortTransactionsCount();
376 if (sender != null) {
377 sender.tell(AbortTransactionReply.instance(cohortEntry.getClientVersion()).toSerializable(), self);
379 } catch (Exception e) {
380 log.error("{}: An exception happened during abort", name, e);
382 if (sender != null) {
383 sender.tell(new Failure(e), self);
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()) {
398 void abortPendingTransactions(final String reason, final Shard shard) {
399 final Failure failure = new Failure(new RuntimeException(reason));
400 Collection<ShardDataTreeCohort> pending = dataTree.getAndClearPendingTransactions();
402 log.debug("{}: Aborting {} pending queued transactions", name, pending.size());
404 for (ShardDataTreeCohort cohort : pending) {
405 CohortEntry cohortEntry = cohortCache.remove(cohort.getIdentifier());
406 if (cohortEntry == null) {
410 if (cohortEntry.getReplySender() != null) {
411 cohortEntry.getReplySender().tell(failure, shard.self());
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) {
426 final Deque<BatchedModifications> newMessages = new ArrayDeque<>();
427 cohortEntry.getDataTreeModification().applyToCursor(new AbstractBatchedModificationsCursor() {
429 protected BatchedModifications getModifications() {
430 final BatchedModifications lastBatch = newMessages.peekLast();
432 if (lastBatch != null && lastBatch.getModifications().size() >= maxModificationsPerBatch) {
436 // Allocate a new message
437 final BatchedModifications ret = new BatchedModifications(cohortEntry.getTransactionID(),
438 cohortEntry.getClientVersion());
439 newMessages.add(ret);
444 final BatchedModifications last = newMessages.peekLast();
446 final boolean immediate = cohortEntry.isDoImmediateCommit();
447 last.setDoCommitOnReady(immediate);
449 last.setTotalMessagesSent(newMessages.size());
451 messages.addAll(newMessages);
454 switch (cohort.getState()) {
455 case CAN_COMMIT_COMPLETE:
456 case CAN_COMMIT_PENDING:
457 messages.add(new CanCommitTransaction(cohortEntry.getTransactionID(),
458 cohortEntry.getClientVersion()));
460 case PRE_COMMIT_COMPLETE:
461 case PRE_COMMIT_PENDING:
462 messages.add(new CommitTransaction(cohortEntry.getTransactionID(),
463 cohortEntry.getClientVersion()));
476 void setCohortDecorator(final CohortDecorator cohortDecorator) {
477 this.cohortDecorator = cohortDecorator;