Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually creating actor hierarchy in akka

Tags:

scala

akka

actor

My application needs to be able to create a tree structure of actors. The standard way to do this I imagine would be to put the instantiation code inside the actors so they can instantiate their children. The approach I would rather would be to be able to instantiate an actor at a given path. Such as create actor A in mySystem and then being able to directly create akka://mySystem/A/B and other actors. Does such functionality exist? It would simplify my code greatly.

EDIT: Now that I'm not on my phone, let me elaborate.

say I have a class

class myActor extends actor

and I need to make an n-way tree of these. Instead of having the code required to instantiate their own children in the receive function with something like

case Create(n:Int) => {}

I am looking to simplify the myActor code by not including any of that and instead being able to create my hierarchy at the start of my code manually. So ideally something like this (assuming hypothetical static function "create"):

val sys = ActorSystem("mySystem")
Akka.Actors.Create("akka://mySystem/a", new myActor())
Akka.Actors.Create("akka://mySystem/a/b", new myActor())
Akka.Actors.Create("akka://mySystem/a/c", new myActor())

which would create the actor tree:

  a
 / \
b   c

Now, does this exist? Is there a better way to do this without cluttering my actor code with the instantiation code?

EDIT, round 2:

Ok, looks like this functionality doesn't exist. I instead created a sub-trait of actor and forced all my instantiation code in there so that my concrete implementation classes are still tidy.

like image 803
Alex Avatar asked Jan 12 '13 02:01

Alex


1 Answers

Your question starts with the postulation of a solution, and it unfortunately does not include a description of the problem.

The actor tree is foremost to be understood as the supervision hierarchy: the parent handles its children’s failures and the life-cycle of the parent places bounds on the life-cycle of its children. Whether or not an actor should be the child of another needs to be determined by considering these implications.

I say that because your question implies that you are modeling the supervision tree according to some other criterion, quite possibly to abuse it as a registry with system.actorFor as the look-up mechanism. There may be rare cases where that works, but a priori I would not recommend it; I’d rather put a hash-map inside a dispatcher actor and have that handle the look-up. If you conclude—by measurement—that that does not perform well enough, you can scale it up by using a router.

To answer your question now: each parent needs to create its own children, which mean that it must in one way or another contain the code to do that. You can pass Props down the hierarchy to make parts of it configurable, but you should not work around what supervision means; ask yourself what should happen if one of the leaves fails, and then ask yourself whether your presumed leaves should be restarted and/or killed when an intermediate actor is restarted.

like image 88
Roland Kuhn Avatar answered Sep 20 '22 22:09

Roland Kuhn