-
Notifications
You must be signed in to change notification settings - Fork 0
/
libra.go
304 lines (257 loc) · 6 KB
/
libra.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package libra
import (
"crypto/md5"
"encoding/binary"
"sync"
"sync/atomic"
"time"
ml "github.com/hashicorp/memberlist"
"go.uber.org/zap"
"golang.org/x/sys/cpu"
)
// Star node unit
type Star struct {
ID string
Weight int
}
func NodeToStar(nodes []*ml.Node) []*Star {
stars := make([]*Star, len(nodes))
for i := range nodes {
stars[i] = nodeToStar(nodes[i])
}
return stars
}
func nodeToStar(node *ml.Node) *Star {
return &Star{
ID: node.Name,
Weight: CalWeight(int(binary.LittleEndian.Uint16(node.Meta))),
}
}
const (
noCD uint64 = iota
inCD
)
// Libra ...
type Libra struct {
conf *Config
logger *zap.SugaredLogger
c *Consistent
// event channel
eventCh chan ml.NodeEvent
// gossip based membership and failure detection
list *ml.Memberlist
updateLoadInCD uint64
close chan struct{}
sync.RWMutex
}
func New() (*Libra, error) {
mlConf := ml.DefaultLANConfig()
mlConf.Name = nodeName()
mlConf.BindPort = *Port
mlConf.AdvertisePort = *Port
mlConf.DisableTcpPings = true
sum := md5.Sum([]byte("service-1"))
println(len(sum))
secret := sum[:]
mlConf.SecretKey = secret
println(mlConf.SecretKey)
//mlConf.LogOutput, _ = os.Open(os.DevNull)
conf := &Config{Seeds: []string{
"10.252.2.231:7946",
"10.252.2.231:7947",
"10.252.2.231:7948",
//"10.5.24.19:7946",
//"10.5.24.19:7947",
//"10.252.2.231:7948",
}, Config: mlConf}
return newLibra(conf)
}
func newLibra(conf *Config) (*Libra, error) {
list, err := ml.Create(conf.Config)
if err != nil {
return nil, err
}
list.LocalNode().Meta = make([]byte, 2) // load multiplier
binary.LittleEndian.PutUint16(list.LocalNode().Meta, uint16(2))
conf.Delegate = &Delegate{Memberlist: list}
ch := make(chan ml.NodeEvent, len(conf.Seeds))
conf.Events = &ml.ChannelEventDelegate{
Ch: ch,
}
mul := conf.LoadMultiplier
if mul == 0 {
mul = *LoadMultiplier
}
return &Libra{
conf: conf,
c: NewConsistent(nil),
logger: NewSugar(),
list: list,
eventCh: ch,
}, nil
}
func (l *Libra) Join() {
l.Lock()
defer l.Unlock()
l.logger.Info("joining cluster...")
for {
_, err := l.list.Join(l.conf.Seeds)
l.logger.Info("try joining cluster...")
if err != nil {
l.logger.Errorw("failed to join cluster. will retry in 10 seconds", "error", err)
time.Sleep(10 * time.Second)
continue
}
//if !l.quorum() {
// l.logger.Errorw("failed to join cluster. will retry in 10 seconds", "error", ErrQuorumNotMet)
// time.Sleep(10 * time.Second)
// continue
//}
l.logger.Info("joined cluster")
break
}
l.c.Add(NodeToStar(l.list.Members())...)
l.logger.Info("initiated consistent hash")
}
func (l *Libra) Leave() error {
l.Lock()
defer l.Unlock()
return l.list.Leave(10 * time.Second)
}
func (l *Libra) Serve() {
l.Join()
for {
select {
case <-time.After(5 * time.Second):
// quorum check
if !l.quorum() {
l.logger.Errorw("quorum is not met. try rejoining...", "alive_node_num", l.list.NumMembers())
//l.Join()
}
l.Lock()
nodes := l.list.Members()
//l.logger.Infow("syncing node list", "nodes", nodes)
add, del := l.diff(nodes)
if len(add) > 0 {
l.c.Add(add...)
l.logger.Infow("added new nodes", "nodes", add)
}
if len(del) > 0 {
l.c.Del(del...)
l.logger.Infow("deleted off-duty nodes", "nodes", del)
}
l.Unlock()
case e := <-l.eventCh:
switch e.Event {
case ml.NodeJoin:
l.c.Add(nodeToStar(e.Node))
l.logger.Infow("added new node", "node", e.Node)
case ml.NodeLeave:
l.c.Del(nodeToStar(e.Node))
l.logger.Infow("deleted off-duty node", "node", e.Node)
case ml.NodeUpdate:
err := l.c.Update(nodeToStar(e.Node))
if err != nil {
l.logger.Errorw("failed to update node", "node", e, "error", err)
continue
}
default:
}
//l.logger.Infow("updated node", "len", len(l.c.hash), "node", l.c.hash)
}
}
}
func (l *Libra) diff(newNodes []*ml.Node) (add, del []*Star) {
o := NewMap(newNodes)
defer o.Collect()
return l.c.Stars.Diff(o)
}
func (l *Libra) Address() string {
return l.list.LocalNode().Address()
}
func (l *Libra) UpdateWeight(mul int) error {
if atomic.LoadUint64(&l.updateLoadInCD) == inCD {
return ErrUpdateLoadInCD
}
l.Lock()
defer l.Unlock()
binary.LittleEndian.PutUint16(l.list.LocalNode().Meta, uint16(mul))
err := l.list.UpdateNode(*UpdateTimeout)
if err != nil {
return err
}
err = l.c.Update(nodeToStar(l.list.LocalNode()))
if err != nil {
l.logger.Errorw("failed to update node", "node", "localhost", "error", err)
return err
}
atomic.CompareAndSwapUint64(&l.updateLoadInCD, noCD, inCD)
go l.updateLoadCoolDown()
//l.logger.Infow("updated node", "len", len(l.c.hash), "node", l.c.hash, "node", "localhost")
return nil
}
func (l *Libra) updateLoadCoolDown() {
time.Sleep(60 * time.Second)
atomic.CompareAndSwapUint64(&l.updateLoadInCD, inCD, noCD)
}
var (
countM = 0
countK = 0
countO = 0
)
var aPool = sync.Pool{New: func() interface{} {
countM++
println("alloc new atlas", countM)
return &Atlas{m: make(map[string][]string)}
}}
var akPool = sync.Pool{New: func() interface{} {
countK++
println("alloc new", countK)
return []string{}
}}
var mPool = sync.Pool{New: func() interface{} {
countO++
println("alloc new k", countK)
return make(map[string][]string)
}}
type Atlas struct {
_ cpu.CacheLinePad
m map[string][]string
_ cpu.CacheLinePad
}
func (a *Atlas) Get(id string) []string {
return a.m[id]
}
func (a *Atlas) GetStar(s *Star) []string {
return a.Get(s.ID)
}
func (a *Atlas) Free() {
for k, v := range a.m {
for i := range v {
v[i] = ""
}
v = v[:0]
akPool.Put(v)
delete(a.m, k)
}
aPool.Put(a)
}
func (l *Libra) Get(keys ...string) *Atlas {
l.RLock()
defer l.RUnlock()
if l.quorum() {
return l.c.Get(keys...)
}
l.logger.Errorw("quorum is not met", "alive_node_num", l.list.NumMembers())
return nil
}
func (l *Libra) LocalID() string {
return l.list.LocalNode().Name
}
func (l *Libra) quorum() bool {
q := len(l.conf.Seeds) >> 1
return l.list.NumMembers() > q && l.c.Len() > q
}
func (l *Libra) Shutdown() {
_ = l.logger.Sync()
}