Class: Redis::HashRing
- Inherits:
-
Object
- Object
- Redis::HashRing
- Defined in:
- lib/redis/hash_ring.rb
Constant Summary collapse
- POINTS_PER_SERVER =
this is the default in libmemcached
160
Instance Attribute Summary collapse
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
-
#replicas ⇒ Object
readonly
Returns the value of attribute replicas.
-
#ring ⇒ Object
readonly
Returns the value of attribute ring.
-
#sorted_keys ⇒ Object
readonly
Returns the value of attribute sorted_keys.
Instance Method Summary collapse
-
#add_node(node) ⇒ Object
Adds a ‘node` to the hash ring (including a number of replicas).
-
#get_node(key) ⇒ Object
get the node in the hash ring for this key.
-
#initialize(nodes = [], replicas = POINTS_PER_SERVER) ⇒ HashRing
constructor
nodes is a list of objects that have a proper to_s representation.
- #iter_nodes(key) ⇒ Object
- #remove_node(node) ⇒ Object
Constructor Details
#initialize(nodes = [], replicas = POINTS_PER_SERVER) ⇒ HashRing
nodes is a list of objects that have a proper to_s representation. replicas indicates how many virtual points should be used pr. node, replicas are required to improve the distribution.
15 16 17 18 19 20 21 22 23 |
# File 'lib/redis/hash_ring.rb', line 15 def initialize(nodes = [], replicas = POINTS_PER_SERVER) @replicas = replicas @ring = {} @nodes = [] @sorted_keys = [] nodes.each do |node| add_node(node) end end |
Instance Attribute Details
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
10 11 12 |
# File 'lib/redis/hash_ring.rb', line 10 def nodes @nodes end |
#replicas ⇒ Object (readonly)
Returns the value of attribute replicas.
10 11 12 |
# File 'lib/redis/hash_ring.rb', line 10 def replicas @replicas end |
#ring ⇒ Object (readonly)
Returns the value of attribute ring.
10 11 12 |
# File 'lib/redis/hash_ring.rb', line 10 def ring @ring end |
#sorted_keys ⇒ Object (readonly)
Returns the value of attribute sorted_keys.
10 11 12 |
# File 'lib/redis/hash_ring.rb', line 10 def sorted_keys @sorted_keys end |
Instance Method Details
#add_node(node) ⇒ Object
Adds a ‘node` to the hash ring (including a number of replicas).
26 27 28 29 30 31 32 33 34 |
# File 'lib/redis/hash_ring.rb', line 26 def add_node(node) @nodes << node @replicas.times do |i| key = server_hash_for("#{node.id}:#{i}") @ring[key] = node @sorted_keys << key end @sorted_keys.sort! end |
#get_node(key) ⇒ Object
get the node in the hash ring for this key
46 47 48 49 50 |
# File 'lib/redis/hash_ring.rb', line 46 def get_node(key) hash = hash_for(key) idx = binary_search(@sorted_keys, hash) @ring[@sorted_keys[idx]] end |
#iter_nodes(key) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/redis/hash_ring.rb', line 52 def iter_nodes(key) return [nil, nil] if @ring.empty? crc = hash_for(key) pos = binary_search(@sorted_keys, crc) @ring.size.times do |n| yield @ring[@sorted_keys[(pos + n) % @ring.size]] end end |
#remove_node(node) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/redis/hash_ring.rb', line 36 def remove_node(node) @nodes.reject! { |n| n.id == node.id } @replicas.times do |i| key = server_hash_for("#{node.id}:#{i}") @ring.delete(key) @sorted_keys.reject! { |k| k == key } end end |