Supervision
The concept of fault tolerance is related to Actors. The use of fault tolerance processing in the Actor model is called supervision processing. The core idea of supervision is to separate the response to failure from the components that may cause the failure, and organize the components that may cause errors in a hierarchical structure for management.
In a distributed system, each component is a time bomb, so we want to be able to ensure that no matter any one of them explodes, it will not trigger a chain reaction and cause other components to explode. It can also be said that we want to be able to isolate errors or separate components that may cause failures.
Hierarchical structure of supervision
Akka uses the Actor hierarchy to describe supervision. When we create an Actor, the newly created Actor serves as a child Actor of another Actor, and the parent Actor is responsible for supervising the child Actor. The path structure of an actor shows its hierarchical structure, so it is a bit like a folder in the file system.
Surveillance strategy
In the Akka framework, the parent actor supervises the child actor and monitors the behavior of the child actor for abnormalities. In general, there are two types of monitoring strategies:
- OneForOneStrategy strategy: The parent Actor will only deal with the child Actor that has the problem. Such as restart or stop. Akka's default strategy is recommended.
- AllForOneStrategy strategy: The parent actor will process the child actor that has the problem and all its sibling nodes. It is only suitable for scenarios where each Actor is very closely connected. If only one of multiple Actors fails, the entire task will be declared as a failure.
The specific processing methods in Actor mainly include the following:
- Resume: Actor continues to process the next message;
- Stop: Stop the Actor and do no more operations;
- Restart: Create a new Actor to replace the original Actor;
- Escalate: Pass the exception information to the next supervisor.
Create a monitoring strategy
public class JavaSupervisorStrategyDemo extends AbstractActor {
private static SupervisorStrategy strategy =
new OneForOneStrategy(
10,
Duration.create("1 minute"),
/*
* resume(): Actor 继续处理下一条消息;
* restart(): 停 止Actor,不再做任何操作;
* escalate(): 新建一个 Actor,代替原来的 Actor;
* stop(): 将异常信息传递给下一个监督者;
*/
DeciderBuilder.match(ArithmeticException.class, e -> SupervisorStrategy.resume())
.match(NullPointerException.class, e -> SupervisorStrategy.restart())
.match(IllegalArgumentException.class, e -> SupervisorStrategy.stop())
.matchAny(o -> SupervisorStrategy.escalate())
.build());
@Override
public SupervisorStrategy supervisorStrategy() {
return strategy;
}
}
One-to-one strategy ( one-for-one strategy
) means that each child is treated individually. In the example above, 10
and Duration.create(1, TimeUnit.MINUTES)
are passed to maxNrOfRetries
and withinTimeRange
parameters, which means that every minute a strategy to restart the child most 10
times. If the withinTimeRange
restart count exceeds within the duration maxNrOfRetries
, the child actor will stop.
If the policy is declared in the Supervisor Actor (rather than a separate class), its decision maker can access all the internal state of the Actor in a thread-safe manner, including obtaining a reference to the currently failed child, which can be used as a failure message getSender()
.
Default supervision strategy
In general, the default behavior is sufficient: if the Actor throws an exception during operation, restart the Actor; if an error occurs, report it upwards or close the application. However, if the Actor throws an exception in the constructor, it will cause an ActorInitializationException and eventually cause the Actor to stop running.
If no supervision policy is defined for the Actor, the following exceptions will be handled by default:
ActorInitializationException
The failed child actor will be stoppedActorKilledException
The failed child actor will be stoppedDeathPactException
The failed child actor will be stoppedException
The failed child actor will be restarted- Other types
Throwable
will be reflected upward to the parent Actor
If the exception has been escalated to the root guardian, it will handle it in the same way as the default policy defined above.
Stop monitoring strategy
Measures are taken to stop the children when they fail, and then DeathWatch
corrective actions are taken by the supervisor when it is revealed that the children are dead. This strategy is also pre-packaged SupervisorStrategy.stoppingStrategy
and comes with a StoppingSupervisorStrategy
configuration program for use when you want /user
the guardian to apply it.
Log the failure of an Actor
By default, unless reflected upward escalate
, or SupervisorStrategy
records a failure. escalate
The failure should be handled and recorded at a higher level in the hierarchy.
By loggingEnabled
setting to when instantiating false
, SupervisorStrategy
the default log can be set to mute. Custom logging can be Decider
completed within.
Note that when declared inside the Supervisor Actor SupervisorStrategy
, a reference to the currently failed child can be used as sender
.
You can also logFailure
customize SupervisorStrategy
the logging in your own by overriding the method .
references
"Akka Introduction and Practice"-Chapter 3 Message Delivery
https://cloud.tencent.com/developer/article/1435518
Pay attention to the public account : 数据工匠记
Reply to Akka
receive the book "Akka Introduction and Practice"
Pay attention to the official account, 数据工匠记
and focus on the offline and real-time technical dry goods in the big data field to share regularly! Personal website www.lllpan.top