2011-04-27 00:40:20 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.actor
|
|
|
|
|
|
|
|
|
|
import collection.immutable.Seq
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap
|
|
|
|
|
|
|
|
|
|
import akka.event.EventHandler
|
|
|
|
|
import akka.actor.DeploymentConfig._
|
|
|
|
|
import akka.config.{ConfigurationException, Config}
|
2011-04-29 15:47:56 +02:00
|
|
|
import akka.util.ReflectiveAccess
|
|
|
|
|
import akka.AkkaException
|
2011-04-27 00:40:20 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Programatic deployment configuration classes. Most values have defaults and can be left out.
|
|
|
|
|
*
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
|
|
|
|
object DeploymentConfig {
|
|
|
|
|
|
|
|
|
|
// --------------------------------
|
|
|
|
|
// --- Deploy
|
|
|
|
|
// --------------------------------
|
2011-05-16 09:47:23 +02:00
|
|
|
case class Deploy(address: String, routing: Routing = Direct, format: String = "N/A", scope: Scope = Local)
|
2011-04-27 00:40:20 +02:00
|
|
|
|
|
|
|
|
// --------------------------------
|
|
|
|
|
// --- Routing
|
|
|
|
|
// --------------------------------
|
|
|
|
|
sealed trait Routing
|
|
|
|
|
case class CustomRouter(router: AnyRef) extends Routing
|
|
|
|
|
|
|
|
|
|
// For Java API
|
|
|
|
|
case class Direct() extends Routing
|
|
|
|
|
case class RoundRobin() extends Routing
|
|
|
|
|
case class Random() extends Routing
|
|
|
|
|
case class LeastCPU() extends Routing
|
|
|
|
|
case class LeastRAM() extends Routing
|
|
|
|
|
case class LeastMessages() extends Routing
|
|
|
|
|
|
|
|
|
|
// For Scala API
|
|
|
|
|
case object Direct extends Routing
|
|
|
|
|
case object RoundRobin extends Routing
|
|
|
|
|
case object Random extends Routing
|
|
|
|
|
case object LeastCPU extends Routing
|
|
|
|
|
case object LeastRAM extends Routing
|
|
|
|
|
case object LeastMessages extends Routing
|
|
|
|
|
|
|
|
|
|
// --------------------------------
|
|
|
|
|
// --- Scope
|
|
|
|
|
// --------------------------------
|
|
|
|
|
sealed trait Scope
|
|
|
|
|
case class Clustered(
|
2011-05-04 21:25:47 +02:00
|
|
|
home: Home = Host("localhost"),
|
2011-04-27 00:40:20 +02:00
|
|
|
replication: Replication = NoReplicas,
|
|
|
|
|
state: State = Stateful) extends Scope
|
|
|
|
|
|
|
|
|
|
// For Java API
|
|
|
|
|
case class Local() extends Scope
|
|
|
|
|
|
|
|
|
|
// For Scala API
|
|
|
|
|
case object Local extends Scope
|
|
|
|
|
|
|
|
|
|
// --------------------------------
|
|
|
|
|
// --- Home
|
|
|
|
|
// --------------------------------
|
2011-05-04 21:25:47 +02:00
|
|
|
sealed trait Home
|
|
|
|
|
case class Host(hostName: String) extends Home
|
|
|
|
|
case class Node(nodeName: String) extends Home
|
|
|
|
|
case class IP(ipAddress: String) extends Home
|
2011-04-27 00:40:20 +02:00
|
|
|
|
|
|
|
|
// --------------------------------
|
|
|
|
|
// --- Replication
|
|
|
|
|
// --------------------------------
|
|
|
|
|
sealed trait Replication
|
2011-05-03 21:04:45 +02:00
|
|
|
case class Replicate(factor: Int) extends Replication {
|
2011-04-27 00:40:20 +02:00
|
|
|
if (factor < 1) throw new IllegalArgumentException("Replication factor can not be negative or zero")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For Java API
|
|
|
|
|
case class AutoReplicate() extends Replication
|
2011-05-03 21:04:45 +02:00
|
|
|
case class NoReplicas() extends Replication
|
2011-04-27 00:40:20 +02:00
|
|
|
|
|
|
|
|
// For Scala API
|
|
|
|
|
case object AutoReplicate extends Replication
|
2011-05-03 21:04:45 +02:00
|
|
|
case object NoReplicas extends Replication
|
2011-04-27 00:40:20 +02:00
|
|
|
|
|
|
|
|
// --------------------------------
|
|
|
|
|
// --- State
|
|
|
|
|
// --------------------------------
|
|
|
|
|
sealed trait State
|
|
|
|
|
|
|
|
|
|
// For Java API
|
|
|
|
|
case class Stateless() extends State
|
|
|
|
|
case class Stateful() extends State
|
|
|
|
|
|
|
|
|
|
// For Scala API
|
|
|
|
|
case object Stateless extends State
|
|
|
|
|
case object Stateful extends State
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Deployer maps actor deployments to actor addresses.
|
|
|
|
|
*
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
|
|
|
|
object Deployer {
|
2011-05-03 21:04:45 +02:00
|
|
|
|
2011-05-04 21:25:47 +02:00
|
|
|
val defaultAddress = Host("localhost") // FIXME allow configuring node-local default hostname and port
|
2011-05-03 21:04:45 +02:00
|
|
|
|
|
|
|
|
lazy val instance: ReflectiveAccess.ClusterModule.ClusterDeployer = {
|
|
|
|
|
val deployer =
|
|
|
|
|
if (ReflectiveAccess.ClusterModule.isEnabled) ReflectiveAccess.ClusterModule.clusterDeployer
|
|
|
|
|
else LocalDeployer
|
|
|
|
|
deployer.init(deploymentsInConfig)
|
|
|
|
|
deployer
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def shutdown() {
|
|
|
|
|
instance.shutdown()
|
|
|
|
|
}
|
2011-04-27 00:40:20 +02:00
|
|
|
|
|
|
|
|
def deploy(deployment: Deploy) {
|
|
|
|
|
if (deployment eq null) throw new IllegalArgumentException("Deploy can not be null")
|
|
|
|
|
val address = deployment.address
|
|
|
|
|
Address.validate(address)
|
2011-05-03 21:04:45 +02:00
|
|
|
instance.deploy(deployment)
|
2011-04-27 00:40:20 +02:00
|
|
|
}
|
|
|
|
|
|
2011-04-30 15:44:46 +02:00
|
|
|
def deploy(deployment: Seq[Deploy]) {
|
|
|
|
|
deployment foreach (deploy(_))
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-27 00:40:20 +02:00
|
|
|
/**
|
|
|
|
|
* Undeploy is idemponent. E.g. safe to invoke multiple times.
|
|
|
|
|
*/
|
|
|
|
|
def undeploy(deployment: Deploy) {
|
2011-05-03 21:04:45 +02:00
|
|
|
instance.undeploy(deployment)
|
2011-04-27 00:40:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def undeployAll() {
|
2011-05-03 21:04:45 +02:00
|
|
|
instance.undeployAll()
|
2011-04-27 00:40:20 +02:00
|
|
|
}
|
|
|
|
|
|
2011-05-03 21:04:45 +02:00
|
|
|
def isLocal(deployment: Deploy): Boolean = deployment match {
|
2011-05-16 09:47:23 +02:00
|
|
|
case Deploy(_, _, _, Local) => true
|
2011-05-03 21:04:45 +02:00
|
|
|
case _ => false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def isClustered(deployment: Deploy): Boolean = isLocal(deployment)
|
|
|
|
|
|
|
|
|
|
def isLocal(address: String): Boolean = isLocal(deploymentFor(address))
|
|
|
|
|
|
|
|
|
|
def isClustered(address: String): Boolean = !isLocal(address)
|
|
|
|
|
|
2011-04-27 15:00:41 +02:00
|
|
|
/**
|
|
|
|
|
* Same as 'lookupDeploymentFor' but throws an exception if no deployment is bound.
|
|
|
|
|
*/
|
2011-05-03 21:04:45 +02:00
|
|
|
private[akka] def deploymentFor(address: String): Deploy = {
|
2011-04-27 15:00:41 +02:00
|
|
|
lookupDeploymentFor(address) match {
|
|
|
|
|
case Some(deployment) => deployment
|
|
|
|
|
case None => thrownNoDeploymentBoundException(address)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-03 21:04:45 +02:00
|
|
|
private[akka] def lookupDeploymentFor(address: String): Option[Deploy] = {
|
|
|
|
|
val deployment_? = instance.lookupDeploymentFor(address)
|
2011-04-29 15:47:56 +02:00
|
|
|
if (deployment_?.isDefined && (deployment_?.get ne null)) deployment_?
|
2011-04-27 00:40:20 +02:00
|
|
|
else {
|
2011-04-29 15:47:56 +02:00
|
|
|
val newDeployment =
|
2011-04-27 00:40:20 +02:00
|
|
|
try {
|
|
|
|
|
lookupInConfig(address)
|
|
|
|
|
} catch {
|
|
|
|
|
case e: ConfigurationException =>
|
|
|
|
|
EventHandler.error(e, this, e.getMessage)
|
|
|
|
|
throw e
|
|
|
|
|
}
|
2011-04-29 15:47:56 +02:00
|
|
|
newDeployment foreach { d =>
|
2011-04-27 15:00:41 +02:00
|
|
|
if (d eq null) {
|
|
|
|
|
val e = new IllegalStateException("Deployment for address [" + address + "] is null")
|
|
|
|
|
EventHandler.error(e, this, e.getMessage)
|
|
|
|
|
throw e
|
|
|
|
|
}
|
2011-04-29 15:47:56 +02:00
|
|
|
deploy(d) // deploy and cache it
|
2011-04-27 15:00:41 +02:00
|
|
|
}
|
2011-04-29 15:47:56 +02:00
|
|
|
newDeployment
|
2011-04-27 00:40:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-03 21:04:45 +02:00
|
|
|
private[akka] def deploymentsInConfig: List[Deploy] = {
|
|
|
|
|
for {
|
|
|
|
|
address <- addressesInConfig
|
|
|
|
|
deployment <- lookupInConfig(address)
|
|
|
|
|
} yield deployment
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private[akka] def addressesInConfig: List[String] = {
|
|
|
|
|
val deploymentPath = "akka.actor.deployment"
|
|
|
|
|
Config.config.getSection(deploymentPath) match {
|
|
|
|
|
case None => Nil
|
|
|
|
|
case Some(addressConfig) =>
|
|
|
|
|
addressConfig.map.keySet
|
|
|
|
|
.map(path => path.substring(0, path.indexOf(".")))
|
|
|
|
|
.toSet.toList // toSet to force uniqueness
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-27 00:40:20 +02:00
|
|
|
/**
|
2011-04-27 15:00:41 +02:00
|
|
|
* Lookup deployment in 'akka.conf' configuration file.
|
2011-04-27 00:40:20 +02:00
|
|
|
*/
|
2011-05-03 21:04:45 +02:00
|
|
|
private[akka] def lookupInConfig(address: String): Option[Deploy] = {
|
2011-04-27 00:40:20 +02:00
|
|
|
|
|
|
|
|
// --------------------------------
|
|
|
|
|
// akka.actor.deployment.<address>
|
|
|
|
|
// --------------------------------
|
|
|
|
|
val addressPath = "akka.actor.deployment." + address
|
|
|
|
|
Config.config.getSection(addressPath) match {
|
2011-05-16 09:47:23 +02:00
|
|
|
case None => Some(Deploy(address, Direct, "N/A", Local))
|
2011-04-27 00:40:20 +02:00
|
|
|
case Some(addressConfig) =>
|
|
|
|
|
|
|
|
|
|
// --------------------------------
|
|
|
|
|
// akka.actor.deployment.<address>.router
|
|
|
|
|
// --------------------------------
|
|
|
|
|
val router = addressConfig.getString("router", "direct") match {
|
|
|
|
|
case "direct" => Direct
|
|
|
|
|
case "round-robin" => RoundRobin
|
|
|
|
|
case "random" => Random
|
|
|
|
|
case "least-cpu" => LeastCPU
|
|
|
|
|
case "least-ram" => LeastRAM
|
|
|
|
|
case "least-messages" => LeastMessages
|
|
|
|
|
case customRouterClassName =>
|
|
|
|
|
val customRouter = try {
|
|
|
|
|
Class.forName(customRouterClassName).newInstance.asInstanceOf[AnyRef]
|
|
|
|
|
} catch {
|
|
|
|
|
case e => throw new ConfigurationException(
|
|
|
|
|
"Config option [" + addressPath + ".router] needs to be one of " +
|
|
|
|
|
"[\"direct\", \"round-robin\", \"random\", \"least-cpu\", \"least-ram\", \"least-messages\" or FQN of router class]")
|
|
|
|
|
}
|
|
|
|
|
CustomRouter(customRouter)
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-16 09:47:23 +02:00
|
|
|
// --------------------------------
|
|
|
|
|
// akka.actor.deployment.<address>.format
|
|
|
|
|
// --------------------------------
|
|
|
|
|
val format = addressConfig.getString("format", "N/A")
|
|
|
|
|
|
2011-04-27 00:40:20 +02:00
|
|
|
// --------------------------------
|
|
|
|
|
// akka.actor.deployment.<address>.clustered
|
|
|
|
|
// --------------------------------
|
|
|
|
|
addressConfig.getSection("clustered") match {
|
|
|
|
|
case None =>
|
2011-05-16 09:47:23 +02:00
|
|
|
Some(Deploy(address, router, "N/A", Local)) // deploy locally
|
2011-04-27 00:40:20 +02:00
|
|
|
|
|
|
|
|
case Some(clusteredConfig) =>
|
|
|
|
|
|
|
|
|
|
// --------------------------------
|
|
|
|
|
// akka.actor.deployment.<address>.clustered.home
|
|
|
|
|
// --------------------------------
|
2011-05-04 21:25:47 +02:00
|
|
|
val home = clusteredConfig.getString("home", "") match {
|
|
|
|
|
case "" => Host("localhost")
|
|
|
|
|
case home =>
|
|
|
|
|
def raiseHomeConfigError() = throw new ConfigurationException(
|
|
|
|
|
"Config option [" + addressPath +
|
|
|
|
|
".clustered.home] needs to be on format 'host:<hostname>', 'ip:<ip address>'' or 'node:<node name>', was [" +
|
|
|
|
|
home + "]")
|
|
|
|
|
|
|
|
|
|
if (!(home.startsWith("host:") || home.startsWith("node:") || home.startsWith("ip:"))) raiseHomeConfigError()
|
|
|
|
|
|
|
|
|
|
val tokenizer = new java.util.StringTokenizer(home, ":")
|
|
|
|
|
val protocol = tokenizer.nextElement
|
|
|
|
|
val address = tokenizer.nextElement.asInstanceOf[String]
|
|
|
|
|
|
|
|
|
|
protocol match {
|
|
|
|
|
case "host" => Host(address)
|
|
|
|
|
case "node" => Node(address)
|
|
|
|
|
case "ip" => IP(address)
|
|
|
|
|
case _ => raiseHomeConfigError()
|
2011-04-27 00:40:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --------------------------------
|
|
|
|
|
// akka.actor.deployment.<address>.clustered.replicas
|
|
|
|
|
// --------------------------------
|
2011-05-03 21:04:45 +02:00
|
|
|
val replicas = clusteredConfig.getAny("replicas", "1") match {
|
2011-04-27 00:40:20 +02:00
|
|
|
case "auto" => AutoReplicate
|
|
|
|
|
case "1" => NoReplicas
|
|
|
|
|
case nrOfReplicas: String =>
|
|
|
|
|
try {
|
|
|
|
|
Replicate(nrOfReplicas.toInt)
|
|
|
|
|
} catch {
|
|
|
|
|
case e: NumberFormatException =>
|
|
|
|
|
throw new ConfigurationException(
|
|
|
|
|
"Config option [" + addressPath +
|
|
|
|
|
".clustered.replicas] needs to be either [\"auto\"] or [1-N] - was [" +
|
|
|
|
|
nrOfReplicas + "]")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --------------------------------
|
|
|
|
|
// akka.actor.deployment.<address>.clustered.stateless
|
|
|
|
|
// --------------------------------
|
|
|
|
|
val state =
|
|
|
|
|
if (clusteredConfig.getBool("stateless", false)) Stateless
|
|
|
|
|
else Stateful
|
|
|
|
|
|
2011-05-16 09:47:23 +02:00
|
|
|
Some(Deploy(address, router, format, Clustered(home, replicas, state)))
|
2011-04-27 00:40:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def throwDeploymentBoundException(deployment: Deploy): Nothing = {
|
2011-04-29 15:47:56 +02:00
|
|
|
val e = new DeploymentAlreadyBoundException(
|
2011-04-27 00:40:20 +02:00
|
|
|
"Address [" + deployment.address +
|
|
|
|
|
"] already bound to [" + deployment +
|
|
|
|
|
"]. You have to invoke 'undeploy(deployment) first.")
|
|
|
|
|
EventHandler.error(e, this, e.getMessage)
|
|
|
|
|
throw e
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def thrownNoDeploymentBoundException(address: String): Nothing = {
|
|
|
|
|
val e = new NoDeploymentBoundException("Address [" + address + "] is not bound to a deployment")
|
|
|
|
|
EventHandler.error(e, this, e.getMessage)
|
|
|
|
|
throw e
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-29 15:47:56 +02:00
|
|
|
/**
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2011-05-03 21:04:45 +02:00
|
|
|
object LocalDeployer {
|
2011-04-29 15:47:56 +02:00
|
|
|
private val deployments = new ConcurrentHashMap[String, Deploy]
|
|
|
|
|
|
2011-05-03 21:04:45 +02:00
|
|
|
private[akka] def init(deployments: List[Deploy]) {
|
2011-05-17 14:48:06 +02:00
|
|
|
EventHandler.info(this, "Deploying actors locally [\n\t%s\n]" format deployments.mkString("\n\t"))
|
2011-05-03 21:04:45 +02:00
|
|
|
deployments foreach (deploy(_)) // deploy
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private[akka] def shutdown() {
|
|
|
|
|
undeployAll()
|
|
|
|
|
deployments.clear()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private[akka] def deploy(deployment: Deploy) {
|
2011-04-29 15:47:56 +02:00
|
|
|
if (deployments.putIfAbsent(deployment.address, deployment) != deployment) {
|
|
|
|
|
// FIXME do automatic 'undeploy' and redeploy (perhaps have it configurable if redeploy should be done or exception thrown)
|
|
|
|
|
// throwDeploymentBoundException(deployment)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-03 21:04:45 +02:00
|
|
|
private[akka] def undeploy(deployment: Deploy) {
|
2011-04-29 15:47:56 +02:00
|
|
|
deployments.remove(deployment.address)
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-03 21:04:45 +02:00
|
|
|
private[akka] def undeployAll() {
|
2011-04-29 15:47:56 +02:00
|
|
|
deployments.clear()
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-03 21:04:45 +02:00
|
|
|
private[akka] def lookupDeploymentFor(address: String): Option[Deploy] = {
|
2011-04-29 15:47:56 +02:00
|
|
|
val deployment = deployments.get(address)
|
|
|
|
|
if (deployment eq null) None
|
|
|
|
|
else Some(deployment)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-27 00:40:20 +02:00
|
|
|
/**
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
|
|
|
|
object Address {
|
|
|
|
|
private val validAddressPattern = java.util.regex.Pattern.compile("[0-9a-zA-Z\\-\\_\\$\\.]+")
|
|
|
|
|
|
|
|
|
|
def validate(address: String) {
|
|
|
|
|
if (validAddressPattern.matcher(address).matches) true
|
|
|
|
|
else {
|
|
|
|
|
val e = new IllegalArgumentException("Address [" + address + "] is not valid, need to follow pattern [0-9a-zA-Z\\-\\_\\$]+")
|
|
|
|
|
EventHandler.error(e, this, e.getMessage)
|
|
|
|
|
throw e
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-29 15:47:56 +02:00
|
|
|
class DeploymentException private[akka](message: String) extends AkkaException(message)
|
|
|
|
|
class DeploymentAlreadyBoundException private[akka](message: String) extends AkkaException(message)
|
2011-04-27 00:40:20 +02:00
|
|
|
class NoDeploymentBoundException private[akka](message: String) extends AkkaException(message)
|