2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
|
2012-05-15 09:40:13 +02:00
|
|
|
*/
|
|
|
|
|
|
2012-09-17 12:54:08 +02:00
|
|
|
package akka.remote.serialization
|
2012-05-15 09:40:13 +02:00
|
|
|
|
2018-11-22 16:18:10 +01:00
|
|
|
import scala.collection.immutable
|
|
|
|
|
|
2017-02-21 16:08:16 +01:00
|
|
|
import akka.serialization.{ BaseSerializer, SerializationExtension, SerializerWithStringManifest }
|
2019-08-15 16:43:19 +01:00
|
|
|
import akka.protobufv3.internal.ByteString
|
2016-02-22 20:18:15 +01:00
|
|
|
import akka.actor.{ Deploy, ExtendedActorSystem, NoScopeGiven, Props, Scope }
|
2012-05-15 09:40:13 +02:00
|
|
|
import akka.remote.DaemonMsgCreate
|
2018-03-16 19:08:29 +08:00
|
|
|
import akka.remote.WireFormats.{ DaemonMsgCreateData, DeployData, PropsData }
|
2012-06-25 16:29:08 +02:00
|
|
|
import akka.routing.{ NoRouter, RouterConfig }
|
2018-12-20 09:10:54 -08:00
|
|
|
import com.typesafe.config.{ Config, ConfigFactory }
|
2018-11-22 16:18:10 +01:00
|
|
|
import akka.util.ccompat._
|
2017-02-21 16:08:16 +01:00
|
|
|
|
2012-06-25 16:29:08 +02:00
|
|
|
import scala.reflect.ClassTag
|
2012-09-06 03:17:51 +02:00
|
|
|
import util.{ Failure, Success }
|
2019-10-14 17:57:11 +02:00
|
|
|
import akka.util.ccompat.JavaConverters._
|
2012-05-15 09:40:13 +02:00
|
|
|
|
|
|
|
|
/**
|
2015-06-02 21:01:00 -07:00
|
|
|
* Serializes Akka's internal DaemonMsgCreate using protobuf
|
2012-05-15 09:40:13 +02:00
|
|
|
* for the core structure of DaemonMsgCreate, Props and Deploy.
|
2012-05-15 18:22:40 +02:00
|
|
|
* Serialization of contained RouterConfig, Config, and Scope
|
|
|
|
|
* is done with configured serializer for those classes, by
|
|
|
|
|
* default java.io.Serializable.
|
2012-05-24 12:19:39 +02:00
|
|
|
*
|
|
|
|
|
* INTERNAL API
|
2012-05-15 09:40:13 +02:00
|
|
|
*/
|
2019-04-16 20:26:09 +02:00
|
|
|
@ccompatUsedUntil213
|
2017-02-21 16:08:16 +01:00
|
|
|
private[akka] final class DaemonMsgCreateSerializer(val system: ExtendedActorSystem) extends BaseSerializer {
|
2012-05-15 09:40:13 +02:00
|
|
|
import ProtobufSerializer.serializeActorRef
|
|
|
|
|
import ProtobufSerializer.deserializeActorRef
|
2013-04-05 16:12:45 +02:00
|
|
|
import Deploy.NoDispatcherGiven
|
2012-05-15 09:40:13 +02:00
|
|
|
|
2017-02-21 16:08:16 +01:00
|
|
|
private lazy val serialization = SerializationExtension(system)
|
2015-03-05 11:55:05 -06:00
|
|
|
|
2017-02-21 16:08:16 +01:00
|
|
|
override val includeManifest: Boolean = false
|
2012-05-15 09:40:13 +02:00
|
|
|
|
|
|
|
|
def toBinary(obj: AnyRef): Array[Byte] = obj match {
|
2019-02-09 15:25:39 +01:00
|
|
|
case DaemonMsgCreate(props, deploy, path, supervisor) =>
|
2013-03-27 17:47:56 +01:00
|
|
|
def deployProto(d: Deploy): DeployData = {
|
|
|
|
|
val builder = DeployData.newBuilder.setPath(d.path)
|
2017-03-16 15:12:35 +01:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
val (serId, _, manifest, bytes) = serialize(d.config)
|
|
|
|
|
builder.setConfigSerializerId(serId)
|
|
|
|
|
builder.setConfigManifest(manifest)
|
|
|
|
|
builder.setConfig(ByteString.copyFrom(bytes))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (d.routerConfig != NoRouter) {
|
|
|
|
|
val (serId, _, manifest, bytes) = serialize(d.routerConfig)
|
|
|
|
|
builder.setRouterConfigSerializerId(serId)
|
|
|
|
|
builder.setRouterConfigManifest(manifest)
|
|
|
|
|
builder.setRouterConfig(ByteString.copyFrom(bytes))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (d.scope != NoScopeGiven) {
|
|
|
|
|
val (serId, _, manifest, bytes) = serialize(d.scope)
|
|
|
|
|
builder.setScopeSerializerId(serId)
|
|
|
|
|
builder.setScopeManifest(manifest)
|
|
|
|
|
builder.setScope(ByteString.copyFrom(bytes))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (d.dispatcher != NoDispatcherGiven) {
|
2013-04-05 16:12:45 +02:00
|
|
|
builder.setDispatcher(d.dispatcher)
|
2017-03-16 15:12:35 +01:00
|
|
|
}
|
2019-10-14 17:57:11 +02:00
|
|
|
if (d.tags.nonEmpty) {
|
|
|
|
|
builder.addAllTags(d.tags.asJava)
|
|
|
|
|
}
|
2012-05-15 09:40:13 +02:00
|
|
|
builder.build
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def propsProto = {
|
2019-03-11 10:38:24 +01:00
|
|
|
val builder = PropsData.newBuilder.setClazz(props.clazz.getName).setDeploy(deployProto(props.deploy))
|
2019-02-09 15:25:39 +01:00
|
|
|
props.args.foreach { arg =>
|
2017-02-21 16:08:16 +01:00
|
|
|
val (serializerId, hasManifest, manifest, bytes) = serialize(arg)
|
|
|
|
|
builder.addArgs(ByteString.copyFrom(bytes))
|
|
|
|
|
builder.addManifests(manifest)
|
|
|
|
|
builder.addSerializerIds(serializerId)
|
|
|
|
|
builder.addHasManifest(hasManifest)
|
2016-12-20 15:27:32 +01:00
|
|
|
}
|
2012-05-15 09:40:13 +02:00
|
|
|
builder.build
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-11 10:38:24 +01:00
|
|
|
DaemonMsgCreateData.newBuilder
|
|
|
|
|
.setProps(propsProto)
|
|
|
|
|
.setDeploy(deployProto(deploy))
|
|
|
|
|
.setPath(path)
|
|
|
|
|
.setSupervisor(serializeActorRef(supervisor))
|
|
|
|
|
.build
|
|
|
|
|
.toByteArray
|
2012-05-15 09:40:13 +02:00
|
|
|
|
2019-02-09 15:25:39 +01:00
|
|
|
case _ =>
|
2012-05-15 09:40:13 +02:00
|
|
|
throw new IllegalArgumentException(
|
|
|
|
|
"Can't serialize a non-DaemonMsgCreate message using DaemonMsgCreateSerializer [%s]".format(obj))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]]): AnyRef = {
|
2013-03-27 17:47:56 +01:00
|
|
|
val proto = DaemonMsgCreateData.parseFrom(bytes)
|
2012-05-15 09:40:13 +02:00
|
|
|
|
2013-03-27 17:47:56 +01:00
|
|
|
def deploy(protoDeploy: DeployData): Deploy = {
|
2017-03-16 15:12:35 +01:00
|
|
|
|
2012-05-15 09:40:13 +02:00
|
|
|
val config =
|
2017-03-16 15:12:35 +01:00
|
|
|
if (protoDeploy.hasConfig) {
|
|
|
|
|
if (protoDeploy.hasConfigSerializerId) {
|
2019-03-11 10:38:24 +01:00
|
|
|
serialization
|
2019-03-13 10:56:20 +01:00
|
|
|
.deserialize(
|
|
|
|
|
protoDeploy.getConfig.toByteArray,
|
|
|
|
|
protoDeploy.getConfigSerializerId,
|
|
|
|
|
protoDeploy.getConfigManifest)
|
2019-03-11 10:38:24 +01:00
|
|
|
.get
|
|
|
|
|
.asInstanceOf[Config]
|
2017-03-16 15:12:35 +01:00
|
|
|
} else {
|
|
|
|
|
// old wire format
|
|
|
|
|
oldDeserialize(protoDeploy.getConfig, classOf[Config])
|
|
|
|
|
}
|
|
|
|
|
} else ConfigFactory.empty
|
|
|
|
|
|
2012-05-15 09:40:13 +02:00
|
|
|
val routerConfig =
|
2017-03-16 15:12:35 +01:00
|
|
|
if (protoDeploy.hasRouterConfig) {
|
|
|
|
|
if (protoDeploy.hasRouterConfigSerializerId) {
|
2019-03-11 10:38:24 +01:00
|
|
|
serialization
|
2019-03-13 10:56:20 +01:00
|
|
|
.deserialize(
|
|
|
|
|
protoDeploy.getRouterConfig.toByteArray,
|
|
|
|
|
protoDeploy.getRouterConfigSerializerId,
|
|
|
|
|
protoDeploy.getRouterConfigManifest)
|
2019-03-11 10:38:24 +01:00
|
|
|
.get
|
|
|
|
|
.asInstanceOf[RouterConfig]
|
2017-03-16 15:12:35 +01:00
|
|
|
} else {
|
|
|
|
|
// old wire format
|
|
|
|
|
oldDeserialize(protoDeploy.getRouterConfig, classOf[RouterConfig])
|
|
|
|
|
}
|
|
|
|
|
} else NoRouter
|
|
|
|
|
|
2012-05-15 09:40:13 +02:00
|
|
|
val scope =
|
2017-03-16 15:12:35 +01:00
|
|
|
if (protoDeploy.hasScope) {
|
|
|
|
|
if (protoDeploy.hasScopeSerializerId) {
|
2019-03-11 10:38:24 +01:00
|
|
|
serialization
|
2019-03-13 10:56:20 +01:00
|
|
|
.deserialize(
|
|
|
|
|
protoDeploy.getScope.toByteArray,
|
|
|
|
|
protoDeploy.getScopeSerializerId,
|
|
|
|
|
protoDeploy.getScopeManifest)
|
2019-03-11 10:38:24 +01:00
|
|
|
.get
|
|
|
|
|
.asInstanceOf[Scope]
|
2017-03-16 15:12:35 +01:00
|
|
|
} else {
|
|
|
|
|
// old wire format
|
|
|
|
|
oldDeserialize(protoDeploy.getScope, classOf[Scope])
|
|
|
|
|
}
|
|
|
|
|
} else NoScopeGiven
|
2013-04-05 16:12:45 +02:00
|
|
|
val dispatcher =
|
|
|
|
|
if (protoDeploy.hasDispatcher) protoDeploy.getDispatcher
|
|
|
|
|
else NoDispatcherGiven
|
2019-10-14 17:57:11 +02:00
|
|
|
|
|
|
|
|
val tags: Set[String] =
|
|
|
|
|
if (protoDeploy.getTagsCount == 0) Set.empty
|
|
|
|
|
else protoDeploy.getTagsList.iterator().asScala.toSet
|
|
|
|
|
val deploy = Deploy(protoDeploy.getPath, config, routerConfig, scope, dispatcher)
|
|
|
|
|
if (tags.isEmpty) deploy
|
|
|
|
|
else deploy.withTags(tags)
|
2012-05-15 09:40:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def props = {
|
2019-05-24 08:11:50 +02:00
|
|
|
import akka.util.ccompat.JavaConverters._
|
2017-02-21 16:08:16 +01:00
|
|
|
val protoProps = proto.getProps
|
|
|
|
|
val actorClass = system.dynamicAccess.getClassFor[AnyRef](protoProps.getClazz).get
|
|
|
|
|
val args: Vector[AnyRef] =
|
|
|
|
|
// message from a newer node always contains serializer ids and possibly a string manifest for each position
|
|
|
|
|
if (protoProps.getSerializerIdsCount > 0) {
|
|
|
|
|
for {
|
2019-02-09 15:25:39 +01:00
|
|
|
idx <- (0 until protoProps.getSerializerIdsCount).toVector
|
2017-02-21 16:08:16 +01:00
|
|
|
} yield {
|
|
|
|
|
val manifest =
|
|
|
|
|
if (protoProps.getHasManifest(idx)) protoProps.getManifests(idx)
|
|
|
|
|
else ""
|
2019-03-11 10:38:24 +01:00
|
|
|
serialization
|
|
|
|
|
.deserialize(protoProps.getArgs(idx).toByteArray(), protoProps.getSerializerIds(idx), manifest)
|
|
|
|
|
.get
|
2017-02-21 16:08:16 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// message from an older node, which only provides data and class name
|
|
|
|
|
// and never any serializer ids
|
2019-03-11 10:38:24 +01:00
|
|
|
proto.getProps.getArgsList.asScala
|
|
|
|
|
.zip(proto.getProps.getManifestsList.asScala)
|
|
|
|
|
.iterator
|
|
|
|
|
.map(oldDeserialize)
|
|
|
|
|
.to(immutable.Vector)
|
2017-02-21 16:08:16 +01:00
|
|
|
}
|
|
|
|
|
Props(deploy(proto.getProps.getDeploy), actorClass, args)
|
2012-05-15 09:40:13 +02:00
|
|
|
}
|
|
|
|
|
|
2019-03-13 10:56:20 +01:00
|
|
|
DaemonMsgCreate(
|
|
|
|
|
props = props,
|
|
|
|
|
deploy = deploy(proto.getDeploy),
|
|
|
|
|
path = proto.getPath,
|
|
|
|
|
supervisor = deserializeActorRef(system, proto.getSupervisor))
|
2012-05-15 09:40:13 +02:00
|
|
|
}
|
|
|
|
|
|
2017-02-21 16:08:16 +01:00
|
|
|
private def serialize(any: Any): (Int, Boolean, String, Array[Byte]) = {
|
|
|
|
|
val m = any.asInstanceOf[AnyRef]
|
|
|
|
|
val serializer = serialization.findSerializerFor(m)
|
|
|
|
|
|
|
|
|
|
// this trixery is to retain backwards wire compatibility while at the same time
|
|
|
|
|
// allowing for usage of serializers with string manifests
|
2018-04-12 19:58:13 +03:00
|
|
|
val hasManifest = serializer.includeManifest
|
2017-02-21 16:08:16 +01:00
|
|
|
val manifest = serializer match {
|
2019-02-09 15:25:39 +01:00
|
|
|
case ser: SerializerWithStringManifest =>
|
2017-02-21 16:08:16 +01:00
|
|
|
ser.manifest(m)
|
2019-02-09 15:25:39 +01:00
|
|
|
case _ =>
|
2017-02-21 16:08:16 +01:00
|
|
|
// we do include class name regardless to retain wire compatibility
|
|
|
|
|
// with older nodes who expect manifest to be the class name
|
|
|
|
|
if (m eq null) {
|
|
|
|
|
"null"
|
|
|
|
|
} else {
|
|
|
|
|
val className = m.getClass.getName
|
2019-04-19 07:53:27 +01:00
|
|
|
if (m.isInstanceOf[java.io.Serializable] && m.getClass.isSynthetic && className.contains("$Lambda$")) {
|
2017-02-21 16:08:16 +01:00
|
|
|
// When the additional-protobuf serializers are not enabled
|
|
|
|
|
// the serialization of the parameters is based on passing class name instead of
|
|
|
|
|
// serializerId and manifest as we usually do. With Scala 2.12 the functions are generated as
|
|
|
|
|
// lambdas and we can't use that load class from that name when deserializing
|
2018-04-12 19:58:13 +03:00
|
|
|
classOf[java.io.Serializable].getName
|
2017-02-21 16:08:16 +01:00
|
|
|
} else {
|
|
|
|
|
className
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(serializer.identifier, hasManifest, manifest, serializer.toBinary(m))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def oldDeserialize(p: (ByteString, String)): AnyRef =
|
|
|
|
|
oldDeserialize(p._1, p._2)
|
2012-05-15 09:40:13 +02:00
|
|
|
|
2017-02-21 16:08:16 +01:00
|
|
|
private def oldDeserialize(data: ByteString, className: String): AnyRef =
|
|
|
|
|
if (data.isEmpty && className == "null") null
|
|
|
|
|
else oldDeserialize(data, system.dynamicAccess.getClassFor[AnyRef](className).get)
|
2014-08-29 14:40:49 +02:00
|
|
|
|
2017-02-21 16:08:16 +01:00
|
|
|
private def oldDeserialize[T: ClassTag](data: ByteString, clazz: Class[T]): T = {
|
2012-05-15 09:40:13 +02:00
|
|
|
val bytes = data.toByteArray
|
|
|
|
|
serialization.deserialize(bytes, clazz) match {
|
2019-03-11 10:38:24 +01:00
|
|
|
case Success(x: T) => x
|
|
|
|
|
case Success(other) =>
|
|
|
|
|
throw new IllegalArgumentException("Can't deserialize to [%s], got [%s]".format(clazz.getName, other))
|
2019-02-09 15:25:39 +01:00
|
|
|
case Failure(e) =>
|
2012-05-15 09:40:13 +02:00
|
|
|
// Fallback to the java serializer, because some interfaces don't implement java.io.Serializable,
|
|
|
|
|
// but the impl instance does. This could be optimized by adding java serializers in reference.conf:
|
|
|
|
|
// com.typesafe.config.Config
|
|
|
|
|
// akka.routing.RouterConfig
|
|
|
|
|
// akka.actor.Scope
|
|
|
|
|
serialization.deserialize(bytes, classOf[java.io.Serializable]) match {
|
2019-02-09 15:25:39 +01:00
|
|
|
case Success(x: T) => x
|
|
|
|
|
case _ => throw e // the first exception
|
2012-05-15 09:40:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-21 16:08:16 +01:00
|
|
|
|
2013-01-09 01:47:48 +01:00
|
|
|
}
|