Guest User

Untitled

a guest
Oct 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. package com.klout.api.models
  2.  
  3. import com.klout.playful2.sugar.config
  4.  
  5. /**
  6. * A status for a MySQL node
  7. */
  8. sealed trait MySQLNodeStatus
  9.  
  10. /**
  11. * MySQL is up!
  12. */
  13. case object MySQLUp extends MySQLNodeStatus
  14.  
  15. /**
  16. * MySQL is down! Ops!
  17. */
  18. case object MySQLDown extends MySQLNodeStatus
  19.  
  20. /**
  21. * MySQL is up but f'ed up!
  22. */
  23. case object MySQLDegraded extends MySQLNodeStatus
  24.  
  25. /**
  26. * This is the class used to identify the current state of the MySQL cluster used through Akka
  27. *
  28. * @author Felipe Oliveira [@_felipera]
  29. */
  30. case class MySQLClusterState(nodes: Map[String, MySQLNodeStatus], overrideNodes: Option[List[String]]) {
  31.  
  32. /**
  33. * Define a new state taking in consideration this update
  34. */
  35. def withNodeUpdate(db: String, status: MySQLNodeStatus): MySQLClusterState = {
  36. this copy (nodes = nodes ++ Map(db -> status))
  37. }
  38.  
  39. /**
  40. * Define a new state taking in consideration an update to the list of override nodes
  41. */
  42. def withOverrideNodes(overrideNodes: Option[String]): MySQLClusterState = overrideNodes match {
  43. case Some(value) => this copy (overrideNodes = Option(value.split(",").toList))
  44. case _ => this copy (overrideNodes = None)
  45. }
  46.  
  47. /**
  48. * Get a list of healthy MySQL nodes
  49. */
  50. def getHealthyNodes: List[String] = nodes.filter(_._2 == MySQLUp).keys.toList
  51.  
  52. /**
  53. * Get Current State
  54. */
  55. def currentState: Map[String, Any] = {
  56. val list = nodes map { node =>
  57. (node._1, node._2.toString)
  58. }
  59. Map("nodes" -> list, "overrideNodes" -> overrideNodes.getOrElse(List()))
  60. }
  61.  
  62. }
  63.  
  64. /**
  65. * Companion for the cluster state class defined above
  66. *
  67. * @author Felipe Oliveira [@_felipera]
  68. */
  69. object MySQLClusterState {
  70.  
  71. /**
  72. * Return the list of all possible MySQL nodes in the cluster
  73. */
  74. lazy val allNodes: List[String] = (config string "slave.dbs" !) split "," toList
  75.  
  76. }
Add Comment
Please, Sign In to add comment