-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.go
469 lines (412 loc) · 10.1 KB
/
lock.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
package fleet
import (
"context"
"encoding/binary"
"fmt"
"io"
"log/slog"
"runtime"
"sync"
"sync/atomic"
"time"
)
type LocalLock struct {
lk *globalLock
once sync.Once // once is used to ensure release() is done only once
}
type globalLock struct {
name string
t uint64
owner string // owner node
ch chan uint32 // wait channel for lock release or updates
status uint32 // 0=new 1=confirmed 2=failed
local bool
lk sync.Mutex // for aye/nay updates
aye []string // peers ids approving lock (if local)
nay []string // peers ids rejecting lock (if local)
a *Agent
timeout time.Time
}
// load a lock from the db
func (a *Agent) getLock(name string) *globalLock {
a.globalLocksLk.RLock()
defer a.globalLocksLk.RUnlock()
v, ok := a.globalLocks[name]
if ok && v.valid() {
return v
}
return nil
}
func (a *Agent) getLocks() []*globalLock {
a.globalLocksLk.RLock()
defer a.globalLocksLk.RUnlock()
res := make([]*globalLock, 0, len(a.globalLocks))
for _, l := range a.globalLocks {
res = append(res, l)
}
return res
}
func (a *Agent) DebugLocks(w io.Writer) {
lks := a.getLocks()
fmt.Fprintf(w, "Locks:\n")
for _, l := range lks {
fmt.Fprintf(w, " * %s t=%d owner=%s status=%d local=%v timeout=%s\n", l.name, l.t, l.owner, l.status, l.local, l.timeout)
}
}
// create a new lock
func (a *Agent) makeLock(name, owner string, tm uint64, force bool) *globalLock {
a.globalLocksLk.Lock()
defer a.globalLocksLk.Unlock()
if v, ok := a.globalLocks[name]; ok {
if !force && v.valid() {
return nil
}
// disable lock
v.setStatus(2)
v.broadcastRelease()
}
lk := &globalLock{
name: name,
owner: owner,
t: tm,
ch: make(chan uint32, 1),
a: a,
timeout: time.Now().Add(30 * time.Minute),
}
// lock it now
lk.lk.Lock()
a.globalLocks[name] = lk
return lk
}
func (l *globalLock) release() {
l.broadcastRelease()
l.dereg()
}
func (l *globalLock) broadcastRelease() {
slog.Debug(fmt.Sprintf("[fleet] releasing lock %s %d %s", l.name, l.t, l.owner), "event", "fleet:lock:release")
if l.local {
// broadcast release
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
l.a.BroadcastPacket(ctx, PacketLockRelease, l.Key())
}
}
func (l *globalLock) dereg() {
l.a.globalLocksLk.Lock()
defer l.a.globalLocksLk.Unlock()
v, ok := l.a.globalLocks[l.name]
if !ok || v != l {
// wrong call?
//log.Printf("[fleet] lock not released because ok=%v v=%p l=%p", ok, v, l)
return
}
delete(l.a.globalLocks, l.name)
close(l.ch)
}
func (l *globalLock) Key() []byte {
return codeLockBytes(l.name, l.t, l.owner)
}
// generate a []byte of a lock name's stamp and owner
func codeLockBytes(name string, t uint64, owner string) []byte {
v := make([]byte, 8+2+len(name)+len(owner))
s := v
s[0] = byte(len(name))
copy(s[1:], name)
s = s[len(name)+1:]
binary.BigEndian.PutUint64(s[:8], t)
s = s[8:]
s[0] = byte(len(owner))
copy(s[1:], owner)
return v
}
// reads a []byte containing info and returns data
func decodeLockBytes(v []byte) (string, uint64, string, []byte) {
// return: name, stamp, owner, and remaining of v
// check if v is long enough
if len(v) < 10 {
return "", 0, "", nil
}
nameLen := v[0]
if len(v) < int(nameLen)+1+8+1 { // nameLen, stamp, ownerLen
return "", 0, "", nil
}
name := v[1 : int(nameLen)+1]
v = v[int(nameLen)+1:]
t := binary.BigEndian.Uint64(v[:8])
v = v[8:]
ownerLen := v[0]
if len(v) < int(ownerLen)+1 {
return "", 0, "", nil
}
owner := v[1 : ownerLen+1]
v = v[ownerLen+1:]
return string(name), t, string(owner), v
}
func (a *Agent) Lock(ctx context.Context, name string) (*LocalLock, error) {
// Lock function attempts to grab a lock and get it confirmed globally
// if >= (1/2+1) of nodes respond aye, the lock is confirmed and this function returns
// if >= (1/3+1) of nodes respond nay, lock acquire fails and is retried unless ctx timeouts
if name == "" {
return nil, ErrInvalidLockName
}
slog.Debug(fmt.Sprintf("[fleet] Attempting to acquire lock %s", name), "event", "fleet:lock:acquiretry")
start := time.Now()
// first, let's check if this isn't already locked, if it is, wait
for {
lk := a.getLock(name)
if lk != nil {
// lock is already acquired or attempting to be acquired by someone
select {
case <-lk.ch:
// something has changed (lock released?)
case <-ctx.Done():
return nil, ctx.Err()
}
continue
}
// let's catch a lock
tm := UniqueTimestamp()
lk = a.makeLock(name, a.id, tm, false)
if lk == nil {
// failed to acquire, retry
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
// do not wait
}
continue
}
lk.local = true
lk.lk.Unlock()
slog.Debug(fmt.Sprintf("[fleet] Lock %s acquire attempt with t=%d", name, tm), "event", "fleet:lock:attempt")
if a.GetPeersCount() <= 1 {
// we can't have global locks with no peers
lk.setStatus(1)
res := &LocalLock{lk: lk}
runtime.SetFinalizer(res, finalizeLocalLock)
slog.Debug(fmt.Sprintf("[fleet] Lock %s acquired in %s (no other peers)", name, time.Since(start)), "event", "fleet:lock:acquire_solo")
return res, nil
}
// attempt acquire
timeout := time.NewTimer(5 * time.Second)
go a.BroadcastPacket(context.Background(), PacketLockReq, lk.Key())
acqLoop:
for {
select {
case st, ok := <-lk.ch:
if !ok {
// lock was cancelled externally
timeout.Stop()
return nil, ErrCancelledLock
}
if st == 0 {
// nothing new
break
}
if st == 1 {
// ready
res := &LocalLock{lk: lk}
runtime.SetFinalizer(res, finalizeLocalLock)
timeout.Stop()
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
a.BroadcastPacket(ctx, PacketLockConfirm, lk.Key())
slog.Debug(fmt.Sprintf("[fleet] Lock %s acquired in %s", name, time.Since(start)), "event", "fleet:lock:acquire_success")
return res, nil
}
if st == 2 {
// reached too many nay or another lock confirmed on top of us
lk.release()
slog.Debug(fmt.Sprintf("[fleet] Lock %s failed acquire, will retry", name), "event", "fleet:lock:acquire_fail")
break acqLoop
}
case <-timeout.C:
// reached timeout
lk.release()
slog.Debug(fmt.Sprintf("[fleet] Lock %s acquire timed out, will retry", name), "event", "fleet:lock:acquire_timeout")
break acqLoop
case <-ctx.Done():
lk.release()
timeout.Stop()
return nil, ctx.Err()
}
}
// wait a small random time before retry (0~65536µs, or up to 65ms)
t := time.NewTimer(time.Duration(rand16()) * time.Microsecond)
select {
case <-t.C:
// things continue
case <-ctx.Done():
// things do not continue
t.Stop()
return nil, ctx.Err()
}
}
}
func finalizeLocalLock(lk *LocalLock) {
lk.Release()
}
func (lk *LocalLock) Release() {
// perform release
lk.once.Do(func() {
lk.lk.release()
})
}
func (lk *globalLock) valid() bool {
if lk.status == 2 {
return false
}
if time.Until(lk.timeout) < 0 {
return false
}
return true
}
func (a *Agent) handleLockReq(p *Peer, data []byte) error {
lk, t, o, _ := decodeLockBytes(data)
if lk == "" {
return nil
}
g := a.getLock(lk)
if g != nil {
if g.t == t && g.owner == o {
// return aye (already obtained)
return p.WritePacket(context.Background(), PacketLockRes, append(data, Aye))
}
// return nay
//log.Printf("[fleet] rejecting request for lock %s by %s:%d because already belonging to %s:%d", lk, o, t, g.owner, g.t)
return p.WritePacket(context.Background(), PacketLockRes, append(data, Nay))
}
// obtain lock
g = a.makeLock(lk, o, t, false)
if g == nil {
// failed → return nay
//log.Printf("[fleet] rejecting request for lock %s because makeLock failed (race condition?)", lk)
return p.WritePacket(context.Background(), PacketLockRes, append(data, Nay))
}
g.timeout = time.Now().Add(10 * time.Second)
g.lk.Unlock()
// good → return aye
return p.WritePacket(context.Background(), PacketLockRes, append(data, Aye))
}
func (a *Agent) handleLockRes(p *Peer, data []byte) error {
lk, t, o, data := decodeLockBytes(data)
if lk == "" {
return nil
}
if len(data) < 1 {
return nil
}
res := data[0]
g := a.getLock(lk)
if g == nil {
// can't
return nil
}
if g.t != t || g.owner != o {
// wrong lock
return nil
}
id := p.id
cnt := a.GetPeersCount()
g.lk.Lock()
defer g.lk.Unlock()
// check if peer is already in aye or nay
for _, v := range g.aye {
if v == id {
return nil
}
}
for _, v := range g.nay {
if v == id {
return nil
}
}
switch res {
case Aye:
g.aye = append(g.aye, id)
case Nay:
g.nay = append(g.nay, id)
}
//log.Printf("[fleet] lock %s status: aye=%d nay=%d out of %d nodes", lk, len(g.aye), len(g.nay), cnt)
if g.getStatus() != 0 {
return nil
}
if cnt == 2 {
// special rule
if uint32(len(g.aye)) >= 1 {
// we got a aye
g.setStatus(1)
return nil
}
if uint32(len(g.nay)) >= 1 {
// give up on this
g.setStatus(2)
return nil
}
return nil
}
if uint32(len(g.aye)) >= ((cnt / 2) + 1) {
// we got a aye
g.setStatus(1)
return nil
}
if uint32(len(g.nay)) >= ((cnt / 3) + 1) {
// give up on this
g.setStatus(2)
return nil
}
return nil
}
func (a *Agent) handleLockConfirm(p *Peer, data []byte) error {
lk, t, o, _ := decodeLockBytes(data)
if lk == "" {
return nil
}
g := a.getLock(lk)
if g != nil && g.t == t && g.owner == o {
g.timeout = time.Now().Add(30 * time.Minute)
return nil
}
// make lock
g = a.makeLock(lk, o, t, true)
g.timeout = time.Now().Add(30 * time.Minute)
g.setStatus(1)
g.lk.Unlock()
return nil
}
func (a *Agent) handleLockRelease(p *Peer, data []byte) error {
lk, t, o, _ := decodeLockBytes(data)
if lk == "" {
return nil
}
g := a.getLock(lk)
if g == nil {
return nil
}
if g.owner != o || g.t != t {
return nil
}
g.setStatus(2)
g.release()
return nil
}
func (lk *globalLock) getStatus() uint32 {
return atomic.LoadUint32(&lk.status)
}
func (lk *globalLock) setStatus(v uint32) {
for {
oldv := lk.getStatus()
if oldv >= v {
// cannot go down
return
}
if atomic.CompareAndSwapUint32(&lk.status, oldv, v) {
break
}
}
select {
case lk.ch <- v:
default:
}
}