-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.go
101 lines (80 loc) · 1.74 KB
/
events.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
package main
import (
"fmt"
"sync"
"github.com/hashicorp/memberlist"
)
// Join node
type Join string
// Leave node
type Leave string
// Events related membership.
type Events interface {
memberlist.EventDelegate
}
// NewEvents returns an events handler instance.
func NewEvents(mutex sync.Locker, ring Ring, sharder Sharder, cache Cache) Events {
return &events{
mutex: mutex,
ring: ring,
sharder: sharder,
cache: cache,
}
}
type events struct {
mutex sync.Locker
ring Ring
sharder Sharder
cache Cache
}
func (e *events) update() {
var waiter sync.WaitGroup
waiter.Add(1)
go func() {
defer waiter.Done()
e.release()
}()
waiter.Add(1)
go func() {
defer waiter.Done()
e.evict()
}()
waiter.Wait()
}
func (e *events) release() {
// release "old" shards for self (node)
for shard := uint16(0); shard < e.sharder.NumShards(); shard++ {
node := e.ring.NodeOf(shard)
if node != e.ring.Self() {
e.sharder.Release(shard)
}
}
}
func (e *events) evict() {
// evict files not for shards of self (node)
for _, path := range e.cache.List() {
shard := e.sharder.ShardOf(path)
node := e.ring.NodeOf(shard)
if node != e.ring.Self() {
if f, ok := e.cache.Get(path); ok {
f.Close()
}
e.cache.Remove(path)
}
}
}
func (e *events) NotifyJoin(node *memberlist.Node) {
e.mutex.Lock()
defer e.mutex.Unlock()
fmt.Printf("JOIN %s\n", node)
e.ring.Add(node.Name)
e.update()
}
func (e *events) NotifyLeave(node *memberlist.Node) {
e.mutex.Lock()
defer e.mutex.Unlock()
fmt.Printf("LEAVE %s\n", node)
e.ring.Remove(node.Name)
e.update()
}
func (e *events) NotifyUpdate(node *memberlist.Node) {}