Code to create actor is :

def routeMessagesToCart(cart_id: UUID, user_id: UUID): ActorRef = {

  //Find if actor for the cart already exists,or is Terminated
  val current_cart_actor = getOrCreateCartActor(cart_id)


  //is user logged in ?
  if ("00000000-0000-0000-0000-000000000000" != user_id.toString) {
    println("Inside If condition")
    //yes now check checkif  a already  cart already exists for user
    var old_cart_id:UUID = null
    var old_cart_id_val = activeUserCarts.get(user_id)
    println("old_cart_id_val "+old_cart_id_val)
    if(old_cart_id_val != None){
      old_cart_id = old_cart_id_val.get
    }
    println("Old Cart from map value  = " + old_cart_id)
    if (old_cart_id != null && cart_id != old_cart_id) {

      val oldCartRef = getOrCreateCartActor(old_cart_id)
      val newCartRef = getOrCreateCartActor(cart_id)
      
      val old_cart = getCart(old_cart_id,user_id)
      println("Old Cart Entries: "+old_cart.cart_entries)
      for ((sku_id, cart_entry) <- old_cart.cart_entries) {
        println("New Cart: "+ newCartRef.path.name)
       newCartRef ! AddSkuToCartCmd(cart_id,user_id, cart_entry.sku_id, 
cart_entry.price, cart_entry.title, cart_entry.imageUrl)
      }

      oldCartRef ! PoisonPill

    }
    activeUserCarts += (user_id -> cart_id)
    
  }


  current_cart_actor

}




On Wednesday, April 12, 2017 at 10:13:25 AM UTC+5:30, 
[email protected] wrote:
>
> It is first time I am implementing akka clustering so I have no prior 
> experience with that. What We want to acheive with akka clustering is that 
> when request goes to each node in cluster each node return same instance of 
> actor (Or actor consist of same data which should just be sum of response 
> to all requests to all nodes). But what we all getting is all nodes 
> returning different response containing different data as if they are 
> working independently. 
>
>
> Here I am sharing some code and configuration and exceptions we are getting
>
> def getOrCreateCartActor(cart_id: UUID): ActorRef = {
>   var cartActor: ActorRef = null
>   implicit val timeout = Timeout(50 seconds)
>   val name = "/user/" + cart_id.toString
>   val roundRobinPool = RoundRobinPool( nrOfInstances =  4)
>
>   val clusterRouterPoolSettings = ClusterRouterPoolSettings( totalInstances = 
> 4, maxInstancesPerNode = 2,
>       allowLocalRoutees = true, useRole = None)
>
>   val clusterRouterPool = ClusterRouterPool(roundRobinPool, 
> clusterRouterPoolSettings)
>
>   val future = actorSystem.actorSelection("/user/" + 
> cart_id.toString).resolveOne()
>
>
>   try{
>   /*try {
>     println("Everything Ok.")
>     cartActor = actorSystem.actorOf(ClusterSingletonProxy.props(
>       singletonManagerPath = "/user/" + cart_id.toString,
>       settings = ClusterSingletonProxySettings(actorSystem)),
>       name = cart_id.toString+"Proxy"
>     )
>   }catch {
>     case ex: InvalidActorNameException =>
>       if(cartActor == null){
>       println("Everything Not Ok.")
>       cartActor = actorSystem.actorOf(
>         ClusterSingletonManager.props(
>           singletonProps = Props(classOf[CartActor]),
>           terminationMessage = PoisonPill,
>           settings = ClusterSingletonManagerSettings(actorSystem)),
>         //Props[CartActor],
>         cart_id.toString)
>     }
>   }*/
>     val actorResult = Await.result(future, 50 seconds)
>     cartActor = 
> actorSystem.actorOf(clusterRouterPool.props(Props[CartActor]), 
> cart_id.toString)
>
>   }
>   catch {
>     case ex: ActorNotFound => {
>
>
>
>       cartActor = 
> actorSystem.actorOf(clusterRouterPool.props(Props[CartActor]), 
> cart_id.toString)
>     }
>   }
>
>   /*future onComplete {
>     case Success(v) => {
>       cartActor = v
>
>
>     }
>     case Failure(e) => {
>       //The cart actor no longer exists and must be recreated
>       cartActor = system.actorOf(Props[CartActor], cart_id.toString)
>
>
>     }
>   }*/
>   //Sleep for Actor to be created
>   cartActor
>
>
> }
>
>
>  As in comments in code you can see I also tried ClusterSingleton and 
> Cluster Pooling using round robin.
>
> Here is configuration :
>
> akka {
>   # "akka.log-config-on-start" is extraordinarly useful because it log the 
> complete
>   # configuration at INFO level, including defaults and overrides, so it s 
> worth
>   # putting at the very top.
>   #
>   # Put the following in your conf/logback.xml file:
>   #
>   # <logger name="akka.actor" level="INFO" />
>   #
>   # And then uncomment this line to debug the configuration.
>   #
>   #log-config-on-start = true
>
>   loglevel = DEBUG
>   loggers = ["akka.event.slf4j.Slf4jLogger"]
>   logging-filter = "akka.event.slf4j.Slf4jLoggingFilter"
>
>   actor {
>     provider = "akka.cluster.ClusterActorRefProvider"
>     debug {
>       receive = on
>       lifecycle = on
>     }
>     serializers {
>       java = "akka.serialization.JavaSerializer"
>     }
>
>   }
> //  remote {
> //    log-remote-lifecycle-events = off
> //    netty.tcp {
> //      hostname = "127.0.0.1"
> //      port = 0
> //    }
> //  }
>
>   remote {
>     enabled-transports = ["akka.remote.netty.tcp"]
>     netty.tcp {
>       hostname = "127.0.0.1"
>       port = 0
>     }
>   }
>
>   cluster {
>     seed-nodes = [
>       "akka.tcp://[email protected]:2551",
>       "akka.tcp://[email protected]:2552"]
>
>
>
>
>     #//#snippet
>     # excluded from snippet
>     //auto-down-unreachable-after = 10s
>     #//#snippet
>     # auto downing is NOT safe for production deployments.
>     # you may want to use it during development, read more about it in the 
> docs.
>     #
>     //auto-down-unreachable-after = 10s
>
> //    distributed-data {
> //      # Actor name of the Replicator actor, /system/ddataReplicator
> //      name = ddataReplicator
> //    }
>   }
>
>   //akka.extensions = ["akka.cluster.pubsub.DistributedPubSub"]
>
> }
>
>
> Then i started nodes using:
>
> sbt "run -Dhttp.port=9001 -Dakka.remote.netty.tcp.port=2551"
> sbt "run -Dhttp.port=9002 -Dakka.remote.netty.tcp.port=2552"
> *and main server with*
> sbt run
>
> and then the exception i am getting is:
>
> [error] application - 
>
> ! @73ji7p2o9 - Internal server error, for (POST) [/addToCart] ->
>  
> play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution 
> exception[[InvalidActorNameException: actor name 
> [1876845a-01d5-4f15-b7f7-abe49ba6e689] is not unique!]]
>         at 
> play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:293)
>         at 
> play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:220)
>         at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
>         at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
>         at 
> play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:100)
>         at 
> play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
>         at 
> play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
>         at 
> scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344)
>         at 
> scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343)
>         at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
> Caused by: akka.actor.InvalidActorNameException: actor name 
> [1876845a-01d5-4f15-b7f7-abe49ba6e689] is not unique!
>         at 
> akka.actor.dungeon.ChildrenContainer$NormalChildrenContainer.reserve(ChildrenContainer.scala:129)
>         at 
> akka.actor.dungeon.Children$class.reserveChild(Children.scala:130)
>         at akka.actor.ActorCell.reserveChild(ActorCell.scala:374)
>         at akka.actor.dungeon.Children$class.makeChild(Children.scala:268)
>         at akka.actor.dungeon.Children$class.attachChild(Children.scala:46)
>         at akka.actor.ActorCell.attachChild(ActorCell.scala:374)
>         at akka.actor.ActorSystemImpl.actorOf(ActorSystem.scala:729)
>         at services.CartService.getOrCreateCartActor(CartService.scala:179)
>         at services.CartService.routeMessagesToCart(CartService.scala:82)
>         at services.CartService.getCart(CartService.scala:42)
>
> Please let me know if i am missing something.
>
>
>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to