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

Clientから見るFinagle

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

 Clientから見るFinagle

Avatar for AGAWA Koji

AGAWA Koji

April 01, 2016
Tweet

More Decks by AGAWA Koji

Other Decks in Programming

Transcript

  1. Your Server as a Function 1 type Service[Req, Rep] =

    Req => Future[Rep] 4 αʔό͸͜ͷؔ਺Λ࣮૷͢Δ 4 ΫϥΠΞϯτ͸͜ͷؔ਺Λར༻͢Δ 1 https://monkey.org/~marius/funsrv.pdf
  2. ؆୯ͳ HTTP αʔό val service = new Service[http.Request, http.Response] {

    def apply(req: http.Request): Future[http.Response] = { req.path match { case "/hello" => Future.value(http.Response(Status.OK, "world")) case _ => Future.value(http.Response(Status.NotFound)) } } } Await.ready(Http.server.serve(":80", service)) 4 αʔό͸ Service Λ࣮૷͢Δ 4 τϥϯεϙʔτΛҙࣝ͢Δͷ͸ serve ͢Δͱ͖͚ͩ
  3. HTTP ΫϥΠΞϯτ val client: Service[http.Request, http.Response] = Http.client.newService("example.com:80") val f:

    Future[http.Response] = client(http.Request("/hello")) 4 ΫϥΠΞϯτ͸ Service Λੜ੒ͯ͠ apply ͢Δ 4 τϥϯεϙʔτΛҙࣝ͢Δͷ͸ Service Λੜ੒͢Δͱ͖ͩ ͚
  4. Thrift ͷ৔߹ service LoggerService { string log(1: string message, 2:

    i32 logLevel); i32 getLogSize(); } 4 RPC ͷΠϯλʔϑΣΠεΛ IDL Ͱఆٛ 4 Scrooge (ίʔυδΣωϨʔλ)͕ίʔυੜ੒
  5. Thrift Server val iface = new LoggerService[Future] { def log(message:

    String, loglevel: Int): Future[String] = ??? def getLogSize(): Future[Int] = ??? } Await.ready(Thrift.server.serveIface(":4000", iface)) 4 αʔό͸ IDL ͷؔ਺Λ࣮૷͢Δ
  6. ͔͜͜Β͸ Client ͷ࿩ 4 ͜Ε·Ͱʹ 1 ߦͰੜ੒͍ͯͨ͠ Client ͷߏ଄ 4

    ಺෦࣮૷ͷ࿩Ͱ͸ͳ͍ 4 ͲΜͳ͜ͱΛ΍ͬͯ͘ΕΔͷ͔?
  7. ελοΫͷߏ੒ 1. client stack 4 ໊લղܾͱ load balancing 2. endpoint

    stack 4 circuit breaker ͱ connection pooling 3. connection stack 4 connection ϥΠϑαΠΫϧ؅ཧ 4 wire protocol ͷ࣮૷
  8. ελοΫ 4 client stack ͸ module ΛੵΈ্͛ͨ΋ͷ 4 client ͷ࡞੒࣌ʹΧελϚΠζͰ͖Δ

    4 module ͷύϥϝʔλมߋ 4 stack ͷߏ੒มߋ(module Λൈ͘ͳͲ)
  9. [Client] Observe 4 StatsReceiver - ౷ܭσʔλͷड৴ 4 σϑΥϧτ͸ͳ͠ 4 Monitor

    - ະॲཧྫ֎ͷϋϯυϦϯά 4 σϑΥϧτ͸ printStackTrace 4 Trace - ϦΫΤετ௥੻ 4 Zipkin
  10. [Client] Retries 4 ϦΫΤετͷࣦഊΛϦτϥΠ 4 σϑΥϧτͰ͸ύέοτΛૹΔલͷࣦഊΛϦτϥΠ 4 ϦτϥΠ͕᫔᫓͠ͳ͍Α͏ʹ RetryBudget ͱ

    Backoff Ͱίϯτϩʔ ϧ 4 RetryBudget - ϦτϥΠʹׂΓ౰ͯΔϦΫΤετྔ 4 Stream[Duration] - Backoff (ϦτϥΠִؒ) 4 σϑΥϧτͰ͸࠷௿ 10 retry/sec ɺ࠷େͰશϦΫΤετͷ 20% Λଈ࣌ (Backoff ͳ͠)ͰϦτϥΠ΁ׂΓ౰ͯ
  11. [Client] Session Timeouts 4 Session Timeout 4 Service/Session ͕ར༻ՄೳʹͳΔ·Ͱ଴ͭ࣌ؒ 4

    ྫ) connection pool ͕ۭ͖Λ଴ͭ࣌ؒ 4 Request Timeout 4 request Λૹ৴͔ͯ͠Β server ͷԠ౴Λ଴ͭ࣌ؒ
  12. [Client] Load Balancer 4 ෳ਺ͷ endpoint ͷͲΕ͔Λબ୒͢Δ 4 ੒ޭ཰ͷ࠷େԽͱϨΠςϯγ࠷খԽͷͨΊʹ࠷దԽ͢Δ 4

    load balancer 4 load metric -- endpoint ͕ෛՙσʔλΛఏڙ͢Δ 4 distributor -- ෛՙσʔλΛ΋ͱʹ෼ࢄ͢Δ 4 σϑΥϧτ͸ P2C ΞϧΰϦζϜͰ΋ͬͱ΋ෛՙͷ௿͍ endpoint Λબ୒͢Δ
  13. [Endpoint] Resolver 4 Endpoint ͷϦετ͸ Resolver ͕ܾఆ͢Δ 4 ϦετΛಈతʹมߋ͢Δ͜ͱ΋Ͱ͖Δ 4

    ୯ʹෳ਺ࢦఆ 4 "1.example.com:80,2.example.com:80" 4 DNS ͷෳ਺ A Ϩίʔυ 4 roundrobin.example.com:80 4 ZooKeeper 4 zk!zk.local:2181/path/to/serverset
  14. [Endpoint] Failure Accrual 4 Request ϕʔεͷ circuit breaker Ͱ open

    ͢Δͱ Load balancing ͷର৅֎ʹͳΔ 4 ࣌ؒ΢Οϯυ΢ͰͷϦΫΤετ੒ޭ཰͕ᮢ஋ΛԼճΔͱ open 4 ࿈ଓͯ͠ n ճϦΫΤετ͕ࣦഊ͢Δͱ open
  15. [Endpoint] Pool 4 ͍ΘΏΔ connection pool 4 ͍͔ͭ͘ͷ࣮૷͕͋Δ 4 SingletonPool

    - 1઀ଓ͚ͩ(Mux) 4 BufferingPool 4 WatermarkPool 4 CachingPool
  16. ·ͱΊ 4 Load Balancer ͱ Endpoint Resolver ͷ૊Έ߹Θ͕ͤڧྗ 4 Service

    Discovery ͱ૊Έ߹ΘͤΕ͹ઐ༻ LB ͕ෆཁʹͳΔ 4 ϦΫΤετͷৄࡉΛ஌͍ͬͯΔͷͰޮ཰తͳ Balancing ͕ Ͱ͖Δ 4 ଞʹ΋ݎ࿚ͳ client Λ࡞ΔͨΊʹඞཁͳ΋ͷ͕٧·͍ͬͯΔ 4 server Λ࣮૷͢ΔϑϨʔϜϫʔΫ͸ͨ͘͞Μ͋Δ͕ Finagle ͸ client ·Ͱ໘౗ΛݟΔ