lithammer / go-jump-consistent-hash Goto Github PK
View Code? Open in Web Editor NEW:zap: Fast, minimal memory, consistent hash algorithm
Home Page: https://pkg.go.dev/github.com/lithammer/go-jump-consistent-hash
License: MIT License
:zap: Fast, minimal memory, consistent hash algorithm
Home Page: https://pkg.go.dev/github.com/lithammer/go-jump-consistent-hash
License: MIT License
go-jump-consistent-hash/jump.go
Line 33 in 7f6b38d
I tested hash "123456789" with fnv.New64a and 2 buckets, the expected res is 0, but in goroutine sometimes res is 1.
func TestHashString(t *testing.T) {
hasher := New(2, fnv.New64a())
input := "123456789"
errCount := make(chan int)
for i := 0; i < 1000; i++ {
go func(i int) {
res := hasher.Hash(input)
if res != 0 {
errCount <- 1
t.Errorf("hash %d error: res is %d", i, res)
}
}(i)
}
s := 0
go func() {
for j := range errCount {
s += j
}
}()
time.Sleep(1 * time.Second)
fmt.Println("errCount:", s)
}
I think the HashString function is not goroutine safe.
Really appreciate this library, we use it a lot.
Just wanted to make others aware if you use the examples in highly concurrent code, you will want to create your own unique instance of the hashers rather than the constants in the library as you will get inconsistent results:
Instead of this:
h := jump.HashString("127.0.0.1", 8, jump.CRC64)
You would do this:
h := jump.HashString("127.0.0.1", 8, crc64.New(crc64.MakeTable(crc64.ECMA)))
Just sharing for others.
I think the benifit of consistent-hash is this: when bucket size changed, only a litte group of data need rebalanced
so I wrote a program to test this feature, but it shows that every number got remapped
func main(){
rt := 0
num := 10000
for i:=0; i<num; i++{
var a uint64 = 10000
i1 := jump.Hash(a, 10)
i2 := jump.Hash(a, 20)
if i1 != i2{
rt++
}
}
fmt.Println(rt == num) // ********* true **********
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.