$30 off During Our Annual Pro Sale. View Details »

Clientから見るFinagle

 Clientから見るFinagle

AGAWA Koji

April 01, 2016
Tweet

More Decks by AGAWA Koji

Other Decks in Programming

Transcript

  1. Client ͔ΒݟΔ
    Finagle

    View Slide

  2. Finagle?
    4 ϑΟωʔάϧ
    4 Twitter ੡
    4 ϓϩτίϧதཱͳ RPC ϑϨʔϜϫʔΫ

    View Slide

  3. ϓϩτίϧதཱ
    4 Finagle ͸ʮRPC ࣮૷ʯͦͷ΋Ͱ͸ Ͱ͸ͳ͍
    4 Apache Thrift
    4 Apache Avro
    4 Google Protocol Buffer

    View Slide

  4. Bulitin protocols
    Server and Client
    4 HTTP
    4 Thrift
    Client only
    4 MySQL
    4 Redis
    4 Memcached

    View Slide

  5. Microservice built with Fiangle

    View Slide

  6. Your Server as a Function 1
    type Service[Req, Rep] = Req => Future[Rep]
    4 αʔό͸͜ͷؔ਺Λ࣮૷͢Δ
    4 ΫϥΠΞϯτ͸͜ͷؔ਺Λར༻͢Δ
    1 https://monkey.org/~marius/funsrv.pdf

    View Slide

  7. ؆୯ͳ 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 ͢Δͱ͖͚ͩ

    View Slide

  8. 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 Λੜ੒͢Δͱ͖ͩ
    ͚

    View Slide

  9. Your Server as a Function

    View Slide

  10. Thrift ͷ৔߹
    service LoggerService {
    string log(1: string message, 2: i32 logLevel);
    i32 getLogSize();
    }
    4 RPC ͷΠϯλʔϑΣΠεΛ IDL Ͱఆٛ
    4 Scrooge (ίʔυδΣωϨʔλ)͕ίʔυੜ੒

    View Slide

  11. 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 ͷؔ਺Λ࣮૷͢Δ

    View Slide

  12. Thrift Client
    val iface = Thrift.client.newIface[LoggerService[Future]]("example.com:4000")
    val r: Future[String] = iface.log("hello", 1)
    4 ΫϥΠΞϯτ͸ IDL ͷؔ਺Λར༻Ͱ͖Δ

    View Slide

  13. Your Server as a Function

    View Slide

  14. ͔͜͜Β͸ Client ͷ࿩
    4 ͜Ε·Ͱʹ 1 ߦͰੜ੒͍ͯͨ͠ Client ͷߏ଄
    4 ಺෦࣮૷ͷ࿩Ͱ͸ͳ͍
    4 ͲΜͳ͜ͱΛ΍ͬͯ͘ΕΔͷ͔?

    View Slide

  15. Finagle client stack

    View Slide

  16. ελοΫͷߏ੒
    1. client stack
    4 ໊લղܾͱ load balancing
    2. endpoint stack
    4 circuit breaker ͱ connection pooling
    3. connection stack
    4 connection ϥΠϑαΠΫϧ؅ཧ
    4 wire protocol ͷ࣮૷

    View Slide

  17. ελοΫ
    4 client stack ͸ module ΛੵΈ্͛ͨ΋ͷ
    4 client ͷ࡞੒࣌ʹΧελϚΠζͰ͖Δ
    4 module ͷύϥϝʔλมߋ
    4 stack ͷߏ੒มߋ(module Λൈ͘ͳͲ)

    View Slide

  18. [Client] Observe
    4 StatsReceiver - ౷ܭσʔλͷड৴
    4 σϑΥϧτ͸ͳ͠
    4 Monitor - ະॲཧྫ֎ͷϋϯυϦϯά
    4 σϑΥϧτ͸ printStackTrace
    4 Trace - ϦΫΤετ௥੻
    4 Zipkin

    View Slide

  19. [Client] Retries
    4 ϦΫΤετͷࣦഊΛϦτϥΠ
    4 σϑΥϧτͰ͸ύέοτΛૹΔલͷࣦഊΛϦτϥΠ
    4 ϦτϥΠ͕᫔᫓͠ͳ͍Α͏ʹ RetryBudget ͱ Backoff Ͱίϯτϩʔ
    ϧ
    4 RetryBudget - ϦτϥΠʹׂΓ౰ͯΔϦΫΤετྔ
    4 Stream[Duration] - Backoff (ϦτϥΠִؒ)
    4 σϑΥϧτͰ͸࠷௿ 10 retry/sec ɺ࠷େͰશϦΫΤετͷ 20% Λଈ࣌
    (Backoff ͳ͠)ͰϦτϥΠ΁ׂΓ౰ͯ

    View Slide

  20. [Client] Session Timeouts
    4 Session Timeout
    4 Service/Session ͕ར༻ՄೳʹͳΔ·Ͱ଴ͭ࣌ؒ
    4 ྫ) connection pool ͕ۭ͖Λ଴ͭ࣌ؒ
    4 Request Timeout
    4 request Λૹ৴͔ͯ͠Β server ͷԠ౴Λ଴ͭ࣌ؒ

    View Slide

  21. [Client] Request Draining
    4 Client ͷ close ࣌ʹ࣮ߦதͷ Request ͕׬ྃ͢ΔͷΛ଴
    ͭ

    View Slide

  22. [Client] Load Balancer
    4 ෳ਺ͷ endpoint ͷͲΕ͔Λબ୒͢Δ
    4 ੒ޭ཰ͷ࠷େԽͱϨΠςϯγ࠷খԽͷͨΊʹ࠷దԽ͢Δ
    4 load balancer
    4 load metric -- endpoint ͕ෛՙσʔλΛఏڙ͢Δ
    4 distributor -- ෛՙσʔλΛ΋ͱʹ෼ࢄ͢Δ
    4 σϑΥϧτ͸ P2C ΞϧΰϦζϜͰ΋ͬͱ΋ෛՙͷ௿͍
    endpoint Λબ୒͢Δ

    View Slide

  23. Finagle client stack - Endpoint

    View Slide

  24. [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

    View Slide

  25. [Endpoint] Failure Accrual
    4 Request ϕʔεͷ circuit breaker Ͱ open ͢Δͱ Load
    balancing ͷର৅֎ʹͳΔ
    4 ࣌ؒ΢Οϯυ΢ͰͷϦΫΤετ੒ޭ཰͕ᮢ஋ΛԼճΔͱ
    open
    4 ࿈ଓͯ͠ n ճϦΫΤετ͕ࣦഊ͢Δͱ open

    View Slide

  26. [Endpoint] Pool
    4 ͍ΘΏΔ connection pool
    4 ͍͔ͭ͘ͷ࣮૷͕͋Δ
    4 SingletonPool - 1઀ଓ͚ͩ(Mux)
    4 BufferingPool
    4 WatermarkPool
    4 CachingPool

    View Slide

  27. [Endpoint] Fail Fast
    4 Connection ϕʔεͷ circuit breaker
    4 ઀ଓ͕੾Εͨͱ͖ open ͢Δ
    4 ཪͰ઀ଓΛϦτϥΠͯ͠ܨΕ͹ close

    View Slide

  28. [Connection] Expiration
    4 ઀ଓͷ༗ޮظؒΛ੍ݶ
    4 maxLifeTime -- connection ͷ࠷େੜଘ࣌ؒ
    4 maxIdleTime -- connection ͷ࠷େΞΠυϧ࣌ؒ

    View Slide

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

    View Slide

  30. Next ?
    4 High-performance RPC with Finagle/ThriftMux

    View Slide