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(1970-01-01T00:00:00Z, 54946a63-31ab-4d9c-8096-a7f5d25983d3), // Ended(1969-12-31T23:59:59.059Z, 8699a537-a2cd-497f-9c37-c8b90aef89fa), // Ended(1579-09-16T23:59:59.059Z, f9c1b338-5a73-49e3-9a56-94854d426038), // Paused(3800-10-28T11:02:42.628Z, d674c7e9-471d-4594-8710-4067379b573f), // Started(1969-12-31T23:59:59.999Z, e8178bb4-b055-486e-8f34-e3cc1ab181f0), // Paused(1958-06-16T02:46:25.742Z, b5a3b32c-26c5-4241-abe0-da8dc4612cd5), // Paused(1970-01-01T00:00:00.001Z, 0b7a7936-4461-424c-bfae-9c410b82452f), // Ended(1970-01-31T00:00:00.001Z, bf11438b-ce4b-4072-819c-9ee07ef44be5), // Paused(1739-12-20T03:58:44.257Z, d095d96e-b550-475e-8522-7a4ed5f8c3ca), // Ended(1324-12-20T00:00:00Z, eb5fa352-762e-4a19-9a00-9cd187e359fb) // ) ``` 😱 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, f0e4aad6-a11b-4b10-aa65-24236495e59c), // Ended(2019-11-26T12:01:00Z, f0e4aad6-a11b-4b10-aa65-24236495e59c) // ) ``` --- ## 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,452f1cb1-5b5b-4c45-a8fe-3cbc60675d84) testContext.tick(1.hour) // Ended(2018-12-20T00:30:00Z,452f1cb1-5b5b-4c45-a8fe-3cbc60675d84) ``` --- ## 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,a160b00b-12a3-4f7e-9d58-5886582459ca) // Started(2018-12-20T01:00:00Z,571da93f-ac3d-4f9e-b94a-c15d14f1a47d) // Started(2018-12-20T01:00:00Z,433f442f-70c8-4064-9ceb-044055b70667) // Ended(2018-12-20T01:30:00Z,433f442f-70c8-4064-9ceb-044055b70667) // Ended(2018-12-20T01:30:00Z,571da93f-ac3d-4f9e-b94a-c15d14f1a47d) // Ended(2018-12-20T01:30:00Z,a160b00b-12a3-4f7e-9d58-5886582459ca) // Started(2018-12-20T01:30:00Z,ae740b1a-85c1-4f17-a236-714f464a4760) // Started(2018-12-20T01:30:00Z,59a77519-3f63-4c1a-9f2d-958e5ad29deb) // Ended(2018-12-20T02:00:00Z,59a77519-3f63-4c1a-9f2d-958e5ad29deb) // Ended(2018-12-20T02:00:00Z,ae740b1a-85c1-4f17-a236-714f464a4760) ``` ??? 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,3c07cff7-0cff-4cbe-862f-c60b30385bf2) // Started(2018-12-20T02:00:00Z,a9c4c076-fca6-4489-8d04-8730d702ef0b) // Started(2018-12-20T02:00:00Z,13e60401-f82b-4d36-9fdc-3741cf3eb156) // Paused(2018-12-20T02:00:20Z,3c07cff7-0cff-4cbe-862f-c60b30385bf2) // Resumed(2018-12-20T02:00:25Z,3c07cff7-0cff-4cbe-862f-c60b30385bf2) // Ended(2018-12-20T02:20:25Z,3c07cff7-0cff-4cbe-862f-c60b30385bf2) // Started(2018-12-20T02:20:25Z,605c0f6f-3b8a-4bfe-9c64-0c40f710dd87) // Paused(2018-12-20T02:20:45Z,605c0f6f-3b8a-4bfe-9c64-0c40f710dd87) // Resumed(2018-12-20T02:20:50Z,605c0f6f-3b8a-4bfe-9c64-0c40f710dd87) // Ended(2018-12-20T02:30:00Z,a9c4c076-fca6-4489-8d04-8730d702ef0b) // Ended(2018-12-20T02:30:00Z,13e60401-f82b-4d36-9fdc-3741cf3eb156) // Ended(2018-12-20T02:40:50Z,605c0f6f-3b8a-4bfe-9c64-0c40f710dd87) ``` --- ## 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, 447c38e0-8eab-4369-8bf5-dafa7a241852), // Ended(2018-12-20T03:30:00Z, 447c38e0-8eab-4369-8bf5-dafa7a241852) // ) // ) // ) ``` --- ## 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?