my myprocessingactor actor routed using consistenthashingrouter on entity id, such there may multiple instances of actor in process @ 1 time (across multiple threads)
the actor uses map perform calculation logic. therefore, 2 different actor instances reading , writing map @ same time.
sharing map across these actor instances seems blatant violation of actor model, if java concurrenthashmap.
what best options dealing kind of problem? didn't see addressed in akka documentation, unless missed this.
i can see 2 options:
- a single instance of
mymapmanageractormanage reading/writing map. if single threaded thoughmyprocessingactorsinglethreaded - use of akka stm - not see though in latest version of project
what other recommended approaches there?
as said in question, sharing mutable state between actors bad thing. have used both approaches mention (using mapmanageractor , using akka stm) , both can work, although mapmanageractor approach feels better me. far mapmanageractor concerned, parts serialized actual reads , writes map, not calculations themselves, depending on use case, might find parallelization adequate.
another option replace map actors. have 1 parent actor manages children, , 1 child of actor per map entry. if set of keys static, pre-create child actors on startup, using, example, key (or hashcode thereof) actor name. use actorselection access child actor directly, in /user/parentactor/<key>. if actors need created dynamically, best ask parent actor specific child, , have reply actorref specific child (creating if necessary). like:
override def receive = { case getactor(key) => context.children(key) match { case some(ref) => sender ! getactorreply(ref) case none => sender ! context.actorof(childactor.props, key) } } } the myprocessingactor use actorref processing.
Comments
Post a Comment