*
This commit is contained in:
parent
2570b67f5e
commit
18cc8265c5
@ -36,3 +36,9 @@ hash.AddNode("node3", 3)
|
|||||||
node := hash.GetNode("key")
|
node := hash.GetNode("key")
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
坑点:
|
||||||
|
// 一个9064767行的文件 每行做hashring,结果不一致 原因hashBytes只取4位 529和83 一致 ,这个库是一对多的关系,非多对一
|
||||||
|
// node=1000 vitualSpots=20000
|
||||||
|
//9063340 key=49f57cf0 node=529
|
||||||
|
//9063340 key=49f57cf0 node=83
|
@ -127,7 +127,6 @@ func (h *HashRing) GetNode(s string) string {
|
|||||||
if len(h.nodes) == 0 {
|
if len(h.nodes) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
hash := sha1.New()
|
hash := sha1.New()
|
||||||
hash.Write([]byte(s))
|
hash.Write([]byte(s))
|
||||||
hashBytes := hash.Sum(nil)
|
hashBytes := hash.Sum(nil)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package hashring
|
package hashring
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha1"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -114,3 +115,46 @@ func TestSpeed(t *testing.T) {
|
|||||||
|
|
||||||
hash.AddNodes(nodeWeight)
|
hash.AddNodes(nodeWeight)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestManyTime(t *testing.T) {
|
||||||
|
// 一个9064767行的文件 每行做hashring,结果不一致 原因hashBytes只取4位 529和83 一致 ,这个库是一对多的关系,非多对一
|
||||||
|
// node=1000 vitualSpots=20000
|
||||||
|
//9063340 key=49f57cf0 node=529
|
||||||
|
//9063340 key=49f57cf0 node=83
|
||||||
|
|
||||||
|
hash := sha1.New()
|
||||||
|
hash.Write([]byte("529" + ":" + strconv.Itoa(1)))
|
||||||
|
hashBytes := hash.Sum(nil)
|
||||||
|
a := hashBytes[6:10]
|
||||||
|
|
||||||
|
hash2 := sha1.New()
|
||||||
|
hash2.Write([]byte("83" + ":" + strconv.Itoa(1)))
|
||||||
|
hashBytes2 := hash.Sum(nil)
|
||||||
|
a2 := hashBytes2[6:10]
|
||||||
|
|
||||||
|
fmt.Printf("%s %s\n", a, a2)
|
||||||
|
|
||||||
|
//defer TimeTrack(time.Now())
|
||||||
|
//nodeWeight := make(map[string]int)
|
||||||
|
//for i := 0; i < 1000; i += 1 {
|
||||||
|
// nodeWeight[strconv.Itoa(i)] = 1
|
||||||
|
//}
|
||||||
|
//vitualSpots := 20000
|
||||||
|
//hash := NewHashRing(vitualSpots)
|
||||||
|
//hash.AddNodes(nodeWeight)
|
||||||
|
//for _, nn := range hash.nodes {
|
||||||
|
// if "529" != nn.nodeKey {
|
||||||
|
// fmt.Printf("529 %s\n", nn.nodeKey)
|
||||||
|
// }
|
||||||
|
// if "83" != nn.nodeKey {
|
||||||
|
// fmt.Printf("83 %s\n", nn.nodeKey)
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
//for i := 0; i <= 906476700; i++ {
|
||||||
|
// node := hash.GetNode("49f57cf0")
|
||||||
|
// if "83" != node {
|
||||||
|
// t.Errorf("err:%s\n", node)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user