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=0737d2020bcde75125eafebbfe963fc19f2258f7;hb=refs%2Fchanges%2F90%2F10890%2F5;hp=a1858f5f91c82fe20f56461138ba19daa4fa6681;hpb=25b805c6685467f561506dbb5187a744fc12096b;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 a1858f5f91..0737d2020b 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 @@ -32,9 +32,9 @@ import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier import org.opendaylight.controller.cluster.datastore.identifiers.ShardTransactionIdentifier; 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.CloseTransactionChain; import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply; import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction; -import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChain; import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply; import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply; import org.opendaylight.controller.cluster.datastore.messages.EnableNotification; @@ -61,6 +61,7 @@ import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessa import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction; import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort; import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain; +import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionFactory; import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; @@ -109,11 +110,12 @@ public class Shard extends RaftActor { private final DatastoreContext datastoreContext; - private SchemaContext schemaContext; private ActorRef createSnapshotTransaction; + private final Map transactionChains = new HashMap<>(); + private Shard(ShardIdentifier name, Map peerAddresses, DatastoreContext datastoreContext, SchemaContext schemaContext) { super(name.toString(), mapPeerAddresses(peerAddresses), Optional.of(configParams)); @@ -183,22 +185,19 @@ public class Shard extends RaftActor { LOG.debug("onReceiveCommand: Received message {} from {}", message.getClass().toString(), getSender()); - if (message.getClass() - .equals(CreateTransactionChain.SERIALIZABLE_CLASS)) { - if (isLeader()) { - createTransactionChain(); - } else if (getLeader() != null) { - getLeader().forward(message, getContext()); - } - } else if(message.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)) { + if(message.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)) { // This must be for install snapshot. Don't want to open this up and trigger // deSerialization - self().tell(new CaptureSnapshotReply(ReadDataReply.getNormalizedNodeByteString(message)), self()); + self() + .tell(new CaptureSnapshotReply(ReadDataReply.getNormalizedNodeByteString(message)), + self()); // Send a PoisonPill instead of sending close transaction because we do not really need // a response getSender().tell(PoisonPill.getInstance(), self()); + } else if (message.getClass().equals(CloseTransactionChain.SERIALIZABLE_CLASS)){ + closeTransactionChain(CloseTransactionChain.fromSerializable(message)); } else if (message instanceof RegisterChangeListener) { registerChangeListener((RegisterChangeListener) message); } else if (message instanceof UpdateSchemaContext) { @@ -211,6 +210,9 @@ public class Shard extends RaftActor { createTransaction(CreateTransaction.fromSerializable(message)); } else if (getLeader() != null) { getLeader().forward(message, getContext()); + } else { + getSender().tell(new akka.actor.Status.Failure(new IllegalStateException( + "Could not find leader so transaction cannot be created")), getSelf()); } } else if (message instanceof PeerAddressResolved) { PeerAddressResolved resolved = (PeerAddressResolved) message; @@ -221,9 +223,30 @@ public class Shard extends RaftActor { } } + private void closeTransactionChain(CloseTransactionChain closeTransactionChain) { + DOMStoreTransactionChain chain = + transactionChains.remove(closeTransactionChain.getTransactionChainId()); + + if(chain != null) { + chain.close(); + } + } + private ActorRef createTypedTransactionActor( int transactionType, - ShardTransactionIdentifier transactionId) { + ShardTransactionIdentifier transactionId, + String transactionChainId ) { + + DOMStoreTransactionFactory factory = store; + + if(!transactionChainId.isEmpty()) { + factory = transactionChains.get(transactionChainId); + if(factory == null){ + DOMStoreTransactionChain transactionChain = store.createTransactionChain(); + transactionChains.put(transactionChainId, transactionChain); + factory = transactionChain; + } + } if(this.schemaContext == null){ throw new NullPointerException("schemaContext should not be null"); @@ -235,7 +258,7 @@ public class Shard extends RaftActor { shardMBean.incrementReadOnlyTransactionCount(); return getContext().actorOf( - ShardTransaction.props(store.newReadOnlyTransaction(), getSelf(), + ShardTransaction.props(factory.newReadOnlyTransaction(), getSelf(), schemaContext,datastoreContext, shardMBean), transactionId.toString()); } else if (transactionType @@ -244,7 +267,7 @@ public class Shard extends RaftActor { shardMBean.incrementReadWriteTransactionCount(); return getContext().actorOf( - ShardTransaction.props(store.newReadWriteTransaction(), getSelf(), + ShardTransaction.props(factory.newReadWriteTransaction(), getSelf(), schemaContext, datastoreContext, shardMBean), transactionId.toString()); @@ -254,7 +277,7 @@ public class Shard extends RaftActor { shardMBean.incrementWriteOnlyTransactionCount(); return getContext().actorOf( - ShardTransaction.props(store.newWriteOnlyTransaction(), getSelf(), + ShardTransaction.props(factory.newWriteOnlyTransaction(), getSelf(), schemaContext, datastoreContext, shardMBean), transactionId.toString()); } else { throw new IllegalArgumentException( @@ -265,10 +288,10 @@ public class Shard extends RaftActor { private void createTransaction(CreateTransaction createTransaction) { createTransaction(createTransaction.getTransactionType(), - createTransaction.getTransactionId()); + createTransaction.getTransactionId(), createTransaction.getTransactionChainId()); } - private ActorRef createTransaction(int transactionType, String remoteTransactionId) { + private ActorRef createTransaction(int transactionType, String remoteTransactionId, String transactionChainId) { ShardTransactionIdentifier transactionId = ShardTransactionIdentifier.builder() @@ -276,7 +299,7 @@ public class Shard extends RaftActor { .build(); LOG.debug("Creating transaction : {} ", transactionId); ActorRef transactionActor = - createTypedTransactionActor(transactionType, transactionId); + createTypedTransactionActor(transactionType, transactionId, transactionChainId); getSender() .tell(new CreateTransactionReply( @@ -306,6 +329,9 @@ public class Shard extends RaftActor { modification); DOMStoreWriteTransaction transaction = store.newWriteOnlyTransaction(); + + LOG.debug("Created new transaction {}", transaction.getIdentifier().toString()); + modification.apply(transaction); try { syncCommitTransaction(transaction); @@ -319,6 +345,12 @@ public class Shard extends RaftActor { return; } + + if(sender == null){ + LOG.error("Commit failed. Sender cannot be null"); + return; + } + final ListenableFuture future = cohort.commit(); final ActorRef self = getSelf(); @@ -458,7 +490,7 @@ public class Shard extends RaftActor { // so that this actor does not get block building the snapshot createSnapshotTransaction = createTransaction( TransactionProxy.TransactionType.READ_ONLY.ordinal(), - "createSnapshot"); + "createSnapshot", ""); createSnapshotTransaction.tell( new ReadData(YangInstanceIdentifier.builder().build()).toSerializable(), self()); @@ -499,6 +531,16 @@ public class Shard extends RaftActor { shardMBean.setRaftState(getRaftState().name()); shardMBean.setCurrentTerm(getCurrentTerm()); + + // If this actor is no longer the leader close all the transaction chains + if(!isLeader()){ + for(Map.Entry entry : transactionChains.entrySet()){ + LOG.debug("onStateChanged: Closing transaction chain {} because shard {} is no longer the leader", entry.getKey(), getId()); + entry.getValue().close(); + } + + transactionChains.clear(); + } } @Override public String persistenceId() {