This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
peers.go
175 lines (140 loc) · 3.15 KB
/
peers.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
package main
import (
"fmt"
"log"
"path"
"strings"
"sync"
"time"
"stathat.com/c/consistent"
)
// TODO testable
const peerSelf = "(self)"
// peers represents a remote list of peers, synced with zookeeper. It's also
// responsible for advertising this particular node's existence.
type peers struct {
shardID string
address string
peers map[peer]bool
ring *consistent.Consistent
lock sync.RWMutex
resetConvergenceTimer chan bool
}
type peer struct {
shardID string
address string
}
func watchPeers(zkWatcher *zkWatcher, shardID, address string) *peers {
p := &peers{
shardID: shardID,
address: address,
peers: make(map[peer]bool),
ring: consistent.New(),
resetConvergenceTimer: make(chan bool),
}
node := path.Join("nodes", fmt.Sprintf("%s@%s", p.shardID, p.address))
zkWatcher.createEphemeral(node)
updates, disconnected := zkWatcher.watchChildren("nodes")
go p.sync(updates, disconnected)
return p
}
func (p *peers) sync(updates chan []string, disconnected chan bool) {
for {
var nodes []string
select {
case nodes = <-updates:
case <-disconnected:
}
select {
case p.resetConvergenceTimer <- true:
default:
}
if nodes != nil {
p.updatePeers(nodes)
}
}
}
func (p *peers) updatePeers(addrs []string) {
p.lock.Lock()
defer p.lock.Unlock()
// Log any new peers.
newPeers := make(map[peer]bool)
shards := make(map[string]bool)
disp := make([]string, 0, len(addrs))
for _, node := range addrs {
parts := strings.SplitN(node, "@", 2)
id := parts[0]
addr := parts[1]
if addr == p.address {
continue
}
peer := peer{shardID: id, address: addr}
disp = append(disp, peer.display())
if !p.peers[peer] {
log.Println("New peer:", peer.display())
}
shards[id] = true
newPeers[peer] = true
}
// Log for any lost peers.
for peer := range p.peers {
if !newPeers[peer] {
log.Println("Lost peer:", peer.display())
}
}
log.Println("Peers: ", disp)
shards[p.shardID] = true
allShards := make([]string, 0, len(shards))
for shard := range shards {
allShards = append(allShards, shard)
}
p.ring.Set(allShards)
p.peers = newPeers
}
func (p *peers) getAll() []string {
p.lock.RLock()
defer p.lock.RUnlock()
addrs := make([]string, 0, len(p.peers))
for peer := range p.peers {
addrs = append(addrs, peer.address)
}
return addrs
}
func (p *peers) pick(partitionId string, n int) []string {
p.lock.RLock()
defer p.lock.RUnlock()
picked, _ := p.ring.GetN(partitionId, n)
shards := make(map[string]bool)
for _, shard := range picked {
shards[shard] = true
}
addrs := make([]string, 0, len(shards))
for peer := range p.peers {
if shards[peer.shardID] {
addrs = append(addrs, peer.address)
}
}
if shards[p.shardID] {
addrs = append(addrs, peerSelf)
}
return addrs
}
func (p *peers) waitToConverge(dur time.Duration) {
log.Printf("Waiting for list of peers to stabilize for %v...", dur)
timer := time.NewTimer(dur)
for {
timer.Reset(dur)
select {
case <-p.resetConvergenceTimer:
case <-timer.C:
return
}
}
}
func (p *peer) display() string {
if p.shardID == p.address {
return p.address
} else {
return fmt.Sprintf("%s (%s)", p.address, p.shardID)
}
}