-
Notifications
You must be signed in to change notification settings - Fork 0
/
pusher.go
347 lines (311 loc) · 9.73 KB
/
pusher.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
// Package pusher provides protocol-orchestrating functionality
// over the pushsync protocol. It makes sure that chunks meant
// to be distributed over the network are sent used using the
// pushsync protocol.
package pusher
import (
"context"
"errors"
"fmt"
"strconv"
"sync"
"time"
"github.com/redesblock/mop/core/cluster"
"github.com/redesblock/mop/core/crypto"
"github.com/redesblock/mop/core/incentives/voucher"
"github.com/redesblock/mop/core/log"
"github.com/redesblock/mop/core/p2p/topology"
"github.com/redesblock/mop/core/protocol/pushsync"
"github.com/redesblock/mop/core/storer/storage"
"github.com/redesblock/mop/core/tags"
"github.com/redesblock/mop/core/tracer"
)
// loggerName is the tree path name of the logger for this package.
const loggerName = "pusher"
type Op struct {
Chunk cluster.Chunk
Err chan error
Direct bool
}
type OpChan <-chan *Op
type Service struct {
networkID uint64
storer storage.Storer
pushSyncer pushsync.PushSyncer
validStamp voucher.ValidStampFn
depther topology.NeighborhoodDepther
logger log.Logger
tag *tags.Tags
metrics metrics
quit chan struct{}
chunksWorkerQuitC chan struct{}
inflight *inflight
attempts *attempts
sem chan struct{}
smugler chan OpChan
}
var (
retryInterval = 5 * time.Second // time interval between retries
traceDuration = 30 * time.Second // duration for every root tracer span
concurrentPushes = 200 // how many chunks to push simultaneously
retryCount = 6
)
var (
ErrInvalidAddress = errors.New("invalid address")
ErrShallowReceipt = errors.New("shallow recipt")
)
const chunkStoreTimeout = 2 * time.Second
func New(networkID uint64, storer storage.Storer, depther topology.NeighborhoodDepther, pushSyncer pushsync.PushSyncer, validStamp voucher.ValidStampFn, tagger *tags.Tags, logger log.Logger, tracer *tracer.Tracer, warmupTime time.Duration) *Service {
p := &Service{
networkID: networkID,
storer: storer,
pushSyncer: pushSyncer,
validStamp: validStamp,
depther: depther,
tag: tagger,
logger: logger.WithName(loggerName).Register(),
metrics: newMetrics(),
quit: make(chan struct{}),
chunksWorkerQuitC: make(chan struct{}),
inflight: newInflight(),
attempts: &attempts{attempts: make(map[string]int)},
sem: make(chan struct{}, concurrentPushes),
smugler: make(chan OpChan),
}
go p.chunksWorker(warmupTime, tracer)
return p
}
// chunksWorker is a loop that keeps looking for chunks that are locally uploaded ( by monitoring pushIndex )
// and pushes them to the closest peer and get a receipt.
func (s *Service) chunksWorker(warmupTime time.Duration, tracer *tracer.Tracer) {
defer close(s.chunksWorkerQuitC)
select {
case <-time.After(warmupTime):
s.logger.Info("pusher: warmup period complete, worker starting.")
case <-s.quit:
return
}
var (
cctx, cancel = context.WithCancel(context.Background())
mtx sync.Mutex
wg sync.WaitGroup
span, logger, ctx = tracer.StartSpanFromContext(cctx, "pusher-chainsync-batch", s.logger)
loggerV1 = logger.V(1).Build()
timer = time.NewTimer(traceDuration)
)
// inflight.set handles the backpressure for the maximum amount of inflight chunks
// and duplicate handling.
chunks, repeat, unsubscribe := s.storer.SubscribePush(ctx, s.inflight.set)
go func() {
<-s.quit
unsubscribe()
cancel()
if !timer.Stop() {
<-timer.C
}
}()
ctxLogger := func() (context.Context, log.Logger) {
mtx.Lock()
defer mtx.Unlock()
return ctx, logger
}
push := func(op *Op) {
s.metrics.TotalToPush.Inc()
ctx, logger := ctxLogger()
startTime := time.Now()
wg.Add(1)
go func() {
defer func() {
wg.Done()
<-s.sem
}()
if err := s.pushChunk(ctx, op.Chunk, logger, op.Direct); err != nil {
// warning: ugly flow control
// if errc is set it means we are in a direct push,
// we therefore communicate the error into the channel
// otherwise we assume this is a buffered upload and
// therefore we repeat().
if op.Err != nil {
op.Err <- err
}
repeat()
s.metrics.TotalErrors.Inc()
s.metrics.ErrorTime.Observe(time.Since(startTime).Seconds())
loggerV1.Debug("cannot push chunk", "chunk_address", op.Chunk.Address(), "error", err)
return
}
if op.Err != nil {
op.Err <- nil
}
s.metrics.TotalSynced.Inc()
}()
}
go func() {
for {
select {
case <-s.quit:
return
case <-timer.C:
// reset the span
mtx.Lock()
span.Finish()
span, logger, ctx = tracer.StartSpanFromContext(cctx, "pusher-chainsync-batch", s.logger)
loggerV1 = logger.V(1).Build()
mtx.Unlock()
}
}
}()
// fan-in channel
cc := make(chan *Op)
go func() {
for ch := range chunks {
// If the stamp is invalid, the chunk is not synced with the network
// since other nodes would reject the chunk, so the chunk is marked as
// synced which makes it available to the node but not to the network
if err := s.valid(ch); err != nil {
logger.Warning("stamp with is no longer valid, skipping syncing for chunk", "batch_id", fmt.Sprintf("%x", ch.Stamp().BatchID()), "chunk_address", ch.Address(), "error", err)
ctx, cancel := context.WithTimeout(ctx, chunkStoreTimeout)
if err = s.storer.Set(ctx, storage.ModeSetSync, ch.Address()); err != nil {
s.logger.Error(err, "set chainsync failed")
}
cancel()
}
cc <- &Op{Chunk: ch, Direct: false}
}
}()
defer wg.Wait()
for {
select {
case apiC := <-s.smugler:
go func() {
for op := range apiC {
select {
case cc <- op:
case <-s.quit:
return
}
}
}()
case op, ok := <-cc:
if !ok {
chunks = nil
continue
}
select {
case s.sem <- struct{}{}:
case <-s.quit:
return
}
push(op)
case <-s.quit:
return
}
}
}
func (s *Service) pushChunk(ctx context.Context, ch cluster.Chunk, logger log.Logger, directUpload bool) error {
loggerV1 := logger.V(1).Build()
defer s.inflight.delete(ch)
var wantSelf bool
// Later when we process receipt, get the receipt and process it
// for now ignoring the receipt and checking only for error
receipt, err := s.pushSyncer.PushChunkToClosest(ctx, ch)
if err != nil {
// when doing a direct upload from a light node this will never happen because the light node
// never includes self in kademlia iterator. This is only hit when doing a direct upload from a full node
if directUpload && errors.Is(err, topology.ErrWantSelf) {
return err
}
if !errors.Is(err, topology.ErrWantSelf) {
return err
}
// we are the closest ones - this is fine
// this is to make sure that the sent number does not diverge from the synced counter
// the edge case is on the uploader node, in the case where the uploader node is
// connected to other nodes, but is the closest one to the chunk.
wantSelf = true
loggerV1.Debug("chunk stays here, i'm the closest node", "chunk_address", ch.Address())
} else if err = s.checkReceipt(receipt); err != nil {
return err
}
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
if err = s.storer.Set(ctx, storage.ModeSetSync, ch.Address()); err != nil {
return fmt.Errorf("pusher: set chainsync: %w", err)
}
if ch.TagID() > 0 {
// for individual chunks uploaded using the
// /chunks api endpoint the tag will be missing
// by default, unless the api consumer specifies one
t, err := s.tag.Get(ch.TagID())
if err == nil && t != nil {
err = t.Inc(tags.StateSynced)
if err != nil {
logger.Debug("increment synced failed", "error", err)
return nil // tag error is non-fatal
}
if wantSelf {
err = t.Inc(tags.StateSent)
if err != nil {
logger.Debug("increment sent failed", "error", err)
return nil // tag error is non-fatal
}
}
}
}
return nil
}
func (s *Service) checkReceipt(receipt *pushsync.Receipt) error {
loggerV1 := s.logger.V(1).Register()
addr := receipt.Address
publicKey, err := crypto.Recover(receipt.Signature, addr.Bytes())
if err != nil {
return fmt.Errorf("pusher: receipt recover: %w", err)
}
peer, err := crypto.NewOverlayAddress(*publicKey, s.networkID, receipt.Nonce)
if err != nil {
return fmt.Errorf("pusher: receipt storer address: %w", err)
}
po := cluster.Proximity(addr.Bytes(), peer.Bytes())
d := s.depther.NeighborhoodDepth()
// if the receipt po is out of depth AND the receipt has not yet hit the maximum retry limit, reject the receipt.
if po < d && s.attempts.try(addr) {
s.metrics.ShallowReceiptDepth.WithLabelValues(strconv.Itoa(int(po))).Inc()
return fmt.Errorf("pusher: shallow receipt depth %d, want at least %d", po, d)
}
loggerV1.Debug("chunk pushed", "chunk_address", addr, "peer_address", peer, "proximity_order", po)
s.metrics.ReceiptDepth.WithLabelValues(strconv.Itoa(int(po))).Inc()
s.attempts.delete(addr)
return nil
}
// valid checks whether the stamp for a chunk is valid before sending
// it out on the network.
func (s *Service) valid(ch cluster.Chunk) error {
stampBytes, err := ch.Stamp().MarshalBinary()
if err != nil {
return fmt.Errorf("pusher: valid stamp marshal: %w", err)
}
_, err = s.validStamp(ch, stampBytes)
if err != nil {
return fmt.Errorf("pusher: valid stamp: %w", err)
}
return nil
}
func (s *Service) AddFeed(c <-chan *Op) {
go func() {
select {
case s.smugler <- c:
case <-s.quit:
// if we're quitting: don't do anything
}
}()
}
func (s *Service) Close() error {
s.logger.Info("pusher shutting down")
close(s.quit)
// Wait for chunks worker to finish
select {
case <-s.chunksWorkerQuitC:
case <-time.After(6 * time.Second):
}
return nil
}