import akka.util.Timeout;
import com.google.common.base.Preconditions;
import java.util.concurrent.TimeUnit;
import akka.util.Timeout;
import com.google.common.base.Preconditions;
import java.util.concurrent.TimeUnit;
import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
import org.opendaylight.controller.cluster.datastore.exceptions.ShardLeaderNotRespondingException;
import org.opendaylight.controller.cluster.datastore.exceptions.NoShardLeaderException;
import org.opendaylight.controller.cluster.datastore.exceptions.ShardLeaderNotRespondingException;
import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo;
* Handles creation of TransactionContext instances for remote transactions. This class creates
* remote transactions, if necessary, by sending CreateTransaction messages with retries, up to a limit,
* if the shard doesn't have a leader yet. This is done by scheduling a retry task after a short delay.
* Handles creation of TransactionContext instances for remote transactions. This class creates
* remote transactions, if necessary, by sending CreateTransaction messages with retries, up to a limit,
* if the shard doesn't have a leader yet. This is done by scheduling a retry task after a short delay.
* The end result from a completed CreateTransaction message is a TransactionContext that is
* used to perform transaction operations. Transaction operations that occur before the
* CreateTransaction completes are cache via a TransactionContextWrapper and executed once the
* The end result from a completed CreateTransaction message is a TransactionContext that is
* used to perform transaction operations. Transaction operations that occur before the
* CreateTransaction completes are cache via a TransactionContextWrapper and executed once the
- RemoteTransactionContextSupport(final TransactionContextWrapper transactionContextWrapper, final TransactionProxy parent,
- final String shardName) {
+ RemoteTransactionContextSupport(final TransactionContextWrapper transactionContextWrapper,
+ final TransactionProxy parent, final String shardName) {
this.parent = Preconditions.checkNotNull(parent);
this.shardName = shardName;
this.transactionContextWrapper = transactionContextWrapper;
this.parent = Preconditions.checkNotNull(parent);
this.shardName = shardName;
this.transactionContextWrapper = transactionContextWrapper;
- if (getTransactionType() == TransactionType.WRITE_ONLY &&
- getActorContext().getDatastoreContext().isWriteOnlyTransactionOptimizationsEnabled()) {
+ if (getTransactionType() == TransactionType.WRITE_ONLY
+ && getActorContext().getDatastoreContext().isWriteOnlyTransactionOptimizationsEnabled()) {
ActorSelection primaryShard = primaryShardInfo.getPrimaryShardActor();
LOG.debug("Tx {} Primary shard {} found - creating WRITE_ONLY transaction context",
ActorSelection primaryShard = primaryShardInfo.getPrimaryShardActor();
LOG.debug("Tx {} Primary shard {} found - creating WRITE_ONLY transaction context",
// For write-only Tx's we prepare the transaction modifications directly on the shard actor
// to avoid the overhead of creating a separate transaction actor.
transactionContextWrapper.executePriorTransactionOperations(createValidTransactionContext(
// For write-only Tx's we prepare the transaction modifications directly on the shard actor
// to avoid the overhead of creating a separate transaction actor.
transactionContextWrapper.executePriorTransactionOperations(createValidTransactionContext(
- if(LOG.isDebugEnabled()) {
- LOG.debug("Tx {} Primary shard {} found - trying create transaction", getIdentifier(),
- primaryShardInfo.getPrimaryShardActor());
- }
+ LOG.debug("Tx {} Primary shard {} found - trying create transaction", getIdentifier(),
+ primaryShardInfo.getPrimaryShardActor());
- Object serializedCreateMessage = new CreateTransaction(getIdentifier().toString(),
- getTransactionType().ordinal(), getIdentifier().getChainId(),
+ Object serializedCreateMessage = new CreateTransaction(getIdentifier(), getTransactionType().ordinal(),
primaryShardInfo.getPrimaryShardVersion()).toSerializable();
Future<Object> createTxFuture = getActorContext().executeOperationAsync(
primaryShardInfo.getPrimaryShardVersion()).toSerializable();
Future<Object> createTxFuture = getActorContext().executeOperationAsync(
Future<PrimaryShardInfo> findPrimaryFuture = getActorContext().findPrimaryShardAsync(shardName);
findPrimaryFuture.onComplete(new OnComplete<PrimaryShardInfo>() {
@Override
Future<PrimaryShardInfo> findPrimaryFuture = getActorContext().findPrimaryShardAsync(shardName);
findPrimaryFuture.onComplete(new OnComplete<PrimaryShardInfo>() {
@Override
- public void onComplete(final Throwable failure, final PrimaryShardInfo primaryShardInfo) {
- onFindPrimaryShardComplete(failure, primaryShardInfo);
+ public void onComplete(final Throwable failure, final PrimaryShardInfo newPrimaryShardInfo) {
+ onFindPrimaryShardComplete(failure, newPrimaryShardInfo);
tryCreateTransaction();
} else {
LOG.debug("Tx {}: Find primary for shard {} failed", getIdentifier(), shardName, failure);
tryCreateTransaction();
} else {
LOG.debug("Tx {}: Find primary for shard {} failed", getIdentifier(), shardName, failure);
private void onCreateTransactionComplete(Throwable failure, Object response) {
// An AskTimeoutException will occur if the local shard forwards to an unavailable remote leader or
// the cached remote leader actor is no longer available.
private void onCreateTransactionComplete(Throwable failure, Object response) {
// An AskTimeoutException will occur if the local shard forwards to an unavailable remote leader or
// the cached remote leader actor is no longer available.
- boolean retryCreateTransaction = primaryShardInfo != null &&
- (failure instanceof NoShardLeaderException || failure instanceof AskTimeoutException);
- if(retryCreateTransaction) {
+ boolean retryCreateTransaction = primaryShardInfo != null
+ && (failure instanceof NoShardLeaderException || failure instanceof AskTimeoutException);
+ if (retryCreateTransaction) {
// Schedule a retry unless we're out of retries. Note: totalCreateTxTimeout is volatile as it may
// be written by different threads however not concurrently, therefore decrementing it
// non-atomically here is ok.
// Schedule a retry unless we're out of retries. Note: totalCreateTxTimeout is volatile as it may
// be written by different threads however not concurrently, therefore decrementing it
// non-atomically here is ok.
// Since we use the createTxMessageTimeout for the CreateTransaction request and it timed
// out, subtract it from the total timeout. Also since the createTxMessageTimeout period
// has already elapsed, we can immediately schedule the retry (10 ms is virtually immediate).
// Since we use the createTxMessageTimeout for the CreateTransaction request and it timed
// out, subtract it from the total timeout. Also since the createTxMessageTimeout period
// has already elapsed, we can immediately schedule the retry (10 ms is virtually immediate).
getIdentifier(), shardName, failure, scheduleInterval);
getActorContext().getActorSystem().scheduler().scheduleOnce(
getIdentifier(), shardName, failure, scheduleInterval);
getActorContext().getActorSystem().scheduler().scheduleOnce(
- FiniteDuration.create(scheduleInterval, TimeUnit.MILLISECONDS),
- new Runnable() {
- @Override
- public void run() {
- tryFindPrimaryShard();
- }
- }, getActorContext().getClientDispatcher());
+ FiniteDuration.create(scheduleInterval, TimeUnit.MILLISECONDS),
+ this::tryFindPrimaryShard, getActorContext().getClientDispatcher());
// TransactionOperations. So to avoid thus timing, we don't publish the
// TransactionContext until after we've executed all cached TransactionOperations.
TransactionContext localTransactionContext;
// TransactionOperations. So to avoid thus timing, we don't publish the
// TransactionContext until after we've executed all cached TransactionOperations.
TransactionContext localTransactionContext;
LOG.debug("Tx {} Creating NoOpTransaction because of error", getIdentifier(), failure);
Throwable resultingEx = failure;
LOG.debug("Tx {} Creating NoOpTransaction because of error", getIdentifier(), failure);
Throwable resultingEx = failure;
resultingEx = new ShardLeaderNotRespondingException(String.format(
"Could not create a %s transaction on shard %s. The shard leader isn't responding.",
parent.getType(), shardName), failure);
resultingEx = new ShardLeaderNotRespondingException(String.format(
"Could not create a %s transaction on shard %s. The shard leader isn't responding.",
parent.getType(), shardName), failure);
resultingEx = new Exception(String.format(
"Error creating %s transaction on shard %s", parent.getType(), shardName), failure);
}
resultingEx = new Exception(String.format(
"Error creating %s transaction on shard %s", parent.getType(), shardName), failure);
}
final TransactionContext ret = new RemoteTransactionContext(transactionContextWrapper.getIdentifier(),
transactionActor, getActorContext(), remoteTransactionVersion, transactionContextWrapper.getLimiter());
final TransactionContext ret = new RemoteTransactionContext(transactionContextWrapper.getIdentifier(),
transactionActor, getActorContext(), remoteTransactionVersion, transactionContextWrapper.getLimiter());
- if(parent.getType() == TransactionType.READ_ONLY) {
- TransactionContextCleanup.track(this, ret);
+ if (parent.getType() == TransactionType.READ_ONLY) {
+ TransactionContextCleanup.track(parent, ret);