4. Akka fault-tolerant processing

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.
Insert picture description here

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:

  1. 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.
  2. 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, 10and Duration.create(1, TimeUnit.MINUTES)are passed to maxNrOfRetriesand withinTimeRangeparameters, which means that every minute a strategy to restart the child most 10times. If the withinTimeRangerestart 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:

  • ActorInitializationExceptionThe failed child actor will be stopped
  • ActorKilledExceptionThe failed child actor will be stopped
  • DeathPactExceptionThe failed child actor will be stopped
  • ExceptionThe failed child actor will be restarted
  • Other types Throwablewill 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 DeathWatchcorrective actions are taken by the supervisor when it is revealed that the children are dead. This strategy is also pre-packaged SupervisorStrategy.stoppingStrategyand comes with a StoppingSupervisorStrategyconfiguration program for use when you want /userthe guardian to apply it.

Log the failure of an Actor

By default, unless reflected upward escalate, or SupervisorStrategyrecords a failure. escalateThe failure should be handled and recorded at a higher level in the hierarchy.

By loggingEnabledsetting to when instantiating false, SupervisorStrategythe default log can be set to mute. Custom logging can be Decidercompleted 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 logFailurecustomize SupervisorStrategythe 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 Akkareceive 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
Insert picture description here

Guess you like

Origin blog.csdn.net/lp284558195/article/details/112466024