Slide 1

Slide 1 text

΋Ζ΋ΖFINAGLE @huydx Adtech Scala Meetup

Slide 2

Slide 2 text

ࢲ͸FINAGLE ʹચ೴͞Ε· ͨ͠ ***஫ҙ***

Slide 3

Slide 3 text

FINAGLEͱ͸ • Your server as a Function (https://monkey.org/ ~marius/funsrv.pdf) • ៉ྷʹந৅Խ͞Ε͍ͯΔαʔόϑϨʔϜϫʔΫ • ࡾͭͷίϯηϓτɿ • Future / Service / Filter • [߹੒]Ͱ͖Δ͜ͱΛॏࢹ͢Δ • Future߹੒ɺService߹੒ɺFilter߹੒ • Protocol Agnostic ɿprotocolෆՄ஌తʁʁʁʁ

Slide 4

Slide 4 text

Πϝʔδͱͯ͠ type Service[Req, Rep] = Req => Future[Rep] type Filter[ReqIn, RepOut, ReqOut, RepIn] = (ReqIn, Service[ReqOut, RepIn]) => Future[RepOut]

Slide 5

Slide 5 text

ͦͷந৅࢓ํʹΑΓ • αʔόϓϩάϥϛϯάͷେମͷϩδοΫΛදͤΔ • ϚΠΫϩαʔϏε͍͢͝޲͍͍ͯΔ • Ұͭͷαʔό == ҰͭͷαʔϏε • αʔόͷࣦഊ == αʔϏεͷࣦഊɹˠ FutureͰ FailureΛ୲อͰ͖ɺFailureͷ߹੒ɺ஝ੵɺ propagationͳͲ΋Ͱ͖·͢ • ܕͰprotocolΛந৅ԽͰ͖Δ • αʔόΛ·ΔͰϨΰϒϩοΫ

Slide 6

Slide 6 text

FUTURE • େମ scalaͷඪ४Futureͱಉ͡ʢscalaͷFuture ͕Ͱ͖Δલʹଘࡏ͍ͯ͠Δʣ val xF = Future.value(1) val yF = Future.value(1) for { x <- xF y <- yF } { println(x+y) } Future.collect(Seq(xF, yF)).onSuccess(seq => println(seq.foldLeft(0)(_+_)))

Slide 7

Slide 7 text

SERVICE • Կ͔InputΛͱͬͯɺॲཧͯ͠Future(Output) Λฦ͢ͷந৅Խ • ඇಉظؔ਺ͷߟ͑ํɺRPCΛ౤͛ΔΤϯυϙΠ ϯτͱ͢Δଘࡏ val client: Service[HttpReq, HttpRep] = Http.newService("twitter.com:80") val f: Future[HttpRep] = client(HttpReq("/"))

Slide 8

Slide 8 text

FILTER • ΞϓϦέʔγϣϯͱಠཱ͍ͯ͠ΔɺαʔϏεͱ ߹੒Ͱ͖ΔσίϨʔλʔతͳ΋ͷ • ྫɿfilter , timeOut, retries, authentication class HeaderFilter extends SimpleFilter[HttpRequest, HttpResponse] { def apply(request: HttpRequest, service: Service[HttpRequest, HttpResponse]) = { // Look for a X-Request-ID header and add one if missing. val id = request.getHeader("X-Request-ID") if (id == null) { request.setHeader("X-Request-ID", UUID.randomUUID().toString) } // Add X-Processes to the response. service(request).onSuccess(r => { r.addHeader("X-Processed", "TRUE") }) } }

Slide 9

Slide 9 text

ΞυςΫϋοΫͰ࢖͍ͬͯͨFINAGLEͷػೳ • HttpServiceͰHttpΤϯυϙΠϯτͷߏ੒ • ThriftΛϓϩτίϧʹͯ͠ϕʔεService࣮૷ɺͦͷ্ Http + Thrift྆ํͷΤϯυϙΠϯτʹServiceΛόΠϯ υͩ • Twitter-server௒ศར • Finagle-redisʹΑΓඇಉظॲཧ

Slide 10

Slide 10 text

ίʔυσϞ

Slide 11

Slide 11 text

FINAGLE VS AKKA Σʢɾ□ɾʀʣ • Akka͸Ψνͳ෼ࢄγεςϜΛΰʔϧʹ͍ͯ͠Δ͕ɺͦͷ͔ΘΓʹ Finagle͸Ͳ͏΍ͬͯ៉ྷʹRPCγεςϜΛந৅ԽͰ͖Δ͔ɺͦͯ͠ Ͳ͏΍ͬͯଟ͘ͷRPCϓϩτίϧΛྲྀ༻Ͱ͖Δͷ͕ΰʔϧ • Akka͸ErlangͷActorγεςϜΛݩʹ͍ͯ͠ΔͨΊϝοηʔδͷܕ͕ ͳ͍ɺͦͷ͔ΘΓʹFinagle͸ܕΛඇৗʹॏࢹ͢Δ • ͦͷͨΊ • Reactiveܥ͕޷͖ͳਓ͸ Akka΁ • Microservice, ͋Δ͍͸៉ྷͳίʔυ͕៉ྷͳਓ͸ Finagle΁ • ྆ํ޷͖ͳਓ͸྆ํΛ࢖͑͹ʁʁʁ(ίΞͳॴ͸Akka, Ϗδωεϩ δοΫͳͱ͜Ζ͸Finagleʣ

Slide 12

Slide 12 text

ॴײ • ͍͍ͱ͜Ζ • Service, Future, Filterͷந৅Խ͕ඇৗʹ៉ྷɺશ෦ͷॲཧΛඇಉظʹ ͢ΔͨΊͷϑΝετεςοϓͱͯ͠͸͍͍͘͢͝ • twitter-server͕ϚδͰ͍͢͝ɺ࠷ߴʂ • twitter-util΋͍Ζ͍Ζ੔උͯ͠࠷ߴʂ • Eco-system΋๛͔ʢzipkin,finch,twitter-server,finagle-xxxͳͲͳͲʣ • NettyϕʔεͳͷͰωοτϫʔΫૹ৴ͳͲ΋༏लʢͷ͸ͣʣ • ΋͏ͪΐͬͱؤுͬͯཉ͍͠ͱ͜Ζ • Thriftͱͷந৅Խ͸΋͏ͪΐͬͱFinagleΆ͘ • എѹ੍ޚ͕·ͩͰ͖͍ͯͳ͍ • Ongoing: muxϓϩδΣΫτʢgc੍ޚɺdrainɺΤϥʔΤϯίʔυʣ