-
Notifications
You must be signed in to change notification settings - Fork 0
/
consistent.go
198 lines (165 loc) · 3.38 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package consistent
import (
"encoding/binary"
"unsafe"
"sort"
"github.com/pkg/errors"
)
const DefaultReplica = 20
var (
ErrNoHost = errors.New("no host")
)
type circleEntry struct {
hash uint32
host string
}
type circle []circleEntry
// Len returns the length of the circle array.
func (x circle) Len() int { return len(x) }
// Less returns true if element i is less than element j.
func (x circle) Less(i, j int) bool {
h1, h2 := x[i].hash, x[j].hash
if h1 != h2 {
return h1 < h2
}
return x[i].host < x[j].host
}
// Swap exchanges elements i and j.
func (x circle) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
type Consistent struct {
replica int
circle circle
hosts map[string]struct{}
}
func New() *Consistent {
c := &Consistent{
replica: DefaultReplica,
hosts: map[string]struct{}{},
}
return c
}
func (c *Consistent) SetReplica(replica int) {
if replica <= 0 {
panic(errors.Errorf("wrong replica value"))
}
c.replica = replica
c.rebuildCircle()
}
func (c *Consistent) Add(host string) {
if _, ok := c.hosts[host]; ok {
return
}
c.hosts[host] = struct{}{}
c.rebuildCircle()
}
func (c *Consistent) Remove(host string) {
if _, ok := c.hosts[host]; !ok {
return
}
delete(c.hosts, host)
c.rebuildCircle()
}
func (c *Consistent) Hash(key string) (host string, err error) {
if len(c.circle) == 0 {
return "", ErrNoHost
}
keyhash := c.hashKey(key)
i := sort.Search(len(c.circle), func(i int) bool {
return c.circle[i].hash > keyhash
})
if i >= len(c.circle) {
i = 0
}
return c.circle[i].host, nil
}
func (c *Consistent) hashHost(host string, replicaIdx uint32) uint32 {
return hashPrefix(replicaIdx, unsafeStringToByteSlice(host))
}
func (c *Consistent) hashKey(key string) uint32 {
return hash(unsafeStringToByteSlice(key))
}
func (c *Consistent) rebuildCircle() {
c.circle = make(circle, 0, len(c.hosts)*c.replica)
for host := range c.hosts {
for i := 0; i < c.replica; i++ {
replicaIdx := uint32(i)
c.circle = append(c.circle, circleEntry{
hash: c.hashHost(host, replicaIdx),
host: host,
})
}
}
sort.Sort(c.circle)
}
func unsafeStringToByteSlice(s string) []byte {
return *((*[]byte)(unsafe.Pointer(&s)))
}
// hash return hash of the given data.
func hash(data []byte) uint32 {
// Similar to murmur hash
const (
m = uint32(0xc6a4a793)
r = uint32(24)
)
var (
h = uint32(0xbc9f1d34) ^ (uint32(len(data)) * m)
i int
)
for n := len(data) - len(data)%4; i < n; i += 4 {
h += binary.LittleEndian.Uint32(data[i:])
h *= m
h ^= (h >> 16)
}
switch len(data) - i {
default:
panic("not reached")
case 3:
h += uint32(data[i+2]) << 16
fallthrough
case 2:
h += uint32(data[i+1]) << 8
fallthrough
case 1:
h += uint32(data[i])
h *= m
h ^= (h >> r)
case 0:
}
return h
}
// hash return hash of the given data.
func hashPrefix(prefix uint32, data []byte) uint32 {
// Similar to murmur hash
const (
m = uint32(0xc6a4a793)
r = uint32(24)
)
var (
h = uint32(0xbc9f1d34) ^ (uint32(len(data)) * m)
i int
)
h += prefix
h *= m
h ^= (h >> 16)
for n := len(data) - len(data)%4; i < n; i += 4 {
h += binary.LittleEndian.Uint32(data[i:])
h *= m
h ^= (h >> 16)
}
switch len(data) - i {
default:
panic("not reached")
case 3:
h += uint32(data[i+2]) << 16
fallthrough
case 2:
h += uint32(data[i+1]) << 8
fallthrough
case 1:
h += uint32(data[i])
h *= m
h ^= (h >> r)
case 0:
}
return h
}