Slide 1

Slide 1 text

Client ͔ΒݟΔ Finagle

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Microservice built with Fiangle

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Your Server as a Function

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Your Server as a Function

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Finagle client stack

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Finagle client stack - Endpoint

Slide 24

Slide 24 text

[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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Next ? 4 High-performance RPC with Finagle/ThriftMux