Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Finagle もろもろ

huydx
April 01, 2016

Finagle もろもろ

huydx

April 01, 2016
Tweet

More Decks by huydx

Other Decks in Programming

Transcript

  1. ΋Ζ΋ΖFINAGLE @huydx Adtech Scala Meetup

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

  3. FINAGLEͱ͸ • Your server as a Function (https://monkey.org/ ~marius/funsrv.pdf) •

    ៉ྷʹந৅Խ͞Ε͍ͯΔαʔόϑϨʔϜϫʔΫ • ࡾͭͷίϯηϓτɿ • Future / Service / Filter • [߹੒]Ͱ͖Δ͜ͱΛॏࢹ͢Δ • Future߹੒ɺService߹੒ɺFilter߹੒ • Protocol Agnostic ɿprotocolෆՄ஌తʁʁʁʁ
  4. Πϝʔδͱͯ͠ type Service[Req, Rep] = Req => Future[Rep] type Filter[ReqIn,

    RepOut, ReqOut, RepIn] = (ReqIn, Service[ReqOut, RepIn]) => Future[RepOut]
  5. ͦͷந৅࢓ํʹΑΓ • αʔόϓϩάϥϛϯάͷେମͷϩδοΫΛදͤΔ • ϚΠΫϩαʔϏε͍͢͝޲͍͍ͯΔ • Ұͭͷαʔό == ҰͭͷαʔϏε •

    αʔόͷࣦഊ == αʔϏεͷࣦഊɹˠ FutureͰ FailureΛ୲อͰ͖ɺFailureͷ߹੒ɺ஝ੵɺ propagationͳͲ΋Ͱ͖·͢ • ܕͰprotocolΛந৅ԽͰ͖Δ • αʔόΛ·ΔͰϨΰϒϩοΫ
  6. 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)(_+_)))
  7. SERVICE • Կ͔InputΛͱͬͯɺॲཧͯ͠Future(Output) Λฦ͢ͷந৅Խ • ඇಉظؔ਺ͷߟ͑ํɺRPCΛ౤͛ΔΤϯυϙΠ ϯτͱ͢Δଘࡏ val client: Service[HttpReq,

    HttpRep] = Http.newService("twitter.com:80") val f: Future[HttpRep] = client(HttpReq("/"))
  8. 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") }) } }
  9. ΞυςΫϋοΫͰ࢖͍ͬͯͨFINAGLEͷػೳ • HttpServiceͰHttpΤϯυϙΠϯτͷߏ੒ • ThriftΛϓϩτίϧʹͯ͠ϕʔεService࣮૷ɺͦͷ্ Http + Thrift྆ํͷΤϯυϙΠϯτʹServiceΛόΠϯ υͩ •

    Twitter-server௒ศར • Finagle-redisʹΑΓඇಉظॲཧ
  10. ίʔυσϞ

  11. FINAGLE VS AKKA Σʢɾ□ɾʀʣ • Akka͸Ψνͳ෼ࢄγεςϜΛΰʔϧʹ͍ͯ͠Δ͕ɺͦͷ͔ΘΓʹ Finagle͸Ͳ͏΍ͬͯ៉ྷʹRPCγεςϜΛந৅ԽͰ͖Δ͔ɺͦͯ͠ Ͳ͏΍ͬͯଟ͘ͷRPCϓϩτίϧΛྲྀ༻Ͱ͖Δͷ͕ΰʔϧ • Akka͸ErlangͷActorγεςϜΛݩʹ͍ͯ͠ΔͨΊϝοηʔδͷܕ͕

    ͳ͍ɺͦͷ͔ΘΓʹFinagle͸ܕΛඇৗʹॏࢹ͢Δ • ͦͷͨΊ • Reactiveܥ͕޷͖ͳਓ͸ Akka΁ • Microservice, ͋Δ͍͸៉ྷͳίʔυ͕៉ྷͳਓ͸ Finagle΁ • ྆ํ޷͖ͳਓ͸྆ํΛ࢖͑͹ʁʁʁ(ίΞͳॴ͸Akka, Ϗδωεϩ δοΫͳͱ͜Ζ͸Finagleʣ
  12. ॴײ • ͍͍ͱ͜Ζ • Service, Future, Filterͷந৅Խ͕ඇৗʹ៉ྷɺશ෦ͷॲཧΛඇಉظʹ ͢ΔͨΊͷϑΝετεςοϓͱͯ͠͸͍͍͘͢͝ • twitter-server͕ϚδͰ͍͢͝ɺ࠷ߴʂ

    • twitter-util΋͍Ζ͍Ζ੔උͯ͠࠷ߴʂ • Eco-system΋๛͔ʢzipkin,finch,twitter-server,finagle-xxxͳͲͳͲʣ • NettyϕʔεͳͷͰωοτϫʔΫૹ৴ͳͲ΋༏लʢͷ͸ͣʣ • ΋͏ͪΐͬͱؤுͬͯཉ͍͠ͱ͜Ζ • Thriftͱͷந৅Խ͸΋͏ͪΐͬͱFinagleΆ͘ • എѹ੍ޚ͕·ͩͰ͖͍ͯͳ͍ • Ongoing: muxϓϩδΣΫτʢgc੍ޚɺdrainɺΤϥʔΤϯίʔυʣ