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(+91560516-12-31T00:00:00Z, 56d509d0-793f-48d9-bf00-a083667702c6), // Ended(3914-06-30T09:02:45.926Z, b98faff0-1da7-4dcd-a441-89a91b5c5a30), // Paused(2979-04-10T23:59:59.059Z, 022eb5a0-60e0-478b-9bc3-b649603533b7), // Resumed(3014-06-21T21:57:33.788Z, fd420381-3ee5-4c3b-af56-994fce5656a8), // Paused(1592-10-27T00:00:00Z, a64e75d1-c98d-4449-a8e5-b37bfe877937), // Resumed(1969-12-31T23:59:59.999Z, 78bb87ae-93b8-48be-9703-416d8bd1cb92), // Started(4000-11-03T20:17:29.116Z, 48de9b7b-821d-4864-8f48-587f6b5e58fe), // Paused(1258-09-23T12:00:29.469Z, b63d855f-b086-4d4a-a242-fda7d767edb8), // Started(0166-03-14T10:43:00.095Z, 60b91ad1-300e-4da7-9dae-5cfc01ae4f63), // Resumed(0123-12-31T23:19:58.852Z, a9d854e4-90cd-4681-851c-951f4ebab8ae) // ) ``` 😱 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, c4abd44d-c1f8-4a76-a81c-9a885aa57d89), // Ended(2019-11-26T12:01:00Z, c4abd44d-c1f8-4a76-a81c-9a885aa57d89) // ) ``` --- ## 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,6d03b9a5-4e5c-422b-a7dc-a726878730e7) testContext.tick(1.hour) // Ended(2018-12-20T00:30:00Z,6d03b9a5-4e5c-422b-a7dc-a726878730e7) ``` --- ## 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,a1f0f374-84b0-4ad5-a716-ab631948a177) // Started(2018-12-20T01:00:00Z,d51c085f-af3a-4de9-ac34-1f09010f7e5b) // Started(2018-12-20T01:00:00Z,79a86935-87fe-48b4-a55d-0cf8dbf28dd9) // Ended(2018-12-20T01:30:00Z,a1f0f374-84b0-4ad5-a716-ab631948a177) // Ended(2018-12-20T01:30:00Z,79a86935-87fe-48b4-a55d-0cf8dbf28dd9) // Ended(2018-12-20T01:30:00Z,d51c085f-af3a-4de9-ac34-1f09010f7e5b) // Started(2018-12-20T01:30:00Z,69a8b6ff-1ca3-4eed-9411-0ffc31775791) // Started(2018-12-20T01:30:00Z,3fecff2a-db5b-42f9-bfb5-a525f1948d1b) // Ended(2018-12-20T02:00:00Z,69a8b6ff-1ca3-4eed-9411-0ffc31775791) // Ended(2018-12-20T02:00:00Z,3fecff2a-db5b-42f9-bfb5-a525f1948d1b) ``` ??? 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,b66585ea-b3d3-456e-9342-deb207cb9925) // Started(2018-12-20T02:00:00Z,242f6e38-50f3-4fd2-ac6d-0951d77fa66a) // Started(2018-12-20T02:00:00Z,6c42a426-8aa4-4b69-8e4f-413ed09502b5) // Paused(2018-12-20T02:00:20Z,b66585ea-b3d3-456e-9342-deb207cb9925) // Resumed(2018-12-20T02:00:25Z,b66585ea-b3d3-456e-9342-deb207cb9925) // Ended(2018-12-20T02:20:25Z,b66585ea-b3d3-456e-9342-deb207cb9925) // Started(2018-12-20T02:20:25Z,43ecebca-c30a-499d-9dfd-5c1425783555) // Paused(2018-12-20T02:20:45Z,43ecebca-c30a-499d-9dfd-5c1425783555) // Resumed(2018-12-20T02:20:50Z,43ecebca-c30a-499d-9dfd-5c1425783555) // Ended(2018-12-20T02:30:00Z,242f6e38-50f3-4fd2-ac6d-0951d77fa66a) // Ended(2018-12-20T02:30:00Z,6c42a426-8aa4-4b69-8e4f-413ed09502b5) // Ended(2018-12-20T02:40:50Z,43ecebca-c30a-499d-9dfd-5c1425783555) ``` --- ## 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, 1dfa54a3-fb6d-45ca-a8a1-bc0b4a64e3b5), // Ended(2018-12-20T03:30:00Z, 1dfa54a3-fb6d-45ca-a8a1-bc0b4a64e3b5) // ) // ) // ) ``` --- ## 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?