Preserve (part of) Actor State on Restart?

475 views
Skip to first unread message

oschulz

unread,
Dec 12, 2010, 9:40:14 AM12/12/10
to Akka User List
From what I've seen, when an actor is restarted, a new instance of the
actors
class is created, using the same constructor arguments used to create
the original
actor object. This means, of course, that the complete state of the
actor is lost
on restart.

Since restart means that there was a problem with the actor, I guess
that is mostly
a good thing - since the actor's state may have been responsible for
it's crash in
the first place, it should be deleted to make sure the actor won't
crash again right
away.

However, what if i need to preserve at least part of that state - in
particular,
necessary references to other actors (possibly not linked to this
one)? Is
that possible somehow?


Cheers,

Oliver

Jonas Bonér

unread,
Dec 12, 2010, 9:45:01 AM12/12/10
to Akka User List
You have the preRestart and postRestart callbacks. Use them to store away state and reinit Actor state. Use self.uuid as key. You can either send it to another Actor, store it in one of Akka's persistent backends or some other way persist it. Hope it helps.
--
Jonas Bonér
Specialist at Large
work: http://scalablesolutions.se
code: http://akka.io
twtr: @jboner

"oschulz" <oliver...@tu-dortmund.de> wrote:

>--
>You received this message because you are subscribed to the Google Groups "Akka User List" group.
>To post to this group, send email to akka...@googlegroups.com.
>To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
>For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.
>

oschulz

unread,
Dec 12, 2010, 9:52:45 AM12/12/10
to Akka User List
That sounds good. I wasn't fully sure if an Actor's UUID is
guaranteed to be the same after a restart.

Thanks, Jonas!

Tushar Deshpande

unread,
Dec 13, 2010, 9:16:25 PM12/13/10
to Akka User List, oliver...@tu-dortmund.de, jo...@jonasboner.com
Hi,

I implemented this as follows.

I created a custom exception called InformedException that takes
"State"
of the actor as a parameter. Then, I designed the receive method as
follows

def receive = {
case "mesg1" =>
oldState = state // save current state
try{
updateState1()
}catch {
case ex:Throwable => throw new InformedException(oldState)
}
case "mesg2" =>
oldState = state // save current state
try{
updateState2()
}catch {
case ex:Throwable => throw new InformedException(oldState)
}
case _ => throw new InformedException(state)
}

This approach works fine and it avoids the need to save the state
explicitly in some data structure.

Any comments?

Best regards,

Tushar Deshpande

Reply all
Reply to author
Forward
0 new messages