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
|
2012-09-21 11:47:50 +02:00
|
|
|
import org.scalatest.matchers.MustMatchers
|
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
|
2013-01-14 14:09:53 +01:00
|
|
|
import akka.contrib.pattern.ClusterSingletonManager
|
2012-09-13 10:54:14 +02:00
|
|
|
import akka.cluster.Cluster
|
|
|
|
|
import akka.cluster.Member
|
|
|
|
|
import akka.cluster.MemberStatus
|
|
|
|
|
import akka.cluster.ClusterEvent.CurrentClusterState
|
|
|
|
|
import akka.cluster.ClusterEvent.MemberUp
|
|
|
|
|
import akka.remote.testkit.MultiNodeConfig
|
|
|
|
|
import akka.remote.testkit.MultiNodeSpec
|
|
|
|
|
import akka.testkit.ImplicitSender
|
|
|
|
|
|
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-02-25 14:39:47 +01:00
|
|
|
val third = role("third")
|
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]
|
2012-11-08 18:49:54 +01:00
|
|
|
# don't use sigar for tests, native lib not in path
|
|
|
|
|
akka.cluster.metrics.collector-class = akka.cluster.JmxMetricsCollector
|
2012-09-13 10:54:14 +02:00
|
|
|
#//#router-deploy-config
|
|
|
|
|
akka.actor.deployment {
|
2013-01-14 14:09:53 +01:00
|
|
|
/singleton/statsService/workerRouter {
|
2012-09-20 12:58:51 +02:00
|
|
|
router = consistent-hashing
|
2012-09-13 10:54:14 +02:00
|
|
|
nr-of-instances = 100
|
|
|
|
|
cluster {
|
|
|
|
|
enabled = on
|
|
|
|
|
max-nr-of-instances-per-node = 3
|
|
|
|
|
allow-local-routees = off
|
2013-03-14 20:32:43 +01:00
|
|
|
use-role = compute
|
2012-09-13 10:54:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#//#router-deploy-config
|
|
|
|
|
"""))
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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-08-19 12:06:17 +02:00
|
|
|
with WordSpecLike with MustMatchers 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()
|
|
|
|
|
|
2012-09-13 10:54:14 +02:00
|
|
|
"The 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-04-19 07:54:23 +02:00
|
|
|
receiveN(3).collect { case MemberUp(m) => m.address }.toSet must be (
|
|
|
|
|
Set(firstAddress, secondAddress, thirdAddress))
|
2012-09-13 10:54:14 +02:00
|
|
|
|
|
|
|
|
Cluster(system).unsubscribe(testActor)
|
2013-01-14 14:09:53 +01:00
|
|
|
|
2013-04-17 22:14:19 +02:00
|
|
|
system.actorOf(ClusterSingletonManager.props(
|
2013-09-10 13:35:51 +02:00
|
|
|
singletonProps = Props[StatsService], singletonName = "statsService",
|
2013-04-17 22:14:19 +02:00
|
|
|
terminationMessage = PoisonPill, role = Some("compute")), name = "singleton")
|
2013-01-14 14:09:53 +01:00
|
|
|
|
2012-11-22 16:09:19 +01:00
|
|
|
system.actorOf(Props[StatsFacade], "statsFacade")
|
2012-09-13 10:54:14 +02:00
|
|
|
|
|
|
|
|
testConductor.enter("all-up")
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-14 14:09:53 +01:00
|
|
|
"show usage of the statsFacade" in within(40 seconds) {
|
2013-03-26 18:17:50 +01:00
|
|
|
val facade = system.actorSelection(RootActorPath(node(third).address) / "user" / "statsFacade")
|
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 {
|
2012-09-13 10:54:14 +02:00
|
|
|
facade ! StatsJob("this is the text that will be analyzed")
|
2013-05-29 17:20:18 +02:00
|
|
|
expectMsgType[StatsResult](1.second).meanWordLength must be(
|
|
|
|
|
3.875 plusOrMinus 0.001)
|
2012-09-13 10:54:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testConductor.enter("done")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|