A Cameo That Is Worth an Oscar
Proper context handling is essential when working with Akka actors. Let's see the Cameo design pattern in action and see how it solves some complex problems.
Join the DZone community and get the full member experience.Join For Free
rarely during my life as a developer have i found pre-packaged solutions that fit my problem perfectly. design patterns are an abstraction of both problems and solutions. so, they often need some kind of customization on the specific problem. while i was developing my concrete instance of the actorbase specification , i came across the cameo pattern . it enlighted my way and my vision about how to use actors profitably. let’s see how and why.
the problem: capturing context
jamie allen, in his short but worthwhile book effective akka , begins the chapter dedicated to actor patterns with the following words:
one of the most difficult tasks in asynchronous programming is trying to capture context so that the state of the world at the time the task was started can be accurately represented at the time the task finishes.
this is exactly the problem we are going to try to resolve.
actors often model long-lived asynchronous processes in which a response in the future corresponds to one or more messages sent earlier. meanwhile, the context of execution of the actor could be changed. in the case of an actor, its context is represented by all the mutable variables owned by the actor itself. a notable example is the
variable that stores the sender of the current message being processed by an actor.
context handling in actorbase actors
let’s make a concrete example. in actorbase, there are two types of actors among the others:
. each actor of type
, but it does not physically store the key-value couples. this information is stored by
actors. so, each
owns a distributed set of its key-value couples, which means that owns a set of
actors that stores the information for it.
can route many types of messages to its
, representing crud operations on the data stored. the problem here is that if a
which value corresponds to a
(if any), it has to send
messages of type
. once all the
answer the query messages, the
can answer to its caller with the requested
the sequence diagram below depicts exactly the above scenario.
the number of answers of
actors and the body of their responses represent the execution context of a
actor’s context handling
so, we need to identify a concrete method to handle the execution context of an actor. the problem is that between the sending of a message and the time when the relative response is received, an actor processes many other messages.
using nothing more than my ignorance, the first solution i depicted in actorbase was the following.
that's a lot of code to merely handle a bunch of messages, isn’t it? as you can see, to handle the execution context, i defined a dedicated class:
. for each
message identified by a
, this class stores the following information:
- the original sender
the list of responses from
storekeeperactors for the message
the values the
as you can imagine, the handling process of this context is not simple, as a single
has to handle all the messages that have not received a final response from all the relative
we can do much better, trust me.
asking the future
a first attempt to reach a more elegant and concise solution might be the use of the
this is a great way to design your actors, in that they will not block waiting for responses, allowing them to handle more messages concurrently and increasing your application’s performance.
using the ask pattern, the code that handles the
message and its responses will reduce to the following.
whoa! this code is fairly concise with respect to the previous snippet. in addition, using
and a syntax that is fairly declarative, we can achieve, quite easily, the right grade of asynchronous execution that we need.
however, there are a couple of things about it that are not ideal. first of all, it is using futures to ask other actors for responses, which creates a new
promiseactorreffor every message sent behind the scenes. this is a waste of resources.
furthermore, there is a glaring race condition in this code — can you see it? we’re referencing the “sender” in our map operation on the result from
futurequeryack, which may not be the same
actorrefwhen the future completes, because the
storefinderactorref may now be handling another message from a different sender at that point!
even more annoying!
the problem here is that we are attempting to take the result of the off-thread operations of retrieving data from multiple sources and return it to whoever sent the original request to the
. but the actor will likely have move onto handling additional messages in its mailbox by the time the above futures complete.
the trick is capturing the execution context of a request in a dedicated inner actor. let’s see how our code will become.
much better. we have captured the context for a single request to
as the context of a dedicated actor. the original sender of the
actor was captured by the constant
and shared with the anonymous actor using a
it’s easy, isn’t it? this simple trick is known as the extra pattern . however, we are searching for a cameo in our movie.
finally presenting the cameo pattern
the extra pattern is very useful when the code inside the anonymous actor is very small and trivial. otherwise, it pollutes the main actor with details that do not belong to its responsibility (one for all, actor creation).
it is also similar to lambdas, in that using an anonymous instance gives you less information in stack traces on the jvm, is harder to use with a debugging tool, and is easier to close over state.
luckily, the solution is quite easy. we can move the anonymous implementation of the actor into its own type definition.
this results in a type only used for simple interactions between actors, similar to a cameo role in the movies.
doing so, the code finally becomes the following.
much cleaner, so satisfying.
notice that the router in the
tells the routees to answer to the actor that handles the query messages,
broadcastrouter.route(get(key, u), handler)
. moreover, remember to capture the
in a local variable in the main actor before passing its reference to the inner actor.
make certain you follow that pattern, since passing the sender
actorrefwithout first capturing it will expose your handler to the same problem that we saw earlier, where the sender
so far so good. we started by stating that context handling is not so trivial when we talk about akka actors. i showed you my first solution to such a problem in actorbase, the database based on the actor model i am developing.
we agreed that we do not like it.
so, we moved on and we tried to use
s. the solution was elegant but suffered from race conditions. in the path through the last solution, we met the
, which solved the original problem without any potential drawback. the only problem is that this solution was not clean enough. finally, we approached the cameo pattern, and it shined in all its beauty.
p.s.: all the code relative to actorbase can be found on my github .
Published at DZone with permission of Riccardo Cardin, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.