Akka ã«ã¤ãã¦åå¼·ããã
çµäºã¾ã§éææ´æ°
æç§æ¸ã¯ãã
http://www.slideshare.net/scalaconfjp/scaling-software-with-akka
æ¦è¦ããèªãã§ããã
éè¦ãããªç®æã ãã¾ãã¯ã¡ã¢
Program at higher level
- å ±æããã¹ãã¼ã¿ã¹ãã¹ãã¼ã¿ã¹ã®å¯è¦æ§ãã¹ã¬ãããããã¯ãåæãã¹ã¬ããã®éç¥ãªã©çãèããªã
- ä½ã¬ãã«ã®ä¸¦ååä½*1ã¯ã·ã³ãã«ãªã¯ã¼ã¯ããã¼ã«è½ã¡ããããã°ã©ãã¯ã·ã¹ãã ã®ã¡ãã»ã¼ã¸ããã¼ã ãèããã°ããã
- é«ã¬ãã«ãªCOUã¦ã¼ãã£ãªãã£ãä½ã¬ã¤ãã³ã·ãé«ã¹ã«ã¼ãããã¨ã¹ã±ã¼ã©ããªãã£ãç°¡åã«å¾ããã
- ã¨ã©ã¼ãæ¤åºããå復ããããã®å®è¨¼æ¸ã¿ã®åªããã¢ãã«ã®æä¾
Distributable by Design
- Actor ã¯ãã±ã¼ã·ã§ã³ééæ§ã¨é å¸å¯è½ãªãã¶ã¤ã³ããã¦ãã
- ã¹ã±ã¼ã«ã¢ããã»ãã¦ã³ã容æ
- ã¯ã©ã¦ãã«ãããå®ç§ãªæ§é
- 伸縮æ§ã¨åçæ§
- ãã©ã¼ã«ããã¬ã©ã³ãã¨èªå·±ä¿®å¾©
- ã¢ãããã£ããªè² è·åæ£ãã¯ã©ã¹ã¿ã®ãªãã©ã³ã·ã³ã°ãã¢ã¯ã¿ã¼ã®ãã¤ã°ã¬ã¼ã·ã§ã³
- å®è¡æå¤æ´ã»é©ç¨å¯è½ãªã·ã¹ãã ãã極端ãªççµåã¨ããã¤ãããã¯åçæ§ã«ããå®ç¾ã
ããããAkkaãå®ç¾ããã¨ã
What is an Actor?
- ã³ã¼ãæ§æãå«ãAkkaã¦ããããActorã¨ãã
- Actorã§ã¯ã並åçãã¹ã±ã¼ã©ãã«ã§ãã©ã¼ã«ããã¬ã©ã³ããªã¢ããªã±ã¼ã·ã§ã³ãä½æããã®ã«å½¹ç«ã¡ã¾ã
- Java EE servlets 㨠session beans ã®ããã«ããã¸ãã¹ãã¸ãã¯ããæ§ã ãªæ±ºå®ãã¹ãããªã·ã¼ãåãé¢ã
- Actor ã¯å¤ãã®Javaã³ãã¥ããã£ã«ã¨ã£ã¦æ°ãããã®ãããããªããã ããé»æ°éä¿¡ã·ã¹ãã ã¨9 nines *2ã«ãã£ã¦ãé·å¹´ã«ããã£ã¦å®è¨¼ããã¦ããã³ã³ã»ãã(Hewitt 1973)ã§ã
What can I use Actors for?(ä½ã«ä½¿ãã®ï¼)
- a thread
- an object instance or component
- a callback or listener
- a singleton or service
- a router, load-balancer or pool
- a Java EE Session Bean or Message-Driven Bean
- an out-of-process service
- a Finite State Machine (FSM)
4 core Actor operations
- DEFINE
- CREATE
- SEND
- BECOME
- SUPERVISE
æ¨æ¥å¯ã¡ãã£ãã®ã§ç¶ã
CREATE
説æãããããã
import akka.actor.{ActorLogging, Props, Actor, ActorSystem} import org.slf4j.LoggerFactory case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } object Sample extends App { val logger = LoggerFactory.getLogger("timeLogging") val system = ActorSystem("SampleSystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") }
system.actorOf ã使ã£ã¦ActorRef ãå¾ãããé層æ§é ãä½ãå ´åãActor å
㧠context.actorOf 㧠Actor ãæ¾ãã°ããã
ãã®é層æ§é ã¯ãã£ã¬ã¯ããªåã®åå解決ã§ã¢ã¯ã»ã¹ã§ããã
SEND
Actor ã«ã¡ãã»ã¼ã¸ãéç¥ãããéåæãã¤ãããã¯ããã«åä½å¯è½ã ã
greeter ! Greeting("White azalea")
ROUTERS
ã¦ãã«ã¼ã¿ã£ã¦ä½ãªã®ã説æããªãã®ã§ã©ããããã®ãªã®ãï¼
http://doc.akka.io/docs/akka/snapshot/java/routing.html
ã«ã¼ã¿ã¯ãã¡ãã»ã¼ã¸ãåä¿¡ãã¦ãå¹ççã«routeesã¨ãã¦ç¥ããã¦ããä»ã®ã¢ã¯ã¿ã¼ã«ã«ã¼ãã£ã³ã°ããã¢ã¯ã¿ã¼ã§ããã
ç°ãªãã«ã¼ãã£ã³ã°æ¦ç¥ãã¢ããªã±ã¼ã·ã§ã³ã®ãã¼ãºã«å¿ãã¦ä½¿ç¨ãããã¨ãã§ããã
ä½ããã¨ãã§ããããããã©ã ã¨ãããªããããã
akka.routing.RoundRobinRouter
akka.routing.RandomRouter
akka.routing.SmallestMailboxRouter
akka.routing.BroadcastRouter
akka.routing.ScatterGatherFirstCompletedRouter
akka.routing.ConsistentHashingRouter
ã¨ãªãã¾ãååã®éãã§å®å¿ãã¾ããã
val greeter = system.actorOf( Props[GreetingActor].withRouter(RoundRobinRouter(nrOfInstances = 5)), name = "greeter")
Router+Resizer
Resizer ã¨ã¯ä½ããï¼âãã£ãã®URL
åçã«ãµã¤ãºå¤æ´å¯è½ãªã«ã¼ã¿
Dynamically Resizable Routers
rutouters ã¯ãå®æ°ã® routees ããªãµã¤ãºæ¦ç¥ãrouteesæ°ã®èª¿æ´ãè¡ããã¨ãã§ãã¾ãã
All routers can be used with a fixed number of routees or with a resize strategy to adjust the number of routees dynamically.
val system = ActorSystem("SampleSystem") val resizer = DefaultResizer(lowerBound=2, upperBound = 15) val greeter = system.actorOf( Props[GreetingActor].withRouter(RoundRobinRouter(resizer = Some(resizer))), name = "greeter")
ãããã¯è¨å®ãã¡ã¤ã«ãããããã
ããã¯ãã£ãã¯ãã
akka.actor.deployment { /greeter { router = round-robin resizer { lower-bound = 8 upper-bound = 15 } } }
å®è¡ãã¦ã¿ã
[info] Compiling 1 Scala source to C:\common\projects\AkkaSample\target\scala-2.10\classes... [info] Running Sample [INFO] [04/07/2013 18:30:32.334] [SampleSystem-akka.actor.default-dispatcher-5] [akka://SampleSystem/user/greeter/$a] Hello alpha [INFO] [04/07/2013 18:30:32.353] [SampleSystem-akka.actor.default-dispatcher-1] [akka://SampleSystem/user/greeter/$b] Hello bravo [INFO] [04/07/2013 18:30:32.354] [SampleSystem-akka.actor.default-dispatcher-1] [akka://SampleSystem/user/greeter/$b] Hello delta [INFO] [04/07/2013 18:30:32.354] [SampleSystem-akka.actor.default-dispatcher-5] [akka://SampleSystem/user/greeter/$a] Hello cherie [INFO] [04/07/2013 18:30:32.355] [SampleSystem-akka.actor.default-dispatcher-1] [akka://SampleSystem/user/greeter/$b] Hello foxtrot [INFO] [04/07/2013 18:30:32.355] [SampleSystem-akka.actor.default-dispatcher-5] [akka://SampleSystem/user/greeter/$a] Hello echo [INFO] [04/07/2013 18:30:32.356] [SampleSystem-akka.actor.default-dispatcher-5] [akka://SampleSystem/user/greeter/$a] Hello golf
BECOME
- BECOME - åçãªã¢ã¯ã¿ã¼ã®åå®ç¾©
- ã¡ãã»ã¼ã¸åä¿¡ã«ããå¿çã®ããªã¬ã¼
- In a type system analogy it is as if the object changed type - changed interface, protocol & implementation*3
- ããããã¡ãã»ã¼ã¸ã«å¯¾ãã¦ç°ãªãåå¿ã示ãã¾ã
- ãããã¯åä½ãã¹ã¿ãã¯ãããããã¯ããã·ã¥ãããããããã¨ãã§ããã
ã¯ã©ãã·ã¥èæ§
Akka ã«é¢ãã¦ããã° Java/C/C# ããã以å¤ã®æç¶ã+ãªãã¸ã§ã¯ãæåãªé²å¾¡çããã°ã©ãã³ã°ãªãã¦ããªãã
ã¦ã¼ããã®ã¹ã¬ããè½ã¡ããçµããã¨ããããã話ã«ãªã£ã¦ãã¾ãã
ã¯ã©ãã·ã¥ãããã¼ãï¼
SUPERVISE
- SUPERVISE ã¯ã»ãã®ã¢ã¯ã¿ã¼ã®è½ã¡ã管çãã
- ã¨ã©ã¼ãã³ããªã³ã°ã¯ãã®ã¹ã¼ããã¤ã¶ã¼ã«ä¸ä»»ããã
- ãããä½ãæå³ãããã¨ããã¨ãActorã«ä½ãåé¡ãçºçããå ´åããã®Supervisorã«éç¥ããã
- ããã§ã¨ã©ã¼ãã³ããªã³ã°ããããã«åé¢ã§ããããã
SUPERVISE Actor
ã²ã¨ã¤ã²ã¨ã¤ã®ã¢ã¯ã¿ã¼ã¯ãããã©ã«ãã®ã¹ã¼ãã¼ãã¤ã¶æ¦ç¥ãæã£ã¦ãã¾ããããã¯é常ã¯ååã§ããããããããã¯ä¸æ¸ããããã¨ãã§ãã¾ãã
import akka.actor.SupervisorStrategy._ override def supervisorStrategy = AllForOneStrategy(maxNrOfRetries = 10, withinTimeRange = Duration(1, "minute")) { case _: NullPointerException => Restart case _:Exception => Escalate }
åæ§ã«ãActor ã® preRestart, postRestart ãããã§ãªã
ãªã¢ã¼ãã«ãããã¤ãããã¨ãã§ãã¾ãããã
akka.actor{ provider = akka.remote.RemoteActorRefProvider deployment { /greeter { remote=akka://RemoteSystem@hostName:2552 } } }
ãããã¤ã¯ããã ãã
使ãã»ãã¯
val sample = system.actorFor("akka://SampleSystem@hostName:2552/path/to/actor")
ã¯ã©ã¹ã¿ãªã³ã°ãè¨å®ã§ããã
akka{ actor{ provider = "akka.cluster.ClusterActorRefProvider" deployment { /greeter { router = consistent-hashing nr-of-instances = 180 cluster { enabled=on max-nr-of-instances-per-node=3 allow-local-routers=on } } } } cluster { seed-nodes = { "akka://ClusterSystem1@host:2552", "akka://ClusterSystem2@host:2552", "akka://ClusterSystem3@host:2552" } auto-down = on } }