Can not always reuse Actor's name after graceful stop - scala

I studied Akka's "Graceful Stop" for actors and created a small test app to test it.
Application shows that "Graceful Stop" mentioned in http://doc.akka.io/docs/akka/snapshot/scala/actors.html#Graceful_Stop
does not always guarantee that you can reuse the gracefully stopped Actor's name.
Every now and then the following exception appears:
Exception in thread "main" akka.actor.InvalidActorNameException: actor name [DummyActor] is not unique!
Why is that? How can I fix the app so that InvalidActorNameExceptions would not appear every now and then?
Here is the code:
main class...
import akka.actor._
import akka.pattern.gracefulStop
import DummyActor.Stop
import DummyActor
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
object AkkaTest {
def main(args: Array[String]) {
val actorsystem = ActorSystem("testSystem")
1 to 5 foreach { _ =>
// Create an actor with name: DummyActor
val dummyActor = actorsystem.actorOf(Props[DummyActor], "DummyActor")
// Gracefully stop the DummyActor
val stopped: Future[Boolean] = gracefulStop(dummyActor, 5 seconds, Stop)
Await.result(stopped, 6 seconds)
}
val terminated: Future[Terminated] = actorsystem.terminate()
Await.result(terminated, 10 seconds)
System.out.println("Finished successfully. Try again, eventually it will fail. You can also increase the amount of loops.")
}
}
and the actor...
import akka.actor.Actor
import DummyActor.Stop
object DummyActor {
case object Stop
}
class DummyActor extends Actor {
override def receive: Receive = {
case Stop => context stop self
}
}
I have Scala 2.11.7 and with Java 8 and Akka 2.4.0.

Commend has awfull formatting so, i'll copy it here
I just followed the link and found red alert here
Warning
Keep in mind that an actor stopping and its name being deregistered
are separate events which happen asynchronously from each other.
Therefore it may be that you will find the name still in use after
gracefulStop() returned. In order to guarantee proper
deregistration, only reuse names from within a supervisor you control
and only in response to a Terminated message, i.e. not for top-level
actors.
So make supervisor actor and start namesake only on Terminated reception

Related

Does Akka automatically copy over variables when an actor fails

In Akka Cookbook by Héctor Veiga Ortiz, the reader is told that
When an actor throws an exception, it sends a message to the supervisor, and the supervisor handles the failure by restarting that actor. It clears out the accumulated state of the actor, and creates a fresh new actor, means, it then restores the last value assigned to the state of old actor to the preRestart value.
However, I tried testing the following code, which suggests that what the author says isn't true.
import akka.actor._
import akka.actor.SupervisorStrategy._
import akka.util.Timeout
import scala.concurrent.Await
import scala.concurrent.duration._
import akka.pattern.ask
case object Error
case class StopActor(actorRef: ActorRef)
case object Inc
class LifeCycleActor extends Actor {
var sum = 1
override def preRestart(reason: Throwable, message: Option[Any]):Unit =
println(s"sum in preRestart is $sum")
override def preStart(): Unit = println(s"sum in preStart is $sum")
def receive = {
case Inc => sum += 1
case Error => throw new ArithmeticException()
case _ => println("default msg")
}
override def postStop(): Unit =
println(s"sum in postStop is ${sum * 3}")
override def postRestart(reason: Throwable): Unit = {
sum = sum * 2
println(s"sum in postRestart is $sum")
}
}
class Supervisor extends Actor {
override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute){
case _: ArithmeticException => Restart
case t =>
super.supervisorStrategy.decider.applyOrElse(t, (_:Any)=>Escalate)
}
def receive = {
case (props: Props, name: String) => sender ! context.actorOf(props, name)
case StopActor(actorRef) => context.stop(actorRef)
}
}
object ActorLifeCycle extends App {
implicit val timeout = Timeout(2 seconds)
val actorSystem = ActorSystem("Supervision")
val supervisor = actorSystem.actorOf(Props[Supervisor], "supervisor")
val childFuture = supervisor ? (Props(new LifeCycleActor), "LifeCycleActor")
val child = Await.result(childFuture.mapTo[ActorRef], 2 seconds)
child ! Inc
child ! Error
Thread.sleep(1000)
supervisor ! StopActor(child)
}
The output I get is as follows.
sbt:chpt2_ActorLifeCycle> runMain ActorLifeCycle
sum in preStart is 1
sum in preRestart is 2
[ERROR] [11/08/2018 20:06:01.423]
[Supervision-akka.actor.default-dispatcher-4]
[akka://Supervision/user/supervisor/LifeCycleActor] null
java.lang.ArithmeticException
sum in postRestart is 2
sum in postStop is 6
If what the author says is true, the final value should be double what it is.
First I guess you forget to add sum += 1 when receive message Inc in child actor when post the question, please modify. Otherwise, as I test, you cannot get your output.
Next explain your code:
From next diagram, you can see the preReStart is called on old instance, not on new instance.
There is also a desc about this, detail here
The old actor is informed by calling preRestart with the exception which caused the restart and the message which triggered that exception; the latter may be None if the restart was not caused by processing a message, e.g. when a supervisor does not trap the exception and is restarted in turn by its supervisor, or if an actor is restarted due to a sibling’s failure. If the message is available, then that message’s sender is also accessible in the usual way (i.e. by calling sender). This method is the best place for cleaning up, preparing hand-over to the fresh actor instance, etc. By default it stops all children and calls postStop.
The initial factory from the actorOf call is used to produce the fresh instance.
The new actor’s postRestart method is invoked with the exception which caused the restart. By default the preStart is called, just as in the normal start-up case.
So, for your example:
when call preRestart, it print the sum of old actor, that is 2, notice: you have inc it.
when call postRestart, it print the sum of new actor, that is the initial value 1 with the calculation sum = sum * 2, finally print 2, not 4. The Inc message just receive on old instance, not on new instance.
Finally, the content of the book:
When an actor throws an exception, it sends a message to the supervisor, and the supervisor handles the failure by restarting that actor. It clears out the accumulated state of the actor, and creates a fresh new actor, means, it then restores the last value assigned to the state of old actor to the preRestart value.
I think what you concern is it then restores the last value assigned to the state of old actor to the preRestart value. I don't quite know what it means, if you just think it assign the last value of old actor to preRestart function, then it is correct, as it just run on old instance, otherwise, it seems conflict with akka official guide & experiment; And, if want to restore the value, we may had to use resume not restart.
Anyway, I think we should use the akka offical document as a standard, and understand the correct logic.

ExecutionContext causes Akka dead letter

For some reason I have to use gRPC and Akka at the same time. When this actor is started as a top actor, nothing goes wrong (in this little demo). But when it becomes a child actor, it cannot receive any messages, and the following is logged:
[default-akka.actor.default-dispatcher-6] [akka://default/user/Grpc] Message [AkkaMessage.package$GlobalStart] from Actor[akka://default/user/TrackerCore#-808631363] to Actor[akka://default/user/Grpc#-1834173068] was not delivered. [1] dead letters encountered.
The example core:
class GrpcActor() extends Actor {
val ec = scala.concurrent.ExecutionContext.global
val service = grpcService.bindService(new GrpcServerImpl(), ec)
override def receive: Receive = {
case GlobalStart() => {
println("GlobalStart")
}
...
}
}
I tried to create a new ExecutionContext like:
scala.concurrent.ExecutionContext.fromExecutor(Executors.newFixedThreadPool(10))
Why is this happening, and how do can I debug a dead letters problem like this (no exception is thrown)?
Update:
Sorry I didn't list everything here. I used normal Main method to test GrpcActor as top actor, and ScalaTest to test it as child actor, which is a mistake.
class GrpcActorTest extends FlatSpec with Matchers{
implicit val system = ActorSystem()
val actor: ActorRef = system.actorOf(Props[GrpcActor])
actor ! GlobalStart()
}
It is this empty test suite that active shutdown the whole actor system. But the problem is with this line
val service = grpcService.bindService(new GrpcServerImpl(), ec)
the delivery of GlobalStart() was delayed after the shutdown.
Without that line, message can be delivered before the shutdown.
Is this a normal behavior?
(My guess: it happened that the GlobalStart() was queued after the shutdown message with that line, which did some heavy work and made the difference in time)
One way to address the problem is to make service a lazy val:
class GrpcActor extends Actor {
...
lazy val service = grpcService.bindService(new GrpcServerImpl(), ec)
...
}
A lazy val is useful for long-running operations: in this case, it defers the initialization of service until it is used for the first time. Without the lazy modifier, service is initialized when the actor is created.
An alternative approach is to add a Thread.sleep in your test to prevent the actor system from shutting down before the actor has fully initialized:
class GrpcActorTest extends FlatSpec with Matchers {
...
actor ! GlobalStart()
Thread.sleep(5000) // or whatever length of time is needed to initialize the actor
}
(As a side note, consider using the Akka Testkit for your actor tests.)
Add a supervisor strategy to its parent, add println to the actor lifecycle. There is something that kill your actor . Finally, if you provide a complete example maybe I can say more :)

Failed on Injecting Akka Actor to Play Framework 2.5 with Guice

I am trying to use an Akka actor in my Play 2.5 application with dependency injection. I basically followed the documentation on that, and here's a glimpse of my code:
The Actor
Basically, I have a simple actor that receive some data, process it, and send it back to the caller.
package actors
import akka.actor._
import akka.pattern.{AskTimeoutException, ask}
import akka.util.Timeout
import javax.inject._
import com.google.inject.assistedinject.Assisted
import play.api.Configuration
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
object ServiceActor {
val ConfigKey = "actors.path" // remote actor path
trait Factory {
def apply(key: String): Actor
}
}
class ServiceActor #Inject()(configuration: Configuration,
actorSystem: ActorSystem,
#Assisted key: String)
(implicit val ec: ExecutionContext)
extends Actor {
import ServiceActor._
implicit val timeout = Timeout(5.minutes)
def receive = {
case data: SomeData => sender ! someFunction(data)
}
// implemented in the real code, omitted because not important for the question
def someFunction = ???
}
The Module
As per documentation, I need to make a simple module, and activate it. Here's the module:
package modules
import com.google.inject.AbstractModule
import play.api.libs.concurrent.AkkaGuiceSupport
import actors.ServiceActor
class ServiceModule extends AbstractModule with AkkaGuiceSupport {
def configure = {
bindActor[ServiceActor]("service-actor")
}
}
And then I activate this module by adding the following line to application.conf:
play.modules {
enabled += modules.ServiceModule
}
The Controller
Finally, I try to use the actor in a controller:
package controllers
import akka.actor._
import akka.pattern.ask
import akka.util.Timeout
import javax.inject._
import play.api.mvc._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext
#Singleton
class SearchController #Inject()(#Named("service-actor") serviceActor: ActorRef)
(implicit ec: ExecutionContext)
extends Controller {
implicit val timeout = Timeout(5.minutes)
def search(param: String) = Action.async {
val request = SomeRequestType(param)
(serviceActor ? request).mapTo[SomeResponseType] map { result =>
Ok(result.toString)
}
}
}
As far as I can understand, I have followed the documentation perfectly, but calling the controller give me this error:
[AskTimeoutException: Recipient[Actor[akka://application/user/service-actor#-366470383]]
had already been terminated.
Sender[null] sent the message of type "some.namespace.SomeRequestType".]
As far as I can understand, the error is about how the actor is a null, and we can't send message to it. While the error code said "had already been terminated", it looks like the actor's never been initiated in the first place.
I'm not sure where to look first, and what am I doing wrong as I have no experience in using Guice.
The question is, what am I doing wrong? Why didn't the actor initiated, or if it is indeed already terminated, why is it terminated? How do I fix this?
Thank you very much.
Hope you have already solved the issue. However I am still posting answers here just for some new comers' reference since it took me a while to figure out the cause of this issue.
As far as I can understand, the error is about how the actor is a null, and we can't send message to it.
Not exactly. Ask pattern ? has an implicit parameter of sender, which defaults to be ActorRef.noSender, the source code is here.
Normally, if you're inside an Actor you have an implicit ActorRef in scope called self, but since you're not in an Actor it's just taking the default.
While the error code said "had already been terminated", it looks like the actor's never been initiated in the first place.
Correct. Most likely your actor was never initiated correctly in the first place. In your console, you should see more error messages than what you are seeing from the response of Play Webpage.
For example below is the error I actually got before I got the AskTimeoutException.
1 error
akka.actor.ActorInitializationException: akka://application/user/user-actor: exception during creation
at akka.actor.ActorInitializationException$.apply(Actor.scala:193)
at akka.actor.ActorCell.create(ActorCell.scala:608)
at akka.actor.ActorCell.invokeAll$1(ActorCell.scala:462)
at akka.actor.ActorCell.systemInvoke(ActorCell.scala:484)
at akka.dispatch.Mailbox.processAllSystemMessages(Mailbox.scala:282)
at akka.dispatch.Mailbox.run(Mailbox.scala:223)
at akka.dispatch.Mailbox.exec(Mailbox.scala:234)
at akka.dispatch.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at akka.dispatch.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
Caused by: com.google.inject.ConfigurationException: Guice configuration errors:
To wrap up, you may want to check the sbt console again and also your injection to make sure the actor was initiated in the first place.

Akka Http Route Test: Request was neither completed nor rejected within 1 second

I am trying to write a test case for my application with akka-http. One of the testcases is given below:
import akka.http.scaladsl.model.headers.RawHeader
import akka.http.scaladsl.testkit.{ ScalatestRouteTest}
import com.reactore.common.core.{CommonCoreSystem, CommonActors, BootedCommonCoreSystem}
import scala.concurrent.duration._
import com.reactore.common.feature.referencedata.{DepartmentRepository, DepartmentService, DepartmentController, DepartmentRest}
import org.scalatest.concurrent.AsyncAssertions
import org.scalatest.time.Span
import org.scalatest.{WordSpec, Matchers}
import akka.http.scaladsl.model.StatusCodes._
/**
* Created by krishna on 18/6/15.
*/
class DepartmentITTest extends WordSpec with Matchers with ScalatestRouteTest with CommonCoreSystem with CommonActors {
// override val departmentRouter = system.actorOf(Props(classOf[DepartmentService], DepartmentRepository), Constants.DEPARTMENT_ROUTER_NAME)
val deptRoute = (new DepartmentRest(departmentRouter)(DepartmentController).deptRoutes)
implicit val timeout = AsyncAssertions.timeout(Span.convertDurationToSpan(5.second))
val departmentJson = """{"id":13,"name":"ENGP22","shortCode":"ENGP2","parentDepartmentId":null,"mineId":null,"photoName":null,"isRemoved":false,"isPendingForApproval":false,"createdBy":0,"createDate":"2015-03-09 00:00:00","modifiedBy":0,"modifiedDate":"2015-03-09 00:00:00"}"""
val header = RawHeader("apiKey", "xxxxx")
"Service" should {
"return department by id" in {
Get("/departments/13").withHeaders(header) ~> deptRoute ~> check {
// Thread.sleep(500)
status shouldBe OK
responseAs[String] shouldBe departmentJson
}
}
}
}
When I run this, it works correctly sometimes, and sometimes I get the error as Request was neither completed nor rejected within 1 second. I added a Thread.sleep for making it work now. I know that is not the correct solution. Could anyone tell me how to make the test wait for more than 1 second?
The following is working for me:
import akka.actor.ActorSystem
import scala.concurrent.duration._
import spray.testkit.ScalatestRouteTest
class RouteSpec extends ScalatestRouteTest {
implicit def default(implicit system: ActorSystem) = RouteTestTimeout(2.second)
...
You can use the "eventually" matcher in ScalaTest to wait for a condition to become true:
eventually { status shouldBe OK }
http://www.artima.com/docs-scalatest-2.0.M5/org/scalatest/concurrent/Eventually.html
That should suffice if the Thread.sleep you commented out above fixes things for you.
However, it looks to me like the actual error is that the timeout that the RouteTest trait is using is too short. The error message "Request was neither completed nor rejected within 1 second." comes from RouteTestResultComponent via akka.http.scaladsl.testkit.RouteTest.
I think that the Thread.sleep is a distraction. The default timeout for routing tests is 1 second; see akka.http.scaladsl.testkit.RouteTestTimeout.default. You provide a 5 second implicit timeout in your code, but I think it's of a different type. Try making a RouteTestTimeout implicitly available with a longer timeout.
you can simply update the config to dilate the timeout
akka {
test {
# factor by which to scale timeouts during tests, e.g. to account for shared
# build system load
timefactor = 3.0
}
}

Correct way of creating an ActorSystem inside an Actor

I'm using a third-party library (rediscala) to access a Redis DB inside my own Actor. Following is an example of how I'm currently doing it. Is this correct ? Are there any potential problems with the following code because I'm creating an akkaSystem inside my actor. If SimpleRedisClientActor crashes then I need to restart SimpleRedisClientActor which will create another Actor system. Should I override preStart and postStop ?
import akka.actor.{Props, Actor, ActorLogging}
import redis.RedisClient
import scala.util.{Failure, Success}
import scala.concurrent.ExecutionContext.Implicits.global
class SimpleRedisClientActor extends Actor with ActorLogging {
implicit val akkaSystem = akka.actor.ActorSystem()
val redis = RedisClient()
def receive: Receive = {
case PingRedis => {
val futurePong = redis.ping()
futurePong.onComplete{
case Success(s) => log.info("Redis replied back with a pong")
case Failure(f) => log.info("Something was wrong ...")
}
}
}
}
object RedisActorDemo extends App {
implicit val akkaSystem = akka.actor.ActorSystem()
val simpleActor = akkaSystem.actorOf(Props(classOf[SimpleRedisClientActor]))
simpleActor ! PingRedis
}
object PingRedis
Not a good idea. ActorSystem provides runtime support for actors, so you need an ActorSystem to create an actor, not the other way around. Also, it takes about 500ms to start an ActorSystem, so you would not create lots of them - they are extremely heavyweight. In contrast, Actors are very lightweight. There should only be one ActorSystem per network node.