为什么我的演员调度在Akka缩小了?

时间:2011-04-28 12:38:04

标签: scala akka

我有一个100个运行Actors的actor池,它共享一个工作窃取调度程序,其CorePoolSize设置为100.但是现在当向其中一个Actors发送19条消息时,19条消息没有并行化为19个Actors,只有5条消息并行运行。当这5条消息完成后,接下来的5条消息将由这些相同的5个Actors再次处理,依此类推。为什么我的19条消息并行运行,我在这里缺少什么?

我的代码看起来基本上是这样的:

object TestActor {
  val dispatcher = Dispatchers.newExecutorBasedEventDrivenWorkStealingDispatcher("pool")
                   .setCorePoolSize(100)
                   .setMaxPoolSize(100)
                   .build
}

class TestActor(val name: Integer) extends Actor {
    self.lifeCycle = Permanent
    self.dispatcher = TestActor.dispatcher
    def receive = {
       case num: Integer => {  println("Actor: " + name + " Received: " + num)
                               Thread.sleep(10000)
                            }
    }
}

trait CyclicLoadBalancing extends LoadBalancer { this: Actor =>
    val testActors: List[ActorRef]
    val seq = new CyclicIterator[ActorRef](testActors)
}

trait TestActorManager extends Actor {
     self.lifeCycle = Permanent
     self.faultHandler = OneForOneStrategy(List(classOf[Exception]), 5, 5000)
     val testActors: List[ActorRef]
     override def preStart = testActors foreach { self.startLink(_) }
     override def postStop = self.shutdownLinkedActors()
}

val supervisor = actorOf(new TestActorManager with CyclicLoadBalancing {
val testActors = (1 until 100 toList) map (i => actorOf(new TestActor(i)))   
}).start

println("Number of Actors: " +  Actor.registry.actorsFor(classOf[TestActor]).length)

val testActor = Actor.registry.actorsFor(classOf[TestActor]).head

(1 until 20 toList) foreach { testActor ! _ }

输出:

Actor: 4 Received: 16
Actor: 3 Received: 17
Actor: 1 Received: 19
Actor: 59 Received: 1
Actor: 2 Received: 18

// 10 secs. are passing..

Actor: 4 Received: 15
Actor: 3 Received: 14
Actor: 1 Received: 13
Actor: 59 Received: 2
Actor: 2 Received: 12

// 10 secs. are passing..

Actor: 4 Received: 11
Actor: 3 Received: 10
Actor: 59 Received: 3
Actor: 2 Received: 8
Actor: 1 Received: 9

// 10 secs. are passing..

Actor: 4 Received: 7
Actor: 3 Received: 6
Actor: 59 Received: 4
Actor: 2 Received: 5

编辑:我正在使用Akka 1.0

2 个答案:

答案 0 :(得分:12)

感谢您的询问,我对瓶颈进行了本地化并将其修改为Akka master:

https://github.com/akka/akka/commit/e4e99ef56399e892206ce4a46b9a9107da6c7770

它将在Akka 1.1-RC1中发布

干杯, √

答案 1 :(得分:2)

我认为调度程序允许您自定义吞吐量属性。这定义了调度程序在一次扫描中应处理的特定Actor的消息数。您可以在akka.conf文件中添加以下配置

actor {
  throughput = 20
}

默认情况下为5