2016-05-09 07:31:41 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2016 Lightbend Inc. <http://www.lightbend.com>
|
|
|
|
|
*/
|
|
|
|
|
package akka.remote.artery
|
|
|
|
|
|
2016-05-17 17:34:57 +02:00
|
|
|
import java.util.concurrent.CountDownLatch
|
|
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
|
|
2016-05-13 15:34:37 +02:00
|
|
|
import scala.annotation.tailrec
|
2016-05-17 17:34:57 +02:00
|
|
|
import scala.concurrent.Future
|
2016-05-09 07:31:41 +02:00
|
|
|
import scala.concurrent.Promise
|
2016-05-17 17:34:57 +02:00
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
import scala.concurrent.duration.FiniteDuration
|
2016-05-13 08:06:13 +02:00
|
|
|
import scala.util.Success
|
2016-05-20 12:40:56 +02:00
|
|
|
import akka.{ Done, NotUsed }
|
2016-05-09 07:31:41 +02:00
|
|
|
import akka.actor.ActorRef
|
2016-05-17 17:34:57 +02:00
|
|
|
import akka.actor.ActorSelectionMessage
|
2016-05-09 07:31:41 +02:00
|
|
|
import akka.actor.Address
|
|
|
|
|
import akka.actor.RootActorPath
|
|
|
|
|
import akka.dispatch.sysmsg.SystemMessage
|
2016-05-13 08:06:13 +02:00
|
|
|
import akka.event.Logging
|
2016-05-09 07:31:41 +02:00
|
|
|
import akka.remote.EndpointManager.Send
|
2016-05-20 12:40:56 +02:00
|
|
|
import akka.remote.{ LargeDestination, RegularDestination, RemoteActorRef, UniqueAddress }
|
2016-05-19 08:24:27 +02:00
|
|
|
import akka.remote.artery.AeronSink.GaveUpSendingException
|
2016-05-12 08:56:28 +02:00
|
|
|
import akka.remote.artery.InboundControlJunction.ControlMessageSubject
|
2016-05-13 08:06:13 +02:00
|
|
|
import akka.remote.artery.OutboundControlJunction.OutboundControlIngress
|
2016-05-17 17:34:57 +02:00
|
|
|
import akka.remote.artery.OutboundHandshake.HandshakeTimeoutException
|
|
|
|
|
import akka.remote.artery.SystemMessageDelivery.ClearSystemMessageDelivery
|
|
|
|
|
import akka.stream.AbruptTerminationException
|
2016-05-09 07:31:41 +02:00
|
|
|
import akka.stream.Materializer
|
|
|
|
|
import akka.stream.OverflowStrategy
|
2016-05-13 08:06:13 +02:00
|
|
|
import akka.stream.scaladsl.Keep
|
2016-05-09 07:31:41 +02:00
|
|
|
import akka.stream.scaladsl.Source
|
|
|
|
|
import akka.stream.scaladsl.SourceQueueWithComplete
|
2016-05-20 12:40:56 +02:00
|
|
|
import akka.util.{ Unsafe, WildcardTree }
|
2016-05-09 07:31:41 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
*
|
|
|
|
|
* Thread-safe, mutable holder for association state. Main entry point for remote destined message to a specific
|
|
|
|
|
* remote address.
|
|
|
|
|
*/
|
|
|
|
|
private[akka] class Association(
|
|
|
|
|
val transport: ArteryTransport,
|
|
|
|
|
val materializer: Materializer,
|
|
|
|
|
override val remoteAddress: Address,
|
2016-05-20 12:40:56 +02:00
|
|
|
override val controlSubject: ControlMessageSubject,
|
|
|
|
|
largeMessageDestinations: WildcardTree[NotUsed])
|
2016-05-13 08:06:13 +02:00
|
|
|
extends AbstractAssociation with OutboundContext {
|
|
|
|
|
|
|
|
|
|
private val log = Logging(transport.system, getClass.getName)
|
2016-05-13 15:34:37 +02:00
|
|
|
private val controlQueueSize = transport.provider.remoteSettings.SysMsgBufferSize
|
2016-05-09 07:31:41 +02:00
|
|
|
|
2016-05-17 17:34:57 +02:00
|
|
|
private val restartTimeout: FiniteDuration = 5.seconds // FIXME config
|
|
|
|
|
private val maxRestarts = 5 // FIXME config
|
|
|
|
|
private val restartCounter = new RestartCounter(maxRestarts, restartTimeout)
|
2016-05-20 12:40:56 +02:00
|
|
|
private val largeMessageChannelEnabled = largeMessageDestinations.children.nonEmpty
|
2016-05-17 17:34:57 +02:00
|
|
|
|
2016-05-09 07:31:41 +02:00
|
|
|
@volatile private[this] var queue: SourceQueueWithComplete[Send] = _
|
2016-05-20 12:40:56 +02:00
|
|
|
@volatile private[this] var largeQueue: SourceQueueWithComplete[Send] = _
|
2016-05-13 08:06:13 +02:00
|
|
|
@volatile private[this] var controlQueue: SourceQueueWithComplete[Send] = _
|
2016-05-12 08:56:28 +02:00
|
|
|
@volatile private[this] var _outboundControlIngress: OutboundControlIngress = _
|
2016-05-17 17:34:57 +02:00
|
|
|
@volatile private[this] var materializing = new CountDownLatch(1)
|
2016-05-11 15:55:06 +02:00
|
|
|
|
2016-05-12 08:56:28 +02:00
|
|
|
def outboundControlIngress: OutboundControlIngress = {
|
2016-05-13 08:06:13 +02:00
|
|
|
if (_outboundControlIngress ne null)
|
|
|
|
|
_outboundControlIngress
|
|
|
|
|
else {
|
|
|
|
|
// materialization not completed yet
|
|
|
|
|
materializing.await(10, TimeUnit.SECONDS)
|
|
|
|
|
if (_outboundControlIngress eq null)
|
|
|
|
|
throw new IllegalStateException("outboundControlIngress not initialized yet")
|
|
|
|
|
_outboundControlIngress
|
|
|
|
|
}
|
2016-05-11 15:55:06 +02:00
|
|
|
}
|
2016-05-09 07:31:41 +02:00
|
|
|
|
|
|
|
|
override def localAddress: UniqueAddress = transport.localAddress
|
|
|
|
|
|
2016-05-13 08:06:13 +02:00
|
|
|
/**
|
|
|
|
|
* Holds reference to shared state of Association - *access only via helper methods*
|
|
|
|
|
*/
|
|
|
|
|
@volatile
|
2016-05-13 15:34:37 +02:00
|
|
|
private[this] var _sharedStateDoNotCallMeDirectly: AssociationState = AssociationState()
|
2016-05-13 08:06:13 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper method for access to underlying state via Unsafe
|
|
|
|
|
*
|
|
|
|
|
* @param oldState Previous state
|
|
|
|
|
* @param newState Next state on transition
|
|
|
|
|
* @return Whether the previous state matched correctly
|
|
|
|
|
*/
|
|
|
|
|
@inline
|
|
|
|
|
private[this] def swapState(oldState: AssociationState, newState: AssociationState): Boolean =
|
|
|
|
|
Unsafe.instance.compareAndSwapObject(this, AbstractAssociation.sharedStateOffset, oldState, newState)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Reference to current shared state
|
|
|
|
|
*/
|
|
|
|
|
def associationState: AssociationState =
|
|
|
|
|
Unsafe.instance.getObjectVolatile(this, AbstractAssociation.sharedStateOffset).asInstanceOf[AssociationState]
|
|
|
|
|
|
|
|
|
|
override def completeHandshake(peer: UniqueAddress): Unit = {
|
|
|
|
|
require(remoteAddress == peer.address,
|
|
|
|
|
s"wrong remote address in completeHandshake, got ${peer.address}, expected ${remoteAddress}")
|
|
|
|
|
val current = associationState
|
|
|
|
|
current.uniqueRemoteAddressPromise.trySuccess(peer)
|
2016-05-13 15:34:37 +02:00
|
|
|
current.uniqueRemoteAddressValue() match {
|
2016-05-13 08:06:13 +02:00
|
|
|
case Some(Success(`peer`)) ⇒ // our value
|
|
|
|
|
case _ ⇒
|
2016-05-13 15:34:37 +02:00
|
|
|
val newState = current.newIncarnation(Promise.successful(peer))
|
2016-05-13 08:06:13 +02:00
|
|
|
if (swapState(current, newState)) {
|
2016-05-13 15:34:37 +02:00
|
|
|
current.uniqueRemoteAddressValue() match {
|
2016-05-13 08:06:13 +02:00
|
|
|
case Some(Success(old)) ⇒
|
|
|
|
|
log.debug("Incarnation {} of association to [{}] with new UID [{}] (old UID [{}])",
|
|
|
|
|
newState.incarnation, peer.address, peer.uid, old.uid)
|
2016-05-13 15:34:37 +02:00
|
|
|
case _ ⇒
|
|
|
|
|
// Failed, nothing to do
|
2016-05-13 08:06:13 +02:00
|
|
|
}
|
|
|
|
|
// if swap failed someone else completed before us, and that is fine
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-09 07:31:41 +02:00
|
|
|
}
|
|
|
|
|
|
2016-05-13 08:06:13 +02:00
|
|
|
// OutboundContext
|
|
|
|
|
override def sendControl(message: ControlMessage): Unit =
|
|
|
|
|
outboundControlIngress.sendControlMessage(message)
|
|
|
|
|
|
2016-05-09 07:31:41 +02:00
|
|
|
def send(message: Any, senderOption: Option[ActorRef], recipient: RemoteActorRef): Unit = {
|
2016-05-13 15:34:37 +02:00
|
|
|
// allow ActorSelectionMessage to pass through quarantine, to be able to establish interaction with new system
|
|
|
|
|
// FIXME where is that ActorSelectionMessage check in old remoting?
|
2016-05-17 17:34:57 +02:00
|
|
|
if (message.isInstanceOf[ActorSelectionMessage] || !associationState.isQuarantined() || message == ClearSystemMessageDelivery) {
|
2016-05-13 15:34:37 +02:00
|
|
|
// FIXME: Use a different envelope than the old Send, but make sure the new is handled by deadLetters properly
|
|
|
|
|
message match {
|
|
|
|
|
case _: SystemMessage | ClearSystemMessageDelivery ⇒
|
|
|
|
|
implicit val ec = materializer.executionContext
|
|
|
|
|
controlQueue.offer(Send(message, senderOption, recipient, None)).onFailure {
|
|
|
|
|
case e ⇒
|
|
|
|
|
quarantine(reason = s"Due to overflow of control queue, size [$controlQueueSize]")
|
|
|
|
|
}
|
|
|
|
|
case _ ⇒
|
2016-05-20 12:40:56 +02:00
|
|
|
val send = Send(message, senderOption, recipient, None)
|
|
|
|
|
if (largeMessageChannelEnabled && isLargeMessageDestination(recipient))
|
|
|
|
|
largeQueue.offer(send)
|
|
|
|
|
else
|
|
|
|
|
queue.offer(send)
|
2016-05-13 15:34:37 +02:00
|
|
|
}
|
|
|
|
|
} else if (log.isDebugEnabled)
|
|
|
|
|
log.debug("Dropping message to quarantined system {}", remoteAddress)
|
2016-05-09 07:31:41 +02:00
|
|
|
}
|
|
|
|
|
|
2016-05-20 12:40:56 +02:00
|
|
|
private def isLargeMessageDestination(recipient: ActorRef): Boolean = {
|
|
|
|
|
recipient match {
|
|
|
|
|
case r: RemoteActorRef if r.cachedLargeMessageDestinationFlag ne null ⇒ r.cachedLargeMessageDestinationFlag == LargeDestination
|
|
|
|
|
case r: RemoteActorRef ⇒
|
|
|
|
|
if (largeMessageDestinations.find(r.path.elements.iterator).data.isEmpty) {
|
|
|
|
|
r.cachedLargeMessageDestinationFlag = RegularDestination
|
|
|
|
|
false
|
|
|
|
|
} else {
|
|
|
|
|
log.debug("Using large message stream for {}", r.path)
|
|
|
|
|
r.cachedLargeMessageDestinationFlag = LargeDestination
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
case _ ⇒ false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-09 07:31:41 +02:00
|
|
|
// FIXME we should be able to Send without a recipient ActorRef
|
|
|
|
|
override val dummyRecipient: RemoteActorRef =
|
|
|
|
|
transport.provider.resolveActorRef(RootActorPath(remoteAddress) / "system" / "dummy").asInstanceOf[RemoteActorRef]
|
|
|
|
|
|
2016-05-13 15:34:37 +02:00
|
|
|
// OutboundContext
|
|
|
|
|
override def quarantine(reason: String): Unit = {
|
|
|
|
|
val uid = associationState.uniqueRemoteAddressValue() match {
|
|
|
|
|
case Some(Success(a)) ⇒ Some(a.uid)
|
|
|
|
|
case _ ⇒ None
|
|
|
|
|
}
|
|
|
|
|
quarantine(reason, uid)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@tailrec final def quarantine(reason: String, uid: Option[Int]): Unit = {
|
|
|
|
|
uid match {
|
|
|
|
|
case Some(u) ⇒
|
|
|
|
|
val current = associationState
|
|
|
|
|
current.uniqueRemoteAddressValue() match {
|
|
|
|
|
case Some(Success(peer)) if peer.uid == u ⇒
|
|
|
|
|
if (!current.isQuarantined(u)) {
|
|
|
|
|
val newState = current.newQuarantined()
|
|
|
|
|
if (swapState(current, newState)) {
|
|
|
|
|
// quarantine state change was performed
|
|
|
|
|
log.warning("Association to [{}] with UID [{}] is irrecoverably failed. Quarantining address. {}",
|
|
|
|
|
remoteAddress, u, reason)
|
|
|
|
|
// end delivery of system messages to that incarnation after this point
|
|
|
|
|
send(ClearSystemMessageDelivery, None, dummyRecipient)
|
|
|
|
|
// try to tell the other system that we have quarantined it
|
|
|
|
|
sendControl(Quarantined(localAddress, peer))
|
|
|
|
|
} else
|
|
|
|
|
quarantine(reason, uid) // recursive
|
|
|
|
|
}
|
|
|
|
|
case Some(Success(peer)) ⇒
|
|
|
|
|
log.debug("Quarantine of [{}] ignored due to non-matching UID, quarantine requested for [{}] but current is [{}]. {}",
|
|
|
|
|
remoteAddress, u, peer.uid, reason)
|
|
|
|
|
case None ⇒
|
|
|
|
|
log.debug("Quarantine of [{}] ignored because handshake not completed, quarantine request was for old incarnation. {}",
|
|
|
|
|
remoteAddress, reason)
|
|
|
|
|
}
|
|
|
|
|
case None ⇒
|
|
|
|
|
// FIXME should we do something more, old impl used gating?
|
|
|
|
|
log.warning("Quarantine of [{}] ignored because unknown UID", remoteAddress)
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-13 08:06:13 +02:00
|
|
|
}
|
2016-05-09 07:31:41 +02:00
|
|
|
|
|
|
|
|
// Idempotent
|
|
|
|
|
def associate(): Unit = {
|
2016-05-13 08:06:13 +02:00
|
|
|
if (controlQueue eq null) {
|
2016-05-17 17:34:57 +02:00
|
|
|
// it's important to materialize the outboundControl stream first,
|
|
|
|
|
// so that outboundControlIngress is ready when stages for all streams start
|
|
|
|
|
runOutboundControlStream()
|
|
|
|
|
runOutboundOrdinaryMessagesStream()
|
2016-05-20 12:40:56 +02:00
|
|
|
|
|
|
|
|
if (largeMessageChannelEnabled) {
|
|
|
|
|
runOutboundLargeMessagesStream()
|
|
|
|
|
}
|
2016-05-17 17:34:57 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def runOutboundControlStream(): Unit = {
|
|
|
|
|
// stage in the control stream may access the outboundControlIngress before returned here
|
|
|
|
|
// using CountDownLatch to make sure that materialization is completed before accessing outboundControlIngress
|
|
|
|
|
materializing = new CountDownLatch(1)
|
|
|
|
|
val (q, (control, completed)) = Source.queue(controlQueueSize, OverflowStrategy.backpressure)
|
|
|
|
|
.toMat(transport.outboundControl(this))(Keep.both)
|
|
|
|
|
.run()(materializer)
|
|
|
|
|
controlQueue = q
|
|
|
|
|
_outboundControlIngress = control
|
|
|
|
|
materializing.countDown()
|
|
|
|
|
attachStreamRestart("Outbound control stream", completed, cause ⇒ {
|
|
|
|
|
runOutboundControlStream()
|
|
|
|
|
cause match {
|
|
|
|
|
case _: HandshakeTimeoutException ⇒ // ok, quarantine not possible without UID
|
|
|
|
|
case _ ⇒ quarantine("Outbound control stream restarted")
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def runOutboundOrdinaryMessagesStream(): Unit = {
|
|
|
|
|
val (q, completed) = Source.queue(256, OverflowStrategy.dropBuffer)
|
|
|
|
|
.toMat(transport.outbound(this))(Keep.both)
|
|
|
|
|
.run()(materializer)
|
|
|
|
|
queue = q
|
|
|
|
|
attachStreamRestart("Outbound message stream", completed, _ ⇒ runOutboundOrdinaryMessagesStream())
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 12:40:56 +02:00
|
|
|
private def runOutboundLargeMessagesStream(): Unit = {
|
|
|
|
|
val (q, completed) = Source.queue(256, OverflowStrategy.dropBuffer)
|
|
|
|
|
.toMat(transport.outboundLarge(this))(Keep.both)
|
|
|
|
|
.run()(materializer)
|
|
|
|
|
largeQueue = q
|
|
|
|
|
attachStreamRestart("Outbound large message stream", completed, _ ⇒ runOutboundLargeMessagesStream())
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-17 17:34:57 +02:00
|
|
|
private def attachStreamRestart(streamName: String, streamCompleted: Future[Done], restart: Throwable ⇒ Unit): Unit = {
|
|
|
|
|
implicit val ec = materializer.executionContext
|
|
|
|
|
streamCompleted.onFailure {
|
2016-05-19 08:24:27 +02:00
|
|
|
case _ if transport.isShutdown ⇒ // don't restart after shutdown
|
2016-05-17 17:34:57 +02:00
|
|
|
case _: AbruptTerminationException ⇒ // ActorSystem shutdown
|
2016-05-19 08:24:27 +02:00
|
|
|
case cause: GaveUpSendingException ⇒
|
|
|
|
|
log.error(cause, "{} failed. Restarting it. {}", streamName, cause.getMessage)
|
|
|
|
|
// restart unconditionally, without counting restarts
|
|
|
|
|
restart(cause)
|
2016-05-17 17:34:57 +02:00
|
|
|
case cause ⇒
|
2016-05-19 08:24:27 +02:00
|
|
|
if (restartCounter.restart()) {
|
|
|
|
|
log.error(cause, "{} failed. Restarting it. {}", streamName, cause.getMessage)
|
|
|
|
|
restart(cause)
|
|
|
|
|
} else {
|
|
|
|
|
log.error(cause, "{} failed and restarted {} times within {} seconds. Terminating system. {}",
|
|
|
|
|
streamName, maxRestarts, restartTimeout.toSeconds, cause.getMessage)
|
|
|
|
|
transport.system.terminate()
|
|
|
|
|
}
|
2016-05-11 15:55:06 +02:00
|
|
|
}
|
2016-05-09 07:31:41 +02:00
|
|
|
}
|
|
|
|
|
}
|