/
maglev.go
192 lines (167 loc) · 3.78 KB
/
maglev.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
// Package maglev implements maglev consistent hashing.
//
// http://research.google.com/pubs/pub44824.html
package maglev
import (
"math/big"
"sort"
"sync"
"github.com/dchest/siphash"
"github.com/sipb/spike/common"
)
// Prime numbers of varying scale
const (
SmallM = 65537
BigM = 655373
)
const (
offsetKey = uint64(0x35d53c5371bdf886)
skipKey = uint64(0x9e1dbc702649df3a)
)
type permutation struct {
weight uint
offset uint64
skip uint64
}
// Table represents a Maglev hashing table.
type Table struct {
m uint64 // size of the lookup table
permutations map[*common.Backend]permutation
lookup []*common.Backend
mutex sync.RWMutex
}
// New returns a new Maglev table with the specified size.
func New(m uint64) *Table {
if !(&big.Int{}).SetUint64(m).ProbablyPrime(0) {
panic("m is not prime")
}
return &Table{
m: m,
permutations: make(map[*common.Backend]permutation),
}
}
// A Config is a mapping from backends to weights.
type Config map[*common.Backend]uint
// Reconfig reconfigures the table with the given backend weight
// configuration.
func (t *Table) Reconfig(c Config) {
t.mutex.Lock()
defer t.mutex.Unlock()
t.permutations = make(map[*common.Backend]permutation)
for b, w := range c {
if b == nil {
panic("nil backend in config")
}
if w == 0 {
continue
}
t.permutations[b] = permutation{
weight: w,
offset: siphash.Hash(offsetKey, 0, []byte(b.IP)) % t.m,
skip: siphash.Hash(skipKey, 0, []byte(b.IP))%(t.m-1) + 1,
}
}
t.populate()
}
// SetWeight sets the weight of the given backend to weight, adding it
// to the table if necessary.
func (t *Table) SetWeight(backend *common.Backend, weight uint) {
t.mutex.Lock()
defer t.mutex.Unlock()
if backend == nil {
panic("backend is nil")
}
if weight == 0 {
delete(t.permutations, backend)
t.populate()
return
}
p, ok := t.permutations[backend]
if ok {
p.weight = weight
t.permutations[backend] = p
} else {
t.permutations[backend] = permutation{
weight: weight,
offset: siphash.Hash(offsetKey, 0, []byte(backend.IP)) % t.m,
skip: siphash.Hash(skipKey, 0, []byte(backend.IP))%(t.m-1) + 1,
}
}
t.populate()
}
// Add adds a backend to the table with weight 1.
func (t *Table) Add(backend *common.Backend) {
t.SetWeight(backend, 1)
}
// Remove removes a backend from the table.
func (t *Table) Remove(backend *common.Backend) {
t.mutex.Lock()
defer t.mutex.Unlock()
delete(t.permutations, backend)
t.populate()
}
// Lookup looks up a key in the table and returns the associated
// backend, or false if there are no backends.
func (t *Table) Lookup(key uint64) (*common.Backend, bool) {
t.mutex.RLock()
defer t.mutex.RUnlock()
if t.lookup == nil {
return nil, false
}
return t.lookup[key%t.m], true
}
func (t *Table) populate() {
nonzero := false
for _, p := range t.permutations {
if p.weight > 0 {
nonzero = true
break
}
}
if !nonzero {
t.lookup = nil
return
}
type bstate struct {
backend *common.Backend
loc uint64
permutation
}
state := make([]bstate, 0, len(t.permutations))
for b, p := range t.permutations {
state = append(state, bstate{b, p.offset, p})
}
// sort state to guarantee consistency given identical configurations
sort.Slice(state, func(i, j int) bool {
return state[i].offset < state[j].offset
})
entry := make([]*common.Backend, t.m)
for j := range entry {
entry[j] = nil
}
var inserted uint64
for {
for i, s := range state {
for j := uint(0); j < s.weight; j++ {
c := s.loc
for entry[c] != nil {
c += s.skip
if c >= t.m {
c -= t.m
}
}
entry[c] = s.backend
c += s.skip
if c >= t.m {
c -= t.m
}
state[i].loc = c
inserted++
if inserted == t.m {
t.lookup = entry
return
}
}
}
}
}