Search code examples
scalacluster-computingakkaactor

Is there a way to randomly assign routes or roles for a defined number of actors in Akka?


Suppose I want to implement a cluster system where some actos will be request dispatchers and others will be standard nodes. How can I randomly assign a predefined number of actors (does not matter the hostname and port) a specific route or even role?

Explaining better:

Suppose I have these nodes:

1 - akka.tcp://ClusterSystem@192.168.0.1:2551/user/clusterListener
2 - akka.tcp://ClusterSystem@192.168.0.2:2552/user/clusterListener
3 - akka.tcp://ClusterSystem@192.168.0.3:2553/user/clusterListener
4 - akka.tcp://ClusterSystem@192.168.0.4:2554/user/clusterListener

Now I want 2 of them to have the sub route "dispatcher" (akka.tcp://ClusterSystem@xxx.xxx.xxx.xxx:xxxx/user/clusterListener/dispatcher)


Solution

  • You can use http://doc.akka.io/docs/akka/2.3.0/contrib/cluster-singleton.html for coordination.

    Every actor without role may send "GetRole" message to the singleton and it will pickup role randomly (using some internal RoleMap). Note that you should listen memberDown message from singleton to free role when some node (obtained this role) has removed.