pekko/akka-remote/src/main/scala/akka/remote/artery/Codecs.scala

155 lines
6.2 KiB
Scala
Raw Normal View History

2016-05-05 14:38:48 +02:00
package akka.remote.artery
import akka.actor.{ ActorRef, InternalActorRef }
import akka.remote.EndpointManager.Send
import akka.remote.{ MessageSerializer, UniqueAddress }
import akka.serialization.{ Serialization, SerializationExtension }
import akka.stream._
import akka.stream.stage.{ GraphStage, GraphStageLogic, InHandler, OutHandler }
import akka.actor.ActorSystem
import akka.actor.ExtendedActorSystem
2016-05-05 14:38:48 +02:00
// TODO: Long UID
class Encoder(
uniqueLocalAddress: UniqueAddress,
system: ActorSystem,
compressionTable: LiteralCompressionTable,
pool: EnvelopeBufferPool)
2016-05-05 14:38:48 +02:00
extends GraphStage[FlowShape[Send, EnvelopeBuffer]] {
val in: Inlet[Send] = Inlet("Artery.Encoder.in")
val out: Outlet[EnvelopeBuffer] = Outlet("Artery.Encoder.out")
val shape: FlowShape[Send, EnvelopeBuffer] = FlowShape(in, out)
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
new GraphStageLogic(shape) with InHandler with OutHandler {
private val headerBuilder = HeaderBuilder(compressionTable)
headerBuilder.version = ArteryTransport.Version
headerBuilder.uid = uniqueLocalAddress.uid
private val localAddress = uniqueLocalAddress.address
private val serialization = SerializationExtension(system)
2016-05-05 14:38:48 +02:00
private val noSender = system.deadLetters.path.toSerializationFormatWithAddress(localAddress)
private val senderCache = new java.util.HashMap[ActorRef, String]
private var recipientCache = new java.util.HashMap[ActorRef, String]
2016-05-05 14:38:48 +02:00
override def onPush(): Unit = {
val send = grab(in)
val envelope = pool.acquire()
val recipientStr = recipientCache.get(send.recipient) match {
case null
val s = send.recipient.path.toSerializationFormat
// FIXME this cache will be replaced by compression table
if (recipientCache.size() >= 1000)
recipientCache.clear()
recipientCache.put(send.recipient, s)
s
case s s
}
headerBuilder.recipientActorRef = recipientStr
2016-05-05 14:38:48 +02:00
send.senderOption match {
case Some(sender)
val senderStr = senderCache.get(sender) match {
case null
val s = sender.path.toSerializationFormatWithAddress(localAddress)
// FIXME we might need an efficient LRU cache, or replaced by compression table
if (senderCache.size() >= 1000)
senderCache.clear()
senderCache.put(sender, s)
s
case s s
}
headerBuilder.senderActorRef = senderStr
2016-05-05 14:38:48 +02:00
case None
//headerBuilder.setNoSender()
headerBuilder.senderActorRef = noSender
2016-05-05 14:38:48 +02:00
}
// FIXME: Thunk allocation
Serialization.currentTransportInformation.withValue(Serialization.Information(localAddress, system)) {
2016-05-05 14:38:48 +02:00
MessageSerializer.serializeForArtery(serialization, send.message.asInstanceOf[AnyRef], headerBuilder, envelope)
}
//println(s"${headerBuilder.senderActorRef} --> ${headerBuilder.recipientActorRef} ${headerBuilder.classManifest}")
envelope.byteBuffer.flip()
push(out, envelope)
}
override def onPull(): Unit = pull(in)
setHandlers(in, out, this)
}
}
class Decoder(
uniqueLocalAddress: UniqueAddress,
system: ExtendedActorSystem,
resolveActorRefWithLocalAddress: String InternalActorRef,
compressionTable: LiteralCompressionTable,
pool: EnvelopeBufferPool) extends GraphStage[FlowShape[EnvelopeBuffer, InboundEnvelope]] {
2016-05-05 14:38:48 +02:00
val in: Inlet[EnvelopeBuffer] = Inlet("Artery.Decoder.in")
val out: Outlet[InboundEnvelope] = Outlet("Artery.Decoder.out")
val shape: FlowShape[EnvelopeBuffer, InboundEnvelope] = FlowShape(in, out)
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
new GraphStageLogic(shape) with InHandler with OutHandler {
private val localAddress = uniqueLocalAddress.address
2016-05-05 14:38:48 +02:00
private val headerBuilder = HeaderBuilder(compressionTable)
private val serialization = SerializationExtension(system)
private val recipientCache = new java.util.HashMap[String, InternalActorRef]
private val senderCache = new java.util.HashMap[String, Option[ActorRef]]
2016-05-05 14:38:48 +02:00
override def onPush(): Unit = {
val envelope = grab(in)
envelope.parseHeader(headerBuilder)
//println(s"${headerBuilder.recipientActorRef} <-- ${headerBuilder.senderActorRef} ${headerBuilder.classManifest}")
// FIXME: Instead of using Strings, the headerBuilder should automatically return cached ActorRef instances
// in case of compression is enabled
// FIXME: Is localAddress really needed?
val recipient: InternalActorRef = recipientCache.get(headerBuilder.recipientActorRef) match {
case null
val ref = resolveActorRefWithLocalAddress(headerBuilder.recipientActorRef)
// FIXME we might need an efficient LRU cache, or replaced by compression table
if (recipientCache.size() >= 1000)
recipientCache.clear()
recipientCache.put(headerBuilder.recipientActorRef, ref)
ref
case ref ref
}
val senderOption: Option[ActorRef] = senderCache.get(headerBuilder.senderActorRef) match {
case null
val ref = resolveActorRefWithLocalAddress(headerBuilder.senderActorRef)
// FIXME this cache will be replaced by compression table
if (senderCache.size() >= 1000)
senderCache.clear()
val refOpt = Some(ref)
senderCache.put(headerBuilder.senderActorRef, refOpt)
refOpt
case refOpt refOpt
}
2016-05-05 14:38:48 +02:00
val decoded = InboundEnvelope(
recipient,
localAddress, // FIXME: Is this needed anymore? What should we do here?
MessageSerializer.deserializeForArtery(system, serialization, headerBuilder, envelope),
senderOption, // FIXME: No need for an option, decode simply to deadLetters instead
UniqueAddress(senderOption.get.path.address, headerBuilder.uid)) // FIXME see issue #20568
2016-05-05 14:38:48 +02:00
pool.release(envelope)
push(out, decoded)
}
override def onPull(): Unit = pull(in)
setHandlers(in, out, this)
}
}