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(0000-12-30T05:00:00Z, bb85db28-8d04-4c65-b869-8ce4ab78e4bd), // Ended(0004-11-01T20:17:29.116Z, 988adfe5-6905-41c6-9917-111889731997), // Started(3565-10-22T23:30:54.842Z, 38ab4ab3-2096-404b-92b0-59fa40f3db30), // Ended(1970-01-01T23:59:59.059Z, 47718958-f60b-496f-ba1a-272fefd8f45c), // Resumed(1969-12-31T23:59:59.999Z, 20846bfe-841e-4935-b1bb-49faaa0e9d24), // Paused(1970-01-01T00:00:00.001Z, d78d18c5-a537-4711-a91a-3fe8d99b497f), // Resumed(1970-01-01T00:00:00Z, 1b4df831-942d-4f94-bf51-afc2b5a9c07b), // Started(2214-06-01T18:00:15.272Z, 6b9442d0-4b41-4f26-a2eb-9ef7c3f87ec1), // Resumed(3279-10-22T23:59:59.059Z, 056e8c52-cbd2-4683-91a9-1e44641ed006), // Resumed(2986-02-25T06:53:00.286Z, 69f148ea-372a-4303-9ab0-f05f97204458) // ) ``` 😱 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, 4601c355-079f-4980-a4b0-e6e52f65624c), // Ended(2019-11-26T12:01:00Z, 4601c355-079f-4980-a4b0-e6e52f65624c) // ) ``` --- ## 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,b63cc99f-a373-4001-97ed-415bdce999b4) testContext.tick(1.hour) // Ended(2018-12-20T00:30:00Z,b63cc99f-a373-4001-97ed-415bdce999b4) ``` --- ## 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,cffdb936-c255-4366-99ae-725641c2f520) // Started(2018-12-20T01:00:00Z,ca319457-1f66-465e-bf7e-a7f017cb2726) // Started(2018-12-20T01:00:00Z,ec337c83-08d5-4d42-867e-73f11387030f) // Ended(2018-12-20T01:30:00Z,ec337c83-08d5-4d42-867e-73f11387030f) // Ended(2018-12-20T01:30:00Z,ca319457-1f66-465e-bf7e-a7f017cb2726) // Ended(2018-12-20T01:30:00Z,cffdb936-c255-4366-99ae-725641c2f520) // Started(2018-12-20T01:30:00Z,28df084f-3816-4e93-bdaa-609cd0355c40) // Started(2018-12-20T01:30:00Z,b353632c-1c64-40e9-b7d9-01ce6f58048e) // Ended(2018-12-20T02:00:00Z,b353632c-1c64-40e9-b7d9-01ce6f58048e) // Ended(2018-12-20T02:00:00Z,28df084f-3816-4e93-bdaa-609cd0355c40) ``` ??? 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,9bc1903e-c25d-4dc2-8b7a-46fe0e2e9a30) // Started(2018-12-20T02:00:00Z,5ecfd51e-2ece-47d4-8811-3abf16990008) // Started(2018-12-20T02:00:00Z,142ef96c-9aa2-4d61-af87-433a28368aa1) // Paused(2018-12-20T02:00:20Z,5ecfd51e-2ece-47d4-8811-3abf16990008) // Resumed(2018-12-20T02:00:25Z,5ecfd51e-2ece-47d4-8811-3abf16990008) // Ended(2018-12-20T02:20:25Z,5ecfd51e-2ece-47d4-8811-3abf16990008) // Started(2018-12-20T02:20:25Z,2bfb516e-949e-4403-a42d-e5c36a282742) // Paused(2018-12-20T02:20:45Z,2bfb516e-949e-4403-a42d-e5c36a282742) // Resumed(2018-12-20T02:20:50Z,2bfb516e-949e-4403-a42d-e5c36a282742) // Ended(2018-12-20T02:30:00Z,9bc1903e-c25d-4dc2-8b7a-46fe0e2e9a30) // Ended(2018-12-20T02:30:00Z,142ef96c-9aa2-4d61-af87-433a28368aa1) // Ended(2018-12-20T02:40:50Z,2bfb516e-949e-4403-a42d-e5c36a282742) ``` --- ## 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, 1c47db09-0c79-4ca2-b5ff-44ab1a00440d), // Ended(2018-12-20T03:30:00Z, 1c47db09-0c79-4ca2-b5ff-44ab1a00440d) // ) // ) // ) ``` --- ## 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?