Re: [akka-user] Regarding scheduler's accuracy of sending messages

120 views
Skip to first unread message

Björn Antonsson

unread,
Oct 23, 2012, 2:30:36 AM10/23/12
to Akka User List
Hi Sachin,

On Monday, 22 October 2012 at 07:11, Sachin wrote:

Hi All,

I have written a small application to see if the scheduled messages are arriving in time (in other words how is the akka's time accuracy in scheduling messages?). When I run the following application, I was expecting 25 messages to arrive in 2500 milliseconds (a message is sent every 100 milliseconds). But I do not see more than 20 messages processed and after 2500 milliseconds, my application exits. I am puzzled about missed messages. Either scheduler is not timely sending the messages or those actors are not processed in time. So my questions are:

1. Am I making any wrong assumption?

I don't think that you're making the wrong assumptions. There are several things at play here.

1) The Scheduler should send a message to the actor every 100 milliseconds. I see that you are using Akka 2.0.3, and an interval equal to the default tick-duration. There is an issue with the scheduler not compensating for drift in 2.0.x that can lead to you not getting the "right" amount of messages. This has been fixed in 2.1.x

2) Your test is not checking how many messages that the actor has been sent, but only how many times it has gotten a chance to run. This is not the same thing.

3) Your code is busy-spinning in the main thread to check how much time has passed. This can lead to resource starvation, so the actor don't get to process it's messages.
 
2. How can I fix this (may be by configuring scheduler's tick-duration or tick-per-second)?

Making the tick-duration smaller or trying with Akka 2.1-RC1 would improve the reliability of the scheduling, but your test is still broken. The ticks-per-wheel setting (which I assume you mean by tick-per-second) has nothing to do with the granularity of the timer ticks.

B/

Any help regarding this is highly appreciated. Please see the code and result log below.

Thanks
Sachin

Code:
------------------------------------------------------------------------------------------------------------
import akka.actor._
import akka.util.duration._

case class DetectionTick

class DetectionActor extends Actor {
  var count = 0
 
  def receive = {
    case DetectionTick =>
        println("Tick received " + count)
        count += 1
  }
}

object detectiontick extends App {

  val system = ActorSystem("detectiontick")
  val detectionActor = system.actorOf(Props[DetectionActor], "detectionactor")   
  val cancellable = system.scheduler.schedule(0 milliseconds, 100 milliseconds, detectionActor, DetectionTick)
  var StartTime = System.currentTimeMillis()
  var EndTime = System.currentTimeMillis()
  var timetaken = EndTime - StartTime
  println("Start Time = " + StartTime)

  while (true) {
    EndTime = System.currentTimeMillis()
    timetaken = EndTime - StartTime
   
    if(timetaken > 2500) {
        println("Time taken = " + timetaken)
        println("End Time = " + EndTime)
        cancellable.cancel()
        exit
    }
  }
}

Output:
------------------------------------------------------------------------------------------------------------
$ java -cp /users/tools/akka-2.0.3/lib/scala-library.jar:/users/tools/akka-2.0.3/lib/akka/akka-actor-2.0.3.jar:/users/tools/akka-2.0.3/lib/akka/config-0.3.1.jar:. detectiontick
Start Time = 1350881825940
Tick received 0
Tick received 1
Tick received 2
Tick received 3
Tick received 4
Tick received 5
Tick received 6
Tick received 7
Tick received 8
Tick received 9
Tick received 10
Tick received 11
Tick received 12
Tick received 13
Tick received 14
Tick received 15
Tick received 16
Tick received 17
Tick received 18
Tick received 19
Time taken = 2501
End Time = 1350881828441

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>>>>>>> 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 post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user?hl=en.
 
 

-- 
Björn Antonsson
Typesafe - The software stack for applications that scale

Björn Antonsson

unread,
Oct 23, 2012, 4:30:16 PM10/23/12
to akka...@googlegroups.com
Hi Sachin,

On Tuesday, 23 October 2012 at 18:06, Sachin wrote:

Hi Björn Antonsson,

Thank you for the information. I will certainly try 2.1-RC1 version. I agree that having a busy wait will lead to actor not able to process the message. But I was assuming that actor may run on another thread, which was a wrong assumption. 

I'm not saying that the Actor won't run on a different thread, which it does, but that by busy-spinning you might starve resources on your system. If it is a big machine with lots of cores and nothing else going on, then the test will probably behave like you expect.

B/

Thanks for correcting me, I actually meant to say tick-per-second. 

Regards
Sachin
Reply all
Reply to author
Forward
0 new messages