-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync.go
597 lines (532 loc) · 13.8 KB
/
sync.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
package chotki
import (
"bytes"
"context"
"crypto/sha1"
"encoding/hex"
"errors"
"io"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/cockroachdb/pebble"
"github.com/drpcorg/chotki/protocol"
"github.com/drpcorg/chotki/rdx"
"github.com/drpcorg/chotki/utils"
"github.com/google/uuid"
)
const SyncBlockBits = 28
const SyncBlockMask = (rdx.ID(1) << SyncBlockBits) - 1
type SyncHost interface {
protocol.Drainer
Snapshot() pebble.Reader
Broadcast(ctx context.Context, records protocol.Records, except string)
}
type SyncMode byte
const (
SyncRead SyncMode = 1
SyncWrite SyncMode = 2
SyncLive SyncMode = 4
SyncRW SyncMode = SyncRead | SyncWrite
SyncRL SyncMode = SyncRead | SyncLive
SyncRWLive SyncMode = SyncRead | SyncWrite | SyncLive
)
func (m *SyncMode) Zip() []byte {
return rdx.ZipUint64(uint64(*m))
}
func (m *SyncMode) Unzip(raw []byte) error {
parsed := rdx.UnzipUint64(raw)
if parsed > 0b111 {
return errors.New("invalid mode")
}
*m = SyncMode(parsed)
return nil
}
const PingVal = "ping"
const PongVal = "pong"
type SyncState int
const (
SendHandshake SyncState = iota
SendDiff
SendLive
SendEOF
SendNone
SendPing
SendPong
)
type PingState int
const (
Inactive PingState = iota
Ping
Pong
PingBroken
WaitingForPing
)
func (s SyncState) String() string {
return []string{"SendHandshake", "SendDiff", "SendLive", "SendEOF", "SendNone", "SendPing", "SendPong"}[s]
}
const TraceSize = 10
type Syncer struct {
Src uint64
Name string
Host SyncHost
Mode SyncMode
PingPeriod time.Duration
PingWait time.Duration
WaitUntilNone time.Duration
log utils.Logger
vvit, ffit *pebble.Iterator
snap pebble.Reader
snaplast rdx.ID
feedState SyncState
drainState SyncState
oqueue protocol.FeedCloser
hostvv, peervv rdx.VV
vpack []byte
reason error
myTraceId atomic.Pointer[[TraceSize]byte]
theirsTraceid atomic.Pointer[[TraceSize]byte]
lock sync.Mutex
cond sync.Cond
pingTimer *time.Timer
pingStage atomic.Int32
lctx atomic.Pointer[context.Context]
}
func (sync *Syncer) withDefaultArgs(reset bool) context.Context {
lctx := sync.lctx.Load()
if lctx == nil || reset {
nlctx := sync.log.WithDefaultArgs(context.Background(), "name", sync.Name, "trace_id", sync.GetTraceId())
if !reset {
sync.lctx.CompareAndSwap(lctx, &nlctx)
} else {
sync.lctx.Store(&nlctx)
}
lctx = &nlctx
}
return *lctx
}
func (sync *Syncer) logCtx(ctx context.Context) context.Context {
return sync.log.WithArgsFromCtx(ctx, sync.withDefaultArgs(false))
}
func (sync *Syncer) Close() error {
sync.SetFeedState(context.Background(), SendEOF)
if sync.Host == nil {
return utils.ErrClosed
}
sync.lock.Lock()
defer sync.lock.Unlock()
if sync.snap != nil {
if err := sync.snap.Close(); err != nil {
sync.log.ErrorCtx(sync.logCtx(context.Background()), "failed closing snapshot", "err", err)
}
sync.snap = nil
}
if sync.ffit != nil {
if err := sync.ffit.Close(); err != nil {
sync.log.ErrorCtx(sync.logCtx(context.Background()), "failed closing ffit", "err", err)
}
sync.ffit = nil
}
if sync.vvit != nil {
if err := sync.vvit.Close(); err != nil {
sync.log.ErrorCtx(sync.logCtx(context.Background()), "failed closing vvit", "err", err)
}
sync.vvit = nil
}
sync.log.InfoCtx(sync.logCtx(context.Background()), "sync: connection %s closed: %v\n", sync.Name, sync.reason)
return nil
}
func (sync *Syncer) GetFeedState() SyncState {
sync.lock.Lock()
defer sync.lock.Unlock()
return sync.feedState
}
func (sync *Syncer) pingTransition(ctx context.Context) {
//nolint:exhaustive
switch PingState(sync.pingStage.Load()) {
case Ping:
sync.SetFeedState(ctx, SendPing)
case Pong:
sync.SetFeedState(ctx, SendPong)
case PingBroken:
sync.SetFeedState(ctx, SendEOF)
}
}
func (sync *Syncer) GetDrainState() SyncState {
sync.lock.Lock()
defer sync.lock.Unlock()
return sync.drainState
}
func (sync *Syncer) Feed(ctx context.Context) (recs protocol.Records, err error) {
// other side closed the connection already
if sync.GetDrainState() == SendNone {
sync.SetFeedState(ctx, SendNone)
}
switch sync.GetFeedState() {
case SendHandshake:
recs, err = sync.FeedHandshake()
sync.SetFeedState(ctx, SendDiff)
case SendDiff:
ctx, cancel := context.WithCancel(ctx)
defer cancel()
select {
case <-time.After(sync.PingWait):
sync.log.ErrorCtx(sync.logCtx(ctx), "sync: handshake took too long")
sync.SetFeedState(ctx, SendEOF)
return
case <-sync.WaitDrainState(ctx, SendDiff):
}
recs, err = sync.FeedBlockDiff()
if err == io.EOF {
recs2, _ := sync.FeedDiffVV()
recs = append(recs, recs2...)
if (sync.Mode & SyncLive) != 0 {
sync.SetFeedState(ctx, SendLive)
sync.resetPingTimer()
} else {
sync.SetFeedState(ctx, SendEOF)
}
_ = sync.snap.Close()
sync.snap = nil
err = nil
}
case SendPing:
recs = protocol.Records{
protocol.Record('P', rdx.Stlv(PingVal)),
}
sync.SetFeedState(ctx, SendLive)
sync.pingTimer.Stop()
sync.pingTimer = time.AfterFunc(sync.PingWait, func() {
sync.pingStage.Store(int32(PingBroken))
sync.log.ErrorCtx(sync.logCtx(ctx), "sync: peer did not respond to ping")
})
sync.pingStage.Store(int32(WaitingForPing))
case SendPong:
recs = protocol.Records{
protocol.Record('P', rdx.Stlv(PongVal)),
}
sync.pingStage.Store(int32(Inactive))
sync.SetFeedState(ctx, SendLive)
case SendLive:
recs, err = sync.oqueue.Feed(ctx)
if err == utils.ErrClosed {
sync.log.InfoCtx(sync.logCtx(ctx), "sync: queue closed")
sync.SetFeedState(ctx, SendEOF)
err = nil
}
sync.pingTransition(ctx)
case SendEOF:
reason := []byte("closing")
if sync.reason != nil {
reason = []byte(sync.reason.Error())
}
recs = protocol.Records{protocol.Record('B',
protocol.TinyRecord('T', sync.snaplast.ZipBytes()),
reason,
)}
if sync.snap != nil {
_ = sync.snap.Close()
sync.snap = nil
}
sync.SetFeedState(ctx, SendNone)
case SendNone:
wait := sync.WaitUntilNone
if wait == 0 {
wait = time.Second
}
timer := time.AfterFunc(wait, func() {
sync.SetDrainState(ctx, SendNone)
})
<-sync.WaitDrainState(context.Background(), SendNone)
timer.Stop()
err = io.EOF
}
return
}
func (sync *Syncer) FeedHandshake() (vv protocol.Records, err error) {
sync.snap = sync.Host.Snapshot()
sync.vvit = sync.snap.NewIter(&pebble.IterOptions{
LowerBound: []byte{'V'},
UpperBound: []byte{'W'},
})
sync.ffit = sync.snap.NewIter(&pebble.IterOptions{
LowerBound: []byte{'O'},
UpperBound: []byte{'P'},
})
ok := sync.vvit.SeekGE(VKey0)
if !ok || 0 != bytes.Compare(sync.vvit.Key(), VKey0) {
return nil, rdx.ErrBadV0Record
}
sync.hostvv = make(rdx.VV)
err = sync.hostvv.PutTLV(sync.vvit.Value())
if err != nil {
return nil, err
}
sync.snaplast = sync.hostvv.GetID(sync.Src)
sync.vpack = make([]byte, 0, 4096)
_, sync.vpack = protocol.OpenHeader(sync.vpack, 'V') // 5
sync.vpack = append(sync.vpack, protocol.Record('T', sync.snaplast.ZipBytes())...)
sync.lock.Lock()
mode := sync.Mode.Zip()
sync.lock.Unlock()
uuid, err := uuid.NewV7()
if err != nil {
return nil, err
}
hash := sha1.Sum(uuid[:])
tracePart := [TraceSize]byte(hash[:TraceSize])
sync.myTraceId.Store(&tracePart)
sync.withDefaultArgs(true)
// handshake: H(T{pro,src} M(mode) V(V{p,s}+), T(trace_ids))
hs := protocol.Record('H',
protocol.TinyRecord('T', sync.snaplast.ZipBytes()),
protocol.TinyRecord('M', mode),
protocol.Record('V', sync.vvit.Value()),
protocol.Record('S', tracePart[:]),
)
return protocol.Records{hs}, nil
}
func (sync *Syncer) FeedBlockDiff() (diff protocol.Records, err error) {
if !sync.vvit.Next() {
return nil, io.EOF
}
vv := make(rdx.VV)
err = vv.PutTLV(sync.vvit.Value())
if err != nil {
return nil, rdx.ErrBadVRecord
}
sendvv := make(rdx.VV)
// check for any changes
hasChanges := false // fixme up & repeat
for src, pro := range vv {
peerpro, ok := sync.peervv[src]
if !ok || pro > peerpro {
sendvv[src] = peerpro
hasChanges = true
}
}
if !hasChanges {
return protocol.Records{}, nil
}
block := VKeyId(sync.vvit.Key()).ZeroOff()
key := OKey(block, 0)
sync.ffit.SeekGE(key)
bmark, parcel := protocol.OpenHeader(nil, 'D')
parcel = append(parcel, protocol.Record('T', sync.snaplast.ZipBytes())...)
parcel = append(parcel, protocol.Record('R', block.ZipBytes())...)
till := block + SyncBlockMask + 1
for ; sync.ffit.Valid(); sync.ffit.Next() {
id, rdt := OKeyIdRdt(sync.ffit.Key())
if id == rdx.BadId || id >= till {
break
}
lim, ok := sendvv[id.Src()]
if ok && (id.Pro() > lim || lim == 0) {
parcel = append(parcel, protocol.Record('F', rdx.ZipUint64(uint64(id-block)))...)
parcel = append(parcel, protocol.Record(rdt, sync.ffit.Value())...)
continue
}
diff := rdx.Xdiff(rdt, sync.ffit.Value(), sendvv)
if len(diff) != 0 {
parcel = append(parcel, protocol.Record('F', rdx.ZipUint64(uint64(id-block)))...)
parcel = append(parcel, protocol.Record(rdt, diff)...)
}
}
protocol.CloseHeader(parcel, bmark)
v := protocol.Record('V',
protocol.Record('R', block.ZipBytes()),
sync.vvit.Value()) // todo brief
sync.vpack = append(sync.vpack, v...)
return protocol.Records{parcel}, err
}
func (sync *Syncer) FeedDiffVV() (vv protocol.Records, err error) {
protocol.CloseHeader(sync.vpack, 5)
vv = append(vv, sync.vpack)
sync.vpack = nil
_ = sync.ffit.Close()
sync.ffit = nil
_ = sync.vvit.Close()
sync.vvit = nil
return
}
func (sync *Syncer) SetFeedState(ctx context.Context, state SyncState) {
sync.log.InfoCtx(sync.logCtx(ctx), "sync: feed state", "state", state.String())
sync.lock.Lock()
sync.feedState = state
sync.lock.Unlock()
}
func (sync *Syncer) SetDrainState(ctx context.Context, state SyncState) {
sync.log.InfoCtx(sync.logCtx(ctx), "sync: drain state", "state", state.String())
sync.lock.Lock()
sync.drainState = state
if sync.cond.L == nil {
sync.cond.L = &sync.lock
}
sync.cond.Broadcast()
sync.lock.Unlock()
}
func (sync *Syncer) WaitDrainState(ctx context.Context, state SyncState) chan SyncState {
res := make(chan SyncState)
go func() {
<-ctx.Done()
sync.cond.Broadcast()
}()
go func() {
defer close(res)
sync.lock.Lock()
defer sync.lock.Unlock()
if sync.cond.L == nil {
sync.cond.L = &sync.lock
}
for sync.drainState < state {
if ctx.Err() != nil {
return
}
sync.cond.Wait()
}
ds := sync.drainState
res <- ds
}()
return res
}
func LastLit(recs protocol.Records) byte {
if len(recs) == 0 {
return 0
}
return protocol.Lit(recs[len(recs)-1])
}
func (sync *Syncer) resetPingTimer() {
sync.lock.Lock()
defer sync.lock.Unlock()
if sync.pingTimer != nil && sync.pingStage.Load() != int32(WaitingForPing) {
sync.pingTimer.Reset(sync.PingPeriod)
sync.pingStage.CompareAndSwap(int32(Ping), int32(Inactive))
} else {
if sync.pingTimer != nil {
sync.pingTimer.Stop()
}
sync.pingTimer = time.AfterFunc(sync.PingPeriod, func() {
sync.pingStage.Store(int32(Ping))
})
sync.pingStage.CompareAndSwap(int32(WaitingForPing), int32(Inactive))
}
}
func (sync *Syncer) processPings(recs protocol.Records) protocol.Records {
for i, rec := range recs {
if protocol.Lit(rec) == 'P' {
body, _ := protocol.Take('P', rec)
recs = append(recs[:i], recs[i+1:]...)
switch rdx.Snative(body) {
case PingVal:
sync.log.InfoCtx(sync.logCtx(context.Background()), "ping received")
// go to pong state next time
sync.pingStage.Store(int32(Pong))
case PongVal:
sync.log.InfoCtx(sync.logCtx(context.Background()), "pong received")
}
}
}
return recs
}
func (sync *Syncer) Drain(ctx context.Context, recs protocol.Records) (err error) {
if len(recs) == 0 {
return nil
}
recs = sync.processPings(recs)
switch sync.drainState {
case SendHandshake:
if len(recs) == 0 {
return ErrBadHPacket
}
err = sync.DrainHandshake(recs[0:1])
if err == nil {
err = sync.Host.Drain(sync.logCtx(ctx), recs[0:1])
}
if err != nil {
return
}
sync.Host.Broadcast(sync.logCtx(ctx), recs[0:1], sync.Name)
recs = recs[1:]
sync.SetDrainState(ctx, SendDiff)
if len(recs) == 0 {
break
}
fallthrough
case SendDiff:
lit := LastLit(recs)
if lit != 'D' && lit != 'V' {
if lit == 'B' {
sync.SetDrainState(ctx, SendNone)
} else {
sync.SetDrainState(ctx, SendLive)
}
}
if sync.Mode&SyncLive != 0 {
sync.resetPingTimer()
}
err = sync.Host.Drain(sync.logCtx(ctx), recs)
if err == nil {
sync.Host.Broadcast(sync.logCtx(ctx), recs, sync.Name)
}
case SendLive:
sync.resetPingTimer()
lit := LastLit(recs)
if lit == 'B' {
sync.SetDrainState(ctx, SendNone)
}
err = sync.Host.Drain(sync.logCtx(ctx), recs)
if err == nil {
sync.Host.Broadcast(sync.logCtx(ctx), recs, sync.Name)
}
case SendPong, SendPing:
panic("chotki: unacceptable sync-state")
case SendEOF, SendNone:
return ErrClosed
default:
panic("chotki: unacceptable sync-state")
}
if err != nil { // todo send the error msg
sync.SetDrainState(ctx, SendEOF)
}
return
}
func (sync *Syncer) GetTraceId() string {
theirsP := sync.theirsTraceid.Load()
if theirsP == nil {
theirsP = &[TraceSize]byte{}
}
theirs := hex.EncodeToString((*theirsP)[:])
mineP := sync.myTraceId.Load()
if mineP == nil {
mineP = &[TraceSize]byte{}
}
mine := hex.EncodeToString((*mineP)[:])
if strings.Compare(mine, theirs) >= 0 {
return mine + "-" + theirs
} else {
return theirs + "-" + mine
}
}
func (sync *Syncer) DrainHandshake(recs protocol.Records) (err error) {
lit, _, _, body, e := ParsePacket(recs[0])
if lit != 'H' || e != nil {
return ErrBadHPacket
}
var mode SyncMode
var trace_id []byte
mode, sync.peervv, trace_id, err = ParseHandshake(body)
sync.lock.Lock()
if trace_id != nil {
if len(trace_id) != TraceSize {
err = ErrBadHPacket
} else {
traceId := [TraceSize]byte(trace_id)
sync.theirsTraceid.Store(&traceId)
sync.withDefaultArgs(true)
}
}
sync.Mode &= mode
sync.lock.Unlock()
return
}