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

217 lines
6.7 KiB
Scala
Raw Normal View History

/**
2018-01-04 17:26:29 +00:00
* Copyright (C) 2016-2018 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.remote.artery
import java.util.ArrayDeque
import scala.concurrent.Future
import scala.concurrent.Promise
import scala.util.Try
import akka.Done
import akka.stream.Attributes
import akka.stream.FlowShape
import akka.stream.Inlet
import akka.stream.Outlet
import akka.stream.stage._
import akka.remote.UniqueAddress
import akka.util.OptionVal
import akka.event.Logging
/** INTERNAL API: marker trait for protobuf-serializable artery messages */
2016-09-29 10:50:37 +02:00
private[remote] trait ArteryMessage extends Serializable
/**
* INTERNAL API: Marker trait for reply messages
*/
2016-09-29 10:50:37 +02:00
private[remote] trait Reply extends ControlMessage
/**
* INTERNAL API
* Marker trait for control messages that can be sent via the system message sub-channel
* but don't need full reliable delivery. E.g. `HandshakeReq` and `Reply`.
*/
2016-09-29 10:50:37 +02:00
private[remote] trait ControlMessage extends ArteryMessage
/**
* INTERNAL API
*/
2016-09-29 10:50:37 +02:00
private[remote] final case class Quarantined(from: UniqueAddress, to: UniqueAddress) extends ControlMessage
/**
* INTERNAL API
*/
2016-09-29 10:50:37 +02:00
private[remote] case class ActorSystemTerminating(from: UniqueAddress) extends ControlMessage
/**
* INTERNAL API
*/
2016-09-29 10:50:37 +02:00
private[remote] case class ActorSystemTerminatingAck(from: UniqueAddress) extends ArteryMessage
/**
* INTERNAL API
*/
2016-09-29 10:50:37 +02:00
private[remote] object InboundControlJunction {
2016-05-12 08:56:28 +02:00
/**
* Observer subject for inbound control messages.
* Interested observers can attach themselves to the
* subject to get notification of incoming control
* messages.
*/
2016-09-07 16:07:29 +02:00
private[remote] trait ControlMessageSubject {
2016-05-12 08:56:28 +02:00
def attach(observer: ControlMessageObserver): Future[Done]
def detach(observer: ControlMessageObserver): Unit
}
2016-09-07 16:07:29 +02:00
private[remote] trait ControlMessageObserver {
2016-05-12 08:56:28 +02:00
/**
* Notification of incoming control message. The message
* of the envelope is always a `ControlMessage`.
*/
def notify(inboundEnvelope: InboundEnvelope): Unit
def controlSubjectCompleted(signal: Try[Done]): Unit
}
// messages for the stream callback
2016-05-12 08:56:28 +02:00
private[InboundControlJunction] sealed trait CallbackMessage
private[InboundControlJunction] final case class Attach(observer: ControlMessageObserver, done: Promise[Done])
extends CallbackMessage
2016-05-12 08:56:28 +02:00
private[InboundControlJunction] final case class Dettach(observer: ControlMessageObserver) extends CallbackMessage
}
/**
* INTERNAL API
*/
2016-09-29 10:50:37 +02:00
private[remote] class InboundControlJunction
2016-05-12 08:56:28 +02:00
extends GraphStageWithMaterializedValue[FlowShape[InboundEnvelope, InboundEnvelope], InboundControlJunction.ControlMessageSubject] {
import InboundControlJunction._
2016-05-12 08:56:28 +02:00
val in: Inlet[InboundEnvelope] = Inlet("InboundControlJunction.in")
val out: Outlet[InboundEnvelope] = Outlet("InboundControlJunction.out")
override val shape: FlowShape[InboundEnvelope, InboundEnvelope] = FlowShape(in, out)
override def createLogicAndMaterializedValue(inheritedAttributes: Attributes) = {
val logic = new GraphStageLogic(shape) with InHandler with OutHandler with ControlMessageSubject {
2016-05-12 08:56:28 +02:00
private var observers: Vector[ControlMessageObserver] = Vector.empty
private val callback = getAsyncCallback[CallbackMessage] {
case Attach(observer, done)
2016-05-12 08:56:28 +02:00
observers :+= observer
done.success(Done)
case Dettach(observer)
2016-05-12 08:56:28 +02:00
observers = observers.filterNot(_ == observer)
}
override def postStop(): Unit = {
observers.foreach(_.controlSubjectCompleted(Try(Done)))
observers = Vector.empty
}
// InHandler
override def onPush(): Unit = {
grab(in) match {
2016-06-06 08:26:15 +02:00
case env: InboundEnvelope if env.message.isInstanceOf[ControlMessage]
2016-05-12 08:56:28 +02:00
observers.foreach(_.notify(env))
pull(in)
case env
push(out, env)
}
}
// OutHandler
override def onPull(): Unit = pull(in)
setHandlers(in, out, this)
// ControlMessageSubject impl
2016-05-12 08:56:28 +02:00
override def attach(observer: ControlMessageObserver): Future[Done] = {
val p = Promise[Done]()
callback.invoke(Attach(observer, p))
p.future
}
2016-05-12 08:56:28 +02:00
override def detach(observer: ControlMessageObserver): Unit =
callback.invoke(Dettach(observer))
}
(logic, logic)
}
}
/**
* INTERNAL API
*/
2016-09-29 10:50:37 +02:00
private[remote] object OutboundControlJunction {
private[remote] trait OutboundControlIngress {
def sendControlMessage(message: ControlMessage): Unit
}
}
/**
* INTERNAL API
*/
2016-09-29 10:50:37 +02:00
private[remote] class OutboundControlJunction(
outboundContext: OutboundContext, outboundEnvelopePool: ObjectPool[ReusableOutboundEnvelope])
extends GraphStageWithMaterializedValue[FlowShape[OutboundEnvelope, OutboundEnvelope], OutboundControlJunction.OutboundControlIngress] {
2016-05-12 08:56:28 +02:00
import OutboundControlJunction._
val in: Inlet[OutboundEnvelope] = Inlet("OutboundControlJunction.in")
val out: Outlet[OutboundEnvelope] = Outlet("OutboundControlJunction.out")
override val shape: FlowShape[OutboundEnvelope, OutboundEnvelope] = FlowShape(in, out)
override def createLogicAndMaterializedValue(inheritedAttributes: Attributes) = {
val logic = new GraphStageLogic(shape) with InHandler with OutHandler with StageLogging with OutboundControlIngress {
2016-05-12 08:56:28 +02:00
import OutboundControlJunction._
val sendControlMessageCallback = getAsyncCallback[ControlMessage](internalSendControlMessage)
2016-09-07 10:41:36 +02:00
private val maxControlMessageBufferSize: Int = outboundContext.settings.Advanced.OutboundControlQueueSize
private val buffer = new ArrayDeque[OutboundEnvelope]
// InHandler
override def onPush(): Unit = {
if (buffer.isEmpty && isAvailable(out))
push(out, grab(in))
else
buffer.offer(grab(in))
}
// OutHandler
override def onPull(): Unit = {
if (buffer.isEmpty && !hasBeenPulled(in))
pull(in)
else if (!buffer.isEmpty)
push(out, buffer.poll())
}
private def internalSendControlMessage(message: ControlMessage): Unit = {
if (buffer.isEmpty && isAvailable(out))
push(out, wrap(message))
else if (buffer.size < maxControlMessageBufferSize)
buffer.offer(wrap(message))
else {
// it's alright to drop control messages
log.debug("Dropping control message [{}] due to full buffer.", Logging.messageClassName(message))
}
}
private def wrap(message: ControlMessage): OutboundEnvelope =
outboundEnvelopePool.acquire().init(
recipient = OptionVal.None, message = message, sender = OptionVal.None)
override def sendControlMessage(message: ControlMessage): Unit =
sendControlMessageCallback.invoke(message)
setHandlers(in, out, this)
}
(logic, logic)
}
}