scalable solutions - trifork · the problem it is way too hard to build: 1. correct highly...
TRANSCRIPT
![Page 1: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/1.jpg)
Akka:Simpler Concurrency, Scalability &
Fault-tolerance through Actors
Jonas BonérScalable Solutions
[email protected] : @jboner
![Page 2: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/2.jpg)
The problem
It is way too hard to build:1. correct highly concurrent systems
2. truly scalable systems
3. fault-tolerant systems that self-heals
...using “state-of-the-art” tools
![Page 3: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/3.jpg)
VisionSimpler
Concurrency
Scalability
Fault-tolerance
Through one single unified
Programming model
Runtime service
![Page 4: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/4.jpg)
Manage system overload
![Page 5: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/5.jpg)
Scale up & Scale out
![Page 6: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/6.jpg)
Replicate and distribute for fault-tolerance
![Page 7: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/7.jpg)
Automatic & adaptive load balancing
![Page 8: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/8.jpg)
Introducing
Scalability
PersistentDistributed Secure
Concurrency
Actors
Open Source
Fault-tolerance
STM Agents Dataflow
Clustered
![Page 9: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/9.jpg)
CoreModules
Architecture
![Page 10: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/10.jpg)
Add-onModules
Architecture
Add-onModules
![Page 11: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/11.jpg)
EnterpriseModules
Architecture
![Page 12: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/12.jpg)
Actorsone tool in the toolbox
![Page 13: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/13.jpg)
Akka Actors
![Page 14: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/14.jpg)
• Implements Message-Passing Concurrency• Share NOTHING• Isolated lightweight processes•Communicates through messages•Asynchronous and non-blocking• Each actor has a mailbox (message queue)
Actor Model of Concurrency
![Page 15: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/15.jpg)
Actor Model of Concurrency
• Easier to reason about• Raised abstraction level• Easier to avoid–Race conditions–Deadlocks–Starvation–Live locks
![Page 16: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/16.jpg)
Two different models
• Thread-based• Event-based– Very lightweight (600 bytes per actor)– Can easily create millions on a single workstation (13 million on 8 G RAM)– Does not consume a thread
![Page 17: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/17.jpg)
case object Tick
class Counter extends Actor { private var counter = 0
def receive = { case Tick => counter += 1 println(counter) }}
Actors
![Page 18: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/18.jpg)
import Actor._
val counter = actorOf[Counter]
Create Actors
counter is an ActorRef
![Page 19: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/19.jpg)
val actor = actorOf(new MyActor(..))
Create Actors
create actor with constructor arguments
![Page 20: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/20.jpg)
val counter = actorOf[Counter]counter.start
Start actors
![Page 21: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/21.jpg)
val counter = actorOf[Counter].start
Start actors
![Page 22: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/22.jpg)
val counter = actorOf[Counter].startcounter.stop
Stop actors
![Page 23: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/23.jpg)
class MyActor extends Actor {
override def preStart = { ... // called after ‘start’ }
override def postStop = { ... // called before ‘stop’ }}
init & shutdown callbacks
![Page 24: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/24.jpg)
class RecursiveActor extends Actor { private var counter = 0 self.id = “service:recursive”
def receive = { case Tick => counter += 1 self ! Tick }}
the self reference
![Page 25: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/25.jpg)
val worker = actor { case Work(fn) => fn()}
Actorsanonymous
![Page 26: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/26.jpg)
counter ! Tick
Send: !
fire-forget
![Page 27: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/27.jpg)
counter.sendOneWay(Tick)
Send: !
fire-forget
![Page 28: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/28.jpg)
val result = (actor !! Message).as[String]
Send: !!
uses Future under the hood (with time-out)
![Page 29: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/29.jpg)
val result = counter.sendRequestReply(Tick)
Send: !!
uses Future under the hood (with time-out)
![Page 30: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/30.jpg)
// returns a futureval future = actor !!! Messagefuture.awaitval result = future.get
...Futures.awaitOne(List(fut1, fut2, ...))Futures.awaitAll(List(fut1, fut2, ...))
Send: !!!
returns the Future directly
![Page 31: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/31.jpg)
val result = counter.sendRequestReplyFuture(Tick)
Send: !!!
returns the Future directly
![Page 32: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/32.jpg)
class SomeActor extends Actor { def receive = { case User(name) => // use reply self.reply(“Hi ” + name) }}
Reply
![Page 33: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/33.jpg)
class SomeActor extends Actor { def receive = { case User(name) => // store away the sender // to use later or // somewhere else ... = self.sender }}
Reply
![Page 34: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/34.jpg)
class SomeActor extends Actor { def receive = { case User(name) => // store away the sender future // to resolve later or // somewhere else ... = self.senderFuture }}
Reply
![Page 35: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/35.jpg)
// define the case classcase class Register(user: User)
// create and send a new case class messageactor ! Register(user)
// tuplesactor ! (username, password)
// listsactor ! List(“bill”, “bob”, “alice”)
Immutable messages
![Page 36: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/36.jpg)
val actors = ActorRegistry.actorsval actors = ActorRegistry.actorsFor[TYPE]val actors = ActorRegistry.actorsFor(id)val actor = ActorRegistry.actorFor(uuid)ActorRegistry.foreach(fn)ActorRegistry.shutdownAll
ActorRegistry
![Page 37: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/37.jpg)
TypedActor
![Page 38: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/38.jpg)
class CounterImpl extends TypedActor with Counter {
private var counter = 0;
def count = { counter += 1 println(counter) }}
Typed Actors
![Page 39: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/39.jpg)
val counter = TypedActor.newInstance( classOf[Counter], classof[CounterImpl])
Create Typed Actor
![Page 40: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/40.jpg)
counter.count
Send message
fire-forget
![Page 41: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/41.jpg)
val hits = counter.getNrOfHits
Request Reply
uses Future under the hood (with time-out)
![Page 42: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/42.jpg)
class PingImpl extends TypedActor with Ping { def hit(count: Int) = { val pong = getContext
.getSender.asInstanceOf[Pong] pong.hit(count += 1) }}
the context reference
![Page 43: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/43.jpg)
akka { version = "0.10" time-‐unit = "seconds" actor { timeout = 5 throughput = 5 }}
Actors: config
![Page 44: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/44.jpg)
Scalability BenchmarkSimple Trading system
• Synchronous Scala version• Scala Library Actors 2.8.0
•Fire-forget•Request-reply (Futures)
• Akka • Fire-forget (Hawt dispatcher)• Fire-forget (default dispatcher)• Request-reply
Run it yourself: http://github.com/patriknw/akka-sample-trading
![Page 45: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/45.jpg)
Agentsyet another tool in the toolbox
![Page 46: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/46.jpg)
val agent = Agent(5)
// send function asynchronouslyagent send (_ + 1)
val result = agent() // deref... // use result
agent.close
Agents
Cooperates with STM
![Page 47: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/47.jpg)
Akka Dispatchers
![Page 48: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/48.jpg)
Dispatchers
• Executor-based Dispatcher• Executor-based Work-stealing Dispatcher• Hawt Dispatcher• Thread-based Dispatcher
![Page 49: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/49.jpg)
object Dispatchers { object globalHawtDispatcher extends HawtDispatcher ... def newExecutorBasedEventDrivenDispatcher(name: String)
def newExecutorBasedEventDrivenWorkStealingDispatcher(name: String)
def newHawtDispatcher(aggregate: Boolean)
...}
Dispatchers
![Page 50: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/50.jpg)
class MyActor extends Actor { self.dispatcher = Dispatchers .newThreadBasedDispatcher(self) ...}
actor.dispatcher = dispatcher // before started
Set dispatcher
![Page 51: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/51.jpg)
Let it crash fault-tolerance
![Page 52: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/52.jpg)
Influenced by
Erlang
![Page 53: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/53.jpg)
9 nines
![Page 54: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/54.jpg)
OneForOne fault handling strategy
![Page 55: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/55.jpg)
OneForOne fault handling strategy
![Page 56: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/56.jpg)
OneForOne fault handling strategy
![Page 57: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/57.jpg)
OneForOne fault handling strategy
![Page 58: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/58.jpg)
OneForOne fault handling strategy
![Page 59: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/59.jpg)
OneForOne fault handling strategy
![Page 60: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/60.jpg)
OneForOne fault handling strategy
![Page 61: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/61.jpg)
OneForOne fault handling strategy
![Page 62: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/62.jpg)
AllForOnefault handling strategy
![Page 63: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/63.jpg)
AllForOnefault handling strategy
![Page 64: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/64.jpg)
AllForOnefault handling strategy
![Page 65: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/65.jpg)
AllForOnefault handling strategy
![Page 66: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/66.jpg)
AllForOnefault handling strategy
![Page 67: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/67.jpg)
Supervisor hierarchies
![Page 68: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/68.jpg)
Supervisor hierarchies
![Page 69: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/69.jpg)
Supervisor hierarchies
![Page 70: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/70.jpg)
Supervisor hierarchies
![Page 71: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/71.jpg)
Supervisor hierarchies
![Page 72: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/72.jpg)
Supervisor hierarchies
![Page 73: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/73.jpg)
AllForOneStrategy( maxNrOfRetries, withinTimeRange)
OneForOneStrategy( maxNrOfRetries, withinTimeRange)
Fault handlers
![Page 74: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/74.jpg)
link(actor) unlink(actor)
startLink(actor)spawnLink[MyActor]
Linking
![Page 75: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/75.jpg)
trapExit = List( classOf[ServiceException], classOf[PersistenceException])
trapExit
![Page 76: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/76.jpg)
class Supervisor extends Actor { trapExit = List(classOf[Throwable]) faultHandler = Some(OneForOneStrategy(5, 5000))
def receive = { case Register(actor) => link(actor) }}
Supervision
![Page 77: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/77.jpg)
class FaultTolerantService extends Actor { ... override def preRestart(reason: Throwable) = { ... // clean up before restart } override def postRestart(reason: Throwable) = { ... // init after restart }}
Manage failure
![Page 78: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/78.jpg)
Remote Actors
![Page 79: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/79.jpg)
// use host & port in configRemoteNode.start
RemoteNode.start("localhost", 9999)
Remote Server
Scalable implementation based on NIO (Netty) & Protobuf
![Page 80: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/80.jpg)
Two types of
remote actors
Client managed Server managed
![Page 81: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/81.jpg)
// methods in Actor class
spawnRemote[MyActor](host, port)
spawnLinkRemote[MyActor](host, port)
startLinkRemote(actor, host, port)
Client-managedsupervision works across nodes
![Page 82: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/82.jpg)
val actorProxy = spawnLinkRemote[MyActor]( “darkstar”, 9999)
actorProxy ! message
Client-managedmoves actor to server
client manages through proxy
![Page 83: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/83.jpg)
RemoteNode.register(“service:id”, actorOf[MyService])
Server-managedregister and manage actor on server
client gets “dumb” proxy handle
server part
![Page 84: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/84.jpg)
val handle = RemoteClient.actorFor( “service:id”, “darkstar”, 9999)
handle ! message
Server-managed
client part
![Page 85: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/85.jpg)
Cluster.relayMessage( classOf[TypeOfActor], message)
for (endpoint <-‐ Cluster) spawnRemote[TypeOfActor]( endpoint.host, endpoint.port)
Cluster Membership
![Page 86: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/86.jpg)
STMyet another tool in the toolbox
![Page 87: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/87.jpg)
71
What is STM?
![Page 88: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/88.jpg)
STM: overview• See the memory (heap and stack) as a transactional dataset
• Similar to a database• begin• commit• abort/rollback
• Transactions are retried automatically upon collision
• Rolls back the memory on abort
![Page 89: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/89.jpg)
• Separates Identity from Value- Values are immutable- Identity (Ref) holds Values
•Change is a function•Compare-and-swap (CAS)•Abstraction of time•Must be used within a transaction
Managed References
![Page 90: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/90.jpg)
import se.scalablesolutions.akka.stm.local._ atomic { ...
atomic { ... // transactions compose!!! }}
atomic
![Page 91: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/91.jpg)
Typical OO: direct access to mutable objects
Managed ReferencesTypical OO - Direct
references to Mutable Objects
• Unifies identity and value
• Anything can change at any time
• Consistency is a user problem
• Encapsulation doesn’t solve concurrency problems
?
?
42
?
6:e
:d
:c
:b
:a
foo
Managed Reference: separates Identity & ValueClojure - Indirect references
to Immutable Objects
6
17
"ethel"
"fred"
42
:e
:d
:c
:b
:afoo
@foo
• Separates identity and value
• Obtaining value requires explicit dereference
• Values can never change
• Never an inconsistent value
• Encapsulation is orthogonal
Copyright Rich Hickey 2009
![Page 92: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/92.jpg)
• Separates Identity from Value- Values are immutable- Identity (Ref) holds Values
•Change is a function•Compare-and-swap (CAS)•Abstraction of time•Must be used within a transaction
Managed References
![Page 93: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/93.jpg)
import se.scalablesolutions.akka.stm.local._ // giving an initial valueval ref = Ref(0) // specifying a type but no initial valueval ref = Ref[Int]
Managed References
![Page 94: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/94.jpg)
val ref = Ref(0) atomic { ref.set(5)}// -‐> 0 atomic { ref.get}// -‐> 5
Managed References
![Page 95: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/95.jpg)
val ref = Ref(0) atomic { ref alter (_ + 5)}// -‐> 5 val inc = (i: Int) => i + 1 atomic { ref alter inc}// -‐> 6
Managed References
![Page 96: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/96.jpg)
val ref = Ref(1) val anotherRef = Ref(3) atomic { for { value1 <-‐ ref value2 <-‐ anotherRef } yield (value1 + value2)}// -‐> Ref(4) val emptyRef = Ref[Int] atomic { for { value1 <-‐ ref value2 <-‐ emptyRef } yield (value1 + value2)}// -‐> Ref[Int]
Managed References
![Page 97: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/97.jpg)
// using initial valuesval map = TransactionalMap("bill" -‐> User("bill"))val vector = TransactionalVector(Address("somewhere")) // specifying typesval map = TransactionalMap[String, User]val vector = TransactionalVector[Address]
Transactionaldatastructures
![Page 98: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/98.jpg)
atomic { deferred { // executes when transaction commits } compensating { // executes when transaction aborts }}
life-cycle listeners
![Page 99: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/99.jpg)
Actors + STM = Transactors
![Page 100: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/100.jpg)
class UserRegistry extends Transactor { private lazy val storage = TransactionalMap[String, User]()
def receive = { case NewUser(user) => storage + (user.name -‐> user) ... }}
Transactors
![Page 101: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/101.jpg)
Transactors
![Page 102: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/102.jpg)
TransactorsStart transaction
![Page 103: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/103.jpg)
TransactorsStart transaction
Send message
![Page 104: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/104.jpg)
TransactorsStart transaction
Send message
![Page 105: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/105.jpg)
TransactorsStart transaction
Send message
Update statewithin transaction
![Page 106: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/106.jpg)
TransactorsStart transaction
Send message
Update statewithin transaction
![Page 107: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/107.jpg)
TransactorsStart transaction
Send message
Update statewithin transaction
![Page 108: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/108.jpg)
TransactorsStart transaction
Send message
Update statewithin transaction
![Page 109: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/109.jpg)
TransactorsStart transaction
Send message
Update statewithin transaction
![Page 110: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/110.jpg)
TransactorsStart transaction
Send message
Update statewithin transaction
![Page 111: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/111.jpg)
TransactorsStart transaction
Send message
Update statewithin transaction
Transaction fails
![Page 112: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/112.jpg)
TransactorsStart transaction
Send message
Update statewithin transaction
Transaction fails
![Page 113: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/113.jpg)
Transactors
![Page 114: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/114.jpg)
Transactors
![Page 115: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/115.jpg)
Transactors
![Page 116: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/116.jpg)
Transactors
![Page 117: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/117.jpg)
Transactors
![Page 118: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/118.jpg)
Transactors
![Page 119: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/119.jpg)
TransactorsTransactionautomatically
retried
![Page 120: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/120.jpg)
class Transferer extends Actor { implicit val txFactory = TransactionFactory( blockingAllowed = true, trackReads = true, timeout = 60 seconds) def receive = { case Transfer(from, to, amount) => atomic { if (from.get < amount) { log.info("not enough money -‐ retrying") retry } log.info("transferring") from alter (_ -‐ amount) to alter (_ + amount) } }}
blocking transactions
![Page 121: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/121.jpg)
atomic { either { if (left.get < amount) { log.info("not enough on left -‐ retrying") retry } log.info("going left") } orElse { if (right.get < amount) { log.info("not enough on right -‐ retrying") retry } log.info("going right") } }
either-orElse
![Page 122: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/122.jpg)
akka { stm {
max-‐retries = 1000 timeout = 10 write-‐skew = true blocking-‐allowed = false interruptible = false speculative = true quick-‐release = true propagation = requires trace-‐level = none hooks = true jta-‐aware = off }}
STM: config
![Page 123: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/123.jpg)
Modules
![Page 124: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/124.jpg)
Akka Persistence
![Page 125: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/125.jpg)
STM gives usAtomicConsistentIsolated
![Page 126: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/126.jpg)
AtomicConsistentIsolatedDurable
Persistence module turns STM into
![Page 127: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/127.jpg)
Akka Persistence API
// transactional Cassandra-‐backed Map val map = CassandraStorage.newMap
// transactional Redis-‐backed Vector val vector = RedisStorage.newVector
// transactional Mongo-‐backed Refval ref = MongoStorage.newRef
![Page 128: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/128.jpg)
For Redis only (so far)
val queue: PersistentQueue[ElementType] = RedisStorage.newQueue
val set: PersistentSortedSet[ElementType] = RedisStorage.newSortedSet
![Page 129: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/129.jpg)
Akka Spring
![Page 130: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/130.jpg)
<beans> <akka:typed-‐actor id="myActiveObject" interface="com.biz.MyPOJO" implementation="com.biz.MyPOJO" transactional="true" timeout="1000" /> ...</beans>
Spring integration
![Page 131: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/131.jpg)
<akka:supervision id="my-‐supervisor">
<akka:restart-‐strategy failover="AllForOne" retries="3" timerange="1000"> <akka:trap-‐exits> <akka:trap-‐exit>java.io.IOException</akka:trap-‐exit> </akka:trap-‐exits> </akka:restart-‐strategy>
<akka:typed-‐actors> <akka:typed-‐actor interface="com.biz.MyPOJO" implementation="com.biz.MyPOJOImpl" lifecycle="permanent" timeout="1000"> </akka:typed-‐actor> </akka:typed-‐actors></akka:supervision>
Spring integration
![Page 132: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/132.jpg)
Akka Camel
![Page 133: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/133.jpg)
class MyConsumer extends Actor with Consumer { def endpointUri = "file:data/input" def receive = { case msg: Message => log.info("received %s" format msg.bodyAs(classOf[String])) }}
Camel: consumer
![Page 134: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/134.jpg)
class MyConsumer extends Actor with Consumer { def endpointUri = "jetty:http://0.0.0.0:8877/camel/test" def receive = { case msg: Message => reply("Hello %s" format msg.bodyAs(classOf[String])) }}
Camel: consumer
![Page 135: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/135.jpg)
class CometProducer extends Actor with Producer { def endpointUri = "cometd://localhost:8111/test" def receive = produce // use default impl}
Camel: producer
![Page 136: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/136.jpg)
val producer = actorOf[CometProducer].start
val time = "Current time: " + new Dateproducer ! time
Camel: producer
![Page 137: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/137.jpg)
Akka HotSwap
![Page 138: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/138.jpg)
HotSwapactor ! HotSwap({ // new body case Ping => ... case Pong => ... })
![Page 139: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/139.jpg)
HotSwapself.become({ // new body case Ping => ... case Pong => ... })
![Page 140: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/140.jpg)
Deploy as dependency JAR in WEB-INF/lib etc. Run as stand-alone microkernel OSGi-enabled; drop in any
OSGi container (Spring DM server, Karaf etc.)
How to run it?
![Page 141: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/141.jpg)
Akka Kernel
![Page 142: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/142.jpg)
java -‐jar akka-‐1.0-‐SNAPSHOT.jar \ -‐Dakka.config=<path>/akka.conf
Start Kernel
![Page 143: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/143.jpg)
REST
Comet
Security
...and much much more
Web
GuiceOSGi
PubSub
FSM
![Page 145: Scalable Solutions - Trifork · The problem It is way too hard to build: 1. correct highly concurrent systems 2. truly scalable systems 3. fault-tolerant systems that self-heals](https://reader033.vdocument.in/reader033/viewer/2022050304/5f6d171d9c4e525154497fdf/html5/thumbnails/145.jpg)
EOF