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

Scala@TomTom

 Scala@TomTom

Explaining how we use scala software stack at TomTom to build REST services.

Nami Nasserazad

May 28, 2013
Tweet

More Decks by Nami Nasserazad

Other Decks in Programming

Transcript

  1. © 2013 TomTom. All rights reserved. • Topics • Project

    Description (?) • Non-functional requirements • Technology stack • General overview of the system architecture • REST API • How to define • How to test • Flow as an Actor • How to define • Future Composition • How to test • Store Actors • Brief overview • Bonus • Loan Pattern 2
  2. © 2013 TomTom. All rights reserved. NavCloud • NavCloud is

    a server-based application, implements a data service to store, maintain and share personal navigation application data in the cloud. 3
  3. © 2013 TomTom. All rights reserved. Non-Functional Requirements • Performance

    (short response time, least resource locking and blocking) • Horizontal scalability + Fault tolerance • Testability (functionality and performance) • Easy to build and deploy • Security • Platform availability (through SDKs) • Maintainable codebase 4
  4. © 2013 TomTom. All rights reserved. Client Applicati on Flow

    Overview Server Flow Actor Flow Actors PUT GET GetEntity Flow Actor SetEntity REST API Actor Riak Store Actor Riak Store Actor Store Actors GetEntityById UpdateEntity Riak Riak Client Streaming 6
  5. © 2013 TomTom. All rights reserved. Client Applicati on REST

    API Server Flow Actor Flow Actors PUT GET GetEntity Flow Actor SetEntity REST API Actor Riak Store Actor Riak Store Actor Store Actors GetEntityById UpdateEntity Riak Riak Client Streaming 7
  6. © 2013 TomTom. All rights reserved. Spray • An open-source

    toolkit for REST/HTTP and low-level network IO on top of Scala and Akka. 8
  7. © 2013 TomTom. All rights reserved. Spray-Routing • Provides a

    high-level routing DSL to define RESTful web services. 9
  8. © 2013 TomTom. All rights reserved. Route & Directives •

    Route: RequestContext => Unit • Directives: Building block to construct complex route structures. • It does transforming, filtering, extracting from, and completing the request context and passing the result to inner directives. 11
  9. © 2013 TomTom. All rights reserved. Entity Definition • Simple

    case classes • JSON format is specified in companion objects. • Spray-JSON takes care of unmarshalling (out of scope of this presentation) 13
  10. © 2013 TomTom. All rights reserved. Spray-testkit • provides a

    dedicated test DSL that makes actor-less testing of route logic easy and convenient. • Supports scalatest and specs2 19
  11. © 2013 TomTom. All rights reserved. Client Applicati on Flow

    as an Actor Server Flow Actor Flow Actors PUT GET GetEntity Flow Actor SetEntity REST API Actor Riak Store Actor Riak Store Actor Store Actors GetEntityById UpdateEntity Riak Riak Client Streaming 21
  12. © 2013 TomTom. All rights reserved. Flow as an Actor

    • Message case classes: Actors communication media (Nothing new) 23
  13. © 2013 TomTom. All rights reserved. Flow Flow as an

    Actor • Actors are responsible to handle the business logic of the system • Per request a new flow actor is created (thanks to actor because they are lightweight) • Flow actors are children of ApiActor (implementation of RestApi) • Currently, supervision strategy is default which means upon exception the actor is restarted (is it right???!) 25
  14. © 2013 TomTom. All rights reserved. Base Flow Actor •

    Consists of some helper functions in order to complete the request • All of flow actors extends this actor 26
  15. © 2013 TomTom. All rights reserved. Future Composition • A

    Future is an object holding a value which may become available at some point. • Future Composition: Chaining multiple futures without without being blocked. 27
  16. © 2013 TomTom. All rights reserved. Do You Remember Base

    Flow Actor?? • In unit tests, we override these functions to instead of completing http request, send us back a message to be captured 30
  17. © 2013 TomTom. All rights reserved. Client Applicati on Server

    Flow Actor Flow Actors PUT GET GetEntity Flow Actor SetEntity REST API Actor Riak Store Actor Riak Store Actor Store Actors GetEntityById UpdateEntity Riak Riak Client Store Actors Streaming 33
  18. © 2013 TomTom. All rights reserved. Store Actors • Store

    actors are the children of flow actors. So, per request we will have a flow actor together with a store actor as its child. • Driver friendly data types are the communication media at this level. • We use Riak Client Library to communicate to riak using its HTTP interface. Since it uses actors to provide non-blocking client, its model is consistent with our model. (future composition etc.) • Everything in Riak is key-value. We use JSON format for our values => Same marshalling and unmarshalling mechanism exists at RestApi level needed here which is encapsulated by Riak client. • Decreasing impedance mismatch??! • Bucket settings are done in store actors. 34
  19. © 2013 TomTom. All rights reserved. Wanna Have Some Fun?

    • Loan Pattern: it would loan a resource to your function • Usage: Hiding Java boiler-plate code while working with resources • Ingredient: Typed higher order curried function. 36
  20. © 2013 TomTom. All rights reserved. Conclusion • Almost everything

    can be modeled as actors in an actor system. With this facts, non-blocking calls are there! • Complexities of asynchronous programming are hidden using future composition. • Using internal DSLs increases the readability and maintainability (debatable!) of the code. • Using appropriate internal DSLs breaks the program into isolated building blocks. 39