简体   繁体   中英

Actor retry with back-off and retry limit

I need retry mechanism on akka actors with increasing time between the retries and maximum retry limit. For this purpose I'm trying to use the BackOffSupervisor pattern, provided by akka. The problem is that, from my test, the back-off strategy and the retry limit seems to not work. Or maybe the problem is in the test?

The test looks like this:

A simple actor that throws exception at the first 5 messages

class SomeActor extends AbstractActor {

private static int messageCounter = 0;

//return the message to sender
@Override
public void preRestart(final Throwable reason, final Optional<Object> message) {
    getSelf().tell(message.get(), getSender());
}


@Override
public Receive createReceive() {
    return receiveBuilder()
            .matchEquals("hello", message -> {
                messageCounter++;
                getSender().tell("response", getSelf());

                //Throw Exception at the first 5 messages
                if (messageCounter < 5) {
                    throw new Exception();
                }

            })
            .build();
}

}

The BackOffSupervisor configuration

private ActorRef createSupervisedActor(Class<? extends Actor> actorClass) {
    final Props someActorProps = Props.create(actorClass);

    final Props supervisorProps = BackoffSupervisor.props(
            Backoff.onStop(
                    someActorProps, //actor to be supervised
                    "someActor",
                    Duration.ofSeconds(10), //min back-off time
                    Duration.ofMinutes(2), // max back-off time
                    0.2, // back-off increase factor
                    10) // max retry limit
                    .withSupervisorStrategy(
                            new OneForOneStrategy(
                                    DeciderBuilder
                                            .match(Exception.class, e -> SupervisorStrategy.restart())
                                            .matchAny(o -> SupervisorStrategy.escalate())
                                            .build())
                    )
    );

    return testSystem.actorOf(supervisorProps);

}

And the test method

    @Test
public void test() {
    new TestKit(testSystem) {{
        ActorRef actorRef = createSupervisedActor(SomeActor.class);

        actorRef.tell("hello", getRef());

        //Expect 5 responses in 1 second
        receiveN(5, Duration.ofSeconds(1));
    }};

}

The test finishes way too fast. In under a second, when from the configuration of the BackoffSupervisor, I'm expecting to take at least a 50+ sec.

The problem was due to the following reason:

Throwing an exception in the child actor(someActor in my case) is not handled by Backoff.onStop and therefore handled by normal default supervision, which means immediate restart. - https://github.com/akka/akka/issues/23406#issuecomment-372602568

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM