-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathconsistent.go
More file actions
96 lines (86 loc) · 2.48 KB
/
Copy pathconsistent.go
File metadata and controls
96 lines (86 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package consistent_hash_code
import (
"errors"
"fmt"
"hash/crc32"
"sort"
)
type Consistent struct {
nodesReplicas int //添加虚拟节点数量
hashSortNodes []uint32 //所有节点的排序数组
circle map[uint32]string //所有节点对应的node
nodes map[string]bool //node是否存在
}
func NewConsistent() *Consistent {
return &Consistent{
nodesReplicas: 20,
circle: make(map[uint32]string),
nodes: make(map[string]bool),
}
}
func (c *Consistent) Add(node string) error {
if _, ok := c.nodes[node]; ok { //判断新加节点是否存在
return fmt.Errorf("%s already existed", node)
}
c.nodes[node] = true
for i := 0; i < c.nodesReplicas; i++ { //添加虚拟节点
replicasKey := getReplicasKey(i, node) //虚拟节点
c.circle[replicasKey] = node
c.hashSortNodes = append(c.hashSortNodes, replicasKey) //所有节点ID
}
sort.Slice(c.hashSortNodes, func(i, j int) bool { //排序
return c.hashSortNodes[i] < c.hashSortNodes[j]
})
return nil
}
func (c *Consistent) Remove(node string) error {
if _, ok := c.nodes[node]; !ok { //判断新加节点是否存在
return fmt.Errorf("%s not existed", node)
}
delete(c.nodes, node)
for i := 0; i < c.nodesReplicas; i++ {
replicasKey := getReplicasKey(i, node)
delete(c.circle, replicasKey) //删除虚拟节点
}
c.refreshHashSortNodes()
return nil
}
func (c *Consistent) GetNode() (node []string) {
for v := range c.nodes {
node = append(node, v)
}
return node
}
func (c *Consistent) Get(key string) (string, error) {
if len(c.nodes) == 0 {
return "", errors.New("not add node")
}
index := c.searchNearbyIndex(key)
host := c.circle[c.hashSortNodes[index]]
return host, nil
}
func (c *Consistent) refreshHashSortNodes() {
c.hashSortNodes = nil
for v := range c.circle {
c.hashSortNodes = append(c.hashSortNodes, v)
}
sort.Slice(c.hashSortNodes, func(i, j int) bool { //排序
return c.hashSortNodes[i] < c.hashSortNodes[j]
})
}
func (c *Consistent) searchNearbyIndex(key string) int {
hashKey := hashKey(key)
index := sort.Search(len(c.hashSortNodes), func(i int) bool { //key算出的节点,距离最近的node节点下标 sort.Search数组需要升序排列好
return c.hashSortNodes[i] >= hashKey
})
if index >= len(c.hashSortNodes) {
index = 0
}
return index
}
func getReplicasKey(i int, node string) uint32 {
return hashKey(fmt.Sprintf("%s#%d", node, i))
}
func hashKey(host string) uint32 {
return crc32.ChecksumIEEE([]byte(host))
}