3
votes

I have a Scala application where I have several nodes. Each node has an ActorSystem with a main actor and each actor must have some ActorRef to certain actors (for example "Node 1" has "Actor3" and "Actor3" needs the ActorRef for "Actor7" and "Actor8" to do its work). My problem is that I don't know if another node ("Node2") has the "Actor1" or the "Actor7" I'm looking for.

My idea was to loop inside every MemberUp, using the ActorSelection several times and asking every new member if it has the actors I'm looking for. Is this the only way I can do it? Is there a way to do this more efficiently?

2
if I am not misunderstanding you, why not use akka cluster singleton? Cluster Singleton — Akka Documentation doc.akka.io/docs/akka/snapshot/scala/cluster-singleton.html , you can hold the Map[Address,List[ActorRef]] like Fatih Donmez said or use that singleton to spawn child worker using remote routermingchuno

2 Answers

2
votes

An alternative approach to ActorSelection can be lookup table. If you need to make lots of actor selection and actor creation is not so dynamic, it can be better solution.

On each node you can create a data structure like Map[String,List[String]] first key is Node name and List value is for actor refs in this node.

Trick is when any node has change for its actors (creating, stopping) another actor should notice other nodes about changes so any nodes have synchronised updated map.

If you guaranty it, then each node can lookup actor existence;

map.get(nodeName) match {
  case Some(n) => n.contains(actorName)
  case None => false
}
2
votes

I've solved a very similar problem in our cluster by having a DiscoveryActor at a known path on every node. The protocol of the DiscoveryActor has

Register(name, actorRef)
Subscribe(name)
Up(name, actorRef)
Down(name, actorRef)

Each named actor sends a Register to its local DiscoveryActor which in turn broadcasts the Up to all local subscribers and all other DiscoveryActor's on other nodes, which in turn broadcast to their subscribers

The DiscoveryActor watches MemberUp/MemberDown to determine when to look for a new peer DiscoveryActor and broadcast its local registrations or broadcast Down for registrations of downed peers.