Last active
February 12, 2023 00:18
-
-
Save siennathesane/aba5620e9ff7a299e9fba0dc13c0d5d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ShardRouter struct { | |
nh runtime.IHost | |
logger zerolog.Logger | |
hasher hash.Hash64 // fnv.New64a() since we need fast, not secure | |
shardCount atomic.Int64 | |
} | |
// CalcShard is an implementation of Jump Consistent Hash. It computes a string key and returns the shard the key can be | |
// found in. | |
// ref: https://arxiv.org/ftp/arxiv/papers/1406/1406.2294.pdf | |
func (s *ShardRouter) CalcShard(key []byte) (uint64, error) { | |
s.hasher.Reset() | |
_, err := s.hasher.Write(key) | |
if err != nil { | |
return 0, err | |
} | |
h := s.hasher.Sum64() | |
// todo (sienna): I know there's a bug here, I just can't find it | |
if s.shardCount.Load() == 0 { | |
s.shardCount.Store(1) | |
} | |
var b, j int64 | |
for j < s.shardCount.Load() { | |
b = j | |
h = h*2862933555777941757 + 1 | |
j = int64(float64(b+1) * (float64(int64(1)<<31) / float64((h>>33)+1))) | |
} | |
return h, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment