Slide 1

Slide 1 text

Introduction of finagle 2015 08 01 Scala kansai (Reprint at Oct 14 2017)

Slide 2

Slide 2 text

Who I am ● iwag / @iwag_org ● Search Team at Dwango Corp. ○ Scala Experience : 1 and half year ● Person who doesn’t care about the compilation time in Scala ● Other technical interests: ○ C++11, Ruby, Elasticsearch, Java8 ○

Slide 3

Slide 3 text

Overview ● Introduction finagle ● Case study (next presentation) ● Impressions/lessons (next presentation)

Slide 4

Slide 4 text

What is finagle? ● RPC library in Scala developed by twitter, which attends to extend Future

Slide 5

Slide 5 text

(Short) history of finalge ● Launched twitter in early 2007 ● (at the moment) Ruby on Rails, monolithic application ● A crisis (often appeared whale!) in 2009 ○ The twitter user soared and broke! ○ Make an effort to change scalable system instead of Rails ● System reform Ruby to Scala(Java) from 2011 ○ https://blog.twitter.com/2011/finagle-a-protocol-agnostic-rpc-syt em ○ http://readwrite.com/2011/07/06/twitter-java-scala ○ Scala 2.9.x era

Slide 6

Slide 6 text

Finagle is not like ● Web application framework like Play framework, rails ○ Play provides a bunch of features but finagle does only a few ● Network, Distributed System Framework ● A wrapper of netty ● Mysterious functions ○ Stats, serversets...

Slide 7

Slide 7 text

Central dogma of finagle ● Future/Service/Filter ● Stacks using those concepts ○ Finagle-core ○ Finagle-http ○ finagle-thrift

Slide 8

Slide 8 text

Future ● Type with value available in Future ○ twitter ‘s developers inspired from Facebook’s C++ library ● Nowadays, Future is a general knowledge for Scala developers, though… ○ Http://docs.scala-lang.org/en/overviews/core/futures.html ○ Fairly different from twitter Future and Scala Future

Slide 9

Slide 9 text

Service ● Class with a function that returns Future[Rep] and receive Req abstract class Service [Req, Rep] { def apply (req: Req): Future [Rep] }

Slide 10

Slide 10 text

For Instance of Service ● HTTP Server ○ Service[HttpRequest, HttpResponse] ● Controller in WAF: Service[HttpRequest, HttpResponse] ● MySQL client: Service[MySQLRequest, MySQLResponse] ●

Slide 11

Slide 11 text

Simple server class Hello extends Service [Request, Response] { override def apply (request: Request): Future [Response] = { val resp = Response () resp.contentString = "Hello" Future (resp) }} object Foo extends App { def main = { Await.all (Httpx.serve ("0.0.0.0: 8080", new

Slide 12

Slide 12 text

A test of simple server class HelloTest in Specification {"Hellooooo" should { "reply hello" in { val res = Await.result (service (Request ()) res.contentString must _ == "Hello" } } }

Slide 13

Slide 13 text

Filter ● Filter can convert Req or Rep handling by Service ● Filter [RepI, RepO, ReqO, ReqI]

Slide 14

Slide 14 text

For example Abstract class Filter [RepI, RepO, ReqO, ReqI] { def apply (r: ReqI, next: Service [ReqO, RepI]): Future [RepO] } OR there’s simple ver. abstract class SimpleFilter [Rep, Rep] { def apply (r: Req, next: Service [Req, Rep]): Future [Rep] }

Slide 15

Slide 15 text

Combination of Service and Filter val httpStatsFilter = new HttpxStatsFilter [HttpRequest] (stats) val statsFilter = new StatsFilter [HttpRequest, HttpResponse] (stats) val logging = new QueryLoggingFilter () val retry = new RetryingFilter [HttpRequest, HttpResponse] (RetryPolicy.tries (3), new NullTimer) val httpServiceWithFilter: Service [HttpRequest, HttpResponse] = statsFilter andThen httpStatsFilter andThen retry andThen logging andThen mux

Slide 16

Slide 16 text

When to use ● Filter allows a function plugable ○ We can add/remove a function when we like without modifying core function ○ Like: output logs, retry request, control amount ● Now can concentrate on the core function by separating small filter

Slide 17

Slide 17 text

Advantage ● No longer care about features unimportant ● Easy to put on or remove it ● Easy to test <- important!

Slide 18

Slide 18 text

Conclusion ● Introduction finagle ● History ● Central dogma ○ Future ○ Service ○ Filter