2016-05-09 07:31:41 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2016 Lightbend Inc. <http://www.lightbend.com>
|
|
|
|
|
*/
|
|
|
|
|
package akka.remote.artery
|
|
|
|
|
|
2016-05-13 15:34:37 +02:00
|
|
|
import scala.annotation.tailrec
|
2016-05-09 07:31:41 +02:00
|
|
|
import scala.concurrent.Promise
|
2016-05-13 08:06:13 +02:00
|
|
|
import scala.util.Success
|
2016-05-09 07:31:41 +02:00
|
|
|
import akka.actor.ActorRef
|
|
|
|
|
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
|
|
|
|
|
import akka.remote.RemoteActorRef
|
|
|
|
|
import akka.remote.UniqueAddress
|
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-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-13 08:06:13 +02:00
|
|
|
import akka.util.Unsafe
|
|
|
|
|
import java.util.concurrent.locks.ReentrantLock
|
|
|
|
|
import java.util.concurrent.CountDownLatch
|
|
|
|
|
import java.util.concurrent.TimeUnit
|
2016-05-13 15:34:37 +02:00
|
|
|
import akka.actor.ActorSelectionMessage
|
|
|
|
|
import akka.remote.artery.SystemMessageDelivery.ClearSystemMessageDelivery
|
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-13 08:06:13 +02:00
|
|
|
override val controlSubject: ControlMessageSubject)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
@volatile private[this] var queue: 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-13 08:06:13 +02:00
|
|
|
private val 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?
|
|
|
|
|
if (message.isInstanceOf[ActorSelectionMessage] || !associationState.isQuarantined() || message.isInstanceOf[ClearSystemMessageDelivery.type]) {
|
|
|
|
|
// 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 _ ⇒
|
|
|
|
|
queue.offer(Send(message, senderOption, recipient, None))
|
|
|
|
|
}
|
|
|
|
|
} else if (log.isDebugEnabled)
|
|
|
|
|
log.debug("Dropping message to quarantined system {}", remoteAddress)
|
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 = {
|
|
|
|
|
// FIXME detect and handle stream failure, e.g. handshake timeout
|
2016-05-13 08:06:13 +02:00
|
|
|
|
|
|
|
|
// it's important to materialize the outboundControl stream first,
|
|
|
|
|
// so that outboundControlIngress is ready when stages for all streams start
|
|
|
|
|
if (controlQueue eq null) {
|
2016-05-13 15:34:37 +02:00
|
|
|
val (q, control) = Source.queue(controlQueueSize, OverflowStrategy.backpressure)
|
2016-05-12 08:56:28 +02:00
|
|
|
.toMat(transport.outboundControl(this))(Keep.both)
|
2016-05-11 15:55:06 +02:00
|
|
|
.run()(materializer)
|
2016-05-13 08:06:13 +02:00
|
|
|
controlQueue = q
|
2016-05-12 08:56:28 +02:00
|
|
|
_outboundControlIngress = control
|
2016-05-13 08:06:13 +02:00
|
|
|
// 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.countDown()
|
|
|
|
|
|
|
|
|
|
queue = Source.queue(256, OverflowStrategy.dropBuffer)
|
|
|
|
|
.to(transport.outbound(this)).run()(materializer)
|
2016-05-11 15:55:06 +02:00
|
|
|
}
|
2016-05-09 07:31:41 +02:00
|
|
|
}
|
|
|
|
|
}
|