Akka typed-channels
Upcoming SlideShare
Loading in...5
×
 

Akka typed-channels

on

  • 1,416 views

These slides are for my presentation at North-East Scala Symposium 2013 in Philadelphia, see http://nescala.org. The video is available at http://www.youtube.com/watch?v=PCk2SHufw3E

These slides are for my presentation at North-East Scala Symposium 2013 in Philadelphia, see http://nescala.org. The video is available at http://www.youtube.com/watch?v=PCk2SHufw3E

Statistics

Views

Total Views
1,416
Views on SlideShare
1,411
Embed Views
5

Actions

Likes
2
Downloads
23
Comments
0

1 Embed 5

https://twitter.com 5

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Akka typed-channels Akka typed-channels Presentation Transcript

  • Typed Channels and Macros Dr. Roland Kuhn @rolandkuhnsöndag 10 februari 13
  • The Problem someActor ! CommandOnesöndag 10 februari 13
  • The Problem trait Command case class CommandOne(param: String) extends Command someActor ! CommandOnesöndag 10 februari 13
  • The Vision someActor <-!- CommandOne(”msg”) because the other does not compilesöndag 10 februari 13
  • But How? • ActorRef must know about message types – Actor type must be parameterized • Message type is verified against thatsöndag 10 februari 13
  • And the replies? val f: Future[Response] = someActor <-?- CommandOne(”hello”) because the compiler knowssöndag 10 februari 13
  • And How This? • ActorRef must know reply types – Actor must be parameterized with them • Reply types are extracted at call sitesöndag 10 februari 13
  • No Type Pollution • Generic Filter/Transform Actors – accept management commands – pass on generic other type • Using just one type is not enough! • Need to use type unions and allow multiple possible reply types for one inputsöndag 10 februari 13
  • Actors Do Compose msg -?-> firstActor -?-> secondActor -!-> client msg -?-> someService -*-> (_ map httpOk) -!-> client Process wiring from the outsidesöndag 10 februari 13
  • The Result:söndag 10 februari 13
  • Type-Safe Composability of Actor Systemssöndag 10 februari 13
  • Relation to π-calculus • Actors are composite processes • Actor types are structural, not nominal • wiring A͡B can be done situationally http://doc.akka.io/ see Typed Channelssöndag 10 februari 13
  • That was the Eloi world Now we’re going to visit the Morlockssöndag 10 februari 13
  • The Implementation • Tagged type union with :+:[(In, Out), ChannelList] <: ChannelList • Value class ChannelRef[…](val a: ActorRef) • Actor mixin Channels[…] • WrappedMessage[…, LUB](val m: LUB) • ops desugar to tell/ask after type checksöndag 10 februari 13
  • How to Declare it? class OpinionatedEcho extends Actor with Channels[TNil, (String, String) :+: TNil] { channel[String] { (str, sender) ⇒ sender <-!- str } // or channel[String] { case (”hello”, sender) ⇒ sender <-!- ”world” case (x, sender) ⇒ sender <-!- s”dunno: $x” } } “sender” will accept only String messagessöndag 10 februari 13
  • First Problem: Lambdas • Type-checker transforms lambda before the macro call – pattern matches or PartialFunction literals generate checks depending on static type info • Behavior is not an argument to “channels” • macro only emits object with right “apply”söndag 10 februari 13
  • First Problem: Lambdas private class Behaviorist[-R, Ch: ru.TypeTag]( wrapped: Boolean) extends (R ⇒ Unit) { // ... def apply(recv: R): Unit = { val tt = ru.typeTag[Ch] behavior ++= ( for (t ← inputChannels(ru)(tt.tpe)) yield tt.mirror.runtimeClass(t.widen) -> ff(recv)) } } calling channels[_] registers the behaviorsöndag 10 februari 13
  • The Gory Details def impl[LUB, ReplyChannels <: ChannelList, MsgTChan <: ChannelList, MsgT: c.WeakTypeTag, MyCh <: ChannelList: c.WeakTypeTag, ParentCh <: ChannelList: c.WeakTypeTag]( c: Context { type PrefixType = Channels[ParentCh, MyCh] }): c.Expr[(Nothing ⇒ Unit)] = { // some type calculations happen here val prepTree = reify(...) reify { prepTree.splice c.prefix.splice.behaviorist[ (MsgT, ChannelRef[ReplyChannels]) ⇒ Unit, MsgT]( bool(c, false).splice)(universe.typeTag[MsgT]) } }söndag 10 februari 13
  • The Gory Details trait Channels[P <: ChannelList, C <: ChannelList] { this: Actor ⇒ def channel[T]: (Nothing ⇒ Unit) = macro macros.Channel.impl[Any, ChannelList, ChannelList, T, C, P] def behaviorist[R, Ch: ru.TypeTag](wrapped: Boolean) : (R ⇒ Unit) = new Behaviorist[R, Ch](wrapped) // ... }söndag 10 februari 13
  • Sample Type Calculation final def missingChannels(u: Universe)( channels: u.Type, required: List[u.Type]): List[u.Type] = { import u._ // making the top-level method recursive blows up the compiler def rec(ch: Type, req: List[Type]): List[Type] = { ch match { case TypeRef(_, _, TypeRef(_, _, in :: _) :: tail :: Nil) ⇒ rec(tail, req filterNot (_ <:< in)) case last ⇒ req filterNot (_ <:< last) } } rec(channels, required) }söndag 10 februari 13
  • How to Return a Type def impl[..., ReplyChannels <: ChannelList, ...] (c: Context): c.Expr[(Nothing ⇒ Unit)] = { // calculate “channels: u.Type” and then: implicit val ttReplyChannels = c.TypeTag[ReplyChannels](channels) reify { ...[(..., ...[ReplyChannels]) ⇒ ..., ...]... } } “reify” picks up TypeTags and uses their .tpesöndag 10 februari 13
  • Sharing Code with Runtime final def x(u: Universe)(list: u.Type, msg: u.Type) : List[u.Type] = { val imp = u.mkImporter(ru) val tpeReplyChannels = imp.importType(ru.typeOf[ReplyChannels[_]]) val tpeTNil = imp.importType(ru.typeOf[TNil]) // ... } otherwise there will be weird exceptionssöndag 10 februari 13
  • How to test it? def mkToolbox(compileOptions: String = "") : ToolBox[_ <: scala.reflect.api.Universe] = { val m = scala.reflect.runtime.currentMirror m.mkToolBox(options = compileOptions) } def eval(code: String, compileOptions: String = "-cp akka-actor/target/classes:akka-channels/target/classes") : Any = { val tb = mkToolbox(compileOptions) tb.eval(tb.parse(code)) }söndag 10 februari 13
  • How to test it? intercept[ToolBoxError] { eval(""" import akka.channels._ import ChannelSpec._ implicit val c = new ChannelRef[TNil](null) new ChannelRef[(A, C) :+: TNil](null) <-!- B """) }.message must include( "target ChannelRef does not support messages of " + "types akka.channels.ChannelSpec.B.type")söndag 10 februari 13
  • get it and learn more http://akka.io http://letitcrash.com http://typesafe.comsöndag 10 februari 13
  • E0Fsöndag 10 februari 13