2017-06-26 16:03:06 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (C) 2017 Lightbend Inc. <http://www.lightbend.com>
|
|
|
|
|
*/
|
|
|
|
|
package akka.cluster.singleton
|
|
|
|
|
|
|
|
|
|
import scala.concurrent.duration._
|
|
|
|
|
|
|
|
|
|
import com.typesafe.config.ConfigFactory
|
|
|
|
|
|
|
|
|
|
import akka.actor.{ Actor, ActorLogging, Address, PoisonPill, Props }
|
|
|
|
|
import akka.cluster.Cluster
|
|
|
|
|
|
|
|
|
|
import akka.testkit.ImplicitSender
|
|
|
|
|
import akka.remote.testkit.{ MultiNodeConfig, MultiNodeSpec, STMultiNodeSpec }
|
2017-06-26 15:03:33 +02:00
|
|
|
import akka.cluster.ClusterSettings
|
2017-06-26 16:03:06 +02:00
|
|
|
|
|
|
|
|
object TeamSingletonManagerSpec extends MultiNodeConfig {
|
|
|
|
|
val controller = role("controller")
|
|
|
|
|
val first = role("first")
|
|
|
|
|
val second = role("second")
|
|
|
|
|
val third = role("third")
|
|
|
|
|
|
|
|
|
|
commonConfig(ConfigFactory.parseString("""
|
|
|
|
|
akka.loglevel = INFO
|
|
|
|
|
akka.actor.provider = "cluster"
|
|
|
|
|
akka.actor.serialize-creators = off
|
|
|
|
|
akka.remote.log-remote-lifecycle-events = off"""))
|
|
|
|
|
|
2017-06-26 16:28:44 +02:00
|
|
|
nodeConfig(controller) {
|
|
|
|
|
ConfigFactory.parseString("""
|
|
|
|
|
akka.cluster.team = one
|
|
|
|
|
akka.cluster.roles = []""")
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-26 16:03:06 +02:00
|
|
|
nodeConfig(first) {
|
2017-06-26 16:28:44 +02:00
|
|
|
ConfigFactory.parseString("""
|
|
|
|
|
akka.cluster.team = one
|
|
|
|
|
akka.cluster.roles = [ worker ]""")
|
|
|
|
|
}
|
|
|
|
|
nodeConfig(second, third) {
|
|
|
|
|
ConfigFactory.parseString("""
|
|
|
|
|
akka.cluster.team = two
|
|
|
|
|
akka.cluster.roles = [ worker ]""")
|
2017-06-26 16:03:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TeamSingletonManagerMultiJvmNode1 extends TeamSingletonManagerSpec
|
|
|
|
|
class TeamSingletonManagerMultiJvmNode2 extends TeamSingletonManagerSpec
|
|
|
|
|
class TeamSingletonManagerMultiJvmNode3 extends TeamSingletonManagerSpec
|
|
|
|
|
class TeamSingletonManagerMultiJvmNode4 extends TeamSingletonManagerSpec
|
|
|
|
|
|
|
|
|
|
class TeamSingleton extends Actor with ActorLogging {
|
|
|
|
|
import TeamSingleton._
|
|
|
|
|
|
|
|
|
|
val cluster = Cluster(context.system)
|
|
|
|
|
|
|
|
|
|
override def receive: Receive = {
|
|
|
|
|
case Ping ⇒
|
|
|
|
|
sender() ! Pong(cluster.settings.Team, cluster.selfAddress, cluster.selfRoles)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
object TeamSingleton {
|
|
|
|
|
case object Ping
|
|
|
|
|
case class Pong(fromTeam: String, fromAddress: Address, roles: Set[String])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
abstract class TeamSingletonManagerSpec extends MultiNodeSpec(TeamSingletonManagerSpec) with STMultiNodeSpec with ImplicitSender {
|
|
|
|
|
import TeamSingletonManagerSpec._
|
|
|
|
|
|
|
|
|
|
override def initialParticipants = roles.size
|
|
|
|
|
|
|
|
|
|
val cluster = Cluster(system)
|
|
|
|
|
cluster.join(node(controller).address)
|
|
|
|
|
enterBarrier("nodes-joined")
|
|
|
|
|
|
|
|
|
|
val worker = "worker"
|
|
|
|
|
|
|
|
|
|
"A SingletonManager in a team" must {
|
|
|
|
|
"start a singleton instance for each team" in {
|
|
|
|
|
|
|
|
|
|
runOn(first, second, third) {
|
|
|
|
|
system.actorOf(
|
|
|
|
|
ClusterSingletonManager.props(
|
|
|
|
|
Props[TeamSingleton](),
|
|
|
|
|
PoisonPill,
|
|
|
|
|
ClusterSingletonManagerSettings(system).withRole(worker)),
|
|
|
|
|
"singletonManager")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val proxy = system.actorOf(ClusterSingletonProxy.props(
|
|
|
|
|
"/user/singletonManager",
|
|
|
|
|
ClusterSingletonProxySettings(system).withRole(worker)))
|
|
|
|
|
|
|
|
|
|
enterBarrier("managers-started")
|
|
|
|
|
|
|
|
|
|
proxy ! TeamSingleton.Ping
|
|
|
|
|
val pong = expectMsgType[TeamSingleton.Pong](10.seconds)
|
|
|
|
|
|
|
|
|
|
enterBarrier("pongs-received")
|
|
|
|
|
|
|
|
|
|
pong.fromTeam should equal(Cluster(system).settings.Team)
|
|
|
|
|
pong.roles should contain(worker)
|
|
|
|
|
runOn(controller, first) {
|
2017-06-26 15:03:33 +02:00
|
|
|
pong.roles should contain(ClusterSettings.TeamRolePrefix + "one")
|
2017-06-26 16:03:06 +02:00
|
|
|
}
|
|
|
|
|
runOn(second, third) {
|
2017-06-26 15:03:33 +02:00
|
|
|
pong.roles should contain(ClusterSettings.TeamRolePrefix + "two")
|
2017-06-26 16:03:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enterBarrier("after-1")
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-26 16:28:44 +02:00
|
|
|
"be able to use proxy across different team" in {
|
|
|
|
|
runOn(third) {
|
|
|
|
|
val proxy = system.actorOf(ClusterSingletonProxy.props(
|
|
|
|
|
"/user/singletonManager",
|
|
|
|
|
ClusterSingletonProxySettings(system).withRole(worker).withTeam("one")))
|
|
|
|
|
proxy ! TeamSingleton.Ping
|
|
|
|
|
val pong = expectMsgType[TeamSingleton.Pong](10.seconds)
|
|
|
|
|
pong.fromTeam should ===("one")
|
|
|
|
|
pong.roles should contain(worker)
|
2017-06-26 15:03:33 +02:00
|
|
|
pong.roles should contain(ClusterSettings.TeamRolePrefix + "one")
|
2017-06-26 16:28:44 +02:00
|
|
|
}
|
|
|
|
|
enterBarrier("after-1")
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-26 16:03:06 +02:00
|
|
|
}
|
|
|
|
|
}
|