X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fmd-sal%2Fsal-distributed-datastore%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2FThreePhaseCommitCohortProxy.java;h=36b6efa2f7091d9f178d682aded4fc4888f42dff;hb=a2b92b2d72c28b9913131c0340f87d2424f44108;hp=197b3b70cefa292e3829b776560b4cd2ae99967f;hpb=e55942fd1fcc4748197146b81f7497256de22a94;p=controller.git diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ThreePhaseCommitCohortProxy.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ThreePhaseCommitCohortProxy.java index 197b3b70ce..36b6efa2f7 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ThreePhaseCommitCohortProxy.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ThreePhaseCommitCohortProxy.java @@ -9,11 +9,27 @@ package org.opendaylight.controller.cluster.datastore; import akka.actor.ActorPath; +import akka.actor.ActorSelection; import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListenableFutureTask; +import org.opendaylight.controller.cluster.datastore.messages.AbortTransaction; +import org.opendaylight.controller.cluster.datastore.messages.AbortTransactionReply; +import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction; +import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply; +import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction; +import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply; +import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransaction; +import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply; +import org.opendaylight.controller.cluster.datastore.utils.ActorContext; import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Collections; import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; /** * ThreePhaseCommitCohortProxy represents a set of remote cohort proxies @@ -21,27 +37,101 @@ import java.util.List; public class ThreePhaseCommitCohortProxy implements DOMStoreThreePhaseCommitCohort{ + private static final Logger + LOG = LoggerFactory.getLogger(DistributedDataStore.class); + + private final ActorContext actorContext; private final List cohortPaths; + //FIXME : Use a thread pool here + private final ExecutorService executorService = Executors.newSingleThreadExecutor(); - public ThreePhaseCommitCohortProxy(List cohortPaths) { + public ThreePhaseCommitCohortProxy(ActorContext actorContext, List cohortPaths) { + this.actorContext = actorContext; this.cohortPaths = cohortPaths; } @Override public ListenableFuture canCommit() { - throw new UnsupportedOperationException("canCommit"); + Callable call = new Callable() { + + @Override public Boolean call() throws Exception { + for(ActorPath actorPath : cohortPaths){ + ActorSelection cohort = actorContext.actorSelection(actorPath); + + try { + Object response = + actorContext.executeRemoteOperation(cohort, + new CanCommitTransaction(), + ActorContext.ASK_DURATION); + + if (response instanceof CanCommitTransactionReply) { + CanCommitTransactionReply reply = + (CanCommitTransactionReply) response; + if (!reply.getCanCommit()) { + return false; + } + } + } catch(RuntimeException e){ + LOG.error("Unexpected Exception", e); + return false; + } + + + } + return true; + } + }; + + ListenableFutureTask + future = ListenableFutureTask.create(call); + + executorService.submit(future); + + return future; } @Override public ListenableFuture preCommit() { - throw new UnsupportedOperationException("preCommit"); + return voidOperation(new PreCommitTransaction(), PreCommitTransactionReply.class); } @Override public ListenableFuture abort() { - throw new UnsupportedOperationException("abort"); + return voidOperation(new AbortTransaction(), AbortTransactionReply.class); } @Override public ListenableFuture commit() { - throw new UnsupportedOperationException("commit"); + return voidOperation(new CommitTransaction(), CommitTransactionReply.class); + } + + private ListenableFuture voidOperation(final Object message, final Class expectedResponseClass){ + Callable call = new Callable() { + + @Override public Void call() throws Exception { + for(ActorPath actorPath : cohortPaths){ + ActorSelection cohort = actorContext.actorSelection(actorPath); + + Object response = actorContext.executeRemoteOperation(cohort, + message, + ActorContext.ASK_DURATION); + + if(response != null && !response.getClass().equals(expectedResponseClass)){ + throw new RuntimeException( + String.format( + "did not get the expected response \n\t\t expected : %s \n\t\t actual : %s", + expectedResponseClass.toString(), + response.getClass().toString())); + } + } + return null; + } + }; + + ListenableFutureTask + future = ListenableFutureTask.create(call); + + executorService.submit(future); + + return future; + } public List getCohortPaths() {