2011-04-27 00:40:20 +02:00
|
|
|
/**
|
2012-01-19 18:21:06 +01:00
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
2011-04-27 00:40:20 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.actor
|
|
|
|
|
|
2012-09-21 14:50:06 +02:00
|
|
|
import scala.concurrent.duration.Duration
|
2011-12-09 20:19:59 +01:00
|
|
|
import com.typesafe.config._
|
2011-12-12 23:31:15 +01:00
|
|
|
import akka.routing._
|
2012-04-27 12:17:00 +02:00
|
|
|
import java.util.concurrent.{ TimeUnit }
|
|
|
|
|
import akka.util.WildcardTree
|
2012-04-26 17:24:43 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicReference
|
|
|
|
|
import annotation.tailrec
|
2011-12-12 23:31:15 +01:00
|
|
|
|
2012-01-31 21:19:28 +01:00
|
|
|
/**
|
|
|
|
|
* This class represents deployment configuration for a given actor path. It is
|
|
|
|
|
* marked final in order to guarantee stable merge semantics (i.e. what
|
|
|
|
|
* overrides what in case multiple configuration sources are available) and is
|
|
|
|
|
* fully extensible via its Scope argument, and by the fact that an arbitrary
|
|
|
|
|
* Config section can be passed along with it (which will be merged when merging
|
|
|
|
|
* two Deploys).
|
|
|
|
|
*
|
|
|
|
|
* The path field is used only when inserting the Deploy into a deployer and
|
|
|
|
|
* not needed when just doing deploy-as-you-go:
|
|
|
|
|
*
|
|
|
|
|
* {{{
|
|
|
|
|
* context.actorOf(someProps, "someName", Deploy(scope = RemoteScope("someOtherNodeName")))
|
|
|
|
|
* }}}
|
|
|
|
|
*/
|
2012-07-30 13:26:12 +02:00
|
|
|
@SerialVersionUID(1L)
|
2012-01-31 21:19:28 +01:00
|
|
|
final case class Deploy(
|
|
|
|
|
path: String = "",
|
|
|
|
|
config: Config = ConfigFactory.empty,
|
|
|
|
|
routerConfig: RouterConfig = NoRouter,
|
2012-02-03 09:43:23 +01:00
|
|
|
scope: Scope = NoScopeGiven) {
|
2012-01-31 21:19:28 +01:00
|
|
|
|
2012-05-16 15:22:21 +02:00
|
|
|
/**
|
|
|
|
|
* Java API to create a Deploy with the given RouterConfig
|
|
|
|
|
*/
|
2012-01-31 21:19:28 +01:00
|
|
|
def this(routing: RouterConfig) = this("", ConfigFactory.empty, routing)
|
2012-05-16 15:22:21 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Java API to create a Deploy with the given RouterConfig with Scope
|
|
|
|
|
*/
|
2012-01-31 21:19:28 +01:00
|
|
|
def this(routing: RouterConfig, scope: Scope) = this("", ConfigFactory.empty, routing, scope)
|
2012-05-16 15:22:21 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Java API to create a Deploy with the given Scope
|
|
|
|
|
*/
|
2012-02-02 09:40:17 +01:00
|
|
|
def this(scope: Scope) = this("", ConfigFactory.empty, NoRouter, scope)
|
2012-01-31 21:19:28 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Do a merge between this and the other Deploy, where values from “this” take
|
|
|
|
|
* precedence. The “path” of the other Deploy is not taken into account. All
|
|
|
|
|
* other members are merged using ``<X>.withFallback(other.<X>)``.
|
|
|
|
|
*/
|
2012-02-03 09:43:23 +01:00
|
|
|
def withFallback(other: Deploy): Deploy =
|
2012-01-31 21:19:28 +01:00
|
|
|
Deploy(path, config.withFallback(other.config), routerConfig.withFallback(other.routerConfig), scope.withFallback(other.scope))
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-03 09:43:23 +01:00
|
|
|
/**
|
|
|
|
|
* The scope of a [[akka.actor.Deploy]] serves two purposes: as a marker for
|
|
|
|
|
* pattern matching the “scope” (i.e. local/remote/cluster) as well as for
|
|
|
|
|
* extending the information carried by the final Deploy class. Scopes can be
|
|
|
|
|
* used in conjunction with a custom [[akka.actor.ActorRefProvider]], making
|
|
|
|
|
* Akka actors fully extensible.
|
|
|
|
|
*/
|
2012-01-31 21:19:28 +01:00
|
|
|
trait Scope {
|
2012-02-03 09:43:23 +01:00
|
|
|
/**
|
|
|
|
|
* When merging [[akka.actor.Deploy]] instances using ``withFallback()`` on
|
|
|
|
|
* the left one, this is propagated to “merging” scopes in the same way.
|
|
|
|
|
* The setup is biased towards preferring the callee over the argument, i.e.
|
|
|
|
|
* ``a.withFallback(b)`` is called expecting that ``a`` should in general take
|
|
|
|
|
* precedence.
|
|
|
|
|
*/
|
2012-01-31 21:19:28 +01:00
|
|
|
def withFallback(other: Scope): Scope
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-30 13:26:12 +02:00
|
|
|
@SerialVersionUID(1L)
|
2012-04-10 12:07:14 +02:00
|
|
|
abstract class LocalScope extends Scope
|
2011-12-12 23:31:15 +01:00
|
|
|
|
2012-10-16 12:06:03 +02:00
|
|
|
/**
|
|
|
|
|
* The Local Scope is the default one, which is assumed on all deployments
|
|
|
|
|
* which do not set a different scope. It is also the only scope handled by
|
|
|
|
|
* the LocalActorRefProvider.
|
|
|
|
|
*/
|
2012-05-16 15:22:21 +02:00
|
|
|
case object LocalScope extends LocalScope {
|
2012-04-10 16:49:29 +02:00
|
|
|
/**
|
|
|
|
|
* Java API: get the singleton instance
|
|
|
|
|
*/
|
|
|
|
|
def getInstance = this
|
2012-04-10 12:07:14 +02:00
|
|
|
|
2012-01-31 21:19:28 +01:00
|
|
|
def withFallback(other: Scope): Scope = this
|
|
|
|
|
}
|
2011-12-12 23:31:15 +01:00
|
|
|
|
2012-01-31 21:19:28 +01:00
|
|
|
/**
|
|
|
|
|
* This is the default value and as such allows overrides.
|
|
|
|
|
*/
|
2012-07-30 13:26:12 +02:00
|
|
|
@SerialVersionUID(1L)
|
2012-04-10 12:07:14 +02:00
|
|
|
abstract class NoScopeGiven extends Scope
|
|
|
|
|
case object NoScopeGiven extends NoScopeGiven {
|
2012-01-31 21:19:28 +01:00
|
|
|
def withFallback(other: Scope): Scope = other
|
2012-04-10 12:07:14 +02:00
|
|
|
|
2012-04-10 16:49:29 +02:00
|
|
|
/**
|
|
|
|
|
* Java API: get the singleton instance
|
|
|
|
|
*/
|
|
|
|
|
def getInstance = this
|
2012-01-31 21:19:28 +01:00
|
|
|
}
|
2011-04-27 00:40:20 +02:00
|
|
|
|
|
|
|
|
/**
|
2011-11-08 16:49:50 +01:00
|
|
|
* Deployer maps actor paths to actor deployments.
|
2011-04-27 00:40:20 +02:00
|
|
|
*/
|
2012-04-26 17:24:43 +02:00
|
|
|
private[akka] class Deployer(val settings: ActorSystem.Settings, val dynamicAccess: DynamicAccess) {
|
2011-05-03 21:04:45 +02:00
|
|
|
|
2011-12-09 20:19:59 +01:00
|
|
|
import scala.collection.JavaConverters._
|
2011-05-03 21:04:45 +02:00
|
|
|
|
2012-04-27 12:17:00 +02:00
|
|
|
private val deployments = new AtomicReference(WildcardTree[Deploy]())
|
2011-12-09 20:19:59 +01:00
|
|
|
private val config = settings.config.getConfig("akka.actor.deployment")
|
|
|
|
|
protected val default = config.getConfig("default")
|
2012-04-26 17:24:43 +02:00
|
|
|
|
2011-12-09 20:19:59 +01:00
|
|
|
config.root.asScala flatMap {
|
|
|
|
|
case ("default", _) ⇒ None
|
|
|
|
|
case (key, value: ConfigObject) ⇒ parseConfig(key, value.toConfig)
|
|
|
|
|
case _ ⇒ None
|
|
|
|
|
} foreach deploy
|
2011-05-03 21:04:45 +02:00
|
|
|
|
2012-04-27 12:48:22 +02:00
|
|
|
def lookup(path: ActorPath): Option[Deploy] = lookup(path.elements.drop(1).iterator)
|
2012-04-26 17:24:43 +02:00
|
|
|
|
2012-04-27 12:48:22 +02:00
|
|
|
def lookup(path: Iterable[String]): Option[Deploy] = lookup(path.iterator)
|
2012-04-26 17:24:43 +02:00
|
|
|
|
2012-04-27 12:48:22 +02:00
|
|
|
def lookup(path: Iterator[String]): Option[Deploy] = deployments.get().find(path).data
|
2012-04-26 23:59:18 +02:00
|
|
|
|
2012-04-26 17:24:43 +02:00
|
|
|
def deploy(d: Deploy): Unit = {
|
2012-04-27 12:17:00 +02:00
|
|
|
@tailrec def add(path: Array[String], d: Deploy, w: WildcardTree[Deploy] = deployments.get): Unit =
|
2012-04-26 17:24:43 +02:00
|
|
|
if (!deployments.compareAndSet(w, w.insert(path.iterator, d))) add(path, d)
|
2011-11-15 11:34:39 +01:00
|
|
|
|
2012-04-27 12:48:22 +02:00
|
|
|
add(d.path.split("/").drop(1), d)
|
2012-04-26 17:24:43 +02:00
|
|
|
}
|
2011-05-03 21:04:45 +02:00
|
|
|
|
2012-05-24 12:40:52 +02:00
|
|
|
def parseConfig(key: String, config: Config): Option[Deploy] = {
|
2011-04-27 00:40:20 +02:00
|
|
|
|
2011-12-09 20:19:59 +01:00
|
|
|
val deployment = config.withFallback(default)
|
2011-07-26 17:12:00 +02:00
|
|
|
|
2012-06-25 16:29:08 +02:00
|
|
|
val routees = Vector() ++ deployment.getStringList("routees.paths").asScala
|
2011-12-12 23:31:15 +01:00
|
|
|
|
|
|
|
|
val nrOfInstances = deployment.getInt("nr-of-instances")
|
|
|
|
|
|
2012-04-24 12:04:31 +02:00
|
|
|
val resizer: Option[Resizer] = if (config.hasPath("resizer")) Some(DefaultResizer(deployment.getConfig("resizer"))) else None
|
2012-01-09 20:25:24 +01:00
|
|
|
|
2011-12-12 23:31:15 +01:00
|
|
|
val router: RouterConfig = deployment.getString("router") match {
|
2012-09-13 18:06:35 +02:00
|
|
|
case "from-code" ⇒ NoRouter
|
|
|
|
|
case "round-robin" ⇒ RoundRobinRouter(nrOfInstances, routees, resizer)
|
|
|
|
|
case "random" ⇒ RandomRouter(nrOfInstances, routees, resizer)
|
|
|
|
|
case "smallest-mailbox" ⇒ SmallestMailboxRouter(nrOfInstances, routees, resizer)
|
|
|
|
|
case "broadcast" ⇒ BroadcastRouter(nrOfInstances, routees, resizer)
|
|
|
|
|
case "scatter-gather" ⇒
|
|
|
|
|
val within = Duration(deployment.getMilliseconds("within"), TimeUnit.MILLISECONDS)
|
|
|
|
|
ScatterGatherFirstCompletedRouter(nrOfInstances, routees, within, resizer)
|
|
|
|
|
case "consistent-hashing" ⇒
|
|
|
|
|
val vnodes = deployment.getInt("virtual-nodes-factor")
|
|
|
|
|
ConsistentHashingRouter(nrOfInstances, routees, resizer, virtualNodesFactor = vnodes)
|
2012-01-12 16:37:08 +01:00
|
|
|
case fqn ⇒
|
2012-01-30 11:48:02 +01:00
|
|
|
val args = Seq(classOf[Config] -> deployment)
|
2012-09-06 03:17:51 +02:00
|
|
|
dynamicAccess.createInstanceFor[RouterConfig](fqn, args).recover({
|
|
|
|
|
case exception ⇒ throw new IllegalArgumentException(
|
|
|
|
|
("Cannot instantiate router [%s], defined in [%s], " +
|
|
|
|
|
"make sure it extends [akka.routing.RouterConfig] and has constructor with " +
|
|
|
|
|
"[com.typesafe.config.Config] parameter")
|
|
|
|
|
.format(fqn, key), exception)
|
|
|
|
|
}).get
|
2011-11-19 19:55:44 +01:00
|
|
|
}
|
2011-09-28 14:50:09 +02:00
|
|
|
|
2012-02-03 09:43:23 +01:00
|
|
|
Some(Deploy(key, deployment, router, NoScopeGiven))
|
2011-04-27 00:40:20 +02:00
|
|
|
}
|
|
|
|
|
}
|