Class: Sequel::SchemaSharding::Ring

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel/schema-sharding/ring.rb

Overview

This class is responsible for mapping IDs into shards, using Ketama consistent hashing algorithm, which makes it easier to rehash in the future, should the number of shards increase. For more information see en.wikipedia.org/wiki/Consistent_hashing This implementation is borrowed from Dali memcached library.

Constant Summary collapse

POINTS_PER_SERVER =
1
PRODUCTION_SHARDS =
8192

Instance Method Summary collapse

Constructor Details

#initialize(shards) ⇒ Ring

Returns a new instance of Ring.



15
16
17
18
19
20
21
# File 'lib/sequel/schema-sharding/ring.rb', line 15

def initialize(shards)
  @number_of_shards = shards.size
  if ENV['RACK_ENV'] == "production"
    raise "Expecting production shards to be #{PRODUCTION_SHARDS}, got #{@number_of_shards}" \
      if @number_of_shards != PRODUCTION_SHARDS
  end
end

Instance Method Details

#shard_for_id(id) ⇒ Object



23
24
25
26
27
# File 'lib/sequel/schema-sharding/ring.rb', line 23

def shard_for_id(id)
  id = id.to_i
  raise "id is passed as zero" if id == 0
  id % @number_of_shards + 1
end