akka/akka-core

PersistentActor doesn't stop when RecoveryCompleted message is failed to be processed

开放

#30,439 创建于 2021年7月27日

 (5 条评论) (1 个反应) (0 位负责人)Scala (3,547 个派生)batch import
1 - triagedhelp wantedt:persistence

仓库指标

星标
 (13,277 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

According to the documentation to 'receiveRecover' method "If there is a problem with recovering the state of the actor from the journal, the error will be logged and the actor will be stopped.". However, it doesn't happen if error is raised on RecoveryCompleted message. In this case the actor fails. Here is a code which demonstrates this problem:

import akka.actor.{Actor, ActorSystem, OneForOneStrategy, Props, SupervisorStrategy}
import akka.persistence.{PersistentActor, RecoveryCompleted}

class TestActor(fail: Boolean) extends PersistentActor {
  override def receiveRecover: Receive = {
    case RecoveryCompleted if fail => throw new RuntimeException("RecoveryCompleted BOOM!")
  }

  override def receiveCommand: Receive = {
    case msg: Message => persist(msg)(_ => ())
  }

  override def persistenceId: String = "123"
}

case class Message(data: String)

class TestActorSupervisor extends Actor {

  context.actorOf(Props(new TestActor(true)))

  override def supervisorStrategy: SupervisorStrategy = OneForOneStrategy() {
    case e: Throwable =>
      println("NOT CAUGHT!!!") // demonstrates that the actor was not stopped properly
      SupervisorStrategy.Stop
  }

  override def receive: Receive = PartialFunction.empty
}

object Main extends App {

  val system = ActorSystem()
  val ref = system.actorOf(Props(new TestActor(false)))
  ref ! Message("test")
  Thread.sleep(500)
  system.actorOf(Props[TestActorSupervisor])

}

贡献者指南