-
Notifications
You must be signed in to change notification settings - Fork 339
/
pullsync.go
475 lines (405 loc) · 12.4 KB
/
pullsync.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
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package pullsync provides the pullsync protocol
// implementation.
package pullsync
import (
"context"
"errors"
"fmt"
"io"
"math"
"sync"
"time"
"github.com/ethersphere/bee/pkg/bitvector"
"github.com/ethersphere/bee/pkg/cac"
"github.com/ethersphere/bee/pkg/log"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/p2p/protobuf"
"github.com/ethersphere/bee/pkg/postage"
"github.com/ethersphere/bee/pkg/pullsync/pb"
"github.com/ethersphere/bee/pkg/pullsync/pullstorage"
"github.com/ethersphere/bee/pkg/rate"
"github.com/ethersphere/bee/pkg/soc"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
)
// loggerName is the tree path name of the logger for this package.
const loggerName = "pullsync"
const (
protocolName = "pullsync"
protocolVersion = "1.2.0"
streamName = "pullsync"
cursorStreamName = "cursors"
cancelStreamName = "cancel"
rateWindowSize = 5 * time.Minute // rate tracker window size
)
const MaxCursor = math.MaxUint64
var (
ErrUnsolicitedChunk = errors.New("peer sent unsolicited chunk")
)
const (
storagePutTimeout = 5 * time.Second
)
// how many maximum chunks in a batch
var maxPage = 50
// Interface is the PullSync interface.
type Interface interface {
// SyncInterval syncs a requested interval from the given peer.
// It returns the BinID of highest chunk that was synced from the given
// interval. If the requested interval is too large, the downstream peer
// has the liberty to provide less chunks than requested.
SyncInterval(ctx context.Context, peer swarm.Address, bin uint8, from, to uint64) (topmost uint64, err error)
// GetCursors retrieves all cursors from a downstream peer.
GetCursors(ctx context.Context, peer swarm.Address) ([]uint64, error)
}
type Syncer struct {
streamer p2p.Streamer
metrics metrics
logger log.Logger
storage pullstorage.Storer
quit chan struct{}
wg sync.WaitGroup
unwrap func(swarm.Chunk)
validStamp postage.ValidStampFn
rate *rate.Rate
Interface
io.Closer
}
func New(streamer p2p.Streamer, storage pullstorage.Storer, unwrap func(swarm.Chunk), validStamp postage.ValidStampFn, logger log.Logger) *Syncer {
return &Syncer{
streamer: streamer,
storage: storage,
metrics: newMetrics(),
unwrap: unwrap,
validStamp: validStamp,
logger: logger.WithName(loggerName).Register(),
wg: sync.WaitGroup{},
quit: make(chan struct{}),
rate: rate.New(rateWindowSize),
}
}
func (s *Syncer) Protocol() p2p.ProtocolSpec {
return p2p.ProtocolSpec{
Name: protocolName,
Version: protocolVersion,
StreamSpecs: []p2p.StreamSpec{
{
Name: streamName,
Handler: s.handler,
},
{
Name: cursorStreamName,
Handler: s.cursorHandler,
},
},
}
}
// SyncInterval syncs a requested interval from the given peer.
// It returns the BinID of highest chunk that was synced from the given interval.
// If the requested interval is too large, the downstream peer has the liberty to
// provide less chunks than requested.
func (s *Syncer) SyncInterval(ctx context.Context, peer swarm.Address, bin uint8, from, to uint64) (topmost uint64, err error) {
isLiveSync := to == MaxCursor
loggerV2 := s.logger.V(2).Register()
stream, err := s.streamer.NewStream(ctx, peer, nil, protocolName, protocolVersion, streamName)
if err != nil {
return 0, fmt.Errorf("new stream: %w", err)
}
defer func() {
if err != nil {
_ = stream.Reset()
loggerV2.Debug("error syncing peer", "peer_address", peer, "bin", bin, "from", from, "to", to, "error", err)
} else {
stream.FullClose()
}
}()
w, r := protobuf.NewWriterAndReader(stream)
rangeMsg := &pb.GetRange{Bin: int32(bin), From: from, To: to}
if err = w.WriteMsgWithContext(ctx, rangeMsg); err != nil {
return 0, fmt.Errorf("write get range: %w", err)
}
var offer pb.Offer
if err = r.ReadMsgWithContext(ctx, &offer); err != nil {
return 0, fmt.Errorf("read offer: %w", err)
}
if len(offer.Hashes)%swarm.HashSize != 0 {
return 0, fmt.Errorf("inconsistent hash length")
}
// empty interval (no chunks present in interval).
// return the end of the requested range as topmost.
if len(offer.Hashes) == 0 {
return offer.Topmost, nil
}
topmost = offer.Topmost
var (
bvLen = len(offer.Hashes) / swarm.HashSize
wantChunks = make(map[string]struct{})
ctr = 0
have bool
)
bv, err := bitvector.New(bvLen)
if err != nil {
err = fmt.Errorf("new bitvector: %w", err)
return
}
for i := 0; i < len(offer.Hashes); i += swarm.HashSize {
a := swarm.NewAddress(offer.Hashes[i : i+swarm.HashSize])
if a.Equal(swarm.ZeroAddress) {
// i'd like to have this around to see we don't see any of these in the logs
s.logger.Error(nil, "syncer got a zero address hash on offer")
err = fmt.Errorf("zero address on offer")
return
}
s.metrics.Offered.Inc()
s.metrics.DbOps.Inc()
have, err = s.storage.Has(ctx, a)
if err != nil {
err = fmt.Errorf("storage has: %w", err)
return
}
if !have {
wantChunks[a.String()] = struct{}{}
ctr++
s.metrics.Wanted.Inc()
bv.Set(i / swarm.HashSize)
}
}
wantMsg := &pb.Want{BitVector: bv.Bytes()}
if err = w.WriteMsgWithContext(ctx, wantMsg); err != nil {
err = fmt.Errorf("write want: %w", err)
return
}
// if ctr is zero, it means we don't want any chunk in the batch
// thus, the following loop will not get executed and the method
// returns immediately with the topmost value on the offer, which
// will seal the interval and request the next one
err = nil
var chunksToPut []swarm.Chunk
for ; ctr > 0; ctr-- {
var delivery pb.Delivery
if err = r.ReadMsgWithContext(ctx, &delivery); err != nil {
// this is not a fatal error and we should write
// a partial batch if some chunks have been received.
err = fmt.Errorf("read delivery: %w", err)
break
}
addr := swarm.NewAddress(delivery.Address)
if _, ok := wantChunks[addr.String()]; !ok {
// this is fatal for the entire batch, return the
// error and don't write the partial batch.
err = ErrUnsolicitedChunk
return
}
delete(wantChunks, addr.String())
s.metrics.Delivered.Inc()
chunk := swarm.NewChunk(addr, delivery.Data)
if chunk, err = s.validStamp(chunk, delivery.Stamp); err != nil {
s.logger.Debug("unverified stamp", "error", err)
continue
}
if cac.Valid(chunk) {
go s.unwrap(chunk)
} else if !soc.Valid(chunk) {
// this is fatal for the entire batch, return the
// error and don't write the partial batch.
err = swarm.ErrInvalidChunk
return
}
chunksToPut = append(chunksToPut, chunk)
}
if len(chunksToPut) > 0 {
if !isLiveSync {
s.rate.Add(len(chunksToPut))
}
s.metrics.DbOps.Inc()
ctx, cancel := context.WithTimeout(ctx, storagePutTimeout)
defer cancel()
if ierr := s.storage.Put(ctx, storage.ModePutSync, chunksToPut...); ierr != nil {
if err != nil {
ierr = fmt.Errorf(", sync err: %w", err)
}
err = fmt.Errorf("delivery put: %w", ierr)
return
}
}
return
}
// handler handles an incoming request to sync an interval
func (s *Syncer) handler(streamCtx context.Context, p p2p.Peer, stream p2p.Stream) (err error) {
loggerV2 := s.logger.V(2).Register()
select {
case <-s.quit:
return nil
default:
}
r := protobuf.NewReader(stream)
defer func() {
if err != nil {
_ = stream.Reset()
} else {
_ = stream.FullClose()
}
}()
loggerV2.Debug("peer pulling", "peer_address", p.Address)
ctx, cancel := context.WithCancel(streamCtx)
defer cancel()
go func() {
select {
case <-s.quit:
cancel()
case <-ctx.Done():
return
}
}()
s.wg.Add(1)
defer s.wg.Done()
var rn pb.GetRange
if err := r.ReadMsgWithContext(ctx, &rn); err != nil {
return fmt.Errorf("read get range: %w", err)
}
s.logger.Debug("make offer start", "bin", rn.Bin, "from", rn.From, "to", rn.To, "peer", p.Address)
// make an offer to the upstream peer in return for the requested range
offer, _, err := s.makeOffer(ctx, rn)
if err != nil {
return fmt.Errorf("make offer: %w", err)
}
s.logger.Debug("make offer end", "bin", rn.Bin, "from", rn.From, "topmost", offer.Topmost, "peer", p.Address)
// recreate the reader to allow the first one to be garbage collected
// before the makeOffer function call, to reduce the total memory allocated
// while makeOffer is executing (waiting for the new chunks)
w, r := protobuf.NewWriterAndReader(stream)
if err := w.WriteMsgWithContext(ctx, offer); err != nil {
return fmt.Errorf("write offer: %w", err)
}
// we don't have any hashes to offer in this range (the
// interval is empty). nothing more to do
if len(offer.Hashes) == 0 {
return nil
}
var want pb.Want
if err := r.ReadMsgWithContext(ctx, &want); err != nil {
return fmt.Errorf("read want: %w", err)
}
chs, err := s.processWant(ctx, offer, &want)
if err != nil {
return fmt.Errorf("process want: %w", err)
}
for _, v := range chs {
stamp, err := v.Stamp().MarshalBinary()
if err != nil {
return fmt.Errorf("serialise stamp: %w", err)
}
deliver := pb.Delivery{Address: v.Address().Bytes(), Data: v.Data(), Stamp: stamp}
if err := w.WriteMsgWithContext(ctx, &deliver); err != nil {
return fmt.Errorf("write delivery: %w", err)
}
}
return nil
}
// makeOffer tries to assemble an offer for a given requested interval.
func (s *Syncer) makeOffer(ctx context.Context, rn pb.GetRange) (o *pb.Offer, addrs []swarm.Address, err error) {
chs, top, err := s.storage.IntervalChunks(ctx, uint8(rn.Bin), rn.From, rn.To, maxPage)
if err != nil {
return o, nil, err
}
o = new(pb.Offer)
o.Topmost = top
o.Hashes = make([]byte, 0)
for _, v := range chs {
o.Hashes = append(o.Hashes, v.Bytes()...)
}
return o, chs, nil
}
// processWant compares a received Want to a sent Offer and returns
// the appropriate chunks from the local store.
func (s *Syncer) processWant(ctx context.Context, o *pb.Offer, w *pb.Want) ([]swarm.Chunk, error) {
l := len(o.Hashes) / swarm.HashSize
bv, err := bitvector.NewFromBytes(w.BitVector, l)
if err != nil {
return nil, err
}
var addrs []swarm.Address
for i := 0; i < len(o.Hashes); i += swarm.HashSize {
if bv.Get(i / swarm.HashSize) {
a := swarm.NewAddress(o.Hashes[i : i+swarm.HashSize])
addrs = append(addrs, a)
}
}
s.metrics.DbOps.Inc()
return s.storage.Get(ctx, storage.ModeGetSync, addrs...)
}
func (s *Syncer) GetCursors(ctx context.Context, peer swarm.Address) (retr []uint64, err error) {
loggerV2 := s.logger.V(2).Register()
stream, err := s.streamer.NewStream(ctx, peer, nil, protocolName, protocolVersion, cursorStreamName)
if err != nil {
return nil, fmt.Errorf("new stream: %w", err)
}
loggerV2.Debug("getting cursors from peer", "peer_address", peer)
defer func() {
if err != nil {
_ = stream.Reset()
loggerV2.Debug("error getting cursors from peer", "peer_address", peer, "error", err)
} else {
stream.FullClose()
}
}()
w, r := protobuf.NewWriterAndReader(stream)
syn := &pb.Syn{}
if err = w.WriteMsgWithContext(ctx, syn); err != nil {
return nil, fmt.Errorf("write syn: %w", err)
}
var ack pb.Ack
if err = r.ReadMsgWithContext(ctx, &ack); err != nil {
return nil, fmt.Errorf("read ack: %w", err)
}
retr = ack.Cursors
return retr, nil
}
func (s *Syncer) cursorHandler(ctx context.Context, p p2p.Peer, stream p2p.Stream) (err error) {
loggerV2 := s.logger.V(2).Register()
w, r := protobuf.NewWriterAndReader(stream)
loggerV2.Debug("peer wants cursors", "peer_address", p.Address)
defer func() {
if err != nil {
_ = stream.Reset()
loggerV2.Debug("error getting cursors for peer", "peer_address", p.Address, "error", err)
} else {
_ = stream.FullClose()
}
}()
var syn pb.Syn
if err := r.ReadMsgWithContext(ctx, &syn); err != nil {
return fmt.Errorf("read syn: %w", err)
}
var ack pb.Ack
s.metrics.DbOps.Inc()
ints, err := s.storage.Cursors(ctx)
if err != nil {
return err
}
ack.Cursors = ints
if err = w.WriteMsgWithContext(ctx, &ack); err != nil {
return fmt.Errorf("write ack: %w", err)
}
return nil
}
func (s *Syncer) Rate() float64 {
return s.rate.Rate()
}
func (s *Syncer) Close() error {
s.logger.Info("pull syncer shutting down")
close(s.quit)
cc := make(chan struct{})
go func() {
defer close(cc)
s.wg.Wait()
}()
select {
case <-cc:
case <-time.After(5 * time.Second):
s.logger.Warning("pull syncer shutting down with running goroutines")
}
return nil
}