Class: EM::Voldemort::Router
- Inherits:
-
Object
- Object
- EM::Voldemort::Router
- Defined in:
- lib/em-voldemort/router.rb
Overview
For a given request, the router figures out which partition on which node in the cluster it should be sent to. Ruby port of voldemort.routing.ConsistentRoutingStrategy.
Instance Method Summary collapse
-
#initialize(type, replicas) ⇒ Router
constructor
A new instance of Router.
-
#partitions(key, partitions) ⇒ Object
Returns a list of partitions on which a particular key can be found.
Constructor Details
#initialize(type, replicas) ⇒ Router
Returns a new instance of Router.
6 7 8 9 10 |
# File 'lib/em-voldemort/router.rb', line 6 def initialize(type, replicas) raise ClientError, "unsupported routing strategy: #{type}" if !type && type != 'consistent-routing' raise ClientError, "bad number of replicas: #{replicas.inspect}" if !replicas || replicas <= 0 @replicas = replicas end |
Instance Method Details
#partitions(key, partitions) ⇒ Object
Returns a list of partitions on which a particular key can be found.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/em-voldemort/router.rb', line 17 def partitions(key, partitions) master = fnv_hash(key) % partitions.size selected = [master] nodes = [partitions[master]] current = (master + 1) % partitions.size # Walk clockwise around the ring of partitions, starting from the master partition. # The next few unique nodes in ring order are the replicas. while current != master && selected.size < @replicas if !nodes.include? partitions[current] nodes << partitions[current] selected << current end current = (current + 1) % partitions.size end selected end |