X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FTransactionProxy.java;h=5a0049aa6d05acd5f0e9114ff93e5dcc4ca53dad;hp=32bb7d0951964975b850c8a1a685ce7d95c03f47;hb=516a4b2ea78179c9bd6ebb584862e8fc686ebf08;hpb=51e22e5781fb026423e37d623333e7dc3c3ec7b0 diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/TransactionProxy.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/TransactionProxy.java index 32bb7d0951..5a0049aa6d 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/TransactionProxy.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/TransactionProxy.java @@ -23,18 +23,20 @@ import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply; import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction; import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply; import org.opendaylight.controller.cluster.datastore.messages.WriteData; +import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory; import org.opendaylight.controller.cluster.datastore.utils.ActorContext; import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction; import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort; -import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.Callable; -import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicLong; /** @@ -50,7 +52,6 @@ import java.util.concurrent.atomic.AtomicLong; *

*/ public class TransactionProxy implements DOMStoreReadWriteTransaction { - public enum TransactionType { READ_ONLY, WRITE_ONLY, @@ -59,38 +60,47 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { private static final AtomicLong counter = new AtomicLong(); - private final TransactionType readOnly; + private final TransactionType transactionType; private final ActorContext actorContext; - private final Map remoteTransactionPaths = new HashMap<>(); + private final Map remoteTransactionPaths = new HashMap<>(); private final String identifier; + private final ExecutorService executor; + private final SchemaContext schemaContext; public TransactionProxy( ActorContext actorContext, - TransactionType readOnly) { + TransactionType transactionType, + ExecutorService executor, + SchemaContext schemaContext + ) { - this.identifier = "transaction-" + counter.getAndIncrement(); - this.readOnly = readOnly; + this.identifier = actorContext.getCurrentMemberName() + "-txn-" + counter.getAndIncrement(); + this.transactionType = transactionType; this.actorContext = actorContext; + this.executor = executor; + this.schemaContext = schemaContext; + - Object response = actorContext.executeShardOperation(Shard.DEFAULT_NAME, new CreateTransaction(), ActorContext.ASK_DURATION); - if(response instanceof CreateTransactionReply){ - CreateTransactionReply reply = (CreateTransactionReply) response; - remoteTransactionPaths.put(Shard.DEFAULT_NAME, actorContext.actorSelection(reply.getTransactionPath())); - } } @Override - public ListenableFuture>> read(final InstanceIdentifier path) { + public ListenableFuture>> read(final YangInstanceIdentifier path) { + + createTransactionIfMissing(actorContext, path); + final ActorSelection remoteTransaction = remoteTransactionFromIdentifier(path); Callable>> call = new Callable() { @Override public Optional> call() throws Exception { Object response = actorContext - .executeRemoteOperation(remoteTransaction, new ReadData(path), + .executeRemoteOperation(remoteTransaction, new ReadData(path).toSerializable(), ActorContext.ASK_DURATION); - if(response instanceof ReadDataReply){ - ReadDataReply reply = (ReadDataReply) response; + if(response.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)){ + ReadDataReply reply = ReadDataReply.fromSerializable(schemaContext,path, response); + if(reply.getNormalizedNode() == null){ + return Optional.absent(); + } //FIXME : A cast should not be required here ??? return (Optional>) Optional.of(reply.getNormalizedNode()); } @@ -102,47 +112,57 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { ListenableFutureTask>> future = ListenableFutureTask.create(call); - //FIXME : Use a thread pool here - Executors.newSingleThreadExecutor().submit(future); + executor.submit(future); return future; } @Override - public void write(InstanceIdentifier path, NormalizedNode data) { + public void write(YangInstanceIdentifier path, NormalizedNode data) { + + createTransactionIfMissing(actorContext, path); + final ActorSelection remoteTransaction = remoteTransactionFromIdentifier(path); - remoteTransaction.tell(new WriteData(path, data), null); + remoteTransaction.tell(new WriteData(path, data, schemaContext).toSerializable(), null); } @Override - public void merge(InstanceIdentifier path, NormalizedNode data) { + public void merge(YangInstanceIdentifier path, NormalizedNode data) { + + createTransactionIfMissing(actorContext, path); + final ActorSelection remoteTransaction = remoteTransactionFromIdentifier(path); - remoteTransaction.tell(new MergeData(path, data), null); + remoteTransaction.tell(new MergeData(path, data, schemaContext).toSerializable(), null); } @Override - public void delete(InstanceIdentifier path) { + public void delete(YangInstanceIdentifier path) { + + createTransactionIfMissing(actorContext, path); + final ActorSelection remoteTransaction = remoteTransactionFromIdentifier(path); - remoteTransaction.tell(new DeleteData(path), null); + remoteTransaction.tell(new DeleteData(path).toSerializable(), null); } @Override public DOMStoreThreePhaseCommitCohort ready() { List cohortPaths = new ArrayList<>(); - for(ActorSelection remoteTransaction : remoteTransactionPaths.values()) { - Object result = actorContext.executeRemoteOperation(remoteTransaction, - new ReadyTransaction(), + for(TransactionContext transactionContext : remoteTransactionPaths.values()) { + Object result = actorContext.executeRemoteOperation(transactionContext.getActor(), + new ReadyTransaction().toSerializable(), ActorContext.ASK_DURATION ); - if(result instanceof ReadyTransactionReply){ - ReadyTransactionReply reply = (ReadyTransactionReply) result; - cohortPaths.add(reply.getCohortPath()); + if(result.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)){ + ReadyTransactionReply reply = ReadyTransactionReply.fromSerializable(actorContext.getActorSystem(),result); + String resolvedCohortPath = transactionContext + .getResolvedCohortPath(reply.getCohortPath().toString()); + cohortPaths.add(actorContext.actorFor(resolvedCohortPath)); } } - return new ThreePhaseCommitCohortProxy(cohortPaths); + return new ThreePhaseCommitCohortProxy(actorContext, cohortPaths, identifier, executor); } @Override @@ -152,17 +172,74 @@ public class TransactionProxy implements DOMStoreReadWriteTransaction { @Override public void close() { - for(ActorSelection remoteTransaction : remoteTransactionPaths.values()) { - remoteTransaction.tell(new CloseTransaction(), null); + for(TransactionContext transactionContext : remoteTransactionPaths.values()) { + transactionContext.getActor().tell( + new CloseTransaction().toSerializable(), null); } } - private ActorSelection remoteTransactionFromIdentifier(InstanceIdentifier path){ + private ActorSelection remoteTransactionFromIdentifier(YangInstanceIdentifier path){ String shardName = shardNameFromIdentifier(path); - return remoteTransactionPaths.get(shardName); + return remoteTransactionPaths.get(shardName).getActor(); + } + + private String shardNameFromIdentifier(YangInstanceIdentifier path){ + return ShardStrategyFactory.getStrategy(path).findShard(path); + } + + private void createTransactionIfMissing(ActorContext actorContext, YangInstanceIdentifier path) { + String shardName = ShardStrategyFactory.getStrategy(path).findShard(path); + + TransactionContext transactionContext = + remoteTransactionPaths.get(shardName); + + if(transactionContext != null){ + // A transaction already exists with that shard + return; + } + + Object response = actorContext.executeShardOperation(shardName, new CreateTransaction(identifier).toSerializable(), ActorContext.ASK_DURATION); + if(response.getClass().equals(CreateTransactionReply.SERIALIZABLE_CLASS)){ + CreateTransactionReply reply = CreateTransactionReply.fromSerializable(response); + String transactionPath = actorContext.getRemoteActorPath(shardName, reply.getTransactionPath()); + + ActorSelection transactionActor = actorContext.actorSelection(transactionPath); + transactionContext = new TransactionContext(shardName, transactionPath, transactionActor); + + remoteTransactionPaths.put(shardName, transactionContext); + } } - private String shardNameFromIdentifier(InstanceIdentifier path){ - return Shard.DEFAULT_NAME; + + private class TransactionContext { + private final String shardName; + private final String actorPath; + private final ActorSelection actor; + + + private TransactionContext(String shardName, String actorPath, + ActorSelection actor) { + this.shardName = shardName; + this.actorPath = actorPath; + this.actor = actor; + } + + + public String getShardName() { + return shardName; + } + + public String getActorPath() { + return actorPath; + } + + public ActorSelection getActor() { + return actor; + } + + public String getResolvedCohortPath(String cohortPath){ + return actorContext.resolvePath(actorPath, cohortPath); + } } + }