On Wed, Feb 4, 2015 at 10:22 PM, Steve Rehrauer <[email protected]>
wrote:

> Our clustered app has a cluster singleton actor that constructs a Cluster
> instance, and also subscribes to cluster events.
>
> If you ask it, "What are the current nodes?" it uses cluster.state.members.
>
> I.e., simplified, something like:
>
>   val cluster = Cluster(context.system)
>
>   override def preStart(): Unit =
>     cluster.subscribe(self, classOf[MemberEvent])
>   def receive = {
>     case state: GetNodes =>
>       sender ! cluster.state.members
>
>     ...
>   }
>
>
> Currently, we just info-log events like MemberUp, MemberDown, etc.
>
> But I see an Akka activator example for listening to cluster state that
> does something like this instead:
>
>   val cluster = Cluster(context.system)
>   var nodes = Set.empty[Member]
>   override def preStart(): Unit =
>     cluster.subscribe(self, classOf[MemberEvent])
>   def receive = {
>      case MemberUp(member) =>
>       nodes += member
>     case MemberRemoved(member, _) =>
>       nodes -= member   case state: GetNodes =>
>       sender ! nodes
>
>     ...
>   }
>
> Is there a reason not to use the Cluster object's mutable state?  It seems
> to work doing it that way, so I'm wondering whether the activator example
> is doing it this way just to illustrate how to handle some MemberEvents, or
> whether what our app is doing is incorrect or less reliable in some
> circumstances?
>

There is no ordering guarantee between the update of the
Cluster(system).state and when the member events are received.
For example when you receive MemberUp the Cluster(system).state might not
include that member yet.

If you want to act when membership is changed you should subscribe to the
member events and if needed keep track of the state in your actor to have a
consistent view.

If you only need to grab current state independent of when it changed you
can access a snapshot of it with Cluster(system).state.

In other words, nothing wrong with your example:

  def receive = {
    case state: GetNodes =>
      sender ! cluster.state.members


Cheers,
Patrik


>
>  --
> >>>>>>>>>> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>



-- 

Patrik Nordwall
Typesafe <http://typesafe.com/> -  Reactive apps on the JVM
Twitter: @patriknw

-- 
>>>>>>>>>>      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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to