/
consistent.go
178 lines (138 loc) · 2.84 KB
/
consistent.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package algorithm
import (
"hash/crc32"
"sort"
"strconv"
"sync"
)
type errorString struct {
s string
}
func (e *errorString) Error() string {
return "ConsistentError: " + e.s
}
// 定义错误类型
func ConsistentError(text string) error {
return &errorString{text}
}
//默认虚拟节点数
var _DEFAULT_VIRNODECOUNT int = 255
// 定义环类型
type Circle []uint32
func (c Circle) Len() int {
return len(c)
}
func (c Circle) Less(i, j int) bool {
return c[i] < c[j]
}
func (c Circle) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
type Hash func(date []byte) uint32
type Consistent struct {
hash Hash // 产生uint32类型的函数
circle Circle // 环
virtualNodes int // 虚拟节点个数
virtualMap map[uint32]string // 点到主机的映射
members map[string]bool // 主机列表
sync.RWMutex
}
func NewConsisten(nodecount int) *Consistent {
if nodecount == 0 {
nodecount = _DEFAULT_VIRNODECOUNT
}
return &Consistent{
hash: crc32.ChecksumIEEE,
circle: Circle{},
virtualNodes: nodecount,
virtualMap: make(map[uint32]string),
members: make(map[string]bool),
}
}
func (c *Consistent) eltKey(key string, idx int) string {
return key + "|" + strconv.Itoa(idx)
}
func (c *Consistent) updateCricle() {
c.circle = Circle{}
for k := range c.virtualMap {
c.circle = append(c.circle, k)
}
sort.Sort(c.circle)
}
func (c *Consistent) Members() []string {
c.RLock()
defer c.RUnlock()
m := make([]string, len(c.members))
var i = 0
for k := range c.members {
m[i] = k
i++
}
return m
}
func (c *Consistent) Get(key string) string {
hashKey := c.hash([]byte(key))
c.RLock()
defer c.RUnlock()
i := c.search(hashKey)
return c.virtualMap[c.circle[i]]
}
func (c *Consistent) search(key uint32) int {
f := func(x int) bool {
return c.circle[x] >= key
}
i := sort.Search(len(c.circle), f)
i = i - 1
if i < 0 {
i = len(c.circle) - 1
}
return i
}
func (c *Consistent) ForceSet(keys ...string) {
mems := c.Members()
for _, elt := range mems {
var found = false
FOUNDLOOP:
for _, k := range keys {
if k == elt {
found = true
break FOUNDLOOP
}
}
if !found {
c.Remove(elt)
}
}
for _, k := range keys {
c.RLock()
_, ok := c.members[k]
c.RUnlock()
if !ok {
c.Add(k)
}
}
}
func (c *Consistent) Add(elt string) {
c.Lock()
defer c.Unlock()
if _, ok := c.members[elt]; ok {
return
}
c.members[elt] = true
for idx := 0; idx < c.virtualNodes; idx++ {
c.virtualMap[c.hash([]byte(c.eltKey(elt, idx)))] = elt
}
c.updateCricle()
}
func (c *Consistent) Remove(elt string) {
c.Lock()
defer c.Unlock()
if _, ok := c.members[elt]; !ok {
return
}
delete(c.members, elt)
for idx := 0; idx < c.virtualNodes; idx++ {
delete(c.virtualMap, c.hash([]byte(c.eltKey(elt, idx))))
}
c.updateCricle()
}