forked from buraksezer/olric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouting.go
561 lines (498 loc) · 15.6 KB
/
routing.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// Copyright 2018-2020 Burak Sezer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package olric
import (
"fmt"
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/buraksezer/consistent"
"github.com/buraksezer/olric/config"
"github.com/buraksezer/olric/internal/discovery"
"github.com/buraksezer/olric/internal/protocol"
"github.com/hashicorp/memberlist"
"github.com/vmihailenco/msgpack"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"
)
var routingUpdateMtx sync.Mutex
var routingSignature uint64
type route struct {
Owners []discovery.Member
Backups []discovery.Member
}
type routingTable map[uint64]route
func (db *Olric) getReplicaOwners(partID uint64) ([]consistent.Member, error) {
for i := db.config.ReplicaCount; i > 0; i-- {
newOwners, err := db.consistent.GetClosestNForPartition(int(partID), i)
if err == consistent.ErrInsufficientMemberCount {
continue
}
if err != nil {
// Fail early
return nil, err
}
return newOwners, nil
}
return nil, consistent.ErrInsufficientMemberCount
}
func (db *Olric) distributeBackups(partID uint64) []discovery.Member {
part := db.backups[partID]
owners := make([]discovery.Member, part.ownerCount())
copy(owners, part.loadOwners())
newOwners, err := db.getReplicaOwners(partID)
if err != nil {
db.log.V(3).Printf("[ERROR] Failed to get replica owners for PartID: %d: %v",
partID, err)
return nil
}
// Remove the primary owner
newOwners = newOwners[1:]
// First run
if len(owners) == 0 {
for _, owner := range newOwners {
owners = append(owners, owner.(discovery.Member))
}
return owners
}
// Prune dead nodes
for i := 0; i < len(owners); i++ {
backup := owners[i]
cur, err := db.discovery.FindMemberByName(backup.Name)
if err != nil {
db.log.V(3).Printf("[ERROR] Failed to find %s in the cluster: %v", backup, err)
// Delete it.
owners = append(owners[:i], owners[i+1:]...)
i--
continue
}
if !cmpMembersByID(backup, cur) {
db.log.V(3).Printf("[WARN] One of the backup owners is probably re-joined: %s", cur)
// Delete it.
owners = append(owners[:i], owners[i+1:]...)
i--
continue
}
}
// Prune empty nodes
for i := 0; i < len(owners); i++ {
backup := owners[i]
req := protocol.NewSystemMessage(protocol.OpLengthOfPart)
req.SetExtra(protocol.LengthOfPartExtra{
PartID: partID,
Backup: true,
})
res, err := db.requestTo(backup.String(), req)
if err != nil {
db.log.V(3).Printf("[ERROR] Failed to check key count on backup "+
"partition: %d: %v", partID, err)
// Pass it. If the node is down, memberlist package will send a leave event.
continue
}
var count int32
err = msgpack.Unmarshal(res.Value(), &count)
if err != nil {
db.log.V(3).Printf("[ERROR] Failed to unmarshal key count "+
"while checking backup partition: %d: %v", partID, err)
// This may be a temporary event. Pass it.
continue
}
if count == 0 {
// Delete it.
owners = append(owners[:i], owners[i+1:]...)
i--
}
}
// Here add the new backup owners.
for _, backup := range newOwners {
var exists bool
for i, bkp := range owners {
if cmpMembersByID(bkp, backup.(discovery.Member)) {
exists = true
// Remove it from the current position
owners = append(owners[:i], owners[i+1:]...)
// Append it again to head
owners = append(owners, backup.(discovery.Member))
break
}
}
if !exists {
owners = append(owners, backup.(discovery.Member))
}
}
return owners
}
func (db *Olric) distributePrimaryCopies(partID uint64) []discovery.Member {
// First you need to create a copy of the owners list. Don't modify the current list.
part := db.partitions[partID]
owners := make([]discovery.Member, part.ownerCount())
copy(owners, part.loadOwners())
// Find the new partition owner.
newOwner := db.consistent.GetPartitionOwner(int(partID))
// First run.
if len(owners) == 0 {
owners = append(owners, newOwner.(discovery.Member))
return owners
}
// Prune dead nodes
for i := 0; i < len(owners); i++ {
owner := owners[i]
current, err := db.discovery.FindMemberByName(owner.Name)
if err != nil {
db.log.V(4).Printf("[ERROR] Failed to find %s in the cluster: %v", owner, err)
owners = append(owners[:i], owners[i+1:]...)
i--
continue
}
if !cmpMembersByID(owner, current) {
db.log.V(4).Printf("[WARN] One of the partitions owners is probably re-joined: %s", current)
owners = append(owners[:i], owners[i+1:]...)
i--
continue
}
}
// Prune empty nodes
for i := 0; i < len(owners); i++ {
owner := owners[i]
req := protocol.NewSystemMessage(protocol.OpLengthOfPart)
req.SetExtra(protocol.LengthOfPartExtra{PartID: partID})
res, err := db.requestTo(owner.String(), req)
if err != nil {
db.log.V(3).Printf("[ERROR] Failed to check key count on partition: %d: %v", partID, err)
// Pass it. If the node is gone, memberlist package will notify us.
continue
}
var count int32
err = msgpack.Unmarshal(res.Value(), &count)
if err != nil {
db.log.V(3).Printf("[ERROR] Failed to unmarshal key count "+
"while checking primary partition: %d: %v", partID, err)
// This may be a temporary issue.
// Pass it. If the node is gone, memberlist package will notify us.
continue
}
if count == 0 {
// Empty partition. Delete it from ownership list.
owners = append(owners[:i], owners[i+1:]...)
i--
}
}
// Here add the new partition newOwner.
for i, owner := range owners {
if cmpMembersByID(owner, newOwner.(discovery.Member)) {
// Remove it from the current position
owners = append(owners[:i], owners[i+1:]...)
// Append it again to head
return append(owners, newOwner.(discovery.Member))
}
}
return append(owners, newOwner.(discovery.Member))
}
func (db *Olric) distributePartitions() routingTable {
table := make(routingTable)
for partID := uint64(0); partID < db.config.PartitionCount; partID++ {
item := table[partID]
item.Owners = db.distributePrimaryCopies(partID)
if db.config.ReplicaCount > config.MinimumReplicaCount {
item.Backups = db.distributeBackups(partID)
}
table[partID] = item
}
return table
}
func (db *Olric) updateRoutingTableOnCluster(table routingTable) (map[discovery.Member]ownershipReport, error) {
data, err := msgpack.Marshal(table)
if err != nil {
return nil, err
}
var mtx sync.Mutex
var g errgroup.Group
ownershipReports := make(map[discovery.Member]ownershipReport)
num := int64(runtime.NumCPU())
sem := semaphore.NewWeighted(num)
for _, member := range db.consistent.GetMembers() {
mem := member.(discovery.Member)
g.Go(func() error {
if err := sem.Acquire(db.ctx, 1); err != nil {
db.log.V(3).Printf("[ERROR] Failed to acquire semaphore to update routing table on %s: %v", mem, err)
return err
}
defer sem.Release(1)
req := protocol.NewSystemMessage(protocol.OpUpdateRouting)
req.SetValue(data)
req.SetExtra(protocol.UpdateRoutingExtra{
CoordinatorID: db.this.ID,
})
// TODO: This blocks whole flow. Use timeout for smooth operation.
resp, err := db.requestTo(mem.String(), req)
if err != nil {
db.log.V(3).Printf("[ERROR] Failed to update routing table on %s: %v", mem, err)
return err
}
ow := ownershipReport{}
err = msgpack.Unmarshal(resp.Value(), &ow)
if err != nil {
db.log.V(3).Printf("[ERROR] Failed to call decode ownership report from %s: %v", mem, err)
return err
}
mtx.Lock()
ownershipReports[mem] = ow
mtx.Unlock()
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
return ownershipReports, nil
}
func (db *Olric) updateRouting() {
// This function is only run by the cluster coordinator.
if !db.discovery.IsCoordinator() {
return
}
// This type of quorum function determines the presence of quorum based on the count of members in the cluster,
// as observed by the local member’s cluster membership manager
nr := atomic.LoadInt32(&db.numMembers)
if db.config.MemberCountQuorum > nr {
db.log.V(2).Printf("[ERROR] Impossible to calculate and update routing table: %v", ErrClusterQuorum)
return
}
// This function is called by listenMemberlistEvents and updateRoutingPeriodically
// So this lock prevents parallel execution.
routingMtx.Lock()
defer routingMtx.Unlock()
table := db.distributePartitions()
reports, err := db.updateRoutingTableOnCluster(table)
if err != nil {
db.log.V(2).Printf("[ERROR] Failed to update routing table on cluster: %v", err)
return
}
db.processOwnershipReports(reports)
}
func (db *Olric) processOwnershipReports(reports map[discovery.Member]ownershipReport) {
check := func(member discovery.Member, owners []discovery.Member) bool {
for _, owner := range owners {
if cmpMembersByID(member, owner) {
return true
}
}
return false
}
ensureOwnership := func(member discovery.Member, partID uint64, part *partition) {
owners := part.loadOwners()
if check(member, owners) {
return
}
// This section is protected by routingMtx against parallel writers.
//
// Copy owners and append the member to head
newOwners := make([]discovery.Member, len(owners))
copy(newOwners, owners)
// Prepend
newOwners = append([]discovery.Member{member}, newOwners...)
part.owners.Store(newOwners)
db.log.V(2).Printf("[INFO] %s still have some data for PartID (backup:%v): %d", member, part.backup, partID)
}
// data structures in this function is guarded by routingMtx
for member, report := range reports {
for _, partID := range report.Partitions {
part := db.partitions[partID]
ensureOwnership(member, partID, part)
}
for _, partID := range report.Backups {
part := db.backups[partID]
ensureOwnership(member, partID, part)
}
}
}
func (db *Olric) processClusterEvent(event *discovery.ClusterEvent) {
db.members.mtx.Lock()
defer db.members.mtx.Unlock()
member, _ := db.discovery.DecodeNodeMeta(event.NodeMeta)
if event.Event == memberlist.NodeJoin {
db.members.m[member.ID] = member
db.consistent.Add(member)
db.log.V(2).Printf("[INFO] Node joined: %s", member)
} else if event.Event == memberlist.NodeLeave {
if _, ok := db.members.m[member.ID]; !ok {
db.log.V(2).Printf("[ERROR] Unknown node left: %s: %d", event.NodeName, member.ID)
return
}
delete(db.members.m, member.ID)
db.consistent.Remove(event.NodeName)
// Don't try to used closed sockets again.
db.client.ClosePool(event.NodeName)
db.log.V(2).Printf("[INFO] Node left: %s", event.NodeName)
} else if event.Event == memberlist.NodeUpdate {
// Node's birthdate may be changed. Close the pool and re-add to the hash ring.
// This takes linear time, but member count should be too small for a decent computer!
for id, item := range db.members.m {
if cmpMembersByName(member, item) {
delete(db.members.m, id)
db.consistent.Remove(event.NodeName)
db.client.ClosePool(event.NodeName)
}
}
db.members.m[member.ID] = member
db.consistent.Add(member)
db.log.V(2).Printf("[INFO] Node updated: %s", member)
} else {
db.log.V(2).Printf("[ERROR] Unknown event received: %v", event)
return
}
// Store the current number of members in the member list.
// We need this to implement a simple split-brain protection algorithm.
db.storeNumMembers()
}
func (db *Olric) listenMemberlistEvents(eventCh chan *discovery.ClusterEvent) {
defer db.wg.Done()
for {
select {
case <-db.ctx.Done():
return
case e := <-eventCh:
db.processClusterEvent(e)
db.updateRouting()
}
}
}
func (db *Olric) updateRoutingPeriodically() {
defer db.wg.Done()
// TODO: Make this parametric.
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for {
select {
case <-db.ctx.Done():
return
case <-ticker.C:
db.updateRouting()
}
}
}
func (db *Olric) checkAndGetCoordinator(id uint64) (discovery.Member, error) {
coordinator, err := db.discovery.FindMemberByID(id)
if err != nil {
return discovery.Member{}, err
}
myCoordinator := db.discovery.GetCoordinator()
if !cmpMembersByID(coordinator, myCoordinator) {
return discovery.Member{}, fmt.Errorf("unrecognized cluster coordinator: %s: %s", coordinator, myCoordinator)
}
return coordinator, nil
}
func (db *Olric) setOwnedPartitionCount() {
var count uint64
for partID := uint64(0); partID < db.config.PartitionCount; partID++ {
part := db.partitions[partID]
if cmpMembersByID(part.owner(), db.this) {
count++
}
}
atomic.StoreUint64(&db.ownedPartitionCount, count)
}
func (db *Olric) updateRoutingOperation(w, r protocol.EncodeDecoder) {
routingUpdateMtx.Lock()
defer routingUpdateMtx.Unlock()
req := r.(*protocol.SystemMessage)
table := make(routingTable)
err := msgpack.Unmarshal(req.Value(), &table)
if err != nil {
db.errorResponse(w, err)
return
}
coordinatorID := req.Extra().(protocol.UpdateRoutingExtra).CoordinatorID
coordinator, err := db.checkAndGetCoordinator(coordinatorID)
if err != nil {
db.log.V(2).Printf("[ERROR] Routing table cannot be updated: %v", err)
db.errorResponse(w, err)
return
}
// Compare partition counts to catch a possible inconsistencies in configuration
if db.config.PartitionCount != uint64(len(table)) {
db.log.V(2).Printf("[ERROR] Routing table cannot be updated. "+
"Expected partition count is %d, got: %d", db.config.PartitionCount, uint64(len(table)))
db.errorResponse(w, ErrInvalidArgument)
return
}
// owners(atomic.value) is guarded by routingUpdateMtx against parallel writers.
// Calculate routing signature. This is useful to control rebalancing tasks.
atomic.StoreUint64(&routingSignature, db.hasher.Sum64(req.Value()))
for partID, data := range table {
// Set partition(primary copies) owners
part := db.partitions[partID]
part.owners.Store(data.Owners)
// Set backup owners
bpart := db.backups[partID]
bpart.owners.Store(data.Backups)
}
db.setOwnedPartitionCount()
// Bootstrapped by the coordinator.
atomic.StoreInt32(&db.bootstrapped, 1)
// Collect report
data, err := db.prepareOwnershipReport()
if err != nil {
db.errorResponse(w, ErrInvalidArgument)
return
}
w.SetStatus(protocol.StatusOK)
w.SetValue(data)
// Call rebalancer to rebalance partitions
db.wg.Add(1)
go func() {
defer db.wg.Done()
db.rebalancer()
// Clean stale dmaps
db.deleteStaleDMaps()
}()
db.log.V(3).Printf("[INFO] Routing table has been pushed by %s", coordinator)
}
type ownershipReport struct {
Partitions []uint64
Backups []uint64
}
func (db *Olric) prepareOwnershipReport() ([]byte, error) {
res := ownershipReport{}
for partID := uint64(0); partID < db.config.PartitionCount; partID++ {
part := db.partitions[partID]
if part.length() != 0 {
res.Partitions = append(res.Partitions, partID)
}
backup := db.backups[partID]
if backup.length() != 0 {
res.Backups = append(res.Backups, partID)
}
}
return msgpack.Marshal(res)
}
func (db *Olric) keyCountOnPartOperation(w, r protocol.EncodeDecoder) {
req := r.(*protocol.SystemMessage)
partID := req.Extra().(protocol.LengthOfPartExtra).PartID
isBackup := req.Extra().(protocol.LengthOfPartExtra).Backup
var part *partition
if isBackup {
part = db.backups[partID]
} else {
part = db.partitions[partID]
}
value, err := msgpack.Marshal(part.length())
if err != nil {
db.errorResponse(w, err)
return
}
w.SetStatus(protocol.StatusOK)
w.SetValue(value)
}