【问题标题】:Actor retry with back-off and retry limit带有退避和重试限制的演员重试
【发布时间】:2018-10-18 18:27:28
【问题描述】:

我需要在 akka actor 上使用重试机制,增加重试之间的时间和最大重试限制。 为此,我正在尝试使用 akka 提供的 BackOffSupervisor 模式。问题是,从我的测试来看,退避策略和重试限制似乎不起作用。或者问题出在测试中?

测试看起来像这样:

一个在前 5 条消息中引发异常的简单参与者

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();
}

}

BackOffSupervisor 配置

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);

}

以及测试方法

    @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));
    }};

}

测试结束太快了。在一秒钟内,从 BackoffSupervisor 的配置开始,我预计至少需要 50 多秒。

【问题讨论】:

    标签: java akka akka-testkit retry-logic exponential-backoff


    【解决方案1】:

    问题是由于以下原因造成的:

    Backoff.onStop 不处理子actor(在我的情况下为 someActor)中的异常,因此由正常的默认监督处理,这意味着立即重新启动。 - https://github.com/akka/akka/issues/23406#issuecomment-372602568

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-17
      • 2021-07-04
      • 1970-01-01
      • 2018-08-25
      • 2015-11-01
      • 1970-01-01
      • 2021-07-22
      • 2013-09-02
      相关资源
      最近更新 更多