2012-09-13 10:54:14 +02:00
|
|
|
package sample.cluster.stats
|
|
|
|
|
|
|
|
|
|
import language.postfixOps
|
2012-09-21 14:50:06 +02:00
|
|
|
import scala.concurrent.duration._
|
2012-09-13 10:54:14 +02:00
|
|
|
import com.typesafe.config.ConfigFactory
|
2012-09-21 11:47:50 +02:00
|
|
|
import org.scalatest.BeforeAndAfterAll
|
2013-08-19 12:06:17 +02:00
|
|
|
import org.scalatest.WordSpecLike
|
2013-12-17 14:25:56 +01:00
|
|
|
import org.scalatest.Matchers
|
2013-01-14 14:09:53 +01:00
|
|
|
import akka.actor.PoisonPill
|
2012-09-13 10:54:14 +02:00
|
|
|
import akka.actor.Props
|
|
|
|
|
import akka.actor.RootActorPath
|
|
|
|
|
import akka.cluster.Cluster
|
|
|
|
|
import akka.cluster.Member
|
|
|
|
|
import akka.cluster.MemberStatus
|
|
|
|
|
import akka.cluster.ClusterEvent.CurrentClusterState
|
|
|
|
|
import akka.cluster.ClusterEvent.MemberUp
|
2015-04-27 14:25:10 +02:00
|
|
|
import akka.cluster.singleton.ClusterSingletonManager
|
2015-04-29 18:23:45 +02:00
|
|
|
import akka.cluster.singleton.ClusterSingletonManagerSettings
|
2015-04-27 14:25:10 +02:00
|
|
|
import akka.cluster.singleton.ClusterSingletonProxy
|
2012-09-13 10:54:14 +02:00
|
|
|
import akka.remote.testkit.MultiNodeConfig
|
|
|
|
|
import akka.remote.testkit.MultiNodeSpec
|
|
|
|
|
import akka.testkit.ImplicitSender
|
2013-11-29 16:27:23 +01:00
|
|
|
import sample.cluster.stats.StatsMessages._
|
2015-04-29 18:23:45 +02:00
|
|
|
import akka.cluster.singleton.ClusterSingletonProxySettings
|
2012-09-13 10:54:14 +02:00
|
|
|
|
2012-09-21 11:47:50 +02:00
|
|
|
object StatsSampleSingleMasterSpecConfig extends MultiNodeConfig {
|
2012-09-13 10:54:14 +02:00
|
|
|
// register the named roles (nodes) of the test
|
|
|
|
|
val first = role("first")
|
|
|
|
|
val second = role("second")
|
2013-11-29 16:27:23 +01:00
|
|
|
val third = role("thrid")
|
2012-09-13 10:54:14 +02:00
|
|
|
|
2014-12-12 11:49:32 -06:00
|
|
|
def nodeList = Seq(first, second, third)
|
|
|
|
|
|
|
|
|
|
// Extract individual sigar library for every node.
|
|
|
|
|
nodeList foreach { role ⇒
|
|
|
|
|
nodeConfig(role) {
|
|
|
|
|
ConfigFactory.parseString(s"""
|
|
|
|
|
# Disable legacy metrics in akka-cluster.
|
|
|
|
|
akka.cluster.metrics.enabled=off
|
|
|
|
|
# Enable metrics extension in akka-cluster-metrics.
|
|
|
|
|
akka.extensions=["akka.cluster.metrics.ClusterMetricsExtension"]
|
|
|
|
|
# Sigar native library extract location during tests.
|
|
|
|
|
akka.cluster.metrics.native-library-extract-folder=target/native/${role.name}
|
|
|
|
|
""")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-13 10:54:14 +02:00
|
|
|
// this configuration will be used for all nodes
|
|
|
|
|
// note that no fixed host names and ports are used
|
|
|
|
|
commonConfig(ConfigFactory.parseString("""
|
|
|
|
|
akka.loglevel = INFO
|
|
|
|
|
akka.actor.provider = "akka.cluster.ClusterActorRefProvider"
|
|
|
|
|
akka.remote.log-remote-lifecycle-events = off
|
2013-03-14 20:32:43 +01:00
|
|
|
akka.cluster.roles = [compute]
|
2014-03-14 16:32:54 +01:00
|
|
|
#//#router-deploy-config
|
2012-09-13 10:54:14 +02:00
|
|
|
akka.actor.deployment {
|
2013-01-14 14:09:53 +01:00
|
|
|
/singleton/statsService/workerRouter {
|
2013-09-19 08:00:05 +02:00
|
|
|
router = consistent-hashing-pool
|
2012-09-13 10:54:14 +02:00
|
|
|
nr-of-instances = 100
|
|
|
|
|
cluster {
|
|
|
|
|
enabled = on
|
|
|
|
|
max-nr-of-instances-per-node = 3
|
2013-11-29 16:27:23 +01:00
|
|
|
allow-local-routees = on
|
2013-03-14 20:32:43 +01:00
|
|
|
use-role = compute
|
2012-09-13 10:54:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-14 16:32:54 +01:00
|
|
|
#//#router-deploy-config
|
2012-09-13 10:54:14 +02:00
|
|
|
"""))
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// need one concrete test class per node
|
2012-09-21 11:47:50 +02:00
|
|
|
class StatsSampleSingleMasterSpecMultiJvmNode1 extends StatsSampleSingleMasterSpec
|
|
|
|
|
class StatsSampleSingleMasterSpecMultiJvmNode2 extends StatsSampleSingleMasterSpec
|
|
|
|
|
class StatsSampleSingleMasterSpecMultiJvmNode3 extends StatsSampleSingleMasterSpec
|
2012-09-13 10:54:14 +02:00
|
|
|
|
2012-09-21 11:47:50 +02:00
|
|
|
abstract class StatsSampleSingleMasterSpec extends MultiNodeSpec(StatsSampleSingleMasterSpecConfig)
|
2013-12-17 14:25:56 +01:00
|
|
|
with WordSpecLike with Matchers with BeforeAndAfterAll with ImplicitSender {
|
2012-09-13 10:54:14 +02:00
|
|
|
|
2012-09-21 11:47:50 +02:00
|
|
|
import StatsSampleSingleMasterSpecConfig._
|
2012-09-13 10:54:14 +02:00
|
|
|
|
|
|
|
|
override def initialParticipants = roles.size
|
|
|
|
|
|
2012-09-21 11:47:50 +02:00
|
|
|
override def beforeAll() = multiNodeSpecBeforeAll()
|
|
|
|
|
|
|
|
|
|
override def afterAll() = multiNodeSpecAfterAll()
|
|
|
|
|
|
2013-11-29 16:27:23 +01:00
|
|
|
"The japi stats sample with single master" must {
|
2012-11-15 08:28:35 +01:00
|
|
|
"illustrate how to startup cluster" in within(15 seconds) {
|
2012-09-13 10:54:14 +02:00
|
|
|
Cluster(system).subscribe(testActor, classOf[MemberUp])
|
|
|
|
|
expectMsgClass(classOf[CurrentClusterState])
|
|
|
|
|
|
2013-04-11 09:18:12 +02:00
|
|
|
val firstAddress = node(first).address
|
|
|
|
|
val secondAddress = node(second).address
|
|
|
|
|
val thirdAddress = node(third).address
|
2012-09-13 10:54:14 +02:00
|
|
|
|
2013-04-11 09:18:12 +02:00
|
|
|
Cluster(system) join firstAddress
|
|
|
|
|
|
2013-12-17 14:25:56 +01:00
|
|
|
receiveN(3).collect { case MemberUp(m) => m.address }.toSet should be(
|
2013-10-23 16:51:59 +02:00
|
|
|
Set(firstAddress, secondAddress, thirdAddress))
|
2012-09-13 10:54:14 +02:00
|
|
|
|
|
|
|
|
Cluster(system).unsubscribe(testActor)
|
2013-01-14 14:09:53 +01:00
|
|
|
|
2015-04-29 18:23:45 +02:00
|
|
|
system.actorOf(ClusterSingletonManager.props(
|
2013-11-29 16:27:23 +01:00
|
|
|
Props[StatsService],
|
|
|
|
|
terminationMessage = PoisonPill,
|
2015-04-29 18:23:45 +02:00
|
|
|
settings = ClusterSingletonManagerSettings(system).withSingletonName("statsService")),
|
|
|
|
|
name = "singleton")
|
2013-01-14 14:09:53 +01:00
|
|
|
|
2015-04-29 18:23:45 +02:00
|
|
|
system.actorOf(ClusterSingletonProxy.props("/user/singleton/statsService",
|
|
|
|
|
ClusterSingletonProxySettings(system).withRole("compute")), "statsServiceProxy")
|
2012-09-13 10:54:14 +02:00
|
|
|
|
|
|
|
|
testConductor.enter("all-up")
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-14 16:32:54 +01:00
|
|
|
"show usage of the statsServiceProxy" in within(40 seconds) {
|
|
|
|
|
val proxy = system.actorSelection(RootActorPath(node(third).address) / "user" / "statsServiceProxy")
|
2012-09-13 10:54:14 +02:00
|
|
|
|
|
|
|
|
// eventually the service should be ok,
|
2012-10-24 17:59:49 +02:00
|
|
|
// service and worker nodes might not be up yet
|
2013-03-26 18:17:50 +01:00
|
|
|
awaitAssert {
|
2014-03-14 16:32:54 +01:00
|
|
|
proxy ! new StatsJob("this is the text that will be analyzed")
|
2013-12-17 14:25:56 +01:00
|
|
|
expectMsgType[StatsResult](1.second).getMeanWordLength should be(3.875 +- 0.001)
|
2012-09-13 10:54:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testConductor.enter("done")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-29 18:23:45 +02:00
|
|
|
}
|