Dear all,

For learning Akka, I wrote below in Java to test fault-tolerance.
I have a master actor to assign job to worker actors, in the master actor, 
simply divide the job into several pieces and use round robin router to 
assign to workers (i.e. let workers add slices of a summation).

In the worker onReceive, I let the first worker who does its job to throw 
an Exception, please consider below code:

WorkerActor/onReceive:

if(RetryCount >=1 ) {
RetryCount--;
throw new Exception("exception thrown from worker");
}

WorkerActor/preRestart:
public void preRestart(Throwable cause, Option<Object> msg) {
System.out.println("Thread "+Thread.currentThread().getId()+" get restart 
message ");
if(cause instanceof Exception && msg.nonEmpty()) {
WorkerMessage workerMsg = (WorkerMessage)msg.get();
getSelf().forward(workerMsg, getContext());
//getSelf().tell(workerMsg, getSender());
}
else {
return;
}
}

For the master actor:
router = getContext().actorOf(
Props.create(WorkerActor.class).withRouter(
new RoundRobinRouter(10)));

private static SupervisorStrategy strategy = new OneForOneStrategy(10,
Duration.create(1, TimeUnit.MINUTES),
new Function<Throwable, Directive>() {

public Directive apply(Throwable exception) throws Exception {
if (exception instanceof Exception) {
return SupervisorStrategy.restart();
} else {
return SupervisorStrategy.escalate();
}
}

});

@Override
public SupervisorStrategy supervisorStrategy() {
return strategy;
}

I have 10 workers running and tasks assigned in round-robin algorithm, 
after running the example, the restart run as expected, but I have several 
questions:
1) All 10 workers' preRestart get called, why is that, is that correct as I 
used OneForOneStrategy and I thrown exception only from one worker?
    I use the msg.nonEmpty() to get rid of those null msg passed in as from 
my running result, all 10 workers run the preRestart which I am quite 
confused.

2) After one worker forwards the message, another worker receives the 
message and re-process it, they are not the same thread, is that correct?
Thread 11 processing worker 1
Thread 11 throw exception 
Thread 18 processing worker 1
>From the output, thread 11 throw exception and thread 18 process later for 
fail-over. From understanding, if one child actor failed, then parent actor 
should invoke that child actor's preRestart method only, not for all child 
actors. Is that correct?

Thanks in advance!

Regards,
George

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to