1

I have a spray route where I declare one of my actors.

val myActor = actorRefFactory.actorSelection("/user/my-actor")

And my route looks like:

get {
  path(Segment / Segment) { (poolId, trackId) =>
    respondWithMediaType(MediaTypes.`application/json`) {
      val request = Request(poolId, trackId)

      val f = (myActor ? request)
        .recoverWith {
          case a: AskTimeoutException =>
            Future.failed[StandardRoute](throw new Exception(s"We got a timeout", a))

          case e: Exception => Future.failed[StandardRoute](throw new Exception(s"We got an error", e))
        }

      onComplete(f) {
        case Success(resp) => complete(OK, resp)

        case Failure(e) =>
          log.error(s"Fatal request error: $trackId / $poolId", e)
          complete(InternalServerError, ErrorCodes.ErrorNotHandled)
      }
    }
  }
}

Sometimes, I can see when I get lots of requests at the same time, some of them may fail with the following message:

Caused by: akka.pattern.AskTimeoutException: Ask timed out on [ActorSelection[Anchor(akka://default/), Path(/user/my-actor)]] after [8000 ms]. Sender[null] sent message of type "my.company.messages.Request".

The problem is if I take the same request and try to send it again, it works, only sometimes this happens and I have no idea how to solve that.

The actor indeed is doing a lot of things with lots of futures inside until it returns a value to the spray route.

Inside the actor, I create a val called replyTo in order to keep the value of the sender.

Any ideas on why sometimes I get this error?

EDIT

Just an example on how I'm managing the myActor:

class MyActor extends Actor with ActorLogging {

  private implicit val timeout = Timeout(8.seconds)

  def receive = {

    case req: Request =>

      val replyTo = sender()

      doOneThing.map { one =>

        doSecondThing(one).map { sec =>
          replyTo ! sec
        }
      }
  }
}

Where doOneThing and doSecondThing are Futures... and I have a lot of them spread around this actor for different situations.

8
  • You should not be using throw with Future.failed. See: scala-lang.org/api/current/… May 25, 2016 at 14:17
  • @OlliHelenius good, but that does not help. May 25, 2016 at 14:19
  • Does myActor sometimes take more than 8 seconds to respond? May 25, 2016 at 14:31
  • @OlliHelenius hmm I don't believe in a normal situation, but now maybe this could happen on heavy load? I will try to increase and see what happens. But what I see very strange is the sender to be null, that makes no sense to me. May 25, 2016 at 14:35
  • 1
    Ask pattern internally uses a temporary actor. As it is a temporary actor, the sender reference will change every time. val replyTo = sender() will not work in this case if you use the same sender reference again and again. Reference to code for myActor will help more to understand the problem.
    – Yoda
    May 26, 2016 at 9:59

3 Answers 3

3

The Sender[null] that you're seeing is normal behaviour. The ask method ? takes an implicit parameter sender with default value ActorRef.noSender. 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.

It's likely that the cause of your error is that the Actor that receives your message just isn't responding in time.

1
  • probably... at the beginning I thought the Sender[null] was something completely different, but now looking at some comments, maybe my actor takes more than it should to complete some action in heavy load and I got this strange result. I will indeed start a refactoring. May 26, 2016 at 10:38
0

I think you are not using routing over here that's why you are getting the timeout exception. for example, suppose your one request taking 1 second to get executed. and suppose you are getting 4 requests at a time and when the requests come to an actor it appends in a queue and for every request you are waiting for 8 seconds. so in the queue suppose 4th request will execute in 4 seconds so you will get the response within 8 seconds. But when 100 requests come at a time and your one actor will be available to process them and for the 100th request it will take 100 seconds to get executed but you are waiting only 8.second for the 100th request that's why you are getting the timeout exception.

so the solution is, you can use routing here for example -

system.actorOf(RoundRobinPool(concurrency).props(Props(new MyActor())))

and you can set concurrency on your system available process. suppose so set concurrency value 100 so now for the 100th request, it will take only 2 seconds to get executed. so I think routing can be a solution.

and if you don't know how many requests will be there it may be 1k or maybe more then it then you can go with dynamic creation of the actor where you can create the actor dynamically as per the request. so whenever a request will come, your one separate actor will be there to give the service for it.

0

I would get this kind of error when I didn't send back the response to sender correctly.

Incorrect (simply returning the value):

def receive = {
  case DataFetch =>
    data
}

Correct (sending the value to sender):

  def receive = {
    case DataFetch =>
      sender ! data
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.