2013-05-07 2 views
3

직선적 인 감독 계층 구조를 상상해보십시오. 아이가 죽는다. 아버지는 그 아이를 Restart로 결정합니다. Restart ed 일 때 postRestart과 친구들을 부르지 만, 아버지가 그 아이를 다시 시작하기로 결정했다면 어떻게 될까요? 아동 배우가 그가 재개된다는 것을 알고 있습니까? 그리고 btw. 아버지는 자녀에게 예외를 발생시킨 메시지에 접근 할 수 있습니까?아동 배우는 그가 재개된다는 것을 알고 있습니까?

답변

6

이력서는 "실제로 아무 일도 일어나지 않았다"는 의미이며,이 정신에서 아이에게도 정보가 제공되지 않습니다. 거의 사용하지 않아야하는 지시어입니다.

부모는 문제의 원인이 된 메시지가 아니라 장애 자체 (예 : Throwable) 만 수신합니다. 부모와 자녀의 논리를 건강한 것 이상으로 얽히게 할 수 있기 때문입니다.

+1

동의합니다. 그러나이 점을 이해하는 것이 자녀의 상태를 보존 할 수있는 유일한 방법입니다. – agilesteel

+0

동의합니다 - 때로는 아이의 상태가 재시작시 다시 작성하기가 쉽지 않습니다. 우리는이 사건에서 이력서를 사용하여 전체 수신에 큰 시도/포착을하지 않도록합니다. – sourcedelica

+0

예외 상태에 대한 응답에 대해 이야기하는 경우 이는 사실 일 수 있지만 이력서라는 용어는 akka 설명서에 따라 다시 시작주기의 마지막 단계를 참조 할 수도 있습니다 (자세한 내용은 내 대답 참조). – JasonG

0

감독자의 supervisorStrategy에서 이러한 종류의 동작을 올바르게 설정하면 Supervisor에 오류가 발생할 수 있습니다.

import akka.actor.Actor 
import akka.actor.Props 
import akka.actor.ActorSystem 

object SupervisorTest { 
    def main(args: Array[String]) { 
    val system = ActorSystem("test") 
    val master = system.actorOf(Props[Master], "master") 
    master ! "foo" 
    Thread.sleep(500) 
    val worker = system.actorFor("/user/master/foo") 
    worker ! "bar" 
    } 
} 

class Master extends Actor{ 
    import akka.actor.OneForOneStrategy 
    import akka.actor.SupervisorStrategy._ 
    import scala.concurrent.duration._ 

    override val supervisorStrategy = 
    OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) { 
     case _: Exception    => Escalate 
     Escalate 
    } 
    override def preRestart(ex:Throwable, msg:Option[Any]) = { 
    println("In master restart: " + msg) 
    } 

    def receive = { 
    case msg:String => 
     context.actorOf(Props[Worker], msg) 
    } 
} 

class Worker extends Actor{ 
    override def preRestart(ex:Throwable, msg:Option[Any]) = { 
    println("In worker restart: " + msg) 
    } 
    def receive = { 
    case _ => 
     throw new Exception("error!!") 
    } 
} 

당신은 Master 배우 (내 예에서 관리자), 내가 Escalate에 유형 Exception의 실패를 선택하고에서 볼 수 있습니다 약간의 예는 그 동작을 표시합니다. 이로 인해 실패가 Master 액터의 preRestart까지 버블 링됩니다. 이제 msg 매개 변수가 preRestart으로 작업자 액터로 전달 된 원래의 불쾌 메시지가 될 것으로 예상했지만 그렇지 않았습니다. 내가 보여준 유일한 방법은 하위 액터의 preRestart도 무시하는 것입니다. 예를 들어, 감독관과 자녀 모두의 인쇄물을 순서대로 볼 수 있습니다.

+0

그러나 아이를 다시 시작하면 어떨까요? 그때는 다시 시작하지 않습니다. 그렇죠? 따라서'preRestart'와 친구들은 호출되지 않습니다. – agilesteel

1

다시 시작이라는 용어는 메시지 처리를 계속하며 문서의 두 지점에서 참조됩니다.

제 예외 상태에 대한 응답으로 사용된다 를 akka 문서 당 같이 실제로 재시작

As described in Actor Systems supervision describes a dependency relationship between actors: the supervisor delegates tasks to subordinates and therefore must respond to their failures. When a subordinate detects a failure (i.e. throws an exception), it suspends itself and all its subordinates and sends a message to its supervisor, signaling failureDepending on the nature of the work to be supervised and the nature of the failure, the supervisor has a choice of the following four options: 

Resume the subordinate, keeping its accumulated internal state 
Restart the subordinate, clearing out its accumulated internal state 
Terminate the subordinate permanently 
Escalate the failure, thereby failing itself 

참고 일본어 배우 명. 이력서라는 용어는 여기에서 다시 사용되어 메시지 처리를 계속한다는 의미입니다.

akka 설명서에 따라.

The precise sequence of events during a restart is the following: 

- suspend the actor (which means that it will not process normal messages until resumed), and recursively suspend all children 
- call the old instance’s preRestart hook (defaults to sending termination requests to all children and calling postStop) 
- wait for all children which were requested to terminate (using context.stop()) during preRestart to actually terminate; this—like all actor operations—is non-blocking, the termination notice from the last killed child will effect the progression to the next step 
- create new actor instance by invoking the originally provided factory again 
- invoke postRestart on the new instance (which by default also calls preStart) 
- send restart request to all children which were not killed in step 3; restarted children will follow the same process recursively, from step 2 
- resume the actor 
관련 문제