-
Notifications
You must be signed in to change notification settings - Fork 0
/
peerdb.go
419 lines (321 loc) · 7.24 KB
/
peerdb.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
// Copyright (c) 2018
// Author: Jeff Weisberg <jaw @ tcp4me.com>
// Created: 2018-Nov-04 08:27 (EST)
// Function: peer db
package kibitz
import (
"expvar"
"sync"
"time"
"github.com/jaw0/kibitz/lamport"
)
const (
KEEPDOWN = 10 * lamport.Minute // keep data about down servers for how long?
KEEPLOST = 10 * lamport.Minute // keep data about servers we have not heard about for how long?
)
var serverupds = expvar.NewInt("kibitz_server_updates")
type infoer interface {
Send(string, time.Duration, PeerImport) ([]PeerImport, error)
Change(string, bool, bool)
Update(string, bool, bool)
Myself(*PeerInfo) PeerImport
}
type Conf struct {
Iface infoer
System string
Seed []string
Id string
Hostname string
Environment string
Datacenter string
Rack string
Promiscuous bool
Port int
}
type DB struct {
iface infoer
sys string
id string
env string
dc string
rack string
host string
promiscuous bool // collect data on all system types?
port int // tcp port
seed []string
myaddrs map[string]string
mydoms map[string]bool
nmon *netMon
netinfo []*NetInfo
bestaddr string
stop chan struct{}
done sync.WaitGroup
clock *lamport.Clock
bootTime uint64
lock sync.RWMutex
allpeers map[string]*Peer
skeptical map[string]*Peer
kibitzers map[string]*Peer
}
func New(c *Conf) *DB {
pdb := &DB{
iface: c.Iface,
sys: c.System,
env: c.Environment,
host: c.Hostname,
dc: c.Datacenter,
rack: c.Rack,
promiscuous: c.Promiscuous,
port: c.Port,
seed: c.Seed,
clock: lamport.New(),
nmon: netMonNew(),
stop: make(chan struct{}),
myaddrs: make(map[string]string),
mydoms: make(map[string]bool),
allpeers: make(map[string]*Peer),
skeptical: make(map[string]*Peer),
kibitzers: make(map[string]*Peer),
}
pdb.bootTime = pdb.clock.Now().Uint64()
if pdb.env == "" {
pdb.env = "dev"
}
pdb.learn(c)
return pdb
}
func (pdb *DB) Start() {
pdb.done.Add(1)
go pdb.periodic()
}
func (pdb *DB) Stop() {
close(pdb.stop)
pdb.done.Wait()
}
// ################################################################
// 3rd party reports
func (pdb *DB) Update(px PeerImport) {
pi := px.GetPeerInfo()
dl.Debug("update peer %s", pi.GetServerId())
if !pdb.isOK(pi) {
return
}
serverupds.Add(1)
pdb.lock.Lock()
defer pdb.lock.Unlock()
// update lamport clock
pdb.clock.Update(lamport.ToTime(pi.GetTimeCreated()))
pdb.clock.Update(lamport.ToTime(pi.GetTimeChecked()))
p := pdb.find(pi.GetServerId())
switch {
case p == nil:
dl.Verbose("discovered new peer %s", pi.GetServerId())
p = peerNew(pdb, px, STATUS_UNKNOWN)
pdb.addPeer(p)
case p.status == STATUS_SCEPTICAL:
dl.Verbose("discovered new peer %s", pi.GetServerId())
pdb.upgrade(p)
default:
dl.Debug("update existing peer %s", pi.GetServerId())
}
// update status
p.Update(px, pdb)
switch p.status {
case STATUS_UP, STATUS_DOWN:
go pdb.iface.Update(pi.GetServerId(), p.status != STATUS_DOWN, p.info.GetSubsystem() == p.pdb.sys)
}
}
// their reports
func (pdb *DB) UpdateSceptical(px PeerImport) {
pi := px.GetPeerInfo()
if !pdb.isOK(pi) {
return
}
serverupds.Add(1)
pdb.lock.Lock()
defer pdb.lock.Unlock()
p := pdb.find(pi.GetServerId())
if p == nil {
dl.Debug("add new scept %s", pi.GetServerId())
p = peerNew(pdb, px, STATUS_SCEPTICAL)
pdb.skeptical[p.id] = p
}
}
//################################################################
// our tests
func (pdb *DB) PeerUp(id string) {
dl.Debug("up %s", id)
pdb.lock.Lock()
defer pdb.lock.Unlock()
p := pdb.find(id)
if p == nil {
return
}
os := p.status
if os == STATUS_SCEPTICAL {
pdb.upgrade(p)
}
p.SetIsUp(pdb.clock.Inc())
if os != STATUS_UP {
dl.Debug("peer %s is now up", id)
}
}
func (pdb *DB) PeerDn(id string) {
dl.Debug("dn %s", id)
pdb.lock.Lock()
defer pdb.lock.Unlock()
p := pdb.find(id)
if p == nil {
return
}
os := p.status
if os == STATUS_SCEPTICAL {
pdb.kill(p)
return
}
p.SetMaybeDn(pdb.clock.Inc())
if os != STATUS_DOWN && p.status == STATUS_DOWN {
dl.Debug("peer %s is now down", id)
}
}
// ################################################################
func (pdb *DB) isOK(pi *PeerInfo) bool {
now := pdb.clock.Now().Uint64()
if pi.GetServerId() == pdb.id {
// NB - updates about ourself get discarded here
return false
}
if pi.GetSubsystem() != pdb.sys && !pdb.promiscuous {
dl.Debug("not ok - sys - %v", pi)
return false
}
if pi.GetEnvironment() != pdb.env {
dl.Debug("not ok - env - %v", pi)
return false
}
if pi.GetTimeCreated() < now-KEEPLOST {
dl.Debug("not ok - Tchk - %v", pi)
return false
}
if pi.GetTimeLastUp() < now-KEEPDOWN {
dl.Debug("not ok - Tup - %v", pi)
return false
}
return true
}
// ################################################################
func (pdb *DB) find(id string) *Peer {
p := pdb.allpeers[id]
if p != nil {
return p
}
p = pdb.skeptical[id]
return p
}
func (pdb *DB) addPeer(p *Peer) {
pdb.allpeers[p.id] = p
if pdb.sys == p.info.GetSubsystem() {
pdb.kibitzers[p.id] = p
}
}
func (pdb *DB) upgrade(p *Peer) {
delete(pdb.skeptical, p.id)
pdb.addPeer(p)
}
func (pdb *DB) kill(p *Peer) {
delete(pdb.allpeers, p.id)
delete(pdb.skeptical, p.id)
delete(pdb.kibitzers, p.id)
p.Kill()
}
// ################################################################
func (p *DB) Rack() string {
return p.rack
}
func (p *DB) Datacenter() string {
return p.dc
}
func (p *DB) Host() string {
return p.host
}
func (p *DB) Env() string {
return p.env
}
func (p *DB) Id() string {
return p.id
}
func (p *DB) DomOK(dom string) bool {
return p.mydoms[dom]
}
func (p *DB) ClockBoot() uint64 {
return p.bootTime
}
func (p *DB) ClockNow() uint64 {
return p.clock.Inc().Uint64()
}
func (pdb *DB) Get(id string) *Peer {
pdb.lock.Lock()
defer pdb.lock.Unlock()
p := pdb.find(id)
if p == nil {
return nil
}
return p
}
// NB - does not include myself
func (pdb *DB) GetAll() []*Peer {
var all []*Peer
pdb.lock.RLock()
defer pdb.lock.RUnlock()
for _, p := range pdb.allpeers {
all = append(all, p)
}
return all
}
func (pdb *DB) ForAllData(fnc func(string, bool, interface{})) {
pdb.lock.RLock()
defer pdb.lock.RUnlock()
for _, p := range pdb.allpeers {
fnc(p.id, p.status == STATUS_UP, p.GetData())
}
// and myself
fnc(pdb.id, true, pdb.Myself())
}
func (pdb *DB) ForAllExport(fnc func(*Export)) {
pdb.lock.RLock()
defer pdb.lock.RUnlock()
for _, p := range pdb.allpeers {
fnc(p.GetExport())
}
}
// ################################################################
func (pdb *DB) periodic() {
for {
pdb.kibitzWithRandomPeer()
pdb.Cleanup()
delay := 5 * time.Second
if len(pdb.kibitzers) == 0 || len(pdb.skeptical) != 0 {
// faster at startup
delay = time.Second
}
select {
case <-pdb.stop:
dl.Debug("done")
pdb.done.Done()
return
case <-time.After(delay):
continue
}
}
}
func (pdb *DB) Cleanup() {
pdb.lock.Lock()
defer pdb.lock.Unlock()
// remove old entries
for id, p := range pdb.allpeers {
if !pdb.isOK(p.info) {
dl.Debug("deleting %s", id)
pdb.kill(p)
}
}
}