问题
I'm experimenting with OneForOneStrategy
strategy, which escalates the failure. My structure of Actor is: Supervisor -> Slave
, where Slave is child of Supervisor.
Slave responds to 2 messages - "fail" (throws exception) and "work" (makes some logging)
My first problem was that if Slave failed, then Supervisor got restarted (because of the escalation strategy, the "Guardian" have restarted it), however Slave was stopped and not restarted.
I guess there is a reason to this default behavior of stopping the children instead of restarting them. Could you please explain this design decision? In which cases is this a better approach than restart ?
My problem with the behavior was that Slave's inbox looked like this: "work", "fail", "work".
After the failure, the last "work" message ended in Dead Letter, since the Slave was stopped & started (by initialization of Supervisor).
The solution was to override the preRestart
method:
@Override
public void preRestart(Throwable reason, Optional<Object> message) throws Exception {
postStop();
}
This fixed the problem and caused the children to be restarted. I also had to change the initialization of Slave in Supervisor.
回答1:
The reason the children are stopped is that they are seen as part of the internal state of the actor, and when an actor is restarted it is to start it from a clean slate so to speak.
As you have noted you can opt out of it by overriding preRestart
.
We are revisiting this default behavior for Akka Typed, which is the next generation of the Akka actor API, you can see some discussion here if you are curious: https://github.com/akka/akka/issues/25556
来源:https://stackoverflow.com/questions/52690325/akka-escalate-supervision-strategy-stops-children