X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FShard.java;h=7b34f5df6097ce4ea00c49233f3f4a4a8e5c7728;hb=0b9b1dcba996fd76e0e1bde731692570747f5efd;hp=ce3cae481e766f4ba4b7f93593e8d12a282ee294;hpb=5c5c980e564d2b5f6cd26821ffd26997f59af260;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/Shard.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/Shard.java index ce3cae481e..7b34f5df60 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/Shard.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/Shard.java @@ -18,9 +18,6 @@ import akka.serialization.Serialization; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Optional; import com.google.common.base.Preconditions; -import com.google.common.util.concurrent.FutureCallback; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.ListenableFuture; import java.io.IOException; import java.util.HashMap; import java.util.Map; @@ -35,7 +32,6 @@ import org.opendaylight.controller.cluster.datastore.identifiers.ShardTransactio import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardMBeanFactory; import org.opendaylight.controller.cluster.datastore.jmx.mbeans.shard.ShardStats; import org.opendaylight.controller.cluster.datastore.messages.AbortTransaction; -import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply; import org.opendaylight.controller.cluster.datastore.messages.ActorInitialized; import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications; import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction; @@ -83,7 +79,7 @@ import scala.concurrent.duration.FiniteDuration; */ public class Shard extends RaftActor { - private static final Object TX_COMMIT_TIMEOUT_CHECK_MESSAGE = "txCommitTimeoutCheck"; + protected static final Object TX_COMMIT_TIMEOUT_CHECK_MESSAGE = "txCommitTimeoutCheck"; @VisibleForTesting static final Object GET_SHARD_MBEAN_MESSAGE = "getShardMBeanMessage"; @@ -166,14 +162,8 @@ public class Shard extends RaftActor { datastoreContext.getShardTransactionCommitTimeoutInSeconds(), TimeUnit.SECONDS) / 2; } - public static Props props(final ShardIdentifier name, - final Map peerAddresses, - final DatastoreContext datastoreContext, final SchemaContext schemaContext) { - Preconditions.checkNotNull(name, "name should not be null"); - Preconditions.checkNotNull(peerAddresses, "peerAddresses should not be null"); - Preconditions.checkNotNull(datastoreContext, "dataStoreContext should not be null"); - Preconditions.checkNotNull(schemaContext, "schemaContext should not be null"); - + public static Props props(final ShardIdentifier name, final Map peerAddresses, + final DatastoreContext datastoreContext, final SchemaContext schemaContext) { return Props.create(new ShardCreator(name, peerAddresses, datastoreContext, schemaContext)); } @@ -225,7 +215,7 @@ public class Shard extends RaftActor { if(context.error().isPresent()){ LOG.trace("{} : AppendEntriesReply failed to arrive at the expected interval {}", persistenceId(), - context.error()); + context.error()); } try { @@ -247,9 +237,9 @@ public class Shard extends RaftActor { } else if (CloseTransactionChain.SERIALIZABLE_CLASS.isInstance(message)) { closeTransactionChain(CloseTransactionChain.fromSerializable(message)); } else if (message instanceof RegisterChangeListener) { - changeSupport.onMessage((RegisterChangeListener) message, isLeader()); + changeSupport.onMessage((RegisterChangeListener) message, isLeader(), hasLeader()); } else if (message instanceof RegisterDataTreeChangeListener) { - treeChangeSupport.onMessage((RegisterDataTreeChangeListener) message, isLeader()); + treeChangeSupport.onMessage((RegisterDataTreeChangeListener) message, isLeader(), hasLeader()); } else if (message instanceof UpdateSchemaContext) { updateSchemaContext((UpdateSchemaContext) message); } else if (message instanceof PeerAddressResolved) { @@ -275,6 +265,10 @@ public class Shard extends RaftActor { } } + private boolean hasLeader() { + return getLeaderId() != null; + } + public int getPendingTxCommitQueueSize() { return commitCoordinator.getQueueSize(); } @@ -291,7 +285,7 @@ public class Shard extends RaftActor { leaderPayloadVersion); } - private void onDatastoreContext(DatastoreContext context) { + protected void onDatastoreContext(DatastoreContext context) { datastoreContext = context; commitCoordinator.setQueueCapacity(datastoreContext.getShardTransactionCommitQueueCapacity()); @@ -326,7 +320,7 @@ public class Shard extends RaftActor { } void continueCommit(final CohortEntry cohortEntry) throws Exception { - final DataTreeCandidate candidate = cohortEntry.getCohort().getCandidate(); + final DataTreeCandidate candidate = cohortEntry.getCandidate(); // If we do not have any followers and we are not using persistence // or if cohortEntry has no modifications @@ -349,10 +343,7 @@ public class Shard extends RaftActor { LOG.debug("{}: Finishing commit for transaction {}", persistenceId(), cohortEntry.getTransactionID()); try { - // We block on the future here so we don't have to worry about possibly accessing our - // state on a different thread outside of our dispatcher. Also, the data store - // currently uses a same thread executor anyway. - cohortEntry.getCohort().commit().get(); + cohortEntry.commit(); sender.tell(CommitTransactionReply.INSTANCE.toSerializable(), getSelf()); @@ -385,7 +376,7 @@ public class Shard extends RaftActor { cohortEntry = commitCoordinator.getAndRemoveCohortEntry(transactionID); if(cohortEntry != null) { try { - store.applyForeignCandidate(transactionID, cohortEntry.getCohort().getCandidate()); + store.applyForeignCandidate(transactionID, cohortEntry.getCandidate()); } catch (DataValidationFailedException e) { shardMBean.incrementFailedTransactionsCount(); LOG.error("{}: Failed to re-apply transaction {}", persistenceId(), transactionID, e); @@ -494,38 +485,7 @@ public class Shard extends RaftActor { } void doAbortTransaction(final String transactionID, final ActorRef sender) { - final CohortEntry cohortEntry = commitCoordinator.getCohortEntryIfCurrent(transactionID); - if(cohortEntry != null) { - LOG.debug("{}: Aborting transaction {}", persistenceId(), transactionID); - - // We don't remove the cached cohort entry here (ie pass false) in case the Tx was - // aborted during replication in which case we may still commit locally if replication - // succeeds. - commitCoordinator.currentTransactionComplete(transactionID, false); - - final ListenableFuture future = cohortEntry.getCohort().abort(); - final ActorRef self = getSelf(); - - Futures.addCallback(future, new FutureCallback() { - @Override - public void onSuccess(final Void v) { - shardMBean.incrementAbortTransactionsCount(); - - if(sender != null) { - sender.tell(AbortTransactionReply.INSTANCE.toSerializable(), self); - } - } - - @Override - public void onFailure(final Throwable t) { - LOG.error("{}: An exception happened during abort", persistenceId(), t); - - if(sender != null) { - sender.tell(new akka.actor.Status.Failure(t), self); - } - } - }); - } + commitCoordinator.handleAbort(transactionID, sender, this); } private void handleCreateTransaction(final Object message) { @@ -693,8 +653,9 @@ public class Shard extends RaftActor { @Override protected void onStateChanged() { boolean isLeader = isLeader(); - changeSupport.onLeadershipChange(isLeader); - treeChangeSupport.onLeadershipChange(isLeader); + boolean hasLeader = hasLeader(); + changeSupport.onLeadershipChange(isLeader, hasLeader); + treeChangeSupport.onLeadershipChange(isLeader, hasLeader); // If this actor is no longer the leader close all the transaction chains if (!isLeader) { @@ -723,22 +684,29 @@ public class Shard extends RaftActor { return commitCoordinator; } + protected abstract static class AbstractShardCreator implements Creator { + private static final long serialVersionUID = 1L; + + protected final ShardIdentifier name; + protected final Map peerAddresses; + protected final DatastoreContext datastoreContext; + protected final SchemaContext schemaContext; - private static class ShardCreator implements Creator { + protected AbstractShardCreator(final ShardIdentifier name, final Map peerAddresses, + final DatastoreContext datastoreContext, final SchemaContext schemaContext) { + this.name = Preconditions.checkNotNull(name, "name should not be null"); + this.peerAddresses = Preconditions.checkNotNull(peerAddresses, "peerAddresses should not be null"); + this.datastoreContext = Preconditions.checkNotNull(datastoreContext, "dataStoreContext should not be null"); + this.schemaContext = Preconditions.checkNotNull(schemaContext, "schemaContext should not be null"); + } + } + private static class ShardCreator extends AbstractShardCreator { private static final long serialVersionUID = 1L; - final ShardIdentifier name; - final Map peerAddresses; - final DatastoreContext datastoreContext; - final SchemaContext schemaContext; - ShardCreator(final ShardIdentifier name, final Map peerAddresses, final DatastoreContext datastoreContext, final SchemaContext schemaContext) { - this.name = name; - this.peerAddresses = peerAddresses; - this.datastoreContext = datastoreContext; - this.schemaContext = schemaContext; + super(name, peerAddresses, datastoreContext, schemaContext); } @Override