Akka Typed — between Session Types and the Actor Model

Roland Kuhn
Roland KuhnCTO at Actyx AG
Akka Typed:
Between Session Types
and the Actor Model
Dr. Roland Kuhn
@rolandkuhn — Akka Tech Lead
Actor Model
The Actor Model
• Hewitt, Bishop, Steiger,

“A Universal Modular ACTOR Formalism for Artificial
Intelligence”, IJCAI’73, pp 235–245
• a model of distributed independent agents
• in response to an incoming message an Actor can
• create a finite number of Actors
• send a finite number of messages to Actors it knows
• designate the behavior to be applied to the next message
3
Concrete Implementation: Akka
4
case class Greet(whom: String)
class Greeter extends Actor {
def receive = {
case Greet(whom) =>
sender() ! s"Hello $whom!"
val delegate = context.actorOf(grumpyProps)
context.become(grumpy(delegate))
}
def grumpy(delegate: ActorRef): Receive = {
case g: Greet => delegate forward g
}
}
val grumpyProps = Props(new Actor {
def receive = {
case Greet(whom) => sender() ! s"Go away, $whom!"
}
})
Motivation
Initial Motivation
• make Actors re-factoring friendly
• avoid stupid mistakes

(i.e. provide some weak safety properties)
6
Introducing Akka Typed
The Flaws of Previous Implementations
• retaining sender() is just not feasible
• this feature is inherently dynamic and therefore must go
• defining union types for multiple input types proved
too complex
• TypedChannels used a type map based on HList
• verbose syntax and cryptic error messages
• use only single type parameter and subsume other types
via dedicated adapters (child Actors)
8
The Current Implementation
• parameterized ActorRef accepts type T
• parameterized Behavior responds to type T
• actor creation turns Behavior[T] via Props[T]
into ActorRef[-T]
• implemented as a thin layer on top of untyped
actors
• separated the logic from its execution, no more
Actor trait
9
Behavior is King, no more Actor trait
10
object Server {
sealed trait Command
case class Get(id: Int)(val replyTo: ActorRef[Got]) extends Command
case class Put(name: String, ref: ActorRef[OtherCommand]) extends Command
case class Got(id: Int, contents: Map[String, ActorRef[OtherCommand]])
val initial: Behavior[Command] = withMap(Map.empty)
private def withMap(map: Map[String, ActorRef[OtherCommand]]) =
Total[Command] {
case g @ Get(id) =>
g.replyTo ! Got(id, Map.empty)
Same
case Put(name, ref) =>
withMap(map.updated(name, ref))
}
}
What can we do with it?
Encoding Types with Members
12
class MyClass {
def myMethod(id: Int): String
def otherMethod(name: String): Unit
protected def helper(arg: Double): Unit
}
Encoding Types with Members
• Typed Actors provide complete modules with members
• Typed Actors can encode more flexible access privileges
• more verbose due to syntax being optimized for classes
13
object MyClass {
sealed trait AllCommand
sealed trait Command extends AllCommand
case class MyMethod(id: Int)(replyTo: ActorRef[String]) extends Command
case class OtherMethod(name: String) extends Command
case class Helper(arg: Double) extends AllCommand
val behavior: Behavior[Command] = behavior(42).narrow
private def behavior(x: Int): Behavior[AllCommand] = ???
}
Calling Methods
14
object MyClassDemo {
import MyClass._
val myClass: MyClass = ???
val myActor: ActorRef[Command] = ???
implicit val t: Timeout = ???
myClass.otherMethod("John")
myActor!OtherMethod("John")
val result = myClass.myMethod(42)
val future = myActor?MyMethod(42)
}
But Actors can do more: Protocols
15
object Protocol {
case class GetSession(replyTo: ActorRef[GetSessionResult])
sealed trait GetSessionResult
case class ActiveSession(service: ActorRef[SessionCommand])
extends GetSessionResult with AuthenticateResult
case class NewSession(auth: ActorRef[Authenticate])
extends GetSessionResult
case class Authenticate(username: String,
password: String,
replyTo: ActorRef[AuthenticateResult])
sealed trait AuthenticateResult
case object FailedSession extends AuthenticateResult
trait SessionCommand
}
But Actors can do more: Protocols
16
Session Types
Attempt at a Definition
• Session: aunitofconversation
• Session Type: thestructureofaconversation,

a sequence of interactions in a communication-
centric program model
• originally only binary sessions, multiparty sessions
introduced 2008
• primitives are

sending,receiving,sequence,choice,recursion
18
Scribble
• commonly used language for defining protocols
• defines the global protocol for all participants
• local projection for a single participant preserves safety
• automatic generation of FSA for local runtime validation
• type discipline for local processes requires support
for linear types from the host language
• where that is unavailable use dynamic validation
19
Question
• Can safety be retained while allowing sessions with
dynamically added and removed participants?
20
Question
• Does distributed computing fundamentally need
nominal types to agree on semantics?
21
Case Study: Type-Safe Receptionist
22
object Receptionist {
trait AbstractServiceKey {
type Type
}
trait ServiceKey[T] extends AbstractServiceKey {
final override type Type = T
}
sealed trait Command
final case class Register[T](key: ServiceKey[T], address: ActorRef[T])
(val replyTo: ActorRef[Registered[T]]) extends Command
final case class Find[T](key: ServiceKey[T])(val replyTo: ActorRef[Listing[T]])
extends Command
final case class Registered[T](key: ServiceKey[T], address: ActorRef[T])
final case class Listing[T](key: ServiceKey[T], addresses: Set[ActorRef[T]])
...
}
23
object Receptionist {
...
val behavior: Behavior[Command] = behavior(TypedMultiMap.empty)
private type KV[K <: AbstractServiceKey] = ActorRef[K#Type]
private def behavior(map: TypedMultiMap[AbstractServiceKey, KV]) =
Full[Command] {
case Msg(ctx, r: Register[t]) =>
ctx.watch(r.address)
r.replyTo ! Registered(r.key, r.address)
behavior(map.inserted(r.key)(r.address))
case Msg(ctx, f: Find[t]) =>
val set = map get f.key
f.replyTo ! Listing(f.key, set)
Same
case Sig(ctx, Terminated(ref)) =>
behavior(map valueRemoved ref)
}
}
Question
• the interrupt feature of Scribble introduces non-
determinism since messages can be arbitrarily
delayed
• How much does the theory hinge on guaranteed
delivery?
• Does the session just cease to exist when nodes fail?
24
Linear Types
or
Dynamic Actors?
Question
• protocol progress between fixed participants
implies the destruction of knowledge
• Can this notion be replaced by requiring the
acquisition or deduction of new knowledge?
26
Question
• Instead of relying upon consensus, shouldn’t we
maximize the use of causality and causal
consistency?
27
Lloyd, Freedman, Kaminsky, Andersen: «Don’t Settle for Eventual:
Scalable Causal Consistency for Wide-Area Storage with COPS», SOSP’11, ACM 2011
Dynamic Validation
Dynamic Validation of Actor Behavior
• pure internal formulation using an abstract
machine (process algebra)
• interpreter can run local projection of protocol
• rejects sending untimely messages
• rejects receiving from inactive channels
• due to at-most-once delivery FSA can only advance
upon reception of external input (proof of progress)
29
Question
• Given the following requirements:
• sequencing of actions
• dynamic decision how to continue
• recursion
• composition of actions
• injection of plain values
• Is there another abstraction than Monad to
formulate the API?
30
©Typesafe 2015 – All Rights Reserved
1 of 31

Recommended

Taming Distribution: Formal Protocols for Akka Typed by
Taming Distribution: Formal Protocols for Akka TypedTaming Distribution: Formal Protocols for Akka Typed
Taming Distribution: Formal Protocols for Akka TypedRoland Kuhn
1.2K views36 slides
Project Gålbma – Actors vs Types by
Project Gålbma – Actors vs TypesProject Gålbma – Actors vs Types
Project Gålbma – Actors vs TypesRoland Kuhn
2.7K views49 slides
​"Delegates, Delegates everywhere" Владимир Миронов by
​"Delegates, Delegates everywhere" Владимир Миронов​"Delegates, Delegates everywhere" Владимир Миронов
​"Delegates, Delegates everywhere" Владимир МироновAvitoTech
677 views86 slides
"Kotlin и rx в android" Дмитрий Воронин (Avito) by
"Kotlin и rx в android" Дмитрий Воронин  (Avito)"Kotlin и rx в android" Дмитрий Воронин  (Avito)
"Kotlin и rx в android" Дмитрий Воронин (Avito)AvitoTech
1.1K views67 slides
360|iDev by
360|iDev360|iDev
360|iDevAijaz Ansari
755 views76 slides
Kotlin on Android: Delegate with pleasure by
Kotlin on Android: Delegate with pleasureKotlin on Android: Delegate with pleasure
Kotlin on Android: Delegate with pleasureDmytro Zaitsev
592 views59 slides

More Related Content

What's hot

G3 Summit 2016 - Taking Advantage of Groovy Annotations by
G3 Summit 2016 - Taking Advantage of Groovy AnnotationsG3 Summit 2016 - Taking Advantage of Groovy Annotations
G3 Summit 2016 - Taking Advantage of Groovy AnnotationsIván López Martín
5.9K views86 slides
The Ring programming language version 1.5.1 book - Part 174 of 180 by
The Ring programming language version 1.5.1 book - Part 174 of 180 The Ring programming language version 1.5.1 book - Part 174 of 180
The Ring programming language version 1.5.1 book - Part 174 of 180 Mahmoud Samir Fayed
30 views10 slides
The Ring programming language version 1.9 book - Part 98 of 210 by
The Ring programming language version 1.9 book - Part 98 of 210The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210Mahmoud Samir Fayed
7 views10 slides
Introduction to kotlin + spring boot demo by
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demoMuhammad Abdullah
113 views78 slides
Groovy grails types, operators, objects by
Groovy grails types, operators, objectsGroovy grails types, operators, objects
Groovy grails types, operators, objectsHusain Dalal
458 views51 slides
Knolx session by
Knolx sessionKnolx session
Knolx sessionKnoldus Inc.
3.6K views17 slides

What's hot(19)

G3 Summit 2016 - Taking Advantage of Groovy Annotations by Iván López Martín
G3 Summit 2016 - Taking Advantage of Groovy AnnotationsG3 Summit 2016 - Taking Advantage of Groovy Annotations
G3 Summit 2016 - Taking Advantage of Groovy Annotations
The Ring programming language version 1.5.1 book - Part 174 of 180 by Mahmoud Samir Fayed
The Ring programming language version 1.5.1 book - Part 174 of 180 The Ring programming language version 1.5.1 book - Part 174 of 180
The Ring programming language version 1.5.1 book - Part 174 of 180
The Ring programming language version 1.9 book - Part 98 of 210 by Mahmoud Samir Fayed
The Ring programming language version 1.9 book - Part 98 of 210The Ring programming language version 1.9 book - Part 98 of 210
The Ring programming language version 1.9 book - Part 98 of 210
Introduction to kotlin + spring boot demo by Muhammad Abdullah
Introduction to kotlin + spring boot demoIntroduction to kotlin + spring boot demo
Introduction to kotlin + spring boot demo
Muhammad Abdullah113 views
Groovy grails types, operators, objects by Husain Dalal
Groovy grails types, operators, objectsGroovy grails types, operators, objects
Groovy grails types, operators, objects
Husain Dalal458 views
Martin Fowler's Refactoring Techniques Quick Reference by Seung-Bum Lee
Martin Fowler's Refactoring Techniques Quick ReferenceMartin Fowler's Refactoring Techniques Quick Reference
Martin Fowler's Refactoring Techniques Quick Reference
Seung-Bum Lee112 views
CS101- Introduction to Computing- Lecture 29 by Bilal Ahmed
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29
Bilal Ahmed504 views
TDC218SP | Trilha Kotlin - DSLs in a Kotlin Way by tdc-globalcode
TDC218SP | Trilha Kotlin - DSLs in a Kotlin WayTDC218SP | Trilha Kotlin - DSLs in a Kotlin Way
TDC218SP | Trilha Kotlin - DSLs in a Kotlin Way
tdc-globalcode146 views
Python programming: Anonymous functions, String operations by Megha V
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operations
Megha V211 views
Introduction to Gremlin by Max De Marzi
Introduction to GremlinIntroduction to Gremlin
Introduction to Gremlin
Max De Marzi12.2K views
Pragmatic Real-World Scala (short version) by Jonas Bonér
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
Jonas Bonér72.8K views
Kotlin Advanced - Apalon Kotlin Sprint Part 3 by Kirill Rozov
Kotlin Advanced - Apalon Kotlin Sprint Part 3Kotlin Advanced - Apalon Kotlin Sprint Part 3
Kotlin Advanced - Apalon Kotlin Sprint Part 3
Kirill Rozov328 views
Apache Spark - Key Value RDD - Transformations | Big Data Hadoop Spark Tutori... by CloudxLab
Apache Spark - Key Value RDD - Transformations | Big Data Hadoop Spark Tutori...Apache Spark - Key Value RDD - Transformations | Big Data Hadoop Spark Tutori...
Apache Spark - Key Value RDD - Transformations | Big Data Hadoop Spark Tutori...
CloudxLab293 views
The Ring programming language version 1.6 book - Part 40 of 189 by Mahmoud Samir Fayed
The Ring programming language version 1.6 book - Part 40 of 189The Ring programming language version 1.6 book - Part 40 of 189
The Ring programming language version 1.6 book - Part 40 of 189
Scalaz 8 vs Akka Actors by John De Goes
Scalaz 8 vs Akka ActorsScalaz 8 vs Akka Actors
Scalaz 8 vs Akka Actors
John De Goes7K views

Viewers also liked

Distributed systems vs compositionality by
Distributed systems vs compositionalityDistributed systems vs compositionality
Distributed systems vs compositionalityRoland Kuhn
2.9K views37 slides
The Newest in Session Types by
The Newest in Session TypesThe Newest in Session Types
The Newest in Session TypesRoland Kuhn
3.9K views24 slides
Reactive Design Patterns — J on the Beach by
Reactive Design Patterns — J on the BeachReactive Design Patterns — J on the Beach
Reactive Design Patterns — J on the BeachRoland Kuhn
3.3K views36 slides
Reactive Streams: Handling Data-Flow the Reactive Way by
Reactive Streams: Handling Data-Flow the Reactive WayReactive Streams: Handling Data-Flow the Reactive Way
Reactive Streams: Handling Data-Flow the Reactive WayRoland Kuhn
34.8K views60 slides
Go Reactive: Blueprint for Future Applications by
Go Reactive: Blueprint for Future ApplicationsGo Reactive: Blueprint for Future Applications
Go Reactive: Blueprint for Future ApplicationsRoland Kuhn
3.1K views42 slides
An Actor Model in Go by
An Actor Model in GoAn Actor Model in Go
An Actor Model in GoWeaveworks
7.5K views25 slides

Viewers also liked(12)

Distributed systems vs compositionality by Roland Kuhn
Distributed systems vs compositionalityDistributed systems vs compositionality
Distributed systems vs compositionality
Roland Kuhn2.9K views
The Newest in Session Types by Roland Kuhn
The Newest in Session TypesThe Newest in Session Types
The Newest in Session Types
Roland Kuhn3.9K views
Reactive Design Patterns — J on the Beach by Roland Kuhn
Reactive Design Patterns — J on the BeachReactive Design Patterns — J on the Beach
Reactive Design Patterns — J on the Beach
Roland Kuhn3.3K views
Reactive Streams: Handling Data-Flow the Reactive Way by Roland Kuhn
Reactive Streams: Handling Data-Flow the Reactive WayReactive Streams: Handling Data-Flow the Reactive Way
Reactive Streams: Handling Data-Flow the Reactive Way
Roland Kuhn34.8K views
Go Reactive: Blueprint for Future Applications by Roland Kuhn
Go Reactive: Blueprint for Future ApplicationsGo Reactive: Blueprint for Future Applications
Go Reactive: Blueprint for Future Applications
Roland Kuhn3.1K views
An Actor Model in Go by Weaveworks
An Actor Model in GoAn Actor Model in Go
An Actor Model in Go
Weaveworks7.5K views
HBase RowKey design for Akka Persistence by Konrad Malawski
HBase RowKey design for Akka PersistenceHBase RowKey design for Akka Persistence
HBase RowKey design for Akka Persistence
Konrad Malawski2.3K views
Akka cluster overview at 010dev by Roland Kuhn
Akka cluster overview at 010devAkka cluster overview at 010dev
Akka cluster overview at 010dev
Roland Kuhn6.8K views
Akka and AngularJS – Reactive Applications in Practice by Roland Kuhn
Akka and AngularJS – Reactive Applications in PracticeAkka and AngularJS – Reactive Applications in Practice
Akka and AngularJS – Reactive Applications in Practice
Roland Kuhn17.3K views
Go Reactive: Event-Driven, Scalable, Resilient & Responsive Systems by Jonas Bonér
Go Reactive: Event-Driven, Scalable, Resilient & Responsive SystemsGo Reactive: Event-Driven, Scalable, Resilient & Responsive Systems
Go Reactive: Event-Driven, Scalable, Resilient & Responsive Systems
Jonas Bonér30.9K views
Akka Streams and HTTP by Roland Kuhn
Akka Streams and HTTPAkka Streams and HTTP
Akka Streams and HTTP
Roland Kuhn41.6K views
The Actor Model - Towards Better Concurrency by Dror Bereznitsky
The Actor Model - Towards Better ConcurrencyThe Actor Model - Towards Better Concurrency
The Actor Model - Towards Better Concurrency
Dror Bereznitsky25.1K views

Similar to Akka Typed — between Session Types and the Actor Model

Scala Back to Basics: Type Classes by
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesTomer Gabel
3.7K views35 slides
Improving Correctness with Types by
Improving Correctness with TypesImproving Correctness with Types
Improving Correctness with TypesIain Hull
1.7K views46 slides
Demystifying Type Class derivation with Shapeless by
Demystifying Type Class derivation with ShapelessDemystifying Type Class derivation with Shapeless
Demystifying Type Class derivation with ShapelessYurii Ostapchuk
659 views51 slides
Kotlin Delegates: Reduce the boilerplate by
Kotlin Delegates: Reduce the boilerplateKotlin Delegates: Reduce the boilerplate
Kotlin Delegates: Reduce the boilerplateDmytro Zaitsev
177 views42 slides
Scaling modern JVM applications with Akka toolkit by
Scaling modern JVM applications with Akka toolkitScaling modern JVM applications with Akka toolkit
Scaling modern JVM applications with Akka toolkitBojan Babic
173 views28 slides

Similar to Akka Typed — between Session Types and the Actor Model(20)

Scala Back to Basics: Type Classes by Tomer Gabel
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
Tomer Gabel3.7K views
Improving Correctness with Types by Iain Hull
Improving Correctness with TypesImproving Correctness with Types
Improving Correctness with Types
Iain Hull1.7K views
Demystifying Type Class derivation with Shapeless by Yurii Ostapchuk
Demystifying Type Class derivation with ShapelessDemystifying Type Class derivation with Shapeless
Demystifying Type Class derivation with Shapeless
Yurii Ostapchuk659 views
Kotlin Delegates: Reduce the boilerplate by Dmytro Zaitsev
Kotlin Delegates: Reduce the boilerplateKotlin Delegates: Reduce the boilerplate
Kotlin Delegates: Reduce the boilerplate
Dmytro Zaitsev177 views
Scaling modern JVM applications with Akka toolkit by Bojan Babic
Scaling modern JVM applications with Akka toolkitScaling modern JVM applications with Akka toolkit
Scaling modern JVM applications with Akka toolkit
Bojan Babic173 views
Improving Correctness With Type - Goto Con Berlin by Iain Hull
Improving Correctness With Type - Goto Con BerlinImproving Correctness With Type - Goto Con Berlin
Improving Correctness With Type - Goto Con Berlin
Iain Hull937 views
Improving Correctness with Types Kats Conf by Iain Hull
Improving Correctness with Types Kats ConfImproving Correctness with Types Kats Conf
Improving Correctness with Types Kats Conf
Iain Hull417 views
aming distribution: formal protocols for Akka Typed by J On The Beach
aming distribution: formal protocols for Akka Typedaming distribution: formal protocols for Akka Typed
aming distribution: formal protocols for Akka Typed
J On The Beach122 views
Functions In Scala by Knoldus Inc.
Functions In Scala Functions In Scala
Functions In Scala
Knoldus Inc.2.2K views
Oleksii Holub "Expression trees in C#" by Fwdays
Oleksii Holub "Expression trees in C#" Oleksii Holub "Expression trees in C#"
Oleksii Holub "Expression trees in C#"
Fwdays1.2K views
Scala 3 Is Coming: Martin Odersky Shares What To Know by Lightbend
Scala 3 Is Coming: Martin Odersky Shares What To KnowScala 3 Is Coming: Martin Odersky Shares What To Know
Scala 3 Is Coming: Martin Odersky Shares What To Know
Lightbend6.1K views
Message-based communication patterns in distributed Akka applications by Andrii Lashchenko
Message-based communication patterns in distributed Akka applicationsMessage-based communication patterns in distributed Akka applications
Message-based communication patterns in distributed Akka applications
Andrii Lashchenko327 views
Activator and Reactive at Play NYC meetup by Henrik Engström
Activator and Reactive at Play NYC meetupActivator and Reactive at Play NYC meetup
Activator and Reactive at Play NYC meetup
Henrik Engström1.7K views
CAVE Overview by vdumitrescu
CAVE OverviewCAVE Overview
CAVE Overview
vdumitrescu10.6K views
Akka with Scala by Oto Brglez
Akka with ScalaAkka with Scala
Akka with Scala
Oto Brglez256 views
Futzing with actors (etc.) by league
Futzing with actors (etc.)Futzing with actors (etc.)
Futzing with actors (etc.)
league937 views

Akka Typed — between Session Types and the Actor Model

  • 1. Akka Typed: Between Session Types and the Actor Model Dr. Roland Kuhn @rolandkuhn — Akka Tech Lead
  • 3. The Actor Model • Hewitt, Bishop, Steiger,
 “A Universal Modular ACTOR Formalism for Artificial Intelligence”, IJCAI’73, pp 235–245 • a model of distributed independent agents • in response to an incoming message an Actor can • create a finite number of Actors • send a finite number of messages to Actors it knows • designate the behavior to be applied to the next message 3
  • 4. Concrete Implementation: Akka 4 case class Greet(whom: String) class Greeter extends Actor { def receive = { case Greet(whom) => sender() ! s"Hello $whom!" val delegate = context.actorOf(grumpyProps) context.become(grumpy(delegate)) } def grumpy(delegate: ActorRef): Receive = { case g: Greet => delegate forward g } } val grumpyProps = Props(new Actor { def receive = { case Greet(whom) => sender() ! s"Go away, $whom!" } })
  • 6. Initial Motivation • make Actors re-factoring friendly • avoid stupid mistakes
 (i.e. provide some weak safety properties) 6
  • 8. The Flaws of Previous Implementations • retaining sender() is just not feasible • this feature is inherently dynamic and therefore must go • defining union types for multiple input types proved too complex • TypedChannels used a type map based on HList • verbose syntax and cryptic error messages • use only single type parameter and subsume other types via dedicated adapters (child Actors) 8
  • 9. The Current Implementation • parameterized ActorRef accepts type T • parameterized Behavior responds to type T • actor creation turns Behavior[T] via Props[T] into ActorRef[-T] • implemented as a thin layer on top of untyped actors • separated the logic from its execution, no more Actor trait 9
  • 10. Behavior is King, no more Actor trait 10 object Server { sealed trait Command case class Get(id: Int)(val replyTo: ActorRef[Got]) extends Command case class Put(name: String, ref: ActorRef[OtherCommand]) extends Command case class Got(id: Int, contents: Map[String, ActorRef[OtherCommand]]) val initial: Behavior[Command] = withMap(Map.empty) private def withMap(map: Map[String, ActorRef[OtherCommand]]) = Total[Command] { case g @ Get(id) => g.replyTo ! Got(id, Map.empty) Same case Put(name, ref) => withMap(map.updated(name, ref)) } }
  • 11. What can we do with it?
  • 12. Encoding Types with Members 12 class MyClass { def myMethod(id: Int): String def otherMethod(name: String): Unit protected def helper(arg: Double): Unit }
  • 13. Encoding Types with Members • Typed Actors provide complete modules with members • Typed Actors can encode more flexible access privileges • more verbose due to syntax being optimized for classes 13 object MyClass { sealed trait AllCommand sealed trait Command extends AllCommand case class MyMethod(id: Int)(replyTo: ActorRef[String]) extends Command case class OtherMethod(name: String) extends Command case class Helper(arg: Double) extends AllCommand val behavior: Behavior[Command] = behavior(42).narrow private def behavior(x: Int): Behavior[AllCommand] = ??? }
  • 14. Calling Methods 14 object MyClassDemo { import MyClass._ val myClass: MyClass = ??? val myActor: ActorRef[Command] = ??? implicit val t: Timeout = ??? myClass.otherMethod("John") myActor!OtherMethod("John") val result = myClass.myMethod(42) val future = myActor?MyMethod(42) }
  • 15. But Actors can do more: Protocols 15 object Protocol { case class GetSession(replyTo: ActorRef[GetSessionResult]) sealed trait GetSessionResult case class ActiveSession(service: ActorRef[SessionCommand]) extends GetSessionResult with AuthenticateResult case class NewSession(auth: ActorRef[Authenticate]) extends GetSessionResult case class Authenticate(username: String, password: String, replyTo: ActorRef[AuthenticateResult]) sealed trait AuthenticateResult case object FailedSession extends AuthenticateResult trait SessionCommand }
  • 16. But Actors can do more: Protocols 16
  • 18. Attempt at a Definition • Session: aunitofconversation • Session Type: thestructureofaconversation,
 a sequence of interactions in a communication- centric program model • originally only binary sessions, multiparty sessions introduced 2008 • primitives are
 sending,receiving,sequence,choice,recursion 18
  • 19. Scribble • commonly used language for defining protocols • defines the global protocol for all participants • local projection for a single participant preserves safety • automatic generation of FSA for local runtime validation • type discipline for local processes requires support for linear types from the host language • where that is unavailable use dynamic validation 19
  • 20. Question • Can safety be retained while allowing sessions with dynamically added and removed participants? 20
  • 21. Question • Does distributed computing fundamentally need nominal types to agree on semantics? 21
  • 22. Case Study: Type-Safe Receptionist 22 object Receptionist { trait AbstractServiceKey { type Type } trait ServiceKey[T] extends AbstractServiceKey { final override type Type = T } sealed trait Command final case class Register[T](key: ServiceKey[T], address: ActorRef[T]) (val replyTo: ActorRef[Registered[T]]) extends Command final case class Find[T](key: ServiceKey[T])(val replyTo: ActorRef[Listing[T]]) extends Command final case class Registered[T](key: ServiceKey[T], address: ActorRef[T]) final case class Listing[T](key: ServiceKey[T], addresses: Set[ActorRef[T]]) ... }
  • 23. 23 object Receptionist { ... val behavior: Behavior[Command] = behavior(TypedMultiMap.empty) private type KV[K <: AbstractServiceKey] = ActorRef[K#Type] private def behavior(map: TypedMultiMap[AbstractServiceKey, KV]) = Full[Command] { case Msg(ctx, r: Register[t]) => ctx.watch(r.address) r.replyTo ! Registered(r.key, r.address) behavior(map.inserted(r.key)(r.address)) case Msg(ctx, f: Find[t]) => val set = map get f.key f.replyTo ! Listing(f.key, set) Same case Sig(ctx, Terminated(ref)) => behavior(map valueRemoved ref) } }
  • 24. Question • the interrupt feature of Scribble introduces non- determinism since messages can be arbitrarily delayed • How much does the theory hinge on guaranteed delivery? • Does the session just cease to exist when nodes fail? 24
  • 26. Question • protocol progress between fixed participants implies the destruction of knowledge • Can this notion be replaced by requiring the acquisition or deduction of new knowledge? 26
  • 27. Question • Instead of relying upon consensus, shouldn’t we maximize the use of causality and causal consistency? 27 Lloyd, Freedman, Kaminsky, Andersen: «Don’t Settle for Eventual: Scalable Causal Consistency for Wide-Area Storage with COPS», SOSP’11, ACM 2011
  • 29. Dynamic Validation of Actor Behavior • pure internal formulation using an abstract machine (process algebra) • interpreter can run local projection of protocol • rejects sending untimely messages • rejects receiving from inactive channels • due to at-most-once delivery FSA can only advance upon reception of external input (proof of progress) 29
  • 30. Question • Given the following requirements: • sequencing of actions • dynamic decision how to continue • recursion • composition of actions • injection of plain values • Is there another abstraction than Monad to formulate the API? 30
  • 31. ©Typesafe 2015 – All Rights Reserved