akka:``child. path.name''sak的丢失参考



我正在尝试实现曼宁的" akka in Action"书的"启动和运行"示例的Java版本。这是基于用于保存(仅在内存)并检索某些事件的Actor模型的简单HTTP服务器。我没有问题保存事件。但是在查询演员系统中的事件(所有事件)时,我确实有问题。

这是相关的(我将三个点而不是我认为与我的问题无关的代码)的BoxOffice代码 - 所有TicketSeller S的父级代码(后来负责为每个事件管理状态)。

public class BoxOffice extends AbstractActor {
    ...
    private Timeout timeout;
    final static String NAME = "boxOffice";
    //create child actors
    private ActorRef createTicketSeller(String name) {
        return getContext().actorOf(TicketSeller.props(name));
    }
    public BoxOffice(Timeout timeout) {
        this.timeout = timeout;
    }
    //the only method of an actor
    @Override
    public Receive createReceive() {
        return receiveBuilder()
                ...
                ...
                .match(GetEvent.class, this::receiveMsgGetEvent)
                .match(GetEvents.class, this::receiveMsgGetEvents)
                ...
                .build();
    }
    ...
    private void receiveMsgGetEvent(GetEvent getEvent) {
        Optional<ActorRef> maybeChild = getChildByName(getEvent.getName());
        log.info(String.format("Asking for event %s. Child is present: %s", getEvent.getName(), maybeChild.isPresent()));
        OptionalConsumer.of(maybeChild)
                .ifPresent(child -> child.forward(new TicketSeller.GetEvent(), getContext()))
                .ifNotPresent(() -> getSender().tell(Optional.empty(), getSelf()));
    }
    private void receiveMsgGetEvents(GetEvents getEvents) {
        //ask self() for each of the passed-in event
        List<CompletableFuture<Optional<Event>>> listFutureMaybeEvent =
                allChildrenStream()
                .map(child ->
                        ask(getSelf(), new GetEvent(child.path().name()), timeout)
                        .thenApply(obj -> (Optional<Event>) obj)
                        .toCompletableFuture())
                .collect(toList());
        CompletableFuture<Events> eventsFuture = toFutureEvents(listFutureMaybeEvent);
        pipe(eventsFuture, getContext().dispatcher()).to(sender());
    }
    private Stream<ActorRef> allChildrenStream() {
        return StreamSupport.stream(getContext().getChildren().spliterator(), false);
    }
    ...
    private CompletableFuture<Events> toFutureEvents(List<CompletableFuture<Optional<Event>>> futurePossibleEvents) {
        List<Event> events = futurePossibleEvents.stream()
                .map(CompletableFuture::join)
                .filter(Optional::isPresent)
                .map(Optional::get)
                .collect(toList());
        return CompletableFuture.supplyAsync(() -> new Events(events));
    }
    ...
    private Optional<ActorRef> getChildByName(String name) {
        return getContext().findChild(name);
    }
    static Props props(Timeout timeout) {
        return Props.create(BoxOffice.class, () -> new BoxOffice(timeout));
    }

基本上发生的是,在receiveMsgGetEvents中,我将消息发送给self,其中包含子名称child.path.name的消息。但是,当我收到该消息时(分别为receiveMsgGetEvent),无法通过该名称找到儿童演员:

INFO  [BoxOffice]: Asking for event $a. Child is present: false

也值得注意的是,在GetEvent之间需要很长时间,并且由同一演员接收到(如秒,但我的感觉还不到20岁)。

问题可能是由于我的CompletableFutures操纵而引起的,但我试图复制Scala等效代码。

上面的信息日志以及此消息:

INFO  [DeadLetterActorRef]: Message [java.util.Optional] from Actor[akka://mycompanyAkkaDemo/user/boxOffice#1554115585] to Actor[akka://mycompanyAkkaDemo/deadLetters] was not delivered. [1] dead letters encountered. This logging...

是在配置超时后打印(20秒)的堆叠之后打印的:

ERROR [ActorSystemImpl]: Error during processing of request: 'Ask timed out on [Actor[akka://mycompanyAkkaDemo/user/boxOffice#1554115585]] after [20000 ms]. Sender[null] sent message of type "com.mycompany.demo.messages.boxoffice.GetEvents".'. Completing with 500 Internal Server Error response. To change default exception handling behavior, provide a custom ExceptionHandler.
akka.pattern.AskTimeoutException: Ask timed out on [Actor[akka://mycompanyAkkaDemo/user/boxOffice#1554115585]] after [20000 ms]. Sender[null] sent message of type "com.mycompany.demo.messages.boxoffice.GetEvents".
    at akka.pattern.PromiseActorRef$.$anonfun$defaultOnTimeout$1(AskSupport.scala:595)
    at akka.pattern.PromiseActorRef$.$anonfun$apply$1(AskSupport.scala:605)
    at akka.actor.Scheduler$$anon$4.run(Scheduler.scala:140)
    ...
    at java.lang.Thread.run(Thread.java:748)
ERROR [OneForOneStrategy]: akka.pattern.AskTimeoutException: Ask timed out on [Actor[akka://mycompanyAkkaDemo/user/boxOffice#1554115585]] after [20000 ms]. Sender[null] sent message of type "com.mycompany.demo.messages.boxoffice.GetEvent".
java.util.concurrent.CompletionException: akka.pattern.AskTimeoutException: Ask timed out on [Actor[akka://mycompanyAkkaDemo/user/boxOffice#1554115585]] after [20000 ms]. Sender[null] sent message of type "com.mycompany.demo.messages.boxoffice.GetEvent".
    at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:292)
    at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:308)
    at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:593)
    ...
Caused by: akka.pattern.AskTimeoutException: Ask timed out on [Actor[akka://mycompanyAkkaDemo/user/boxOffice#1554115585]] after [20000 ms]. Sender[null] sent message of type "com.mycompany.demo.messages.boxoffice.GetEvent".
    at akka.pattern.PromiseActorRef$.$anonfun$defaultOnTimeout$1(AskSupport.scala:595)
    ... 11 common frames omitted

这里出了什么问题是调度员上存在阻塞。

在JVM上,由操作系统线程支持的线程在内存和过程调度程序开销中都很昂贵。Akka的优点之一是,它可以通过允许您在较小数量的线程上运行许多演员来更有效地使用线程。

这很棒,但确实意味着您绝对不应该在演员中执行阻止呼叫。CompletableFuture::join的电话在这里被阻止,这可能是您问题的原因。

通过避免阻止呼叫并使用异步API(例如CompletableFuture.allOf)您的问题应该消失。

最新更新