2 * Copyright (c) 2014 Cisco 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
9 package org.opendaylight.controller.cluster.datastore;
11 import akka.actor.ActorSelection;
12 import akka.dispatch.Mapper;
13 import akka.dispatch.OnComplete;
14 import com.google.common.annotations.VisibleForTesting;
15 import com.google.common.base.FinalizablePhantomReference;
16 import com.google.common.base.FinalizableReferenceQueue;
17 import com.google.common.base.Optional;
18 import com.google.common.base.Preconditions;
19 import com.google.common.collect.Lists;
20 import com.google.common.util.concurrent.CheckedFuture;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import com.google.common.util.concurrent.SettableFuture;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
29 import java.util.concurrent.ConcurrentHashMap;
30 import java.util.concurrent.Semaphore;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.atomic.AtomicBoolean;
33 import java.util.concurrent.atomic.AtomicLong;
34 import javax.annotation.concurrent.GuardedBy;
35 import org.opendaylight.controller.cluster.datastore.compat.PreLithiumTransactionContextImpl;
36 import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
37 import org.opendaylight.controller.cluster.datastore.identifiers.TransactionIdentifier;
38 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
39 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
40 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
41 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
42 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
43 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
44 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
45 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
46 import org.opendaylight.yangtools.util.concurrent.MappingCheckedFuture;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
49 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import scala.concurrent.Future;
53 import scala.concurrent.Promise;
54 import scala.concurrent.duration.FiniteDuration;
57 * TransactionProxy acts as a proxy for one or more transactions that were created on a remote shard
59 * Creating a transaction on the consumer side will create one instance of a transaction proxy. If during
60 * the transaction reads and writes are done on data that belongs to different shards then a separate transaction will
61 * be created on each of those shards by the TransactionProxy
64 * The TransactionProxy does not make any guarantees about atomicity or order in which the transactions on the various
65 * shards will be executed.
68 public class TransactionProxy implements DOMStoreReadWriteTransaction {
70 public static enum TransactionType {
75 public static TransactionType fromInt(int type) {
76 if(type == WRITE_ONLY.ordinal()) {
78 } else if(type == READ_WRITE.ordinal()) {
80 } else if(type == READ_ONLY.ordinal()) {
83 throw new IllegalArgumentException("In TransactionType enum value" + type);
88 static final Mapper<Throwable, Throwable> SAME_FAILURE_TRANSFORMER =
89 new Mapper<Throwable, Throwable>() {
91 public Throwable apply(Throwable failure) {
96 private static final AtomicLong counter = new AtomicLong();
98 private static final Logger LOG = LoggerFactory.getLogger(TransactionProxy.class);
101 * Time interval in between transaction create retries.
103 private static final FiniteDuration CREATE_TX_TRY_INTERVAL =
104 FiniteDuration.create(1, TimeUnit.SECONDS);
107 * Used to enqueue the PhantomReferences for read-only TransactionProxy instances. The
108 * FinalizableReferenceQueue is safe to use statically in an OSGi environment as it uses some
109 * trickery to clean up its internal thread when the bundle is unloaded.
111 private static final FinalizableReferenceQueue phantomReferenceQueue =
112 new FinalizableReferenceQueue();
115 * This stores the TransactionProxyCleanupPhantomReference instances statically, This is
116 * necessary because PhantomReferences need a hard reference so they're not garbage collected.
117 * Once finalized, the TransactionProxyCleanupPhantomReference removes itself from this map
118 * and thus becomes eligible for garbage collection.
120 private static final Map<TransactionProxyCleanupPhantomReference,
121 TransactionProxyCleanupPhantomReference> phantomReferenceCache =
122 new ConcurrentHashMap<>();
125 * A PhantomReference that closes remote transactions for a TransactionProxy when it's
126 * garbage collected. This is used for read-only transactions as they're not explicitly closed
127 * by clients. So the only way to detect that a transaction is no longer in use and it's safe
128 * to clean up is when it's garbage collected. It's inexact as to when an instance will be GC'ed
129 * but TransactionProxy instances should generally be short-lived enough to avoid being moved
130 * to the old generation space and thus should be cleaned up in a timely manner as the GC
131 * runs on the young generation (eden, swap1...) space much more frequently.
133 private static class TransactionProxyCleanupPhantomReference
134 extends FinalizablePhantomReference<TransactionProxy> {
136 private final List<ActorSelection> remoteTransactionActors;
137 private final AtomicBoolean remoteTransactionActorsMB;
138 private final ActorContext actorContext;
139 private final TransactionIdentifier identifier;
141 protected TransactionProxyCleanupPhantomReference(TransactionProxy referent) {
142 super(referent, phantomReferenceQueue);
144 // Note we need to cache the relevant fields from the TransactionProxy as we can't
145 // have a hard reference to the TransactionProxy instance itself.
147 remoteTransactionActors = referent.remoteTransactionActors;
148 remoteTransactionActorsMB = referent.remoteTransactionActorsMB;
149 actorContext = referent.actorContext;
150 identifier = referent.identifier;
154 public void finalizeReferent() {
155 LOG.trace("Cleaning up {} Tx actors for TransactionProxy {}",
156 remoteTransactionActors.size(), identifier);
158 phantomReferenceCache.remove(this);
160 // Access the memory barrier volatile to ensure all previous updates to the
161 // remoteTransactionActors list are visible to this thread.
163 if(remoteTransactionActorsMB.get()) {
164 for(ActorSelection actor : remoteTransactionActors) {
165 LOG.trace("Sending CloseTransaction to {}", actor);
166 actorContext.sendOperationAsync(actor, CloseTransaction.INSTANCE.toSerializable());
173 * Stores the remote Tx actors for each requested data store path to be used by the
174 * PhantomReference to close the remote Tx's. This is only used for read-only Tx's. The
175 * remoteTransactionActorsMB volatile serves as a memory barrier to publish updates to the
176 * remoteTransactionActors list so they will be visible to the thread accessing the
179 private List<ActorSelection> remoteTransactionActors;
180 private volatile AtomicBoolean remoteTransactionActorsMB;
183 * Stores the create transaction results per shard.
185 private final Map<String, TransactionFutureCallback> txFutureCallbackMap = new HashMap<>();
187 private final TransactionType transactionType;
188 private final ActorContext actorContext;
189 private final TransactionIdentifier identifier;
190 private final String transactionChainId;
191 private final SchemaContext schemaContext;
192 private boolean inReadyState;
194 private volatile boolean initialized;
195 private Semaphore operationLimiter;
196 private OperationCompleter operationCompleter;
198 public TransactionProxy(ActorContext actorContext, TransactionType transactionType) {
199 this(actorContext, transactionType, "");
202 public TransactionProxy(ActorContext actorContext, TransactionType transactionType,
203 String transactionChainId) {
204 this.actorContext = Preconditions.checkNotNull(actorContext,
205 "actorContext should not be null");
206 this.transactionType = Preconditions.checkNotNull(transactionType,
207 "transactionType should not be null");
208 this.schemaContext = Preconditions.checkNotNull(actorContext.getSchemaContext(),
209 "schemaContext should not be null");
210 this.transactionChainId = transactionChainId;
212 String memberName = actorContext.getCurrentMemberName();
213 if(memberName == null){
214 memberName = "UNKNOWN-MEMBER";
217 this.identifier = new TransactionIdentifier(memberName, counter.getAndIncrement());
219 LOG.debug("Created txn {} of type {} on chain {}", identifier, transactionType, transactionChainId);
223 List<Future<Object>> getRecordedOperationFutures() {
224 List<Future<Object>> recordedOperationFutures = Lists.newArrayList();
225 for(TransactionFutureCallback txFutureCallback : txFutureCallbackMap.values()) {
226 TransactionContext transactionContext = txFutureCallback.getTransactionContext();
227 if(transactionContext != null) {
228 recordedOperationFutures.addAll(transactionContext.getRecordedOperationFutures());
232 return recordedOperationFutures;
236 boolean hasTransactionContext() {
237 for(TransactionFutureCallback txFutureCallback : txFutureCallbackMap.values()) {
238 TransactionContext transactionContext = txFutureCallback.getTransactionContext();
239 if(transactionContext != null) {
248 public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final YangInstanceIdentifier path) {
250 Preconditions.checkState(transactionType != TransactionType.WRITE_ONLY,
251 "Read operation on write-only transaction is not allowed");
253 LOG.debug("Tx {} read {}", identifier, path);
257 final SettableFuture<Optional<NormalizedNode<?, ?>>> proxyFuture = SettableFuture.create();
259 TransactionFutureCallback txFutureCallback = getOrCreateTxFutureCallback(path);
260 txFutureCallback.enqueueTransactionOperation(new TransactionOperation() {
262 public void invoke(TransactionContext transactionContext) {
263 transactionContext.readData(path, proxyFuture);
267 return MappingCheckedFuture.create(proxyFuture, ReadFailedException.MAPPER);
271 public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
273 Preconditions.checkState(transactionType != TransactionType.WRITE_ONLY,
274 "Exists operation on write-only transaction is not allowed");
276 LOG.debug("Tx {} exists {}", identifier, path);
280 final SettableFuture<Boolean> proxyFuture = SettableFuture.create();
282 TransactionFutureCallback txFutureCallback = getOrCreateTxFutureCallback(path);
283 txFutureCallback.enqueueTransactionOperation(new TransactionOperation() {
285 public void invoke(TransactionContext transactionContext) {
286 transactionContext.dataExists(path, proxyFuture);
290 return MappingCheckedFuture.create(proxyFuture, ReadFailedException.MAPPER);
293 private void checkModificationState() {
294 Preconditions.checkState(transactionType != TransactionType.READ_ONLY,
295 "Modification operation on read-only transaction is not allowed");
296 Preconditions.checkState(!inReadyState,
297 "Transaction is sealed - further modifications are not allowed");
300 private void throttleOperation() {
301 throttleOperation(1);
304 private void throttleOperation(int acquirePermits) {
306 // Note : Currently mailbox-capacity comes from akka.conf and not from the config-subsystem
307 operationLimiter = new Semaphore(actorContext.getTransactionOutstandingOperationLimit());
308 operationCompleter = new OperationCompleter(operationLimiter);
310 // Make sure we write this last because it's volatile and will also publish the non-volatile writes
311 // above as well so they'll be visible to other threads.
316 if(!operationLimiter.tryAcquire(acquirePermits,
317 actorContext.getDatastoreContext().getOperationTimeoutInSeconds(), TimeUnit.SECONDS)){
318 LOG.warn("Failed to acquire operation permit for transaction {}", getIdentifier());
320 } catch (InterruptedException e) {
321 if(LOG.isDebugEnabled()) {
322 LOG.debug("Interrupted when trying to acquire operation permit for transaction " + getIdentifier().toString(), e);
324 LOG.warn("Interrupted when trying to acquire operation permit for transaction {}", getIdentifier());
331 public void write(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
333 checkModificationState();
335 LOG.debug("Tx {} write {}", identifier, path);
339 TransactionFutureCallback txFutureCallback = getOrCreateTxFutureCallback(path);
340 txFutureCallback.enqueueTransactionOperation(new TransactionOperation() {
342 public void invoke(TransactionContext transactionContext) {
343 transactionContext.writeData(path, data);
349 public void merge(final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
351 checkModificationState();
353 LOG.debug("Tx {} merge {}", identifier, path);
357 TransactionFutureCallback txFutureCallback = getOrCreateTxFutureCallback(path);
358 txFutureCallback.enqueueTransactionOperation(new TransactionOperation() {
360 public void invoke(TransactionContext transactionContext) {
361 transactionContext.mergeData(path, data);
367 public void delete(final YangInstanceIdentifier path) {
369 checkModificationState();
371 LOG.debug("Tx {} delete {}", identifier, path);
375 TransactionFutureCallback txFutureCallback = getOrCreateTxFutureCallback(path);
376 txFutureCallback.enqueueTransactionOperation(new TransactionOperation() {
378 public void invoke(TransactionContext transactionContext) {
379 transactionContext.deleteData(path);
385 public DOMStoreThreePhaseCommitCohort ready() {
387 checkModificationState();
391 LOG.debug("Tx {} Readying {} transactions for commit", identifier,
392 txFutureCallbackMap.size());
394 if(txFutureCallbackMap.size() == 0) {
395 onTransactionReady(Collections.<Future<ActorSelection>>emptyList());
396 TransactionRateLimitingCallback.adjustRateLimitForUnusedTransaction(actorContext);
397 return NoOpDOMStoreThreePhaseCommitCohort.INSTANCE;
400 throttleOperation(txFutureCallbackMap.size());
402 List<Future<ActorSelection>> cohortFutures = Lists.newArrayList();
404 for(TransactionFutureCallback txFutureCallback : txFutureCallbackMap.values()) {
406 LOG.debug("Tx {} Readying transaction for shard {} chain {}", identifier,
407 txFutureCallback.getShardName(), transactionChainId);
409 final TransactionContext transactionContext = txFutureCallback.getTransactionContext();
410 final Future<ActorSelection> future;
411 if (transactionContext != null) {
412 // avoid the creation of a promise and a TransactionOperation
413 future = transactionContext.readyTransaction();
415 final Promise<ActorSelection> promise = akka.dispatch.Futures.promise();
416 txFutureCallback.enqueueTransactionOperation(new TransactionOperation() {
418 public void invoke(TransactionContext transactionContext) {
419 promise.completeWith(transactionContext.readyTransaction());
422 future = promise.future();
425 cohortFutures.add(future);
428 onTransactionReady(cohortFutures);
430 return new ThreePhaseCommitCohortProxy(actorContext, cohortFutures,
431 identifier.toString());
435 * Method for derived classes to be notified when the transaction has been readied.
437 * @param cohortFutures the cohort Futures for each shard transaction.
439 protected void onTransactionReady(List<Future<ActorSelection>> cohortFutures) {
443 public Object getIdentifier() {
444 return this.identifier;
448 public void close() {
449 for (TransactionFutureCallback txFutureCallback : txFutureCallbackMap.values()) {
450 txFutureCallback.enqueueTransactionOperation(new TransactionOperation() {
452 public void invoke(TransactionContext transactionContext) {
453 transactionContext.closeTransaction();
458 txFutureCallbackMap.clear();
460 if(remoteTransactionActorsMB != null) {
461 remoteTransactionActors.clear();
462 remoteTransactionActorsMB.set(true);
466 private String shardNameFromIdentifier(YangInstanceIdentifier path){
467 return ShardStrategyFactory.getStrategy(path).findShard(path);
470 protected Future<ActorSelection> sendFindPrimaryShardAsync(String shardName) {
471 return actorContext.findPrimaryShardAsync(shardName);
474 private TransactionFutureCallback getOrCreateTxFutureCallback(YangInstanceIdentifier path) {
475 String shardName = shardNameFromIdentifier(path);
476 TransactionFutureCallback txFutureCallback = txFutureCallbackMap.get(shardName);
477 if(txFutureCallback == null) {
478 Future<ActorSelection> findPrimaryFuture = sendFindPrimaryShardAsync(shardName);
480 final TransactionFutureCallback newTxFutureCallback = new TransactionFutureCallback(shardName);
482 txFutureCallback = newTxFutureCallback;
483 txFutureCallbackMap.put(shardName, txFutureCallback);
485 findPrimaryFuture.onComplete(new OnComplete<ActorSelection>() {
487 public void onComplete(Throwable failure, ActorSelection primaryShard) {
488 if(failure != null) {
489 newTxFutureCallback.createTransactionContext(failure, null);
491 newTxFutureCallback.setPrimaryShard(primaryShard);
494 }, actorContext.getClientDispatcher());
497 return txFutureCallback;
500 public String getTransactionChainId() {
501 return transactionChainId;
504 protected ActorContext getActorContext() {
509 * Interfaces for transaction operations to be invoked later.
511 private static interface TransactionOperation {
512 void invoke(TransactionContext transactionContext);
516 * Implements a Future OnComplete callback for a CreateTransaction message. This class handles
517 * retries, up to a limit, if the shard doesn't have a leader yet. This is done by scheduling a
518 * retry task after a short delay.
520 * The end result from a completed CreateTransaction message is a TransactionContext that is
521 * used to perform transaction operations. Transaction operations that occur before the
522 * CreateTransaction completes are cache and executed once the CreateTransaction completes,
523 * successfully or not.
525 private class TransactionFutureCallback extends OnComplete<Object> {
528 * The list of transaction operations to execute once the CreateTransaction completes.
530 @GuardedBy("txOperationsOnComplete")
531 private final List<TransactionOperation> txOperationsOnComplete = Lists.newArrayList();
534 * The TransactionContext resulting from the CreateTransaction reply.
536 private volatile TransactionContext transactionContext;
539 * The target primary shard.
541 private volatile ActorSelection primaryShard;
543 private volatile int createTxTries = (int) (actorContext.getDatastoreContext().
544 getShardLeaderElectionTimeout().duration().toMillis() /
545 CREATE_TX_TRY_INTERVAL.toMillis());
547 private final String shardName;
549 TransactionFutureCallback(String shardName) {
550 this.shardName = shardName;
553 String getShardName() {
557 TransactionContext getTransactionContext() {
558 return transactionContext;
563 * Sets the target primary shard and initiates a CreateTransaction try.
565 void setPrimaryShard(ActorSelection primaryShard) {
566 this.primaryShard = primaryShard;
568 if(transactionType == TransactionType.WRITE_ONLY &&
569 actorContext.getDatastoreContext().isWriteOnlyTransactionOptimizationsEnabled()) {
570 LOG.debug("Tx {} Primary shard {} found - creating WRITE_ONLY transaction context",
571 identifier, primaryShard);
573 // For write-only Tx's we prepare the transaction modifications directly on the shard actor
574 // to avoid the overhead of creating a separate transaction actor.
575 // FIXME: can't assume the shard version is LITHIUM_VERSION - need to obtain it somehow.
576 executeTxOperatonsOnComplete(createValidTransactionContext(this.primaryShard,
577 this.primaryShard.path().toString(), DataStoreVersions.LITHIUM_VERSION));
579 tryCreateTransaction();
584 * Adds a TransactionOperation to be executed after the CreateTransaction completes.
586 void addTxOperationOnComplete(TransactionOperation operation) {
587 boolean invokeOperation = true;
588 synchronized(txOperationsOnComplete) {
589 if(transactionContext == null) {
590 LOG.debug("Tx {} Adding operation on complete", identifier);
592 invokeOperation = false;
593 txOperationsOnComplete.add(operation);
597 if(invokeOperation) {
598 operation.invoke(transactionContext);
602 void enqueueTransactionOperation(final TransactionOperation op) {
604 if (transactionContext != null) {
605 op.invoke(transactionContext);
607 // The shard Tx hasn't been created yet so add the Tx operation to the Tx Future
608 // callback to be executed after the Tx is created.
609 addTxOperationOnComplete(op);
614 * Performs a CreateTransaction try async.
616 private void tryCreateTransaction() {
617 if(LOG.isDebugEnabled()) {
618 LOG.debug("Tx {} Primary shard {} found - trying create transaction", identifier, primaryShard);
621 Object serializedCreateMessage = new CreateTransaction(identifier.toString(),
622 TransactionProxy.this.transactionType.ordinal(),
623 getTransactionChainId()).toSerializable();
625 Future<Object> createTxFuture = actorContext.executeOperationAsync(primaryShard, serializedCreateMessage);
627 createTxFuture.onComplete(this, actorContext.getClientDispatcher());
631 public void onComplete(Throwable failure, Object response) {
632 if(failure instanceof NoShardLeaderException) {
633 // There's no leader for the shard yet - schedule and try again, unless we're out
634 // of retries. Note: createTxTries is volatile as it may be written by different
635 // threads however not concurrently, therefore decrementing it non-atomically here
637 if(--createTxTries > 0) {
638 LOG.debug("Tx {} Shard {} has no leader yet - scheduling create Tx retry",
639 identifier, shardName);
641 actorContext.getActorSystem().scheduler().scheduleOnce(CREATE_TX_TRY_INTERVAL,
645 tryCreateTransaction();
647 }, actorContext.getClientDispatcher());
652 createTransactionContext(failure, response);
655 private void createTransactionContext(Throwable failure, Object response) {
656 // Mainly checking for state violation here to perform a volatile read of "initialized" to
657 // ensure updates to operationLimter et al are visible to this thread (ie we're doing
658 // "piggy-back" synchronization here).
659 Preconditions.checkState(initialized, "Tx was not propertly initialized.");
661 // Create the TransactionContext from the response or failure. Store the new
662 // TransactionContext locally until we've completed invoking the
663 // TransactionOperations. This avoids thread timing issues which could cause
664 // out-of-order TransactionOperations. Eg, on a modification operation, if the
665 // TransactionContext is non-null, then we directly call the TransactionContext.
666 // However, at the same time, the code may be executing the cached
667 // TransactionOperations. So to avoid thus timing, we don't publish the
668 // TransactionContext until after we've executed all cached TransactionOperations.
669 TransactionContext localTransactionContext;
670 if(failure != null) {
671 LOG.debug("Tx {} Creating NoOpTransaction because of error", identifier, failure);
673 localTransactionContext = new NoOpTransactionContext(failure, identifier, operationLimiter);
674 } else if (response.getClass().equals(CreateTransactionReply.SERIALIZABLE_CLASS)) {
675 localTransactionContext = createValidTransactionContext(
676 CreateTransactionReply.fromSerializable(response));
678 IllegalArgumentException exception = new IllegalArgumentException(String.format(
679 "Invalid reply type %s for CreateTransaction", response.getClass()));
681 localTransactionContext = new NoOpTransactionContext(exception, identifier, operationLimiter);
684 executeTxOperatonsOnComplete(localTransactionContext);
687 private void executeTxOperatonsOnComplete(TransactionContext localTransactionContext) {
689 // Access to txOperationsOnComplete and transactionContext must be protected and atomic
690 // (ie synchronized) with respect to #addTxOperationOnComplete to handle timing
691 // issues and ensure no TransactionOperation is missed and that they are processed
692 // in the order they occurred.
694 // We'll make a local copy of the txOperationsOnComplete list to handle re-entrancy
695 // in case a TransactionOperation results in another transaction operation being
696 // queued (eg a put operation from a client read Future callback that is notified
698 Collection<TransactionOperation> operationsBatch = null;
699 synchronized(txOperationsOnComplete) {
700 if(txOperationsOnComplete.isEmpty()) {
701 // We're done invoking the TransactionOperations so we can now publish the
702 // TransactionContext.
703 transactionContext = localTransactionContext;
707 operationsBatch = new ArrayList<>(txOperationsOnComplete);
708 txOperationsOnComplete.clear();
711 // Invoke TransactionOperations outside the sync block to avoid unnecessary blocking.
712 // A slight down-side is that we need to re-acquire the lock below but this should
714 for(TransactionOperation oper: operationsBatch) {
715 oper.invoke(localTransactionContext);
720 private TransactionContext createValidTransactionContext(CreateTransactionReply reply) {
721 LOG.debug("Tx {} Received {}", identifier, reply);
723 return createValidTransactionContext(actorContext.actorSelection(reply.getTransactionPath()),
724 reply.getTransactionPath(), reply.getVersion());
727 private TransactionContext createValidTransactionContext(ActorSelection transactionActor,
728 String transactionPath, short remoteTransactionVersion) {
730 if (transactionType == TransactionType.READ_ONLY) {
731 // Read-only Tx's aren't explicitly closed by the client so we create a PhantomReference
732 // to close the remote Tx's when this instance is no longer in use and is garbage
735 if(remoteTransactionActorsMB == null) {
736 remoteTransactionActors = Lists.newArrayList();
737 remoteTransactionActorsMB = new AtomicBoolean();
739 TransactionProxyCleanupPhantomReference cleanup =
740 new TransactionProxyCleanupPhantomReference(TransactionProxy.this);
741 phantomReferenceCache.put(cleanup, cleanup);
744 // Add the actor to the remoteTransactionActors list for access by the
745 // cleanup PhantonReference.
746 remoteTransactionActors.add(transactionActor);
748 // Write to the memory barrier volatile to publish the above update to the
749 // remoteTransactionActors list for thread visibility.
750 remoteTransactionActorsMB.set(true);
753 // TxActor is always created where the leader of the shard is.
754 // Check if TxActor is created in the same node
755 boolean isTxActorLocal = actorContext.isPathLocal(transactionPath);
757 if(remoteTransactionVersion < DataStoreVersions.LITHIUM_VERSION) {
758 return new PreLithiumTransactionContextImpl(transactionPath, transactionActor, identifier,
759 transactionChainId, actorContext, schemaContext, isTxActorLocal, remoteTransactionVersion,
761 } else if (transactionType == TransactionType.WRITE_ONLY &&
762 actorContext.getDatastoreContext().isWriteOnlyTransactionOptimizationsEnabled()) {
763 return new WriteOnlyTransactionContextImpl(transactionActor, identifier, transactionChainId,
764 actorContext, schemaContext, isTxActorLocal, remoteTransactionVersion, operationCompleter);
766 return new TransactionContextImpl(transactionActor, identifier, transactionChainId,
767 actorContext, schemaContext, isTxActorLocal, remoteTransactionVersion, operationCompleter);
772 private static class NoOpDOMStoreThreePhaseCommitCohort implements DOMStoreThreePhaseCommitCohort {
773 static NoOpDOMStoreThreePhaseCommitCohort INSTANCE = new NoOpDOMStoreThreePhaseCommitCohort();
775 private static final ListenableFuture<Void> IMMEDIATE_VOID_SUCCESS =
776 com.google.common.util.concurrent.Futures.immediateFuture(null);
777 private static final ListenableFuture<Boolean> IMMEDIATE_BOOLEAN_SUCCESS =
778 com.google.common.util.concurrent.Futures.immediateFuture(Boolean.TRUE);
780 private NoOpDOMStoreThreePhaseCommitCohort() {
784 public ListenableFuture<Boolean> canCommit() {
785 return IMMEDIATE_BOOLEAN_SUCCESS;
789 public ListenableFuture<Void> preCommit() {
790 return IMMEDIATE_VOID_SUCCESS;
794 public ListenableFuture<Void> abort() {
795 return IMMEDIATE_VOID_SUCCESS;
799 public ListenableFuture<Void> commit() {
800 return IMMEDIATE_VOID_SUCCESS;