Module: Polyrun::Partition::Hrw
- Defined in:
- lib/polyrun/partition/hrw.rb
Overview
Rendezvous / highest-hash shard assignment (spec_queue.md): stateless, stable when m changes.
Class Method Summary collapse
- .score(path, shard_index, salt) ⇒ Object
-
.shard_for(path:, total_shards:, seed: "") ⇒ Integer
Shard index in 0…m.
Class Method Details
.score(path, shard_index, salt) ⇒ Object
28 29 30 |
# File 'lib/polyrun/partition/hrw.rb', line 28 def score(path, shard_index, salt) Digest::SHA256.digest("#{salt}\n#{path}\n#{shard_index}").unpack1("H*").hex end |
.shard_for(path:, total_shards:, seed: "") ⇒ Integer
Returns shard index in 0…m.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/polyrun/partition/hrw.rb', line 10 def shard_for(path:, total_shards:, seed: "") m = Integer(total_shards) raise Polyrun::Error, "total_shards must be >= 1" if m < 1 best_j = 0 best = -1 salt = seed.to_s p = path.to_s m.times do |j| h = score(p, j, salt) if h > best best = h best_j = j end end best_j end |