class: center, middle, inverse # Fake it till you make it with FS2 James Collier @JRCCollier ![FS2 Logo](./assets/fs2-logo.png) --- ## 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 ![device to cloud](./assets/device-to-cloud.png) --- 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. -- ![gen to cloud](./assets/gen-to-cloud.png) --- ## 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( // Started(1969-12-31T23:59:59.999Z, 9721ddaf-e493-45e3-ad77-426c8486830e), // Ended(+292269054-01-01T00:00:00.001Z, bc3e8982-08ea-48f8-b486-a79f45ac4aba), // Resumed(4000-11-03T00:00:00Z, 04ae83da-f1c3-4136-bbf7-30266c452d69), // Started(2009-12-30T04:03:09.691Z, ec997827-5f1c-41e7-bf9c-6c9a297d5c04), // Paused(1490-11-23T19:35:43.358Z, 1089f4c9-a521-4fc0-bc2d-f60e6e9eeda9), // Ended(+292269054-01-01T12:03:33.892Z, d78a0b10-6c30-43df-9ac4-0539dda38e2f), // Started(+148943484-12-31T01:36:22.260Z, 649f3f19-c0e3-433e-a774-b556d4f7faaf), // Resumed(+7887036-01-01T00:00:00.001Z, f3e50717-0151-45c2-9688-704bc7170a80), // Paused(+292269054-01-01T00:00:00.001Z, 4d7252df-8e35-4605-8081-f98e4827fe27), // Resumed(0956-01-30T07:41:08.081Z, 7b489a98-e020-49f1-b2bd-47de165ade2c) // ) ``` 😱 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: ![FS2 Logo](./assets/fs2-logo.png) ```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, 1d2de628-e653-45a6-86ea-abffbb0643b8), // Ended(2019-11-26T12:01:00Z, 1d2de628-e653-45a6-86ea-abffbb0643b8) // ) ``` --- ## 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,f4cecdcf-5d3e-4138-8a0b-7a5715ca8fc2) testContext.tick(1.hour) // Ended(2018-12-20T00:30:00Z,f4cecdcf-5d3e-4138-8a0b-7a5715ca8fc2) ``` --- ## A stream of sessions ![Session streams](./assets/session-streams.png) ??? 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,9b622c58-544e-42b2-8677-698fa2feb7db) // Started(2018-12-20T01:00:00Z,7a62fa52-75b7-4eb5-b40b-65ff31e122eb) // Started(2018-12-20T01:00:00Z,612549ed-e5b0-4996-ae5d-6dac50360119) // Ended(2018-12-20T01:30:00Z,9b622c58-544e-42b2-8677-698fa2feb7db) // Ended(2018-12-20T01:30:00Z,7a62fa52-75b7-4eb5-b40b-65ff31e122eb) // Started(2018-12-20T01:30:00Z,c0d3744e-d56e-4c60-82f1-3f35312a155d) // Ended(2018-12-20T01:30:00Z,612549ed-e5b0-4996-ae5d-6dac50360119) // Started(2018-12-20T01:30:00Z,0cb9ce57-5322-4720-a94b-9d66d924bff7) // Ended(2018-12-20T02:00:00Z,0cb9ce57-5322-4720-a94b-9d66d924bff7) // Ended(2018-12-20T02:00:00Z,c0d3744e-d56e-4c60-82f1-3f35312a155d) ``` ??? 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,eef52acd-7a7a-41e1-8cbc-7d9f0c984f82) // Started(2018-12-20T02:00:00Z,a73a8620-e135-489e-81ac-d12a479bfa3c) // Started(2018-12-20T02:00:00Z,6e34906a-55da-4ad0-b80a-f04aa35a986f) // Paused(2018-12-20T02:00:20Z,a73a8620-e135-489e-81ac-d12a479bfa3c) // Resumed(2018-12-20T02:00:25Z,a73a8620-e135-489e-81ac-d12a479bfa3c) // Ended(2018-12-20T02:20:25Z,a73a8620-e135-489e-81ac-d12a479bfa3c) // Started(2018-12-20T02:20:25Z,23245650-f313-48cb-81c6-d8955046d0af) // Paused(2018-12-20T02:20:45Z,23245650-f313-48cb-81c6-d8955046d0af) // Resumed(2018-12-20T02:20:50Z,23245650-f313-48cb-81c6-d8955046d0af) // Ended(2018-12-20T02:30:00Z,eef52acd-7a7a-41e1-8cbc-7d9f0c984f82) // Ended(2018-12-20T02:30:00Z,6e34906a-55da-4ad0-b80a-f04aa35a986f) // Ended(2018-12-20T02:40:50Z,23245650-f313-48cb-81c6-d8955046d0af) ``` --- ## 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, 47b031cf-3aa9-43ee-b5fb-18d94e14f47f), // Ended(2018-12-20T03:30:00Z, 47b031cf-3aa9-43ee-b5fb-18d94e14f47f) // ) // ) // ) ``` --- ## 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?