-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaglev.go
197 lines (140 loc) · 2.88 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
193
194
195
196
197
package maglev
import (
"errors"
"fmt"
"net"
"sort"
"github.com/minio/highwayhash"
)
var KEY1 = []byte("Albert Einstein's IQ is Number 1")
var KEY2 = []byte("Ein8's IQ is 8 times of Einstein")
type backend struct {
IP net.IP
Weight int
}
type Maglev struct {
n int
ips []net.IP
weights []int
permutation [][]uint64
Lookup []int
}
const M = 65537
func NewMaglev(backends []backend) (*Maglev, error) {
n := len(backends)
if n == 0 {
return nil, errors.New("backend list is empty")
}
maglev := new(Maglev)
maglev.n = n
var ips []net.IP
var weights []int
for _, backend := range backends {
ips = append(ips, backend.IP)
weights = append(weights, backend.Weight)
}
maglev.ips = ips
maglev.weights = weights
maglev.genPermutation()
maglev.weightedPopulate()
return maglev, nil
}
func (m *Maglev) calcWeights() []int {
var total int
for _, v := range m.weights {
total += v
}
var allocated int
w := make([]int, m.n)
for j, v := range m.weights {
weight := v * M / total
w[j] = weight
allocated += weight
}
indices := make([]int, m.n)
for i := range m.weights {
indices[i] = i
}
sort.Slice(indices, func(i, j int) bool {
return m.weights[indices[i]] > m.weights[indices[j]]
})
i := 0
for remains := M - allocated; remains > 0; remains-- {
w[indices[i]] += 1
i += 1
}
fmt.Println(w)
return w
}
func (m *Maglev) genPermutation() {
permutation := make([][]uint64, 0)
for _, ip := range m.ips {
offset := highwayhash.Sum64(ip, KEY1) % M
skip := highwayhash.Sum64(ip, KEY2)%(M-1) + 1
p := make([]uint64, M)
var j uint64
for j = 0; j < M; j++ {
p[j] = (offset + j*skip) % M
}
permutation = append(permutation, p)
}
m.permutation = permutation
}
func (m *Maglev) weightedPopulate() {
w := m.calcWeights()
next := make([]int, m.n)
entry := make([]int, M)
for i := range entry {
entry[i] = -1
}
n := 0
for {
for i := 0; i < m.n; i++ {
if w[i] == 0 {
continue
}
c := m.permutation[i][next[i]]
for entry[c] >= 0 {
next[i] = next[i] + 1
c = m.permutation[i][next[i]]
}
entry[c] = i
next[i] = next[i] + 1
n++
w[i]--
if n == M {
m.Lookup = entry
return
}
}
}
}
func (m *Maglev) AddNode(b backend) error {
for _, v := range m.ips {
if b.IP.Equal(v) {
return errors.New("existing entries")
}
}
if m.n > 99 {
return errors.New("backend number reached to 100,rejected")
}
m.n += 1
m.ips = append(m.ips, b.IP)
m.weights = append(m.weights, b.Weight)
m.genPermutation()
m.weightedPopulate()
return nil
}
func (m *Maglev) RemoveNode(ip net.IP) error {
for i, v := range m.ips {
if ip.Equal(v) {
m.ips = append(m.ips[:i], m.ips[i+1:]...)
m.n--
m.weights = append(m.weights[:i], m.weights[i+1:]...)
m.genPermutation()
m.weightedPopulate()
return nil
}
}
return errors.New("ip not found")
}