On 23 Jun 2014, at 03:24, Nicholas Phillips <[email protected]> wrote:

> I'm having some difficulty understanding some of the guarantees that the Akka 
> framework provides.  The documentation states:
> 
> To prevent visibility and reordering problems on actors, Akka guarantees the 
> following two "happens before" rules:
> 
> The actor send rule: the send of the message to an actor happens before the 
> receive of that message by the same actor.
> The actor subsequent processing rule: processing of one message happens 
> before processing of the next message by the same actor.
> Both rules only apply for the same actor instance and are not valid if 
> different actors are used.
> 
> I understand the documentation but am wondering if anything is guaranteed 
> about sending a message to a different actor.

Akka gives you at-most-once delivery and guarantees ordering of messages per 
sender-receiver pair, see 
http://doc.akka.io/docs/akka/2.3.3/general/message-delivery-reliability.html.

> The documentation also indicates that messages should (not must) be 
> immutable.  If I send a mutable message to another actor, does the receiving 
> actor run the risk of not seeing the fields that were set in the sending 
> actor?

No, that shouldn't happen when you are using a well designed mailbox. The 
default one is build on top of java.util.concurrent.ConcurrentLinkedQueue which 
gives you the following memory consistency effects: "As with other concurrent 
collections, actions in a thread prior to placing an object into 
aConcurrentLinkedQueue happen-before actions subsequent to the access or 
removal of that element from theConcurrentLinkedQueue in another thread." 
(file:///Users/heiko/tools/java/java-8-docs/api/index.html)

On the other hand, changes applied by the sender after sending might not be 
seen by the receiver. And other fun things like 
ConcurrentModificationExceptions might happen ...

> 
> My java project deals with some pretty unwieldy model objects that have 40+ 
> instance variables, but I'd like to pass those model objects between actors 
> as fields on a message.  I can make the model object a final field on the 
> message, but that obviously doesn't really make it immutable.  Making the 
> fields of the model object final puts incredible bloat on the constructor, so 
> I don't really consider that an option.  My reading of 
> http://java.dzone.com/articles/memory-barriersfences indicates that the final 
> keyword will enforce a store barrier, implying that as long as I set all of 
> the fields on my model object before I set it as a message field, the message 
> recipient should see a fully populated object - is that correct?  Even if the 
> statement is true, I don't think that will be easy to enforce across my team. 
>  Does Akka guarantee anything stronger?

Because of the above, visibility of the values shouldn't be an issue here.

> I have the use case where I pass my (mutable) model object along to another 
> actor who computes and sets some of the values on that model object before 
> constructing a reply with it.  Is this a terrible idea in Akka, or is there a 
> way to make it work?

I consider this a terrible idea, on the long run this  certainly opens many 
cans of worms. You should treat your messages (and objects contained in 
messages) as "effectively immutable", i.e. never change them. Simply create a 
copy, if you need to change them.

Heiko

> 
> -- 
> >>>>>>>>>> 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.


--

Heiko Seeberger
Twitter: @hseeberger
Web: heikoseeberger.de




-- 
>>>>>>>>>>      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