-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathmaglev.go
More file actions
180 lines (145 loc) · 3.54 KB
/
maglev.go
File metadata and controls
180 lines (145 loc) · 3.54 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
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
package maglev
import (
"errors"
"math/big"
"sort"
"sync"
"github.com/dchest/siphash"
)
const (
bigM uint64 = 65537
)
//Maglev :
type Maglev struct {
n uint64 //size of VIP backends
m uint64 //size of the lookup table
permutation [][]uint64
lookup []int64
nodeList []string
lock *sync.RWMutex
}
//NewMaglev :
func NewMaglev(backends []string, m uint64) (*Maglev, error) {
if !big.NewInt(0).SetUint64(m).ProbablyPrime(1) {
return nil, errors.New("Lookup table size is not a prime number")
}
mag := &Maglev{m: m, lock: &sync.RWMutex{}}
if err := mag.Set(backends); err != nil {
return nil, err
}
return mag, nil
}
//Add : Return nil if add success, otherwise return error
func (m *Maglev) Add(backend string) error {
m.lock.Lock()
defer m.lock.Unlock()
// Use binary search since nodeList is always sorted after generatePopulation
index := sort.SearchStrings(m.nodeList, backend)
if index < len(m.nodeList) && m.nodeList[index] == backend {
return errors.New("Exist already")
}
if m.m == m.n {
return errors.New("Number of backends would be greater than lookup table")
}
m.nodeList = append(m.nodeList, backend)
m.n = uint64(len(m.nodeList))
m.generatePopulation()
m.populate()
return nil
}
//Remove :
func (m *Maglev) Remove(backend string) error {
m.lock.Lock()
defer m.lock.Unlock()
index := sort.SearchStrings(m.nodeList, backend)
if index >= len(m.nodeList) || m.nodeList[index] != backend {
return errors.New("Not found")
}
m.nodeList = append(m.nodeList[:index], m.nodeList[index+1:]...)
m.n = uint64(len(m.nodeList))
m.generatePopulation()
m.populate()
return nil
}
func (m *Maglev) Set(backends []string) error {
m.lock.Lock()
defer m.lock.Unlock()
n := uint64(len(backends))
if m.m < n {
return errors.New("Number of backends is greater than lookup table")
}
m.nodeList = make([]string, n)
copy(m.nodeList, backends) // Copy to avoid modifying original input afterwards
m.n = n
m.generatePopulation()
m.populate()
return nil
}
func (m *Maglev) Clear() {
m.lock.Lock()
defer m.lock.Unlock()
m.nodeList = nil
m.permutation = nil
m.lookup = nil
}
//Get :Get node name by object string.
func (m *Maglev) Get(obj string) (string, error) {
m.lock.RLock()
defer m.lock.RUnlock()
if len(m.nodeList) == 0 {
return "", errors.New("Empty")
}
key := m.hashKey(obj)
return m.nodeList[m.lookup[key%m.m]], nil
}
func (m *Maglev) hashKey(obj string) uint64 {
return siphash.Hash(0xdeadbabe, 0, []byte(obj))
}
func (m *Maglev) generatePopulation() {
if len(m.nodeList) == 0 {
m.permutation = nil
return
}
sort.Strings(m.nodeList)
// Pre-allocate permutation slice to avoid repeated memory allocations
m.permutation = make([][]uint64, len(m.nodeList))
for i := 0; i < len(m.nodeList); i++ {
bData := []byte(m.nodeList[i])
offset := siphash.Hash(0xdeadbabe, 0, bData) % m.m
skip := (siphash.Hash(0xdeadbeef, 0, bData) % (m.m - 1)) + 1
iRow := make([]uint64, m.m)
var j uint64
for j = 0; j < m.m; j++ {
iRow[j] = (offset + uint64(j)*skip) % m.m
}
m.permutation[i] = iRow
}
}
func (m *Maglev) populate() {
if len(m.nodeList) == 0 {
return
}
var i, j uint64
next := make([]uint64, m.n)
entry := make([]int64, m.m)
for j = 0; j < m.m; j++ {
entry[j] = -1
}
var n uint64
for { //true
for i = 0; i < m.n; i++ {
c := m.permutation[i][next[i]]
for entry[c] >= 0 {
next[i] = next[i] + 1
c = m.permutation[i][next[i]]
}
entry[c] = int64(i)
next[i] = next[i] + 1
n++
if n == m.m {
m.lookup = entry
return
}
}
}
}