2011-05-17 21:15:27 +02:00
|
|
|
/**
|
2012-01-19 18:21:06 +01:00
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
2011-05-17 21:15:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.routing
|
|
|
|
|
|
2012-09-08 20:54:16 +02:00
|
|
|
import scala.collection.immutable.TreeMap
|
|
|
|
|
|
2011-05-17 21:15:27 +02:00
|
|
|
// =============================================================================================
|
|
|
|
|
// Adapted from HashRing.scala in Debasish Ghosh's Redis Client, licensed under Apache 2 license
|
|
|
|
|
// =============================================================================================
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Consistent Hashing node ring abstraction.
|
|
|
|
|
*
|
2012-09-08 20:54:16 +02:00
|
|
|
* A good explanation of Consistent Hashing:
|
|
|
|
|
* http://weblogs.java.net/blog/tomwhite/archive/2007/11/consistent_hash.html
|
|
|
|
|
*
|
|
|
|
|
* Note that toString of the ring nodes are used for the node
|
|
|
|
|
* hash, i.e. make sure it is different for different nodes.
|
|
|
|
|
*
|
2011-05-17 21:15:27 +02:00
|
|
|
*/
|
2012-09-09 16:47:43 +02:00
|
|
|
class ConsistentHash[T] private (replicas: Int, ring: TreeMap[Int, T]) {
|
2012-09-08 20:54:16 +02:00
|
|
|
|
2012-09-09 16:47:43 +02:00
|
|
|
import ConsistentHash._
|
2011-05-17 21:15:27 +02:00
|
|
|
|
2012-09-09 16:47:43 +02:00
|
|
|
if (replicas < 1) throw new IllegalArgumentException("replicas must be >= 1")
|
2011-05-17 21:15:27 +02:00
|
|
|
|
2012-09-08 20:54:16 +02:00
|
|
|
/**
|
|
|
|
|
* Adds a node to the ring.
|
2012-09-09 16:47:43 +02:00
|
|
|
* Note that the instance is immutable and this
|
|
|
|
|
* operation returns a new instance.
|
2012-09-08 20:54:16 +02:00
|
|
|
*/
|
2012-09-09 16:47:43 +02:00
|
|
|
def :+(node: T): ConsistentHash[T] = {
|
|
|
|
|
new ConsistentHash(replicas, ring ++ ((1 to replicas) map { r ⇒ (nodeHashFor(node, r) -> node) }))
|
2011-05-17 21:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
2012-09-08 20:54:16 +02:00
|
|
|
/**
|
|
|
|
|
* Adds a node to the ring.
|
2012-09-09 16:47:43 +02:00
|
|
|
* Note that the instance is immutable and this
|
|
|
|
|
* operation returns a new instance.
|
2012-09-08 20:54:16 +02:00
|
|
|
* JAVA API
|
|
|
|
|
*/
|
2012-09-09 16:47:43 +02:00
|
|
|
def add(node: T): ConsistentHash[T] = this :+ node
|
2012-09-08 20:54:16 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Removes a node from the ring.
|
2012-09-09 16:47:43 +02:00
|
|
|
* Note that the instance is immutable and this
|
|
|
|
|
* operation returns a new instance.
|
2012-09-08 20:54:16 +02:00
|
|
|
*/
|
2012-09-09 16:47:43 +02:00
|
|
|
def :-(node: T): ConsistentHash[T] = {
|
|
|
|
|
new ConsistentHash(replicas, ring -- ((1 to replicas) map { r ⇒ nodeHashFor(node, r) }))
|
2011-05-17 21:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
2012-09-08 20:54:16 +02:00
|
|
|
/**
|
|
|
|
|
* Removes a node from the ring.
|
2012-09-09 16:47:43 +02:00
|
|
|
* Note that the instance is immutable and this
|
|
|
|
|
* operation returns a new instance.
|
2012-09-08 20:54:16 +02:00
|
|
|
* JAVA API
|
|
|
|
|
*/
|
2012-09-09 16:47:43 +02:00
|
|
|
def remove(node: T): ConsistentHash[T] = this :- node
|
2012-09-08 20:54:16 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the node responsible for the data key.
|
|
|
|
|
* Can only be used if nodes exists in the ring,
|
|
|
|
|
* otherwise throws `IllegalStateException`
|
|
|
|
|
*/
|
2011-05-17 21:15:27 +02:00
|
|
|
def nodeFor(key: Array[Byte]): T = {
|
2012-09-08 20:54:16 +02:00
|
|
|
if (isEmpty) throw new IllegalStateException("Can't get node for [%s] from an empty ring" format key)
|
2011-05-17 21:15:27 +02:00
|
|
|
val hash = hashFor(key)
|
2012-09-08 20:54:16 +02:00
|
|
|
def nextClockwise: T = {
|
|
|
|
|
val (ringKey, node) = ring.rangeImpl(Some(hash), None).headOption.getOrElse(ring.head)
|
|
|
|
|
node
|
2011-05-17 21:15:27 +02:00
|
|
|
}
|
2012-09-08 20:54:16 +02:00
|
|
|
ring.getOrElse(hash, nextClockwise)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Is the ring empty, i.e. no nodes added or all removed.
|
|
|
|
|
*/
|
|
|
|
|
def isEmpty: Boolean = ring.isEmpty
|
|
|
|
|
|
2012-09-09 16:47:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object ConsistentHash {
|
|
|
|
|
def apply[T](nodes: Iterable[T], replicas: Int) = {
|
|
|
|
|
new ConsistentHash(replicas, TreeMap.empty[Int, T] ++
|
|
|
|
|
(for (node ← nodes; replica ← 1 to replicas) yield (nodeHashFor(node, replica) -> node)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Factory method to create a ConsistentHash
|
|
|
|
|
* JAVA API
|
|
|
|
|
*/
|
|
|
|
|
def create[T](nodes: java.lang.Iterable[T], replicas: Int) = {
|
|
|
|
|
import scala.collection.JavaConverters._
|
|
|
|
|
apply(nodes.asScala, replicas)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def nodeHashFor(node: Any, replica: Int): Int = {
|
2012-09-08 20:54:16 +02:00
|
|
|
hashFor((node + ":" + replica).getBytes("UTF-8"))
|
2011-05-17 21:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
2012-09-08 20:54:16 +02:00
|
|
|
private def hashFor(bytes: Array[Byte]): Int = {
|
2011-05-17 21:15:27 +02:00
|
|
|
val hash = MurmurHash.arrayHash(bytes)
|
|
|
|
|
if (hash == Int.MinValue) hash + 1
|
|
|
|
|
math.abs(hash)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|