2011-09-15 10:20:18 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2009-2011 Typesafe Inc. <http://www.typesafe.com>
|
|
|
|
|
*/
|
|
|
|
|
|
2011-09-20 21:44:50 +02:00
|
|
|
package akka.remote
|
2011-09-15 10:20:18 +02:00
|
|
|
|
2011-10-12 09:10:05 +02:00
|
|
|
import akka.AkkaApplication
|
2011-09-15 10:20:18 +02:00
|
|
|
import akka.actor._
|
|
|
|
|
import akka.event.EventHandler
|
|
|
|
|
import akka.dispatch.{ Dispatchers, Future, PinnedDispatcher }
|
2011-09-30 14:52:07 +02:00
|
|
|
import akka.actor.Status._
|
2011-09-15 10:20:18 +02:00
|
|
|
import akka.util._
|
2011-09-30 14:52:07 +02:00
|
|
|
import akka.util.duration._
|
|
|
|
|
import akka.util.Helpers._
|
|
|
|
|
import akka.actor.DeploymentConfig._
|
2011-09-15 10:20:18 +02:00
|
|
|
import akka.serialization.{ Serialization, Serializer, ActorSerialization, Compression }
|
|
|
|
|
import Compression.LZF
|
|
|
|
|
import RemoteProtocol._
|
|
|
|
|
import RemoteDaemonMessageType._
|
|
|
|
|
|
|
|
|
|
import java.net.InetSocketAddress
|
|
|
|
|
|
|
|
|
|
import com.eaio.uuid.UUID
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2011-10-12 09:10:05 +02:00
|
|
|
class Remote(val app: AkkaApplication) extends RemoteService {
|
|
|
|
|
|
|
|
|
|
import app.config
|
2011-10-12 11:34:35 +02:00
|
|
|
import app.AkkaConfig.DefaultTimeUnit
|
2011-10-12 09:10:05 +02:00
|
|
|
|
2011-09-19 15:21:18 +02:00
|
|
|
val shouldCompressData = config.getBool("akka.remote.use-compression", false)
|
2011-10-12 11:34:35 +02:00
|
|
|
val remoteDaemonAckTimeout = Duration(config.getInt("akka.remote.remote-daemon-ack-timeout", 30), DefaultTimeUnit).toMillis.toInt
|
2011-09-15 10:20:18 +02:00
|
|
|
|
2011-10-12 09:10:05 +02:00
|
|
|
val hostname = app.hostname
|
2011-10-13 17:42:26 +02:00
|
|
|
val port = app.port
|
2011-09-15 10:20:18 +02:00
|
|
|
|
2011-09-22 03:36:59 +02:00
|
|
|
val remoteDaemonServiceName = "akka-remote-daemon".intern
|
2011-09-15 10:20:18 +02:00
|
|
|
|
|
|
|
|
// FIXME configure computeGridDispatcher to what?
|
2011-10-12 09:10:05 +02:00
|
|
|
val computeGridDispatcher = app.dispatcherFactory.newDispatcher("akka:compute-grid").build
|
2011-09-15 10:20:18 +02:00
|
|
|
|
2011-10-12 09:10:05 +02:00
|
|
|
private[remote] lazy val remoteDaemonSupervisor = app.createActor(Props(
|
|
|
|
|
OneForOneStrategy(List(classOf[Exception]), None, None))) // is infinite restart what we want?
|
2011-09-30 14:52:07 +02:00
|
|
|
|
|
|
|
|
private[remote] lazy val remoteDaemon =
|
|
|
|
|
new LocalActorRef(
|
2011-10-12 09:10:05 +02:00
|
|
|
app,
|
|
|
|
|
props = Props(new RemoteDaemon(this)).withDispatcher(app.dispatcherFactory.newPinnedDispatcher("Remote")).withSupervisor(remoteDaemonSupervisor),
|
2011-10-13 18:15:09 +02:00
|
|
|
givenAddress = remoteDaemonServiceName,
|
2011-09-30 14:52:07 +02:00
|
|
|
systemService = true)
|
2011-09-15 10:20:18 +02:00
|
|
|
|
2011-10-12 09:10:05 +02:00
|
|
|
private[remote] lazy val remoteClientLifeCycleHandler = app.createActor(Props(new Actor {
|
2011-09-15 10:20:18 +02:00
|
|
|
def receive = {
|
|
|
|
|
case RemoteClientError(cause, client, address) ⇒ client.shutdownClientModule()
|
|
|
|
|
case RemoteClientDisconnected(client, address) ⇒ client.shutdownClientModule()
|
|
|
|
|
case _ ⇒ //ignore other
|
|
|
|
|
}
|
|
|
|
|
}), "akka.cluster.RemoteClientLifeCycleListener")
|
2011-10-12 11:34:35 +02:00
|
|
|
|
2011-10-12 09:10:05 +02:00
|
|
|
lazy val eventStream = new NetworkEventStream(app)
|
2011-09-15 10:20:18 +02:00
|
|
|
|
|
|
|
|
lazy val server: RemoteSupport = {
|
2011-10-12 09:10:05 +02:00
|
|
|
val remote = new akka.remote.netty.NettyRemoteSupport(app)
|
2011-09-15 10:20:18 +02:00
|
|
|
remote.start(hostname, port)
|
2011-10-12 09:10:05 +02:00
|
|
|
remote.register(remoteDaemonServiceName, remoteDaemon)
|
|
|
|
|
remote.addListener(eventStream.channel)
|
2011-09-15 10:20:18 +02:00
|
|
|
remote.addListener(remoteClientLifeCycleHandler)
|
2011-10-13 14:23:44 +02:00
|
|
|
// TODO actually register this provider in app in remote mode
|
2011-10-12 09:10:05 +02:00
|
|
|
//app.provider.register(ActorRefProvider.RemoteProvider, new RemoteActorRefProvider)
|
2011-09-15 10:20:18 +02:00
|
|
|
remote
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lazy val address = server.address
|
|
|
|
|
|
2011-09-22 03:36:59 +02:00
|
|
|
def start() {
|
|
|
|
|
val triggerLazyServerVal = address.toString
|
2011-10-13 13:16:41 +02:00
|
|
|
app.eventHandler.info(this, "Starting remote server on [%s]".format(triggerLazyServerVal))
|
2011-09-22 03:36:59 +02:00
|
|
|
}
|
2011-09-20 21:44:50 +02:00
|
|
|
|
2011-09-15 10:20:18 +02:00
|
|
|
def uuidProtocolToUuid(uuid: UuidProtocol): UUID = new UUID(uuid.getHigh, uuid.getLow)
|
|
|
|
|
|
|
|
|
|
def uuidToUuidProtocol(uuid: UUID): UuidProtocol =
|
|
|
|
|
UuidProtocol.newBuilder
|
|
|
|
|
.setHigh(uuid.getTime)
|
|
|
|
|
.setLow(uuid.getClockSeqAndNode)
|
|
|
|
|
.build
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal "daemon" actor for cluster internal communication.
|
|
|
|
|
*
|
|
|
|
|
* It acts as the brain of the cluster that responds to cluster events (messages) and undertakes action.
|
|
|
|
|
*
|
|
|
|
|
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
|
|
|
|
*/
|
2011-10-12 09:10:05 +02:00
|
|
|
class RemoteDaemon(val remote: Remote) extends Actor {
|
2011-10-12 11:34:35 +02:00
|
|
|
|
2011-10-12 09:10:05 +02:00
|
|
|
import remote._
|
2011-10-12 11:34:35 +02:00
|
|
|
|
2011-09-15 10:20:18 +02:00
|
|
|
override def preRestart(reason: Throwable, msg: Option[Any]) {
|
2011-10-13 13:16:41 +02:00
|
|
|
app.eventHandler.debug(this, "RemoteDaemon failed due to [%s] restarting...".format(reason))
|
2011-09-15 10:20:18 +02:00
|
|
|
}
|
|
|
|
|
|
2011-10-12 09:10:05 +02:00
|
|
|
def receive: Actor.Receive = {
|
2011-09-15 10:20:18 +02:00
|
|
|
case message: RemoteDaemonMessageProtocol ⇒
|
2011-10-13 13:16:41 +02:00
|
|
|
app.eventHandler.debug(this,
|
2011-10-12 09:10:05 +02:00
|
|
|
"Received command [\n%s] to RemoteDaemon on [%s]".format(message, app.nodename))
|
2011-09-15 10:20:18 +02:00
|
|
|
|
|
|
|
|
message.getMessageType match {
|
|
|
|
|
case USE ⇒ handleUse(message)
|
|
|
|
|
case RELEASE ⇒ handleRelease(message)
|
|
|
|
|
// case STOP ⇒ cluster.shutdown()
|
|
|
|
|
// case DISCONNECT ⇒ cluster.disconnect()
|
|
|
|
|
// case RECONNECT ⇒ cluster.reconnect()
|
|
|
|
|
// case RESIGN ⇒ cluster.resign()
|
|
|
|
|
// case FAIL_OVER_CONNECTIONS ⇒ handleFailover(message)
|
|
|
|
|
case FUNCTION_FUN0_UNIT ⇒ handle_fun0_unit(message)
|
|
|
|
|
case FUNCTION_FUN0_ANY ⇒ handle_fun0_any(message)
|
|
|
|
|
case FUNCTION_FUN1_ARG_UNIT ⇒ handle_fun1_arg_unit(message)
|
|
|
|
|
case FUNCTION_FUN1_ARG_ANY ⇒ handle_fun1_arg_any(message)
|
|
|
|
|
//TODO: should we not deal with unrecognized message types?
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-13 13:16:41 +02:00
|
|
|
case unknown ⇒ app.eventHandler.warning(this, "Unknown message [%s]".format(unknown))
|
2011-09-15 10:20:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def handleUse(message: RemoteProtocol.RemoteDaemonMessageProtocol) {
|
|
|
|
|
try {
|
|
|
|
|
if (message.hasActorAddress) {
|
2011-09-19 14:43:28 +02:00
|
|
|
|
|
|
|
|
val actorFactoryBytes =
|
|
|
|
|
if (shouldCompressData) LZF.uncompress(message.getPayload.toByteArray)
|
|
|
|
|
else message.getPayload.toByteArray
|
|
|
|
|
|
|
|
|
|
val actorFactory =
|
2011-10-12 09:10:05 +02:00
|
|
|
app.serialization.deserialize(actorFactoryBytes, classOf[() ⇒ Actor], None) match {
|
2011-09-15 10:20:18 +02:00
|
|
|
case Left(error) ⇒ throw error
|
2011-09-19 14:43:28 +02:00
|
|
|
case Right(instance) ⇒ instance.asInstanceOf[() ⇒ Actor]
|
2011-09-15 10:20:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val actorAddress = message.getActorAddress
|
2011-10-12 09:10:05 +02:00
|
|
|
val newActorRef = app.createActor(Props(creator = actorFactory), actorAddress)
|
2011-09-15 10:20:18 +02:00
|
|
|
|
2011-10-12 09:10:05 +02:00
|
|
|
remote.server.register(actorAddress, newActorRef)
|
2011-09-15 10:20:18 +02:00
|
|
|
|
|
|
|
|
} else {
|
2011-10-13 13:16:41 +02:00
|
|
|
app.eventHandler.error(this, "Actor 'address' is not defined, ignoring remote daemon command [%s]".format(message))
|
2011-09-15 10:20:18 +02:00
|
|
|
}
|
|
|
|
|
|
2011-09-22 04:38:33 +02:00
|
|
|
reply(Success(address.toString))
|
2011-09-15 10:20:18 +02:00
|
|
|
} catch {
|
|
|
|
|
case error: Throwable ⇒
|
2011-09-22 04:38:33 +02:00
|
|
|
reply(Failure(error))
|
2011-09-15 10:20:18 +02:00
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def handleRelease(message: RemoteProtocol.RemoteDaemonMessageProtocol) {
|
2011-09-19 14:43:28 +02:00
|
|
|
// FIXME implement handleRelease without Cluster
|
|
|
|
|
|
|
|
|
|
// if (message.hasActorUuid) {
|
|
|
|
|
// cluster.actorAddressForUuid(uuidProtocolToUuid(message.getActorUuid)) foreach { address ⇒
|
|
|
|
|
// cluster.release(address)
|
|
|
|
|
// }
|
|
|
|
|
// } else if (message.hasActorAddress) {
|
|
|
|
|
// cluster release message.getActorAddress
|
|
|
|
|
// } else {
|
|
|
|
|
// EventHandler.warning(this,
|
|
|
|
|
// "None of 'uuid' or 'actorAddress'' is specified, ignoring remote cluster daemon command [%s]".format(message))
|
|
|
|
|
// }
|
2011-09-15 10:20:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def handle_fun0_unit(message: RemoteProtocol.RemoteDaemonMessageProtocol) {
|
2011-10-12 09:10:05 +02:00
|
|
|
new LocalActorRef(app,
|
2011-09-15 10:20:18 +02:00
|
|
|
Props(
|
2011-09-29 12:44:52 +02:00
|
|
|
context ⇒ {
|
|
|
|
|
case f: Function0[_] ⇒ try { f() } finally { context.self.stop() }
|
2011-09-15 10:20:18 +02:00
|
|
|
}).copy(dispatcher = computeGridDispatcher), newUuid.toString, systemService = true) ! payloadFor(message, classOf[Function0[Unit]])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def handle_fun0_any(message: RemoteProtocol.RemoteDaemonMessageProtocol) {
|
2011-10-12 09:10:05 +02:00
|
|
|
new LocalActorRef(app,
|
2011-09-15 10:20:18 +02:00
|
|
|
Props(
|
2011-09-29 12:44:52 +02:00
|
|
|
context ⇒ {
|
|
|
|
|
case f: Function0[_] ⇒ try { reply(f()) } finally { context.self.stop() }
|
2011-09-15 10:20:18 +02:00
|
|
|
}).copy(dispatcher = computeGridDispatcher), newUuid.toString, systemService = true) forward payloadFor(message, classOf[Function0[Any]])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def handle_fun1_arg_unit(message: RemoteProtocol.RemoteDaemonMessageProtocol) {
|
2011-10-12 09:10:05 +02:00
|
|
|
new LocalActorRef(app,
|
2011-09-15 10:20:18 +02:00
|
|
|
Props(
|
2011-09-29 12:44:52 +02:00
|
|
|
context ⇒ {
|
|
|
|
|
case (fun: Function[_, _], param: Any) ⇒ try { fun.asInstanceOf[Any ⇒ Unit].apply(param) } finally { context.self.stop() }
|
2011-09-15 10:20:18 +02:00
|
|
|
}).copy(dispatcher = computeGridDispatcher), newUuid.toString, systemService = true) ! payloadFor(message, classOf[Tuple2[Function1[Any, Unit], Any]])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def handle_fun1_arg_any(message: RemoteProtocol.RemoteDaemonMessageProtocol) {
|
2011-10-12 09:10:05 +02:00
|
|
|
new LocalActorRef(app,
|
2011-09-15 10:20:18 +02:00
|
|
|
Props(
|
2011-09-29 12:44:52 +02:00
|
|
|
context ⇒ {
|
|
|
|
|
case (fun: Function[_, _], param: Any) ⇒ try { reply(fun.asInstanceOf[Any ⇒ Any](param)) } finally { context.self.stop() }
|
2011-09-15 10:20:18 +02:00
|
|
|
}).copy(dispatcher = computeGridDispatcher), newUuid.toString, systemService = true) forward payloadFor(message, classOf[Tuple2[Function1[Any, Any], Any]])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def handleFailover(message: RemoteProtocol.RemoteDaemonMessageProtocol) {
|
2011-09-22 03:36:59 +02:00
|
|
|
// val (from, to) = payloadFor(message, classOf[(InetSocketremoteDaemonServiceName, InetSocketremoteDaemonServiceName)])
|
2011-09-15 10:20:18 +02:00
|
|
|
// cluster.failOverClusterActorRefConnections(from, to)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def payloadFor[T](message: RemoteDaemonMessageProtocol, clazz: Class[T]): T = {
|
2011-10-12 09:10:05 +02:00
|
|
|
app.serialization.deserialize(message.getPayload.toByteArray, clazz, None) match {
|
2011-09-15 10:20:18 +02:00
|
|
|
case Left(error) ⇒ throw error
|
|
|
|
|
case Right(instance) ⇒ instance.asInstanceOf[T]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|