2012-06-05 22:16:15 +02:00
|
|
|
/**
|
2017-01-04 17:37:10 +01:00
|
|
|
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
|
2012-06-05 22:16:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.cluster
|
|
|
|
|
|
2012-09-21 14:50:06 +02:00
|
|
|
import scala.concurrent.duration._
|
2012-06-05 22:16:15 +02:00
|
|
|
import akka.testkit.AkkaSpec
|
2012-07-04 14:39:27 +02:00
|
|
|
import akka.testkit.ImplicitSender
|
2012-06-05 22:16:15 +02:00
|
|
|
import akka.actor.ExtendedActorSystem
|
|
|
|
|
import akka.actor.Address
|
2012-07-06 17:04:04 +02:00
|
|
|
import akka.cluster.InternalClusterAction._
|
2012-07-05 11:56:54 +02:00
|
|
|
import java.lang.management.ManagementFactory
|
|
|
|
|
import javax.management.ObjectName
|
2017-03-14 22:31:58 +09:00
|
|
|
|
2015-05-26 09:00:40 +02:00
|
|
|
import akka.testkit.TestProbe
|
2015-09-04 08:53:36 +02:00
|
|
|
import akka.actor.ActorSystem
|
|
|
|
|
import akka.actor.Props
|
|
|
|
|
import com.typesafe.config.ConfigFactory
|
2016-12-01 18:49:38 +01:00
|
|
|
import akka.actor.CoordinatedShutdown
|
|
|
|
|
import akka.cluster.ClusterEvent.MemberEvent
|
|
|
|
|
import akka.cluster.ClusterEvent._
|
2017-03-14 22:31:58 +09:00
|
|
|
|
2016-12-01 18:49:38 +01:00
|
|
|
import scala.concurrent.Await
|
2012-06-05 22:16:15 +02:00
|
|
|
|
|
|
|
|
object ClusterSpec {
|
|
|
|
|
val config = """
|
|
|
|
|
akka.cluster {
|
2013-09-11 16:09:51 +02:00
|
|
|
auto-down-unreachable-after = 0s
|
2012-06-05 22:16:15 +02:00
|
|
|
periodic-tasks-initial-delay = 120 seconds // turn off scheduled tasks
|
2012-08-15 16:47:34 +02:00
|
|
|
publish-stats-interval = 0 s # always, when it happens
|
2012-09-06 21:48:40 +02:00
|
|
|
failure-detector.implementation-class = akka.cluster.FailureDetectorPuppet
|
2012-06-05 22:16:15 +02:00
|
|
|
}
|
2016-06-10 15:04:13 +02:00
|
|
|
akka.actor.provider = "cluster"
|
2012-09-06 21:48:40 +02:00
|
|
|
akka.remote.log-remote-lifecycle-events = off
|
2013-01-17 16:19:31 +01:00
|
|
|
akka.remote.netty.tcp.port = 0
|
2016-12-01 18:49:38 +01:00
|
|
|
akka.remote.artery.canonical.port = 0
|
2012-06-05 22:16:15 +02:00
|
|
|
"""
|
|
|
|
|
|
2014-03-07 13:20:01 +01:00
|
|
|
final case class GossipTo(address: Address)
|
2012-06-05 22:16:15 +02:00
|
|
|
}
|
|
|
|
|
|
2012-07-04 14:39:27 +02:00
|
|
|
class ClusterSpec extends AkkaSpec(ClusterSpec.config) with ImplicitSender {
|
2012-06-05 22:16:15 +02:00
|
|
|
|
2013-03-24 22:01:57 +01:00
|
|
|
val selfAddress = system.asInstanceOf[ExtendedActorSystem].provider.getDefaultAddress
|
2012-06-05 22:16:15 +02:00
|
|
|
|
2012-09-06 21:48:40 +02:00
|
|
|
val cluster = Cluster(system)
|
2012-08-16 18:28:01 +02:00
|
|
|
def clusterView = cluster.readView
|
2012-06-05 22:16:15 +02:00
|
|
|
|
2012-09-12 09:23:02 +02:00
|
|
|
def leaderActions(): Unit =
|
2012-07-04 14:39:27 +02:00
|
|
|
cluster.clusterCore ! LeaderActionsTick
|
2012-06-05 22:16:15 +02:00
|
|
|
|
|
|
|
|
"A Cluster" must {
|
|
|
|
|
|
2012-06-21 10:58:35 +02:00
|
|
|
"use the address of the remote transport" in {
|
2015-01-16 11:09:59 +01:00
|
|
|
cluster.selfAddress should ===(selfAddress)
|
2012-06-21 10:58:35 +02:00
|
|
|
}
|
|
|
|
|
|
2012-07-05 11:56:54 +02:00
|
|
|
"register jmx mbean" in {
|
|
|
|
|
val name = new ObjectName("akka:type=Cluster")
|
|
|
|
|
val info = ManagementFactory.getPlatformMBeanServer.getMBeanInfo(name)
|
2013-12-17 14:25:56 +01:00
|
|
|
info.getAttributes.length should be > (0)
|
|
|
|
|
info.getOperations.length should be > (0)
|
2012-07-05 11:56:54 +02:00
|
|
|
}
|
|
|
|
|
|
2012-06-25 21:07:44 +02:00
|
|
|
"initially become singleton cluster when joining itself and reach convergence" in {
|
2015-01-16 11:09:59 +01:00
|
|
|
clusterView.members.size should ===(0)
|
2012-06-25 21:07:44 +02:00
|
|
|
cluster.join(selfAddress)
|
2013-03-05 21:05:11 +01:00
|
|
|
leaderActions() // Joining -> Up
|
2012-08-16 18:28:01 +02:00
|
|
|
awaitCond(clusterView.isSingletonCluster)
|
2015-01-16 11:09:59 +01:00
|
|
|
clusterView.self.address should ===(selfAddress)
|
|
|
|
|
clusterView.members.map(_.address) should ===(Set(selfAddress))
|
|
|
|
|
awaitAssert(clusterView.status should ===(MemberStatus.Up))
|
2012-09-12 09:23:02 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-08 14:14:48 +01:00
|
|
|
"publish inital state as snapshot to subscribers" in {
|
|
|
|
|
try {
|
|
|
|
|
cluster.subscribe(testActor, ClusterEvent.InitialStateAsSnapshot, classOf[ClusterEvent.MemberEvent])
|
|
|
|
|
expectMsgClass(classOf[ClusterEvent.CurrentClusterState])
|
|
|
|
|
} finally {
|
|
|
|
|
cluster.unsubscribe(testActor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"publish inital state as events to subscribers" in {
|
|
|
|
|
try {
|
|
|
|
|
cluster.subscribe(testActor, ClusterEvent.InitialStateAsEvents, classOf[ClusterEvent.MemberEvent])
|
|
|
|
|
expectMsgClass(classOf[ClusterEvent.MemberUp])
|
|
|
|
|
} finally {
|
|
|
|
|
cluster.unsubscribe(testActor)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-12 09:23:02 +02:00
|
|
|
"send CurrentClusterState to one receiver when requested" in {
|
|
|
|
|
cluster.sendCurrentClusterState(testActor)
|
2013-01-14 17:35:56 +01:00
|
|
|
expectMsgClass(classOf[ClusterEvent.CurrentClusterState])
|
2012-06-05 22:16:15 +02:00
|
|
|
}
|
|
|
|
|
|
2013-12-17 14:25:56 +01:00
|
|
|
// this should be the last test step, since the cluster is shutdown
|
2013-02-11 10:40:01 +01:00
|
|
|
"publish MemberRemoved when shutdown" in {
|
2015-05-26 09:00:40 +02:00
|
|
|
val callbackProbe = TestProbe()
|
|
|
|
|
cluster.registerOnMemberRemoved(callbackProbe.ref ! "OnMemberRemoved")
|
|
|
|
|
|
2013-02-11 10:40:01 +01:00
|
|
|
cluster.subscribe(testActor, classOf[ClusterEvent.MemberRemoved])
|
|
|
|
|
// first, is in response to the subscription
|
|
|
|
|
expectMsgClass(classOf[ClusterEvent.CurrentClusterState])
|
|
|
|
|
|
|
|
|
|
cluster.shutdown()
|
2015-01-16 11:09:59 +01:00
|
|
|
expectMsgType[ClusterEvent.MemberRemoved].member.address should ===(selfAddress)
|
2015-05-26 09:00:40 +02:00
|
|
|
|
|
|
|
|
callbackProbe.expectMsg("OnMemberRemoved")
|
2013-02-11 10:40:01 +01:00
|
|
|
}
|
|
|
|
|
|
2015-09-04 08:53:36 +02:00
|
|
|
"allow join and leave with local address" in {
|
|
|
|
|
val sys2 = ActorSystem("ClusterSpec2", ConfigFactory.parseString("""
|
2016-06-10 15:04:13 +02:00
|
|
|
akka.actor.provider = "cluster"
|
2015-09-04 08:53:36 +02:00
|
|
|
akka.remote.netty.tcp.port = 0
|
2016-12-01 18:49:38 +01:00
|
|
|
akka.remote.artery.canonical.port = 0
|
2015-09-04 08:53:36 +02:00
|
|
|
"""))
|
|
|
|
|
try {
|
|
|
|
|
val ref = sys2.actorOf(Props.empty)
|
|
|
|
|
Cluster(sys2).join(ref.path.address) // address doesn't contain full address information
|
|
|
|
|
within(5.seconds) {
|
|
|
|
|
awaitAssert {
|
|
|
|
|
Cluster(sys2).state.members.size should ===(1)
|
|
|
|
|
Cluster(sys2).state.members.head.status should ===(MemberStatus.Up)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Cluster(sys2).leave(ref.path.address)
|
|
|
|
|
within(5.seconds) {
|
|
|
|
|
awaitAssert {
|
|
|
|
|
Cluster(sys2).isTerminated should ===(true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
shutdown(sys2)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-15 22:31:50 +01:00
|
|
|
"allow to resolve remotePathOf any actor" in {
|
|
|
|
|
val remotePath = cluster.remotePathOf(testActor)
|
|
|
|
|
|
|
|
|
|
testActor.path.address.host should ===(None)
|
|
|
|
|
cluster.remotePathOf(testActor).uid should ===(testActor.path.uid)
|
|
|
|
|
cluster.remotePathOf(testActor).address should ===(selfAddress)
|
|
|
|
|
}
|
2016-12-01 18:49:38 +01:00
|
|
|
|
|
|
|
|
"leave via CoordinatedShutdown.run" in {
|
|
|
|
|
val sys2 = ActorSystem("ClusterSpec2", ConfigFactory.parseString("""
|
|
|
|
|
akka.actor.provider = "cluster"
|
|
|
|
|
akka.remote.netty.tcp.port = 0
|
|
|
|
|
akka.remote.artery.canonical.port = 0
|
|
|
|
|
"""))
|
|
|
|
|
try {
|
|
|
|
|
val probe = TestProbe()(sys2)
|
|
|
|
|
Cluster(sys2).subscribe(probe.ref, classOf[MemberEvent])
|
|
|
|
|
probe.expectMsgType[CurrentClusterState]
|
|
|
|
|
Cluster(sys2).join(Cluster(sys2).selfAddress)
|
|
|
|
|
probe.expectMsgType[MemberUp]
|
|
|
|
|
|
2017-12-04 12:22:59 +01:00
|
|
|
CoordinatedShutdown(sys2).run(CoordinatedShutdown.UnknownReason)
|
2016-12-01 18:49:38 +01:00
|
|
|
probe.expectMsgType[MemberLeft]
|
2017-12-05 15:12:19 +01:00
|
|
|
// MemberExited might not be published before MemberRemoved
|
|
|
|
|
val removed = probe.fishForMessage() {
|
|
|
|
|
case _: MemberExited ⇒ false
|
|
|
|
|
case _: MemberRemoved ⇒ true
|
|
|
|
|
}.asInstanceOf[MemberRemoved]
|
|
|
|
|
removed.previousStatus should ===(MemberStatus.Exiting)
|
2016-12-01 18:49:38 +01:00
|
|
|
} finally {
|
|
|
|
|
shutdown(sys2)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-04 13:13:25 +05:30
|
|
|
"leave via CoordinatedShutdown.run when member status is Joining" in {
|
|
|
|
|
val sys2 = ActorSystem("ClusterSpec2", ConfigFactory.parseString("""
|
|
|
|
|
akka.actor.provider = "cluster"
|
|
|
|
|
akka.remote.netty.tcp.port = 0
|
|
|
|
|
akka.remote.artery.canonical.port = 0
|
|
|
|
|
akka.cluster.min-nr-of-members = 2
|
|
|
|
|
"""))
|
|
|
|
|
try {
|
|
|
|
|
val probe = TestProbe()(sys2)
|
|
|
|
|
Cluster(sys2).subscribe(probe.ref, classOf[MemberEvent])
|
|
|
|
|
probe.expectMsgType[CurrentClusterState]
|
|
|
|
|
Cluster(sys2).join(Cluster(sys2).selfAddress)
|
|
|
|
|
probe.expectMsgType[MemberJoined]
|
|
|
|
|
|
|
|
|
|
CoordinatedShutdown(sys2).run(CoordinatedShutdown.UnknownReason)
|
|
|
|
|
probe.expectMsgType[MemberLeft]
|
|
|
|
|
// MemberExited might not be published before MemberRemoved
|
|
|
|
|
val removed = probe.fishForMessage() {
|
|
|
|
|
case _: MemberExited ⇒ false
|
|
|
|
|
case _: MemberRemoved ⇒ true
|
|
|
|
|
}.asInstanceOf[MemberRemoved]
|
|
|
|
|
removed.previousStatus should ===(MemberStatus.Exiting)
|
|
|
|
|
} finally {
|
|
|
|
|
shutdown(sys2)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-01 18:49:38 +01:00
|
|
|
"terminate ActorSystem via leave (CoordinatedShutdown)" in {
|
|
|
|
|
val sys2 = ActorSystem("ClusterSpec2", ConfigFactory.parseString("""
|
|
|
|
|
akka.actor.provider = "cluster"
|
|
|
|
|
akka.remote.netty.tcp.port = 0
|
|
|
|
|
akka.remote.artery.canonical.port = 0
|
|
|
|
|
akka.coordinated-shutdown.terminate-actor-system = on
|
|
|
|
|
"""))
|
|
|
|
|
try {
|
|
|
|
|
val probe = TestProbe()(sys2)
|
|
|
|
|
Cluster(sys2).subscribe(probe.ref, classOf[MemberEvent])
|
|
|
|
|
probe.expectMsgType[CurrentClusterState]
|
|
|
|
|
Cluster(sys2).join(Cluster(sys2).selfAddress)
|
|
|
|
|
probe.expectMsgType[MemberUp]
|
|
|
|
|
|
|
|
|
|
Cluster(sys2).leave(Cluster(sys2).selfAddress)
|
|
|
|
|
probe.expectMsgType[MemberLeft]
|
2017-12-05 15:12:19 +01:00
|
|
|
// MemberExited might not be published before MemberRemoved
|
|
|
|
|
val removed = probe.fishForMessage() {
|
|
|
|
|
case _: MemberExited ⇒ false
|
|
|
|
|
case _: MemberRemoved ⇒ true
|
|
|
|
|
}.asInstanceOf[MemberRemoved]
|
|
|
|
|
removed.previousStatus should ===(MemberStatus.Exiting)
|
2016-12-01 18:49:38 +01:00
|
|
|
Await.result(sys2.whenTerminated, 10.seconds)
|
|
|
|
|
Cluster(sys2).isTerminated should ===(true)
|
2017-12-04 12:22:59 +01:00
|
|
|
CoordinatedShutdown(sys2).shutdownReason() should ===(Some(CoordinatedShutdown.ClusterLeavingReason))
|
2016-12-01 18:49:38 +01:00
|
|
|
} finally {
|
|
|
|
|
shutdown(sys2)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
"terminate ActorSystem via down (CoordinatedShutdown)" in {
|
|
|
|
|
val sys3 = ActorSystem("ClusterSpec3", ConfigFactory.parseString("""
|
|
|
|
|
akka.actor.provider = "cluster"
|
|
|
|
|
akka.remote.netty.tcp.port = 0
|
|
|
|
|
akka.remote.artery.canonical.port = 0
|
|
|
|
|
akka.coordinated-shutdown.terminate-actor-system = on
|
|
|
|
|
akka.cluster.run-coordinated-shutdown-when-down = on
|
|
|
|
|
akka.loglevel=DEBUG
|
|
|
|
|
"""))
|
|
|
|
|
try {
|
|
|
|
|
val probe = TestProbe()(sys3)
|
|
|
|
|
Cluster(sys3).subscribe(probe.ref, classOf[MemberEvent])
|
|
|
|
|
probe.expectMsgType[CurrentClusterState]
|
|
|
|
|
Cluster(sys3).join(Cluster(sys3).selfAddress)
|
|
|
|
|
probe.expectMsgType[MemberUp]
|
|
|
|
|
|
|
|
|
|
Cluster(sys3).down(Cluster(sys3).selfAddress)
|
|
|
|
|
probe.expectMsgType[MemberRemoved]
|
|
|
|
|
Await.result(sys3.whenTerminated, 10.seconds)
|
|
|
|
|
Cluster(sys3).isTerminated should ===(true)
|
2017-12-04 12:22:59 +01:00
|
|
|
CoordinatedShutdown(sys3).shutdownReason() should ===(Some(CoordinatedShutdown.ClusterDowningReason))
|
2016-12-01 18:49:38 +01:00
|
|
|
} finally {
|
|
|
|
|
shutdown(sys3)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-14 22:31:58 +09:00
|
|
|
|
|
|
|
|
"register multiple cluster JMX MBeans with akka.cluster.jmx.multi-mbeans-in-same-jvm = on" in {
|
|
|
|
|
def getConfig = (port: Int) ⇒ ConfigFactory.parseString(
|
|
|
|
|
s"""
|
|
|
|
|
akka.cluster.jmx.multi-mbeans-in-same-jvm = on
|
|
|
|
|
akka.remote.netty.tcp.port = ${port}
|
|
|
|
|
akka.remote.artery.canonical.port = ${port}
|
|
|
|
|
"""
|
|
|
|
|
).withFallback(ConfigFactory.parseString(ClusterSpec.config))
|
|
|
|
|
|
|
|
|
|
val sys1 = ActorSystem("ClusterSpec4", getConfig(2552))
|
|
|
|
|
val sys2 = ActorSystem("ClusterSpec4", getConfig(2553))
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Cluster(sys1)
|
|
|
|
|
Cluster(sys2)
|
|
|
|
|
|
|
|
|
|
val name1 = new ObjectName(s"akka:type=Cluster,port=2552")
|
|
|
|
|
val info1 = ManagementFactory.getPlatformMBeanServer.getMBeanInfo(name1)
|
|
|
|
|
info1.getAttributes.length should be > (0)
|
|
|
|
|
info1.getOperations.length should be > (0)
|
|
|
|
|
|
|
|
|
|
val name2 = new ObjectName(s"akka:type=Cluster,port=2553")
|
|
|
|
|
val info2 = ManagementFactory.getPlatformMBeanServer.getMBeanInfo(name2)
|
|
|
|
|
info2.getAttributes.length should be > (0)
|
|
|
|
|
info2.getOperations.length should be > (0)
|
|
|
|
|
} finally {
|
|
|
|
|
shutdown(sys1)
|
|
|
|
|
shutdown(sys2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2012-06-05 22:16:15 +02:00
|
|
|
}
|
2017-10-06 10:30:28 +02:00
|
|
|
}
|