Dependency injection of ShardRegion actor

125 views
Skip to first unread message

Chris Toomey

unread,
Jul 17, 2014, 11:52:32 PM7/17/14
to akka...@googlegroups.com
We're going to use cluster sharding to route messages to the appropriate actors on the servers in a cluster.  We'll have many actors that will be sending messages via the ShardRegion actor, and were thinking originally we'd inject that actor as a dependency into the actors that need it as we do other dependencies (e.g., an event bus).

But since ClusterSharding.shardRegion() returns an ActorRef, presumably that could become stale (if the ShardRegion actor was restarted) and hence wouldn't make sense as an injected dependency.  The alternatives I can think of are 1) obtaining the ShardRegion's ActorSelection and passing that as the dependency, or 2) passing in the shard entry name and calling ClusterSharding.shardRegion()  every time to get the current ActorRef.  Of these, 1) is definitely cleaner as it doesn't couple the dependent actors to ClusterSharding at all.

Am I right in assuming that the performance overhead of 1) and 2) are comparable, and negligible in the grand scheme of things, compared to sending directly to the ActorRef?

thx,
Chris

Martynas Mickevičius

unread,
Jul 18, 2014, 11:02:50 AM7/18/14
to akka...@googlegroups.com
Hi Chris,

if the ShardRegion will get restarted by its guardian, ActorRefs pointing to it will not go stale. They will go stale if ShardRegion would be stopped and then started again. But I can not see a likely scenario for that to happen.

I am not really sure about alternative 1). How would you construct actor selection?

Alternative 2) is very cheap, because ClusterSharding.shardRegion(...) is a lookup in a map. You could abstract that away in a implicit conversion.


--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Martynas Mickevičius
TypesafeReactive Apps on the JVM

Chris Toomey

unread,
Jul 18, 2014, 1:25:52 PM7/18/14
to akka...@googlegroups.com
I was assuming one should never rely on any specific ActorRef being "permanent" since that'd be assuming something about the implementation of it or its supervisor that could be wrong or could change later, and my application would break if it ever did go stale.  Is that too paranoid, so for certain ActorRefs like this shard region it's safe to assume that it won't ever go stale?

To get the ActorSelection at startup time I'd get the ActorRef from ClusterSharding.shardRegion(...) then get its ActorPath and its ActorSelection from that.

What's the overhead of sending to an ActorSelection vs. ActorRef?

Using an implicit conversion from entry name to ActorRef is an interesting alternative too, thanks.

Chris


You received this message because you are subscribed to a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/ZRc6ftPEeW8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.

Patrik Nordwall

unread,
Jul 18, 2014, 4:25:47 PM7/18/14
to akka...@googlegroups.com




18 jul 2014 kl. 19:25 skrev Chris Toomey <cto...@gmail.com>:

I was assuming one should never rely on any specific ActorRef being "permanent" since that'd be assuming something about the implementation of it or its supervisor that could be wrong or could change later, and my application would break if it ever did go stale.  Is that too paranoid, so for certain ActorRefs like this shard region it's safe to assume that it won't ever go stale?

I can confirm that shard region is not supposed to be stopped, and you can inject the ref and assume that it is valid for the life span of the ActorSystem.
/Patrik

Chris Toomey

unread,
Jul 18, 2014, 10:29:14 PM7/18/14
to akka...@googlegroups.com
Thanks Patrik.

So is a general rule then if one knows enough about the implementation to know that a given type of actor won't ever be stopped, it's safe to use its ActorRef for the lifetime of the ActorSystem, otherwise one should use an ActorSelection instead?

Chris

Akka Team

unread,
Jul 21, 2014, 6:52:49 AM7/21/14
to Akka User List
Hi Chris,

In general, you should use ActorRefs all the time. If the actor is only restarted it does not affect the ActorRef. If an actor is stopped at a path, and another one is created on the same path, then yes, you should use an ActorSelection, but it is preferred to only use the selection to identify the underlying actor and use the ActorRef you get back (you can watch that ref to know when it is gone).

-Endre
Akka Team
Typesafe - The software stack for applications that scale
Blog: letitcrash.com
Twitter: @akkateam
Reply all
Reply to author
Forward
0 new messages