matanox
matanox

Reputation: 13686

Akka: how to make number of actors in balancing pool relative to thread pool size

For an Actor class encompassing a key computation in my application, I am spawning a bunch of actors behind a router:

val concurrency = 4 // to be replaced by something dynamic
val ahoCorasick = AppActorSystem.system.actorOf(MyActorClass.props(ldb)
                                         .withRouter(BalancingPool(nrOfInstances = concurrency)), 
                                          name = "foo") 

How can I get the number of Actor instances relative to the number of cores, or to the size of the thread pool that applies to the actor system? e.g. one Actor per core, or a number of actors equal to the supplied thread pool size? (might also define a thread pool specific to these actors).

Upvotes: 2

Views: 2165

Answers (1)

yǝsʞǝla
yǝsʞǝla

Reputation: 16412

It's best to get number of threads from your Akka config (reference.conf or application.conf). Here is the reference doc that explains how it's configured. You can have a custom dispatcher or a default one, so the best thing is to not blindly use the code below but to understand which dispatcher you are actually using.

You could do something like this:

import akka.actor.ActorSystem
import com.typesafe.config.{Config, ConfigFactory}

val conf = ConfigFactory.load().getConfig("akka.actor.default-dispatcher.fork-join-executor")

def getThreadConf(conf: Config): (Int, Int, Int) = {
  val parallelismFactor = conf.getInt("parallelism-factor")
  val parallelismMin = conf.getInt("parallelism-min")
  val parallelismMax = conf.getInt("parallelism-max")
  (parallelismFactor, parallelismMin, parallelismMax)
}

println(getThreadConf(conf))
val system = ActorSystem()
println(getThreadConf(system.settings.config.getConfig("akka.actor.default-dispatcher.fork-join-executor")))

By default you get:

factor: 3, min: 8, max: 64

Check the name of dispatcher you are using or set one explicitly with val myActor = context.actorOf(Props[MyActor].withDispatcher("my-dispatcher"), "myactor1")

Upvotes: 2

Related Questions