class: center, middle, inverse # Fake it till you make it with FS2 James Collier @JRCCollier  --- ## Playback Session Telemetry events will be emitted from devices. ```scala import java.time.Instant import java.util.UUID sealed trait PlaybackEvent { def sessionId: UUID } final case class Started(timestamp: Instant, sessionId: UUID) extends PlaybackEvent final case class Paused(timestamp: Instant, sessionId: UUID) extends PlaybackEvent final case class Resumed(timestamp: Instant, sessionId: UUID) extends PlaybackEvent final case class Ended(timestamp: Instant, sessionId: UUID) extends PlaybackEvent ``` --- ## 1000ft view  --- class: inverse ## The catch 😱 it's going to be a while till devices are instrumented! -- The show must go on! -- Need to develop while application teams are instrumenting. --- ## Fake it till we instrument it We have the ~~technology~~ schema, we can build the events. --  --- ## Let's just use scalacheck! ```scala import org.scalacheck.ScalacheckShapeless._ import org.scalacheck.{Arbitrary, Gen} private implicit val arbInstant: Arbitrary[Instant] = Arbitrary(Gen.calendar.map(_.toInstant)) val arbPlaybackEvt = implicitly[Arbitrary[PlaybackEvent]] ``` Now let's generate some events... -- ```scala List.fill(10)(arbPlaybackEvt.arbitrary.sample).flatten // res0: List[PlaybackEvent] = List( // Resumed(4000-11-03T20:17:29.116Z, 0db2a126-4e47-4de7-b0de-0689bb3fb5a8), // Ended(2882-06-25T20:13:34.617Z, 3688c612-c59d-4241-8794-dff98098af30), // Ended(0229-04-15T00:00:00Z, 54c9e69a-e47f-4d02-b261-e5788dbe90c0), // Resumed(0000-12-30T00:00:00Z, d0e42676-4cba-42b2-9165-4b3185d73dd6), // Ended(1969-12-31T23:59:59.999Z, 6726a3fe-12ba-4822-8f42-9e2f3f532a1b), // Paused(4000-11-03T00:00:00Z, 975a8773-f4fa-4bea-9912-b4b4cf5aa1d5), // Paused(4000-11-30T20:17:29.116Z, 9672aa2b-07c5-48a5-9cc1-818ffcb7160c), // Started(1127-05-23T18:41:36.654Z, 25d26ca7-120c-4391-aac3-093e432bc9ee), // Ended(1969-12-31T23:59:59.999Z, b3178a96-f17c-48c1-aef7-9324b58cfe18), // Paused(4000-11-30T20:17:29.116Z, 85e1f28a-295e-475a-b237-baed513837b8) // ) ``` 😱 not useful! ??? * timestamp is nonsense. * each event has it's own session. * not a representative lifecycle. --- ## Requirements * Sessions modelled. * Representative delays between events. * Configure the number of concurrent sessions. -- Maybe this can be modelled as streams? -- Step in FS2:  ```scala import cats.effect._ import fs2.Stream import scala.concurrent.ExecutionContext import scala.concurrent.duration._ ``` --- ## Model a playback scenario ```scala def session: Stream[IO, PlaybackEvent] = for { now <- Stream.emit(Instant.parse("2019-11-26T12:00:00Z")) sessionId <- Stream.eval(IO(UUID.randomUUID())) s <- Stream.emits(List( Started(now, sessionId), Ended(now.plusSeconds(60), sessionId) )) } yield s ``` -- ```scala session.compile.toList.unsafeRunSync() // res2: List[PlaybackEvent] = List( // Started(2019-11-26T12:00:00Z, ef2171bb-4dd0-4b57-9fbb-c5631880f187), // Ended(2019-11-26T12:01:00Z, ef2171bb-4dd0-4b57-9fbb-c5631880f187) // ) ``` --- ## Nap between events 💤💤💤 Useful to model the progress of time between emitting events for a session. -- ```scala def nowS: Stream[IO, Instant] = Stream.eval(timer.clock.realTime(MILLISECONDS).map(Instant.ofEpochMilli)) def idealSession: Stream[IO, PlaybackEvent] = for { sessionId <- Stream.eval(IO(UUID.randomUUID())) s <- nowS.map(Started(_, sessionId)) ++ nowS.map(Ended(_, sessionId)).delayBy(30.minutes) } yield s ``` -- ```scala idealSession .evalMap(event => IO(println(event))) .compile .drain .unsafeRunAsyncAndForget() // Started(2018-12-20T00:00:00Z,9577f393-0c1a-4e2a-9cab-fe48318a4715) testContext.tick(1.hour) // Ended(2018-12-20T00:30:00Z,9577f393-0c1a-4e2a-9cab-fe48318a4715) ``` --- ## A stream of sessions  ??? Par join to choose how many concurrent sessions to have. --- ## Concurrent sessions ```scala val numConcurrent = 3 Stream.emit(idealSession) .repeat .take(5) // limit to 5 for example .parJoin(numConcurrent) .evalMap(event => IO(println(event))) .compile .drain .unsafeRunAsyncAndForget() ``` -- ```scala testContext.tick(1.hour) // Started(2018-12-20T01:00:00Z,39a0d3c2-36d0-408d-9352-56917c5a4924) // Started(2018-12-20T01:00:00Z,e030f935-f049-4a1e-b1a0-06eb9342865e) // Started(2018-12-20T01:00:00Z,c5b447c4-aac4-4439-9719-12e34c6e868c) // Ended(2018-12-20T01:30:00Z,e030f935-f049-4a1e-b1a0-06eb9342865e) // Ended(2018-12-20T01:30:00Z,39a0d3c2-36d0-408d-9352-56917c5a4924) // Ended(2018-12-20T01:30:00Z,c5b447c4-aac4-4439-9719-12e34c6e868c) // Started(2018-12-20T01:30:00Z,7b2c4c6c-252b-434a-97aa-e1d2e4c243e6) // Started(2018-12-20T01:30:00Z,1d6a48c2-6359-4f15-a3d6-e667dca253c9) // Ended(2018-12-20T02:00:00Z,7b2c4c6c-252b-434a-97aa-e1d2e4c243e6) // Ended(2018-12-20T02:00:00Z,1d6a48c2-6359-4f15-a3d6-e667dca253c9) ``` ??? Sessions are repeated forever. --- ## Add another scenario! ```scala def pauseSession: Stream[IO, PlaybackEvent] = for { sessionId <- Stream.eval(IO(UUID.randomUUID())) s <- nowS.map(Started(_, sessionId)) ++ nowS.map(Paused(_, sessionId)).delayBy(20.seconds) ++ nowS.map(Resumed(_, sessionId)).delayBy(5.seconds) ++ nowS.map(Ended(_, sessionId)).delayBy(20.minutes) } yield s ``` --- ## Running multiple scenarios ```scala Stream.emits(List(idealSession, pauseSession)) .repeat .take(4) // limit to 4 for example .parJoin(numConcurrent) .evalMap(event => IO(println(event))) .compile .drain .unsafeRunAsyncAndForget() ``` -- ```scala testContext.tick(1.hour) // Started(2018-12-20T02:00:00Z,add70e06-b6a1-4471-af43-455493108d7c) // Started(2018-12-20T02:00:00Z,6821487e-2941-4726-a828-2c0cec99abb9) // Started(2018-12-20T02:00:00Z,0980e41a-0bed-4dd4-b62e-c2480d391e24) // Paused(2018-12-20T02:00:20Z,6821487e-2941-4726-a828-2c0cec99abb9) // Resumed(2018-12-20T02:00:25Z,6821487e-2941-4726-a828-2c0cec99abb9) // Ended(2018-12-20T02:20:25Z,6821487e-2941-4726-a828-2c0cec99abb9) // Started(2018-12-20T02:20:25Z,dcf936bf-d9c4-463b-be44-ce4c29afac2f) // Paused(2018-12-20T02:20:45Z,dcf936bf-d9c4-463b-be44-ce4c29afac2f) // Resumed(2018-12-20T02:20:50Z,dcf936bf-d9c4-463b-be44-ce4c29afac2f) // Ended(2018-12-20T02:30:00Z,add70e06-b6a1-4471-af43-455493108d7c) // Ended(2018-12-20T02:30:00Z,0980e41a-0bed-4dd4-b62e-c2480d391e24) // Ended(2018-12-20T02:40:50Z,dcf936bf-d9c4-463b-be44-ce4c29afac2f) ``` --- ## Testing Can use `cats.effect.laws.util.TestContext` to simulate time. ```scala val f = idealSession .compile .toList .unsafeToFuture() ``` -- ```scala f.value // res9: Option[scala.util.Try[List[PlaybackEvent]]] = None ``` -- ```scala testContext.tick(1.hour) f.value // res11: Option[scala.util.Try[List[PlaybackEvent]]] = Some( // Success( // List( // Started(2018-12-20T03:00:00Z, 403cea5a-630f-4d2f-963c-9c6a8032bbe7), // Ended(2018-12-20T03:30:00Z, 403cea5a-630f-4d2f-963c-9c6a8032bbe7) // ) // ) // ) ``` --- ## Counting sessions ```scala val f2 = Stream.emit(idealSession) .repeat .parJoin(1000) .interruptAfter(1.hour) .compile .toList // count sessions .map(_.groupBy(_.sessionId).keySet.size) .unsafeToFuture() ``` `idealSession` lasts 30 minutes, so after 1 hour there should be ~2000 sessions. -- ```scala testContext.tick(1.hour) f2.value // res13: Option[scala.util.Try[Int]] = Some(Success(2000)) ``` 👌 --- ## Further reading and links * [FS2](https://github.com/functional-streams-for-scala/fs2) by [@mpilquist](https://github.com/mpilquist) * [cats-effect](https://github.com/typelevel/cats-effect) ## Thanks! * [mdoc](https://github.com/scalameta/mdoc) used to evaluate scala examples ## Questions?