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( // Paused(1970-01-31T00:00:00.001Z, 4d71a50b-f26e-4bb3-947e-f2b9c539c917), // Resumed(4000-11-03T20:17:29.116Z, f0349fae-86df-44e4-b754-fc445595153c), // Resumed(0001-12-29T20:17:29.116Z, 61ba7496-f3e3-46f8-a138-b25483e76b20), // Started(+286674005-01-01T01:54:06.255Z, d26859bd-b007-44cf-bd86-c5168946dbc8), // Ended(0324-12-01T13:13:45.031Z, 8071c85b-f8d0-4536-a93f-36c6ec21083d), // Ended(0341-12-31T08:08:51.276Z, 0c477bff-3d4f-420c-b13a-e7ad4553a20e), // Ended(+292269054-12-31T20:14:13.584Z, 5e3eb253-9d38-4bfd-a7e1-2832bb74ad55), // Started(1970-01-01T00:00:00.001Z, 0a93c080-39f7-4cc0-8115-aa6d892e8542), // Started(1970-01-01T00:00:00Z, 547d8c6f-4d53-4f33-8570-e6765ad66090), // Ended(0000-12-30T00:00:00Z, aede2f25-bae3-4ea0-9744-a5d54b20370b) // ) ``` 😱 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, bf0c97b0-a461-4752-8060-060806b268d5), // Ended(2019-11-26T12:01:00Z, bf0c97b0-a461-4752-8060-060806b268d5) // ) ``` --- ## 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,721d9d5a-0b06-4cdd-8e1e-8f0bdb4fe95f) testContext.tick(1.hour) // Ended(2018-12-20T00:30:00Z,721d9d5a-0b06-4cdd-8e1e-8f0bdb4fe95f) ``` --- ## 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,41e893c0-29b9-461a-8ea7-32481d8d640d) // Started(2018-12-20T01:00:00Z,a797016c-68ba-45f6-b6d1-37743491af63) // Started(2018-12-20T01:00:00Z,6067b22f-5c67-438c-80c5-9c884841072a) // Ended(2018-12-20T01:30:00Z,41e893c0-29b9-461a-8ea7-32481d8d640d) // Ended(2018-12-20T01:30:00Z,6067b22f-5c67-438c-80c5-9c884841072a) // Ended(2018-12-20T01:30:00Z,a797016c-68ba-45f6-b6d1-37743491af63) // Started(2018-12-20T01:30:00Z,758d81d5-f914-4649-924e-527d0ae5808b) // Started(2018-12-20T01:30:00Z,b960f986-8ac8-4616-afbe-7537bce55cfc) // Ended(2018-12-20T02:00:00Z,b960f986-8ac8-4616-afbe-7537bce55cfc) // Ended(2018-12-20T02:00:00Z,758d81d5-f914-4649-924e-527d0ae5808b) ``` ??? 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,a4507240-7304-4111-bb97-92c0e147608c) // Started(2018-12-20T02:00:00Z,a4ba15f0-f249-4c9b-bc13-71289933e6b7) // Started(2018-12-20T02:00:00Z,47c86cba-4187-47ce-8fff-04e9426d0de7) // Paused(2018-12-20T02:00:20Z,47c86cba-4187-47ce-8fff-04e9426d0de7) // Resumed(2018-12-20T02:00:25Z,47c86cba-4187-47ce-8fff-04e9426d0de7) // Ended(2018-12-20T02:20:25Z,47c86cba-4187-47ce-8fff-04e9426d0de7) // Started(2018-12-20T02:20:25Z,deb4652e-aaa3-40c8-ae1f-9bd0af572ee6) // Paused(2018-12-20T02:20:45Z,deb4652e-aaa3-40c8-ae1f-9bd0af572ee6) // Resumed(2018-12-20T02:20:50Z,deb4652e-aaa3-40c8-ae1f-9bd0af572ee6) // Ended(2018-12-20T02:30:00Z,a4ba15f0-f249-4c9b-bc13-71289933e6b7) // Ended(2018-12-20T02:30:00Z,a4507240-7304-4111-bb97-92c0e147608c) // Ended(2018-12-20T02:40:50Z,deb4652e-aaa3-40c8-ae1f-9bd0af572ee6) ``` --- ## 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, 9361909c-3306-4509-b8d1-c0550ede25af), // Ended(2018-12-20T03:30:00Z, 9361909c-3306-4509-b8d1-c0550ede25af) // ) // ) // ) ``` --- ## 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?