Boomah
Boomah

Reputation: 1192

How do I send messages to idle akka actors?

I have an actor called a TaskRunner. The tasks can take up to 1 minute to run. Because of the library I use there can only be one actor per jvm/node. I have 1000 of these nodes across various machines.

I would like to distribute tasks to these nodes using various rules but the most important one is:

The way I was thinking of doing this is have an actor on another node (lets call this the Scheduler actor) listen to registrations from the TaskRunner nodes and keep an internal state of what has been sent to where.

Presumably if I did this I could only ever have one instance of this Scheduler actor because if there was more than one they wouldn't know which TaskRunner nodes were currently busy and so we would get tasks in the queue.

Does this mean I should be using a Cluster Singleton for the Scheduler actor?

Is there a better way to achieve my goal?

Upvotes: 3

Views: 207

Answers (3)

Amit Joshi
Amit Joshi

Reputation: 117

Reffering to your approach of singleton master and multiple workers,there can be a situation where your master is over loaded with task to schedule, which may result in more time to schedule the task to the workers.

So Instead of making master as Cluster singleton, you can have multiple masters having subset of workers assigned to them. The distribution of work to different master can be done through cluster sharding based on sharding key. Akka provides cluster sharding, you can refer that.

And for making your master fault tolerant, you can always have the persistent actors as masters.

Upvotes: 1

Leo C
Leo C

Reputation: 22449

Given your requirement, rather than building everything from scratch, you might want to consider adapting Lightbend's distributed-workers template which employs a pull model. It primarily consists of 1) a master cluster singleton that maintains state of workers, and, 2) an actor system of workers which register and pull work from the master singleton actor.

I adapted a repurposed version of the template for a R&D project in the past and it delivered the work-pulling functionality as advertised. Note that the template uses the retired Activator (which can be easily detached or replaced with sbt from the main code). It also does distributed pub-sub and persistence journal, which you can elect to exclude if not needed. Its source code is available at GitHub.

Upvotes: 1

Evgeny
Evgeny

Reputation: 1770

I would say you need:

  • dispatcher actor (cluster singleton) who send task to actor from pool of idle actors

  • your TaskRunner actor should have two states: running, and idle. In idle state it should register itself regularly to dispatcher actor (notifying that it is idle). Regularly, because of possible state losing by dispatcher in case of node shutdown and move singleton to another node.

  • dispatcher itself keep list of idle actors. When new task need to be done and list is not empty, worker is taken from the list and task is sent (worker could be removed from list immediately, but safe to work with Ack to be sure that task is taken for processing, or re-send to another worker if Ack is timed out)

Upvotes: 2

Related Questions