Last active
December 23, 2015 21:59
-
-
Save helena/6700001 to your computer and use it in GitHub Desktop.
Creates a topology based on configurations. How would you improve it, and how would you write it in Scalaz?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.System.{ currentTimeMillis ⇒ newTimestamp } | |
import scala.util.Try | |
import scala.collection.immutable | |
import scala.collection.JavaConverters._ | |
import akka.actor._ | |
import akka.japi.Util.immutableSeq | |
import com.typesafe.config._ | |
object Topology { | |
def apply(config: Config): Topology = { | |
val regions = config.root.asScala.flatMap { | |
case (key, value: ConfigObject) ⇒ parseConfig(key, value.toConfig) | |
case _ ⇒ None | |
}.toSet | |
Topology(regions) | |
} | |
/** | |
* Partitions proximal dataCenters per region. | |
*/ | |
def parseConfig(region: String, config: Config): Option[Region] = { | |
val dataCenters = config.root.asScala.flatMap { | |
case (dataCenterName, deployed: ConfigObject) ⇒ Try { | |
val dc = deployed.toConfig | |
val id = dc.getInt("zone-id") | |
val proximals = immutableSeq(dc.getIntList("proximal-to")).toIndexedSeq | |
val nearest = proximals map { n ⇒ Nearest(n, proximals indexOf n) } | |
val instances = immutableSeq(dc.getStringList("instances")).map { case host if host.nonEmpty ⇒ host }.toSet | |
DataCenter(id, dataCenterName, nearest, instances) | |
}.toOption filter (_.instances.nonEmpty) | |
case _ ⇒ None | |
} | |
if (dataCenters.nonEmpty) Some(Region(region, dataCenters.toSet)) else None | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment