Ivan Suhinin
Ivan Suhinin

Reputation: 758

Akka - Getting info from terminated actor

I'm using Akka actors in Scala for parallel processing of queue items. I have a MasterActor with 10 reusable child ProcessorActors.

Items being processed can be of different types, e.g. red, blue & green items. At one moment of time, only a single item of some type can be processed. Thus, if one red item is being processed, no more reds can be processed at the same time.

All is fine but now I try to implemented good fault tolerance for the app and it turns out I can't get much info about what item type failed in Terminated message. If ProcessorActor fails, I need to mark appropriate type in MasterActor as available for processing. Right now I'm stuck as I just can't get what item type failed. I have an ActorRef in Terminated message but afaiu it's not good to send messages to it right after I get that message.

In the end I can be left with all possible types marked as "being processed" while in reality it's just their appropriate actors are dead.

Please advice.

Upvotes: 1

Views: 795

Answers (4)

Ivan Suhinin
Ivan Suhinin

Reputation: 758

Ok, what I finally did and what seemed best. I set a Restart policy on ProcessorActor failure and handle preRestart method in it. If reason:Option[Any] is not empty and matches StartProcessingMessage(Item(ItemType)) then I send a ProcessingFailedMessage(Item(ItemType)) to sender. Supervisor actor then marks type as not processed and next while loop will initiate processing by some ProcessorActor, maybe even the one that failed and was just restarted.

Upvotes: 0

Robin Green
Robin Green

Reputation: 33033

It seems like what you want to do could be achieved just by redesigning your architecture to have a red actor, a blue actor and a green actor, and setting the supervision strategy to do a restart on actor failure, instead of a stop. Really simple. No need to even handle the Termination message yourself.

Upvotes: 1

Sergiy Prydatchenko
Sergiy Prydatchenko

Reputation: 988

Your ProcessorActor can send a message (with type of the item) to MasterActor just before start processing the item. In your MasterActor you can maintain a Map[ActorRef, ItemType] using which you can determine by the ActorRef (received in the Terminated message) the last item type being processing when the ProcessorActor dies.

Upvotes: 0

idonnie
idonnie

Reputation: 1703

On Terminated set some 'pause' flag of not providing new jobs to children, then ping all ProcessorActors if they have active jobs, if any job can be assigned - assign job and release 'pause' flag. If terminated received when that 'pause' flag is set - do ping once more.

Upvotes: 1

Related Questions