Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Nelson: Functional programming in system design

Nelson: Functional programming in system design

As functional programmers we work hard to keep things immutable and referentially transparent. However, these noble pursuits often stop with our text editors, the resulting code flung over the wall to be built and deployed in mutable, imperative systems. In this talk we will take a look at Nelson, a deployment orchestration system that applies functional programming not only in its implementation, but also in its system behavior in managing the messy world of deployment infrastructure. Where Free algebras and streams allow the code to be easily extended to support different source repositories, schedulers, and health checkers, Nelson's strict stance on immutable deployments inform important system level decisions such as deployment workflows and service discovery. Having now been run at a couple of companies under different configurations, Nelson serves as a prime example of how functional programming is for the real world and can influence not just code, but systems as well.

Adelbert Chang

March 19, 2018
Tweet

More Decks by Adelbert Chang

Other Decks in Programming

Transcript

  1. B 5.4.5 D 2.1.8 F 1.2.3 G 1.0.9 C 2.3.6

    A 1.1.5 Mutable deployments E 4.12.7
  2. B 5.4.5 D 2.1.8 F 1.2.3 G 1.0.9 C 2.3.6

    A 1.1.5 Mutable deployments E 4.12.7 D 2.2.0
  3. B 5.4.5 D 2.2.0 F 1.2.3 G 1.0.9 C 2.3.6

    A 1.1.5 Mutable deployments E 4.12.7
  4. B 5.4.5 D 2.2.0 F 1.2.3 G 1.0.9 C 2.3.6

    A 1.1.5 Mutable deployments E 4.12.7
  5. F 1.2.3 G 1.0.9 C 2.3.6 Immutable deployments A 1.1.5

    E 4.12.7 B 5.4.5 D 2.1.8 D 2.2.0 E 4.12.8
  6. F 1.2.3 G 1.0.9 C 2.3.6 Immutable deployments A 1.1.5

    B 5.4.5 D 2.1.8 D 2.2.0 E 4.12.8 B 5.4.6
  7. F 1.2.3 G 1.0.9 C 2.3.6 Immutable deployments A 1.1.5

    B 5.4.5 D 2.1.8 D 2.2.0 E 4.12.8 B 5.4.6 A 1.1.6
  8. F 1.2.3 G 1.0.9 C 2.3.6 Immutable deployments B 5.4.5

    D 2.1.8 D 2.2.0 E 4.12.8 B 5.4.6 A 1.1.6
  9. Reify the call graph B 5.4.5 D 2.1.8 F 1.2.3

    G 1.0.9 C 2.3.6 A 1.1.5 E 4.12.7
  10. Reify the call graph B 5.4.5 D 2.1.8 F 1.2.3

    G 1.0.9 C 2.3.6 A 1.1.5 E 4.12.7
  11. Reify the call graph B 5.4.5 D 2.1.8 F 1.2.3

    G 1.0.9 C 2.3.6 A 1.1.5 E 4.12.7
  12. Reify the call graph B 5.4.5 D 2.1.8 F 1.2.3

    G 1.0.9 C 2.3.6 A 1.1.5 E 4.12.7
  13. Reify the call graph B 5.4.5 D 2.1.8 F 1.2.3

    G 1.0.9 C 2.3.6 A 1.1.5 E 4.12.7
  14. Reify the call graph B 5.4.5 D 2.1.8 F 1.2.3

    G 1.0.9 C 2.3.6 A 1.1.5 E 4.12.7
  15. Graph management: traffic shifting C 2.3.6 A 1.1.5 E 4.12.7

    F 1.2.3 G 1.0.9 D 2.1.8 B 5.4.5 C 2.3.7
  16. Graph management: traffic shifting C 2.3.6 A 1.1.5 E 4.12.7

    F 1.2.3 G 1.0.9 D 2.1.8 B 5.4.5 C 2.3.7 B 5.4.6
  17. Graph management: traffic shifting C 2.3.6 A 1.1.5 E 4.12.7

    F 1.2.3 G 1.0.9 D 2.1.8 B 5.4.5 C 2.3.7 B 5.4.6 A 1.1.6
  18. Graph management: traffic shifting C 2.3.6 A 1.1.5 E 4.12.7

    F 1.2.3 G 1.0.9 D 2.1.8 B 5.4.5 C 2.3.7
  19. Graph management: traffic shifting C 2.3.6 A 1.1.5 E 4.12.7

    F 1.2.3 G 1.0.9 D 2.1.8 B 5.4.5 C 2.3.7
  20. Graph management: traffic shifting D 2.1.8 C 2.3.6 A 1.1.5

    B 5.4.5 C 2.3.7 E 4.12.7 F 1.2.3 G 1.0.9
  21. Graph management: pruning A 1.1.5 E 4.12.7 F 1.2.3 G

    1.0.9 D 2.1.8 B 5.4.5 F 1.3.0 C 2.3.7
  22. Graph management: pruning A 1.1.5 E 4.12.7 F 1.2.3 G

    1.0.9 D 2.1.8 B 5.4.5 F 1.3.0 E 4.12.8 C 2.3.7
  23. Graph management: pruning A 1.1.5 F 1.2.3 G 1.0.9 D

    2.1.8 B 5.4.5 F 1.3.0 E 4.12.8 C 2.3.7
  24. D 2.1.8 A 1.1.5 F 1.3.0 E 4.12.8 C 2.3.7

    B 5.4.5 Graph management: pruning
  25. D 2.1.8 A 1.1.5 F 1.3.0 E 4.12.8 C 2.3.7

    B 5.4.5 Graph management: pruning B 5.4.6
  26. D 2.1.8 A 1.1.5 F 1.3.0 E 4.12.8 C 2.3.7

    B 5.4.5 Graph management: pruning B 5.4.6
  27. D 2.1.8 A 1.1.5 F 1.3.0 E 4.12.8 C 2.3.7

    B 5.4.5 Graph management: pruning B 5.4.6
  28. D 2.1.8 A 1.1.5 F 1.3.0 E 4.12.8 C 2.3.7

    Graph management: pruning B 5.4.6
  29. D 2.1.8 A 1.1.5 F 1.3.0 E 4.12.8 C 2.3.7

    Graph management: pruning B 5.4.6
  30. D 2.1.8 A 1.1.5 F 1.3.0 E 4.12.8 C 2.3.7

    Job X 1.8.5 Graph management: pruning B 5.4.6
  31. X 3.2.0 X 3.2.1 X 3.3.0 X 4.0.0 X 4.1.0

    Time retain-latest X 3.2.0 X 3.2.1 X 3.3.0 X 4.0.0 X 4.1.0 retain-latest-two-major
  32. X 3.2.0 X 3.2.1 X 3.3.0 X 4.0.0 X 4.1.0

    Time retain-latest X 3.2.0 X 3.2.1 X 3.3.0 X 4.0.0 X 4.1.0 retain-latest-two-major X 3.2.0 X 3.2.1 X 3.3.0 X 4.0.0 X 4.1.0 retain-latest-two-feature
  33. D 2.1.8 A 1.1.5 F 1.2.4 E 4.12.8 C 2.3.7

    Job X 1.8.5 Graph management: pruning B 5.4.6
  34. D 2.1.8 A 1.1.5 F 1.2.4 E 4.12.8 C 2.3.7

    Job X 1.8.5 Job X 1.9.0 Graph management: pruning B 5.4.6
  35. D 2.1.8 A 1.1.5 F 1.2.4 E 4.12.8 C 2.3.7

    Job X 1.8.5 Job X 1.9.0 Job X 1.8.6 Graph management: pruning B 5.4.6
  36. D 2.1.8 A 1.1.5 F 1.2.4 E 4.12.8 C 2.3.7

    Job X 1.9.0 Job X 1.8.6 Graph management: pruning B 5.4.6
  37. D 2.1.8 A 1.1.5 F 1.2.4 E 4.12.8 C 2.3.7

    Job X 1.9.0 Job X 1.8.6 Graph management: pruning B 5.4.6
  38. D 2.1.8 A 1.1.5 F 1.2.4 E 4.12.8 C 2.3.7

    Job X 1.9.0 Job X 1.8.6 Job X 1.9.1 Graph management: pruning B 5.4.6
  39. D 2.1.8 A 1.1.5 F 1.2.4 E 4.12.8 C 2.3.7

    Job X 1.8.6 Job X 1.9.1 Graph management: pruning B 5.4.6
  40. for { _ <- status(id, Pending, "workflow about to start")

    i <- dockerOps(id, unit, dc.docker.registry) Launch workflow
  41. for { _ <- status(id, Pending, "workflow about to start")

    i <- dockerOps(id, unit, dc.docker.registry) _ <- status(id, Deploying, s"writing alert definitions to ${dc.name}'s consul") _ <- writeAlertsToConsul(sn, ns.name, p.name, unit, p.environment.alertOptOuts) Launch workflow
  42. for { _ <- status(id, Pending, "workflow about to start")

    i <- dockerOps(id, unit, dc.docker.registry) _ <- status(id, Deploying, s"writing alert definitions to ${dc.name}'s consul") _ <- writeAlertsToConsul(sn, ns.name, p.name, unit, p.environment.alertOptOuts) Launch workflow _ <- logToFile(...) _ <- writePolicyToVault(cfg = dc.policy, sn = sn, ns = ns.name, rs = rs)
  43. for { _ <- status(id, Pending, "workflow about to start")

    i <- dockerOps(id, unit, dc.docker.registry) _ <- status(id, Deploying, s"writing alert definitions to ${dc.name}'s consul") _ <- writeAlertsToConsul(sn, ns.name, p.name, unit, p.environment.alertOptOuts) _ <- logToFile(...) _ <- writeDiscoveryToConsul(id, sn, ns.name, dc) Launch workflow _ <- logToFile(...) _ <- writePolicyToVault(cfg = dc.policy, sn = sn, ns = ns.name, rs = rs)
  44. for { _ <- status(id, Pending, "workflow about to start")

    i <- dockerOps(id, unit, dc.docker.registry) _ <- status(id, Deploying, s"writing alert definitions to ${dc.name}'s consul") _ <- writeAlertsToConsul(sn, ns.name, p.name, unit, p.environment.alertOptOuts) _ <- logToFile(...) _ <- writeDiscoveryToConsul(id, sn, ns.name, dc) _ <- getTrafficShift.cata(...) _ <- logToFile(...) Launch workflow _ <- logToFile(...) _ <- writePolicyToVault(cfg = dc.policy, sn = sn, ns = ns.name, rs = rs)
  45. for { _ <- status(id, Pending, "workflow about to start")

    i <- dockerOps(id, unit, dc.docker.registry) _ <- status(id, Deploying, s"writing alert definitions to ${dc.name}'s consul") _ <- writeAlertsToConsul(sn, ns.name, p.name, unit, p.environment.alertOptOuts) _ <- logToFile(...) _ <- writeDiscoveryToConsul(id, sn, ns.name, dc) _ <- getTrafficShift.cata(...) _ <- logToFile(...) Launch workflow _ <- logToFile(...) _ <- writePolicyToVault(cfg = dc.policy, sn = sn, ns = ns.name, rs = rs) l <- launch(i, dc, ns.name, vunit, p, hash) _ <- debug(s"response from scheduler $l")
  46. for { _ <- status(id, Pending, "workflow about to start")

    i <- dockerOps(id, unit, dc.docker.registry) _ <- status(id, Deploying, s"writing alert definitions to ${dc.name}'s consul") _ <- writeAlertsToConsul(sn, ns.name, p.name, unit, p.environment.alertOptOuts) _ <- logToFile(...) _ <- writeDiscoveryToConsul(id, sn, ns.name, dc) _ <- getTrafficShift.cata(...) _ <- logToFile(...) _ <- status(id, getStatus(unit, p), "======> workflow completed <======") } yield () Launch workflow _ <- logToFile(...) _ <- writePolicyToVault(cfg = dc.policy, sn = sn, ns = ns.name, rs = rs) l <- launch(i, dc, ns.name, vunit, p, hash) _ <- debug(s"response from scheduler $l")
  47. logToFile(d.id, s"removing policy from vault: ${vaultLoggingFields(...)}") >> deletePolicyFromVault(d.stackName, ns.name) >>

    Delete workflow logToFile(d.id, s"removing alerts from consul ${alerts.alertingKey(sn)}") >> deleteAlertsFromConsul(d.stackName) >>
  48. logToFile(d.id, s"removing policy from vault: ${vaultLoggingFields(...)}") >> deletePolicyFromVault(d.stackName, ns.name) >>

    Delete workflow logToFile(d.id, ...) >> deleteDiscoveryInfoFromConsul(sn) >> logToFile(d.id, s"removing alerts from consul ${alerts.alertingKey(sn)}") >> deleteAlertsFromConsul(d.stackName) >>
  49. logToFile(d.id, s"removing policy from vault: ${vaultLoggingFields(...)}") >> deletePolicyFromVault(d.stackName, ns.name) >>

    Delete workflow logToFile(d.id, ...) >> deleteDiscoveryInfoFromConsul(sn) >> logToFile(d.id, s"removing alerts from consul ${alerts.alertingKey(sn)}") >> deleteAlertsFromConsul(d.stackName) >> logToFile(d.id, s"instructing ${dc.name}'s scheduler to decommission ${sn}") >> delete(dc,d) >>
  50. logToFile(d.id, s"removing policy from vault: ${vaultLoggingFields(...)}") >> deletePolicyFromVault(d.stackName, ns.name) >>

    Delete workflow logToFile(d.id, ...) >> deleteDiscoveryInfoFromConsul(sn) >> logToFile(d.id, s"removing alerts from consul ${alerts.alertingKey(sn)}") >> deleteAlertsFromConsul(d.stackName) >> logToFile(d.id, s"instructing ${dc.name}'s scheduler to decommission ${sn}") >> delete(dc,d) >> status(d.id, Terminated, s"Decommissioning deployment ${sn} in ${dc.name}")
  51. runBackgroundJob("auditor", cfg.auditor.process(cfg.storage)) runBackgroundJob("pipeline_processor", Process.eval(Pipeline.task(cfg)(Pipeline.sinks.runAction(cfg)))) runBackgroundJob("workflow_logger", cfg.workflowLogger.process) runBackgroundJob("routing_cron", routing.cron.consulRefresh(cfg) to Http4sConsul.consulSink)

    runBackgroundJob("cleanup_pipeline", cleanup.CleanupCron.pipeline(cfg)) runBackgroundJob("sweeper", cleanup.Sweeper.process(cfg)) runBackgroundJob("deployment_monitor", DeploymentMonitor.loop(cfg)) Background processes
  52. type Op0[A] = Coproduct[DockerOp, ConsulOp, A] type Op1[A] = Coproduct[LoggingOp,

    Op0,A] type Op2[A] = Coproduct[StoreOp, Op1, A] type Op3[A] = Coproduct[WorkflowControlOp, Op2,A] type Op4[A] = Coproduct[Vault, Op3, A] type WorkflowOp[A] = Coproduct[SchedulerOp, Op4, A] type WorkflowF[A] = Free.FreeC[WorkflowOp, A] Workflow algebra
  53. final case class Interpreters( scheduler: SchedulerOp ~> Task, consul: ConsulOp

    ~> Task, vault: Vault ~> Task, storage: StoreOp ~> Task, logger: LoggingOp ~> Task, docker: DockerOp ~> Task, control: WorkflowControlOp ~> Task, health: HealthCheckOp ~> Task ) Workflow interpreter(s)