Skip to content

Instantly share code, notes, and snippets.

@feliperazeek
Created June 16, 2012 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feliperazeek/2942664 to your computer and use it in GitHub Desktop.
Save feliperazeek/2942664 to your computer and use it in GitHub Desktop.
package com.klout.api.models
import com.klout.playful2.sugar.config
/**
* A status for a MySQL node
*/
sealed trait MySQLNodeStatus
/**
* MySQL is up!
*/
case object MySQLUp extends MySQLNodeStatus
/**
* MySQL is down! Ops!
*/
case object MySQLDown extends MySQLNodeStatus
/**
* MySQL is up but f'ed up!
*/
case object MySQLDegraded extends MySQLNodeStatus
/**
* This is the class used to identify the current state of the MySQL cluster used through Akka
*
* @author Felipe Oliveira [@_felipera]
*/
case class MySQLClusterState(nodes: Map[String, MySQLNodeStatus], overrideNodes: Option[List[String]]) {
/**
* Define a new state taking in consideration this update
*/
def withNodeUpdate(db: String, status: MySQLNodeStatus): MySQLClusterState = {
this copy (nodes = nodes ++ Map(db -> status))
}
/**
* Define a new state taking in consideration an update to the list of override nodes
*/
def withOverrideNodes(overrideNodes: Option[String]): MySQLClusterState = overrideNodes match {
case Some(value) => this copy (overrideNodes = Option(value.split(",").toList))
case _ => this copy (overrideNodes = None)
}
/**
* Get a list of healthy MySQL nodes
*/
def getHealthyNodes: List[String] = nodes.filter(_._2 == MySQLUp).keys.toList
/**
* Get Current State
*/
def currentState: Map[String, Any] = {
val list = nodes map { node =>
(node._1, node._2.toString)
}
Map("nodes" -> list, "overrideNodes" -> overrideNodes.getOrElse(List()))
}
}
/**
* Companion for the cluster state class defined above
*
* @author Felipe Oliveira [@_felipera]
*/
object MySQLClusterState {
/**
* Return the list of all possible MySQL nodes in the cluster
*/
lazy val allNodes: List[String] = (config string "slave.dbs" !) split "," toList
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment