Skip to content

Instantly share code, notes, and snippets.

@ericacm
Last active December 13, 2015 21:59
Show Gist options
  • Save ericacm/4981652 to your computer and use it in GitHub Desktop.
Save ericacm/4981652 to your computer and use it in GitHub Desktop.
/**
* hosts(i) corresponds to serversKey(i) and serversVal(i)
*
* hosts are "hostname:port" (same as cluster nodeId).
* Note: port is the server's client port. port+1 and port+2 are used for serversVal.
*
* serversKey are "server.1" (see Zookeeper Admin Guide)
* serversVal are "hostname:nnnn:nnnn"
*/
case class ServerNames(hosts: IndexedSeq[String],
serversKey: IndexedSeq[String], serversVal: IndexedSeq[String]) {
def useReplicated = hosts.size > 1
}
def genServerNames(hosts: String): ServerNames = {
val hostsArr = hosts.split(",")
val serversKeyArr = new Array[String](hostsArr.length)
val serversValArr = new Array[String](hostsArr.length)
for ((server, idx) <- hostsArr.zipWithIndex) {
val (host, port) = if (server.contains(":")) {
val parts = server.split(":")
(parts(0), parts(1).toInt)
} else {
(server, clientPort)
}
hostsArr(idx) = host + ":" + port
serversKeyArr(idx) = "server." + (idx+1)
serversValArr(idx) = host + ":" + (port+1) + ":" + (port+2)
}
ServerNames(hostsArr.toIndexedSeq,
serversKeyArr.toIndexedSeq, serversValArr.toIndexedSeq)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment