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;
12 import akka.serialization.Serialization;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.base.Preconditions;
15 import com.google.common.cache.Cache;
16 import com.google.common.cache.CacheBuilder;
17 import com.google.common.cache.RemovalCause;
18 import com.google.common.cache.RemovalListener;
19 import com.google.common.cache.RemovalNotification;
20 import java.util.LinkedList;
21 import java.util.Queue;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeUnit;
24 import org.opendaylight.controller.cluster.datastore.compat.BackwardsCompatibleThreePhaseCommitCohort;
25 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
26 import org.opendaylight.controller.cluster.datastore.messages.BatchedModificationsReply;
27 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
28 import org.opendaylight.controller.cluster.datastore.messages.ForwardedReadyTransaction;
29 import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction;
30 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
31 import org.opendaylight.controller.cluster.datastore.modification.Modification;
32 import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
33 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
34 import org.slf4j.Logger;
37 * Coordinates commits for a shard ensuring only one concurrent 3-phase commit.
39 * @author Thomas Pantelis
41 public class ShardCommitCoordinator {
43 // Interface hook for unit tests to replace or decorate the DOMStoreThreePhaseCommitCohorts.
44 public interface CohortDecorator {
45 ShardDataTreeCohort decorate(String transactionID, ShardDataTreeCohort actual);
48 private final Cache<String, CohortEntry> cohortCache;
50 private CohortEntry currentCohortEntry;
52 private final ShardDataTree dataTree;
54 private final Queue<CohortEntry> queuedCohortEntries;
56 private int queueCapacity;
58 private final Logger log;
60 private final String name;
62 private final RemovalListener<String, CohortEntry> cacheRemovalListener =
63 new RemovalListener<String, CohortEntry>() {
65 public void onRemoval(RemovalNotification<String, CohortEntry> notification) {
66 if(notification.getCause() == RemovalCause.EXPIRED) {
67 log.warn("{}: Transaction {} was timed out of the cache", name, notification.getKey());
72 // This is a hook for unit tests to replace or decorate the DOMStoreThreePhaseCommitCohorts.
73 private CohortDecorator cohortDecorator;
75 private ReadyTransactionReply readyTransactionReply;
77 public ShardCommitCoordinator(ShardDataTree dataTree,
78 long cacheExpiryTimeoutInSec, int queueCapacity, ActorRef shardActor, Logger log, String name) {
80 this.queueCapacity = queueCapacity;
83 this.dataTree = Preconditions.checkNotNull(dataTree);
85 cohortCache = CacheBuilder.newBuilder().expireAfterAccess(cacheExpiryTimeoutInSec, TimeUnit.SECONDS).
86 removalListener(cacheRemovalListener).build();
88 // We use a LinkedList here to avoid synchronization overhead with concurrent queue impls
89 // since this should only be accessed on the shard's dispatcher.
90 queuedCohortEntries = new LinkedList<>();
93 public void setQueueCapacity(int queueCapacity) {
94 this.queueCapacity = queueCapacity;
97 private ReadyTransactionReply readyTransactionReply(Shard shard) {
98 if(readyTransactionReply == null) {
99 readyTransactionReply = new ReadyTransactionReply(Serialization.serializedActorPath(shard.self()));
102 return readyTransactionReply;
106 * This method is called to ready a transaction that was prepared by ShardTransaction actor. It caches
107 * the prepared cohort entry for the given transactions ID in preparation for the subsequent 3-phase commit.
109 public void handleForwardedReadyTransaction(ForwardedReadyTransaction ready, ActorRef sender, Shard shard) {
110 log.debug("{}: Readying transaction {}, client version {}", name,
111 ready.getTransactionID(), ready.getTxnClientVersion());
113 CohortEntry cohortEntry = new CohortEntry(ready.getTransactionID(), ready.getCohort(),
114 (MutableCompositeModification) ready.getModification());
115 cohortCache.put(ready.getTransactionID(), cohortEntry);
117 if(ready.getTxnClientVersion() < DataStoreVersions.LITHIUM_VERSION) {
118 // Return our actor path as we'll handle the three phase commit except if the Tx client
119 // version < Helium-1 version which means the Tx was initiated by a base Helium version node.
120 // In that case, the subsequent 3-phase commit messages won't contain the transactionId so to
121 // maintain backwards compatibility, we create a separate cohort actor to provide the compatible behavior.
122 ActorRef replyActorPath = shard.self();
123 if(ready.getTxnClientVersion() < DataStoreVersions.HELIUM_1_VERSION) {
124 log.debug("{}: Creating BackwardsCompatibleThreePhaseCommitCohort", name);
125 replyActorPath = shard.getContext().actorOf(BackwardsCompatibleThreePhaseCommitCohort.props(
126 ready.getTransactionID()));
129 ReadyTransactionReply readyTransactionReply =
130 new ReadyTransactionReply(Serialization.serializedActorPath(replyActorPath),
131 ready.getTxnClientVersion());
132 sender.tell(ready.isReturnSerialized() ? readyTransactionReply.toSerializable() :
133 readyTransactionReply, shard.self());
135 if(ready.isDoImmediateCommit()) {
136 cohortEntry.setDoImmediateCommit(true);
137 cohortEntry.setReplySender(sender);
138 cohortEntry.setShard(shard);
139 handleCanCommit(cohortEntry);
141 // The caller does not want immediate commit - the 3-phase commit will be coordinated by the
142 // front-end so send back a ReadyTransactionReply with our actor path.
143 sender.tell(readyTransactionReply(shard), shard.self());
149 * This method handles a BatchedModifications message for a transaction being prepared directly on the
150 * Shard actor instead of via a ShardTransaction actor. If there's no currently cached
151 * DOMStoreWriteTransaction, one is created. The batched modifications are applied to the write Tx. If
152 * the BatchedModifications is ready to commit then a DOMStoreThreePhaseCommitCohort is created.
154 * @param batched the BatchedModifications
155 * @param shardActor the transaction's shard actor
157 * @throws ExecutionException if an error occurs loading the cache
159 boolean handleBatchedModifications(BatchedModifications batched, ActorRef sender, Shard shard)
160 throws ExecutionException {
161 CohortEntry cohortEntry = cohortCache.getIfPresent(batched.getTransactionID());
162 if(cohortEntry == null) {
163 cohortEntry = new CohortEntry(batched.getTransactionID(),
164 dataTree.newReadWriteTransaction(batched.getTransactionID(),
165 batched.getTransactionChainID()));
166 cohortCache.put(batched.getTransactionID(), cohortEntry);
169 if(log.isDebugEnabled()) {
170 log.debug("{}: Applying {} batched modifications for Tx {}", name,
171 batched.getModifications().size(), batched.getTransactionID());
174 cohortEntry.applyModifications(batched.getModifications());
176 if(batched.isReady()) {
177 if(log.isDebugEnabled()) {
178 log.debug("{}: Readying Tx {}, client version {}", name,
179 batched.getTransactionID(), batched.getVersion());
182 cohortEntry.ready(cohortDecorator, batched.isDoCommitOnReady());
184 if(batched.isDoCommitOnReady()) {
185 cohortEntry.setReplySender(sender);
186 cohortEntry.setShard(shard);
187 handleCanCommit(cohortEntry);
189 sender.tell(readyTransactionReply(shard), shard.self());
192 sender.tell(new BatchedModificationsReply(batched.getModifications().size()), shard.self());
195 return batched.isReady();
199 * This method handles {@link ReadyLocalTransaction} message. All transaction modifications have
200 * been prepared beforehand by the sender and we just need to drive them through into the dataTree.
206 void handleReadyLocalTransaction(ReadyLocalTransaction message, ActorRef sender, Shard shard) {
207 final ShardDataTreeCohort cohort = new SimpleShardDataTreeCohort(dataTree, message.getModification());
208 final CohortEntry cohortEntry = new CohortEntry(message.getTransactionID(), cohort);
209 cohortCache.put(message.getTransactionID(), cohortEntry);
210 cohortEntry.setDoImmediateCommit(message.isDoCommitOnReady());
211 log.debug("{}: Applying local modifications for Tx {}", name, message.getTransactionID());
213 if (message.isDoCommitOnReady()) {
214 cohortEntry.setReplySender(sender);
215 cohortEntry.setShard(shard);
216 handleCanCommit(cohortEntry);
218 sender.tell(readyTransactionReply(shard), shard.self());
222 private void handleCanCommit(CohortEntry cohortEntry) {
223 String transactionID = cohortEntry.getTransactionID();
225 if(log.isDebugEnabled()) {
226 log.debug("{}: Processing canCommit for transaction {} for shard {}",
227 name, transactionID, cohortEntry.getShard().self().path());
230 if(currentCohortEntry != null) {
231 // There's already a Tx commit in progress - attempt to queue this entry to be
232 // committed after the current Tx completes.
233 log.debug("{}: Transaction {} is already in progress - queueing transaction {}",
234 name, currentCohortEntry.getTransactionID(), transactionID);
236 if(queuedCohortEntries.size() < queueCapacity) {
237 queuedCohortEntries.offer(cohortEntry);
239 removeCohortEntry(transactionID);
241 RuntimeException ex = new RuntimeException(
242 String.format("%s: Could not enqueue transaction %s - the maximum commit queue"+
243 " capacity %d has been reached.",
244 name, transactionID, queueCapacity));
245 log.error(ex.getMessage());
246 cohortEntry.getReplySender().tell(new Status.Failure(ex), cohortEntry.getShard().self());
249 // No Tx commit currently in progress - make this the current entry and proceed with
251 cohortEntry.updateLastAccessTime();
252 currentCohortEntry = cohortEntry;
254 doCanCommit(cohortEntry);
259 * This method handles the canCommit phase for a transaction.
261 * @param canCommit the CanCommitTransaction message
262 * @param sender the actor that sent the message
263 * @param shard the transaction's shard actor
265 public void handleCanCommit(String transactionID, final ActorRef sender, final Shard shard) {
266 // Lookup the cohort entry that was cached previously (or should have been) by
267 // transactionReady (via the ForwardedReadyTransaction message).
268 final CohortEntry cohortEntry = cohortCache.getIfPresent(transactionID);
269 if(cohortEntry == null) {
270 // Either canCommit was invoked before ready(shouldn't happen) or a long time passed
271 // between canCommit and ready and the entry was expired from the cache.
272 IllegalStateException ex = new IllegalStateException(
273 String.format("%s: No cohort entry found for transaction %s", name, transactionID));
274 log.error(ex.getMessage());
275 sender.tell(new Status.Failure(ex), shard.self());
279 cohortEntry.setReplySender(sender);
280 cohortEntry.setShard(shard);
282 handleCanCommit(cohortEntry);
285 private void doCanCommit(final CohortEntry cohortEntry) {
287 boolean canCommit = false;
289 // We block on the future here so we don't have to worry about possibly accessing our
290 // state on a different thread outside of our dispatcher. Also, the data store
291 // currently uses a same thread executor anyway.
292 canCommit = cohortEntry.getCohort().canCommit().get();
294 if(cohortEntry.isDoImmediateCommit()) {
296 doCommit(cohortEntry);
298 cohortEntry.getReplySender().tell(new Status.Failure(new TransactionCommitFailedException(
299 "Can Commit failed, no detailed cause available.")), cohortEntry.getShard().self());
302 cohortEntry.getReplySender().tell(
303 canCommit ? CanCommitTransactionReply.YES.toSerializable() :
304 CanCommitTransactionReply.NO.toSerializable(), cohortEntry.getShard().self());
306 } catch (Exception e) {
307 log.debug("{}: An exception occurred during canCommit: {}", name, e);
309 Throwable failure = e;
310 if(e instanceof ExecutionException) {
311 failure = e.getCause();
314 cohortEntry.getReplySender().tell(new Status.Failure(failure), cohortEntry.getShard().self());
317 // Remove the entry from the cache now.
318 currentTransactionComplete(cohortEntry.getTransactionID(), true);
323 private boolean doCommit(CohortEntry cohortEntry) {
324 log.debug("{}: Committing transaction {}", name, cohortEntry.getTransactionID());
326 boolean success = false;
328 // We perform the preCommit phase here atomically with the commit phase. This is an
329 // optimization to eliminate the overhead of an extra preCommit message. We lose front-end
330 // coordination of preCommit across shards in case of failure but preCommit should not
331 // normally fail since we ensure only one concurrent 3-phase commit.
334 // We block on the future here so we don't have to worry about possibly accessing our
335 // state on a different thread outside of our dispatcher. Also, the data store
336 // currently uses a same thread executor anyway.
337 cohortEntry.getCohort().preCommit().get();
339 cohortEntry.getShard().continueCommit(cohortEntry);
341 cohortEntry.updateLastAccessTime();
344 } catch (Exception e) {
345 log.error("{} An exception occurred while preCommitting transaction {}",
346 name, cohortEntry.getTransactionID(), e);
347 cohortEntry.getReplySender().tell(new akka.actor.Status.Failure(e), cohortEntry.getShard().self());
349 currentTransactionComplete(cohortEntry.getTransactionID(), true);
355 boolean handleCommit(final String transactionID, final ActorRef sender, final Shard shard) {
356 // Get the current in-progress cohort entry in the commitCoordinator if it corresponds to
358 final CohortEntry cohortEntry = getCohortEntryIfCurrent(transactionID);
359 if(cohortEntry == null) {
360 // We're not the current Tx - the Tx was likely expired b/c it took too long in
361 // between the canCommit and commit messages.
362 IllegalStateException ex = new IllegalStateException(
363 String.format("%s: Cannot commit transaction %s - it is not the current transaction",
364 name, transactionID));
365 log.error(ex.getMessage());
366 sender.tell(new akka.actor.Status.Failure(ex), shard.self());
370 return doCommit(cohortEntry);
374 * Returns the cohort entry for the Tx commit currently in progress if the given transaction ID
375 * matches the current entry.
377 * @param transactionID the ID of the transaction
378 * @return the current CohortEntry or null if the given transaction ID does not match the
381 public CohortEntry getCohortEntryIfCurrent(String transactionID) {
382 if(isCurrentTransaction(transactionID)) {
383 return currentCohortEntry;
389 public CohortEntry getCurrentCohortEntry() {
390 return currentCohortEntry;
393 public CohortEntry getAndRemoveCohortEntry(String transactionID) {
394 CohortEntry cohortEntry = cohortCache.getIfPresent(transactionID);
395 cohortCache.invalidate(transactionID);
399 public void removeCohortEntry(String transactionID) {
400 cohortCache.invalidate(transactionID);
403 public boolean isCurrentTransaction(String transactionID) {
404 return currentCohortEntry != null &&
405 currentCohortEntry.getTransactionID().equals(transactionID);
409 * This method is called when a transaction is complete, successful or not. If the given
410 * given transaction ID matches the current in-progress transaction, the next cohort entry,
411 * if any, is dequeued and processed.
413 * @param transactionID the ID of the completed transaction
414 * @param removeCohortEntry if true the CohortEntry for the transaction is also removed from
417 public void currentTransactionComplete(String transactionID, boolean removeCohortEntry) {
418 if(removeCohortEntry) {
419 removeCohortEntry(transactionID);
422 if(isCurrentTransaction(transactionID)) {
423 // Dequeue the next cohort entry waiting in the queue.
424 currentCohortEntry = queuedCohortEntries.poll();
425 if(currentCohortEntry != null) {
426 currentCohortEntry.updateLastAccessTime();
427 doCanCommit(currentCohortEntry);
433 void setCohortDecorator(CohortDecorator cohortDecorator) {
434 this.cohortDecorator = cohortDecorator;
438 static class CohortEntry {
439 private final String transactionID;
440 private ShardDataTreeCohort cohort;
441 private final ReadWriteShardDataTreeTransaction transaction;
442 private ActorRef replySender;
444 private long lastAccessTime;
445 private boolean doImmediateCommit;
447 CohortEntry(String transactionID, ReadWriteShardDataTreeTransaction transaction) {
448 this.transaction = Preconditions.checkNotNull(transaction);
449 this.transactionID = transactionID;
452 CohortEntry(String transactionID, ShardDataTreeCohort cohort,
453 MutableCompositeModification compositeModification) {
454 this.transactionID = transactionID;
455 this.cohort = cohort;
456 this.transaction = null;
459 CohortEntry(String transactionID, ShardDataTreeCohort cohort) {
460 this.transactionID = transactionID;
461 this.cohort = cohort;
462 this.transaction = null;
465 void updateLastAccessTime() {
466 lastAccessTime = System.currentTimeMillis();
469 long getLastAccessTime() {
470 return lastAccessTime;
473 String getTransactionID() {
474 return transactionID;
477 ShardDataTreeCohort getCohort() {
481 void applyModifications(Iterable<Modification> modifications) {
482 for (Modification modification : modifications) {
483 modification.apply(transaction.getSnapshot());
487 void ready(CohortDecorator cohortDecorator, boolean doImmediateCommit) {
488 Preconditions.checkState(cohort == null, "cohort was already set");
490 setDoImmediateCommit(doImmediateCommit);
492 cohort = transaction.ready();
494 if(cohortDecorator != null) {
495 // Call the hook for unit tests.
496 cohort = cohortDecorator.decorate(transactionID, cohort);
500 boolean isDoImmediateCommit() {
501 return doImmediateCommit;
504 void setDoImmediateCommit(boolean doImmediateCommit) {
505 this.doImmediateCommit = doImmediateCommit;
508 ActorRef getReplySender() {
512 void setReplySender(ActorRef replySender) {
513 this.replySender = replySender;
520 void setShard(Shard shard) {