This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pool.go
621 lines (519 loc) · 16.8 KB
/
pool.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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
package caboose
import (
"context"
cryptoRand "crypto/rand"
"encoding/json"
"errors"
"fmt"
"io"
"math/big"
"math/rand"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/asecurityteam/rolling"
"github.com/patrickmn/go-cache"
"github.com/prometheus/client_golang/prometheus"
"github.com/filecoin-saturn/caboose/tieredhashing"
"github.com/ipfs/boxo/path"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipld/go-car"
)
const (
tierMainToUnknown = "main-to-unknown"
tierUnknownToMain = "unknown-to-main"
BackendOverrideKey = "CABOOSE_BACKEND_OVERRIDE"
defaultMirroredConcurrency = 5
)
var complianceCidReqTemplate = "/ipfs/%s?format=car&dag-scope=entity"
// loadPool refreshes the set of Saturn endpoints in the pool by fetching an updated list of responsive Saturn nodes from the
// Saturn Orchestrator.
func (p *pool) loadPool() ([]tieredhashing.NodeInfo, error) {
if p.config.OrchestratorOverride != nil {
return p.config.OrchestratorOverride, nil
}
client := p.config.OrchestratorClient
req, err := http.NewRequest("GET", p.config.OrchestratorEndpoint.String(), nil)
if err != nil {
goLogger.Warnw("failed to create request to orchestrator", "err", err, "endpoint", p.config.OrchestratorEndpoint)
return nil, err
}
resp, err := client.Do(req)
if err != nil {
goLogger.Warnw("failed to get backends from orchestrator", "err", err, "endpoint", p.config.OrchestratorEndpoint)
return nil, err
}
defer resp.Body.Close()
responses := make([]tieredhashing.NodeInfo, 0)
if err := json.NewDecoder(resp.Body).Decode(&responses); err != nil {
goLogger.Warnw("failed to decode backends from orchestrator", "err", err, "endpoint", p.config.OrchestratorEndpoint.String())
return nil, err
}
goLogger.Debugw("got backends from orchestrator", "backends", responses, "endpoint", p.config.OrchestratorEndpoint.String())
goLogger.Infow("got backends from orchestrator", "cnt", len(responses), "endpoint", p.config.OrchestratorEndpoint.String())
return responses, nil
}
type mirroredPoolRequest struct {
node string
path string
// the key for node affinity for the request
key string
}
type pool struct {
config *Config
logger *logger
started chan struct{} // started signals that we've already initialized the pool once with Saturn endpoints.
refresh chan struct{} // refresh is used to signal the need for doing a refresh of the Saturn endpoints pool.
done chan struct{} // done is used to signal that we're shutting down the Saturn endpoints pool and don't need to refresh it anymore.
mirrorSamples chan mirroredPoolRequest
fetchKeyLk sync.RWMutex
fetchKeyFailureCache *cache.Cache // guarded by fetchKeyLk
fetchKeyCoolDownCache *cache.Cache // guarded by fetchKeyLk
lk sync.RWMutex
th *tieredhashing.TieredHashing
poolInitDone sync.Once
}
func newPool(c *Config) *pool {
noRemove := false
if len(os.Getenv(BackendOverrideKey)) > 0 {
noRemove = true
}
topts := append(c.TieredHashingOpts, tieredhashing.WithNoRemove(noRemove))
p := pool{
config: c,
started: make(chan struct{}),
refresh: make(chan struct{}, 1),
done: make(chan struct{}, 1),
mirrorSamples: make(chan mirroredPoolRequest, 10),
fetchKeyCoolDownCache: cache.New(c.FetchKeyCoolDownDuration, 1*time.Minute),
fetchKeyFailureCache: cache.New(c.FetchKeyCoolDownDuration, 1*time.Minute),
th: tieredhashing.New(topts...),
}
return &p
}
func (p *pool) Start() {
go p.refreshPool()
go p.checkPool()
}
func (p *pool) doRefresh() {
newEP, err := p.loadPool()
if err == nil {
p.refreshWithNodes(newEP)
} else {
poolRefreshErrorMetric.Add(1)
}
}
func (p *pool) refreshWithNodes(newEP []tieredhashing.NodeInfo) {
p.lk.Lock()
defer p.lk.Unlock()
// for tests to pass the -race check when accessing global vars
distLk.Lock()
defer distLk.Unlock()
added, alreadyRemoved, back := p.th.AddOrchestratorNodes(newEP)
poolNewMembersMetric.Set(float64(added))
poolMembersNotAddedBecauseRemovedMetric.Set(float64(alreadyRemoved))
poolMembersRemovedAndAddedBackMetric.Set(float64(back))
p.th.UpdateAverageCorrectnessPct()
// update the tier set
mu, um := p.th.UpdateMainTierWithTopN()
poolTierChangeMetric.WithLabelValues(tierMainToUnknown).Set(float64(mu))
poolTierChangeMetric.WithLabelValues(tierUnknownToMain).Set(float64(um))
mt := p.th.GetPoolMetrics()
poolSizeMetric.WithLabelValues(string(tieredhashing.TierUnknown)).Set(float64(mt.Unknown))
poolSizeMetric.WithLabelValues(string(tieredhashing.TierMain)).Set(float64(mt.Main))
// Update aggregate latency & speed distribution for peers
latencyHist := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: prometheus.BuildFQName("ipfs", "caboose", "fetch_peer_latency_dist"),
Help: "Fetch latency distribution for peers in millis",
Buckets: latencyDistMsHistogram,
}, []string{"tier", "percentile"})
percentiles := []float64{25, 50, 75, 90, 95}
for _, perf := range p.th.GetPerf() {
perf := perf
if perf.NLatencyDigest <= 0 {
continue
}
for _, pt := range percentiles {
latencyHist.WithLabelValues(string(perf.Tier), fmt.Sprintf("P%f", pt)).Observe(perf.LatencyDigest.Reduce(rolling.Percentile(pt)))
}
}
peerLatencyDistribution = latencyHist
}
func (p *pool) refreshPool() {
t := time.NewTimer(0)
started := sync.Once{}
for {
select {
case <-t.C:
p.doRefresh()
started.Do(func() {
close(p.started)
})
t.Reset(p.config.PoolRefresh)
case <-p.refresh:
p.doRefresh()
started.Do(func() {
close(p.started)
})
if !t.Stop() {
<-t.C
}
t.Reset(p.config.PoolRefresh)
case <-p.done:
return
}
}
}
func (p *pool) fetchComplianceCid(node string) error {
sc, err := p.th.GetComplianceCid(node)
if err != nil {
goLogger.Warnw("failed to find compliance cid ", "err", err)
return err
}
trialTimeout, cancel := context.WithTimeout(context.Background(), 30*time.Second)
reqUrl := fmt.Sprintf(complianceCidReqTemplate, sc)
goLogger.Debugw("fetching compliance cid", "cid", reqUrl, "from", node)
err = p.fetchResourceAndUpdate(trialTimeout, node, reqUrl, 0, p.mirrorValidator)
cancel()
return err
}
func (p *pool) checkPool() {
sem := make(chan struct{}, defaultMirroredConcurrency)
for {
select {
case msg := <-p.mirrorSamples:
sem <- struct{}{}
go func(msg mirroredPoolRequest) {
defer func() { <-sem }()
p.lk.RLock()
testNodes := p.th.GetNodes(tieredhashing.TierUnknown, msg.key, 1)
p.lk.RUnlock()
if len(testNodes) == 0 {
return
}
node := testNodes[0]
// Send compliance cid
rand := big.NewInt(1)
if p.config.ComplianceCidPeriod > 0 {
rand, _ = cryptoRand.Int(cryptoRand.Reader, big.NewInt(p.config.ComplianceCidPeriod))
}
if rand.Cmp(big.NewInt(0)) == 0 {
_ = p.fetchComplianceCid(node)
complianceCidCallsTotalMetric.WithLabelValues("success").Add(1)
}
// see if it is to a main-tier node - if so find appropriate test node to test against.
// --- Mirroring
p.lk.RLock()
if p.th.NodeTier(msg.node) != tieredhashing.TierMain {
p.lk.RUnlock()
return
}
p.lk.RUnlock()
trialTimeout, cancel := context.WithTimeout(context.Background(), DefaultSaturnMirrorRequestTimeout)
err := p.fetchResourceAndUpdate(trialTimeout, node, msg.path, 0, p.mirrorValidator)
cancel()
if err != nil {
goLogger.Warnw("mirrored request failed", "err", err.Error())
if strings.Contains(err.Error(), "empty car") {
mirroredTrafficTotalMetric.WithLabelValues("error-empty-car").Inc()
}
mirroredTrafficTotalMetric.WithLabelValues("error").Inc()
} else {
mirroredTrafficTotalMetric.WithLabelValues("no-error").Inc()
}
}(msg)
case <-p.done:
return
}
}
}
// TODO: this should be replaced with a real validator once one exists from boxo.
func (p *pool) mirrorValidator(resource string, reader io.Reader) error {
// first get the 'path' part to remove query string if present.
pth, err := url.Parse(resource)
if err != nil {
return err
}
parse, err := path.ParsePath(pth.Path)
if err != nil {
return err
}
matchedCid := cid.Undef
if parse.IsJustAKey() && len(parse) == 1 {
matchedCid, err = cid.Parse(parse.Segments()[0])
} else if len(parse) > 1 {
matchedCid, err = cid.Parse(parse.Segments()[1])
} else {
err = fmt.Errorf("unrecognized resource: %s", resource)
}
if err != nil {
return err
}
br, err := car.NewCarReaderWithOptions(reader, car.WithErrorOnEmptyRoots(false))
if err != nil {
return err
}
has := false
for {
blk, err := br.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if matchedCid.Equals(blk.Cid()) {
has = true
}
}
if !has {
return fmt.Errorf("response did not have requested root")
}
return nil
}
func (p *pool) Close() {
select {
case p.done <- struct{}{}:
return
default:
return
}
}
func cidToKey(c cid.Cid) string {
return c.Hash().B58String()
}
func (p *pool) fetchBlockWith(ctx context.Context, c cid.Cid, with string) (blk blocks.Block, err error) {
fetchCalledTotalMetric.WithLabelValues(resourceTypeBlock).Add(1)
if recordIfContextErr(resourceTypeBlock, ctx, "fetchBlockWith") {
return nil, ctx.Err()
}
// wait for pool to be initialised
<-p.started
// if the cid is in the cool down cache, we fail the request.
p.fetchKeyLk.RLock()
if at, ok := p.fetchKeyCoolDownCache.Get(cidToKey(c)); ok {
p.fetchKeyLk.RUnlock()
expireAt := at.(time.Time)
return nil, &ErrCoolDown{
Cid: c,
retryAfter: time.Until(expireAt),
}
}
p.fetchKeyLk.RUnlock()
aff := with
if aff == "" {
aff = cidToKey(c)
}
p.lk.RLock()
nodes := p.th.GetNodes(tieredhashing.TierMain, aff, p.config.MaxRetrievalAttempts)
if len(nodes) < p.config.MaxRetrievalAttempts {
nodes = append(nodes,
p.th.GetNodes(tieredhashing.TierUnknown, aff, p.config.MaxRetrievalAttempts-len(nodes))...,
)
}
p.lk.RUnlock()
if len(nodes) == 0 {
return nil, ErrNoBackend
}
blockFetchStart := time.Now()
for i := 0; i < len(nodes); i++ {
if recordIfContextErr(resourceTypeBlock, ctx, "fetchBlockWithLoop") {
return nil, ctx.Err()
}
blk, err = p.fetchBlockAndUpdate(ctx, nodes[i], c, i)
if err != nil && errors.Is(err, context.Canceled) {
return nil, err
}
if err == nil {
durationMs := time.Since(blockFetchStart).Milliseconds()
fetchDurationBlockSuccessMetric.Observe(float64(durationMs))
// mirror successful request
if p.config.MirrorFraction > rand.Float64() {
select {
case p.mirrorSamples <- mirroredPoolRequest{node: nodes[i], path: fmt.Sprintf("/ipfs/%s?format=car&car-scope=block", c), key: aff}:
default:
}
}
return
}
}
fetchDurationBlockFailureMetric.Observe(float64(time.Since(blockFetchStart).Milliseconds()))
p.updateFetchKeyCoolDown(cidToKey(c))
// Saturn fetch failed after exhausting all retrieval attempts, we can return the error.
return
}
// record the failure in the cid failure cache and
// if the number of cid fetch failures has crossed a certain threshold, add the cid to a cool down cache.
func (p *pool) updateFetchKeyCoolDown(key string) {
p.fetchKeyLk.Lock()
defer p.fetchKeyLk.Unlock()
expireAt := time.Now().Add(p.config.FetchKeyCoolDownDuration)
if p.config.MaxFetchFailuresBeforeCoolDown == 1 {
p.fetchKeyCoolDownCache.Set(key, expireAt, time.Until(expireAt)+100)
return
}
v, ok := p.fetchKeyFailureCache.Get(key)
if !ok {
p.fetchKeyFailureCache.Set(key, 1, cache.DefaultExpiration)
return
}
count := v.(int)
if p.config.MaxFetchFailuresBeforeCoolDown == 1 || count+1 == p.config.MaxFetchFailuresBeforeCoolDown {
p.fetchKeyCoolDownCache.Set(key, expireAt, time.Until(expireAt)+100)
p.fetchKeyFailureCache.Delete(key)
} else {
p.fetchKeyFailureCache.Set(key, count+1, cache.DefaultExpiration)
}
}
func (p *pool) fetchResourceWith(ctx context.Context, path string, cb DataCallback, with string) (err error) {
fetchCalledTotalMetric.WithLabelValues(resourceTypeCar).Add(1)
if recordIfContextErr(resourceTypeCar, ctx, "fetchResourceWith") {
return ctx.Err()
}
// wait for pool to be initialised
<-p.started
// if the cid is in the cool down cache, we fail the request.
p.fetchKeyLk.RLock()
if at, ok := p.fetchKeyCoolDownCache.Get(path); ok {
p.fetchKeyLk.RUnlock()
expireAt := at.(time.Time)
return &ErrCoolDown{
Path: path,
retryAfter: time.Until(expireAt),
}
}
p.fetchKeyLk.RUnlock()
aff := with
if aff == "" {
aff = path
}
p.lk.RLock()
nodes := p.th.GetNodes(tieredhashing.TierMain, aff, p.config.MaxRetrievalAttempts)
if len(nodes) < p.config.MaxRetrievalAttempts {
nodes = append(nodes,
p.th.GetNodes(tieredhashing.TierUnknown, aff, p.config.MaxRetrievalAttempts-len(nodes))...,
)
} else {
goLogger.Infow("using all main set nodes for CAR", "path", path, "aff", aff, "numNodes", len(nodes))
}
p.lk.RUnlock()
if len(nodes) == 0 {
return ErrNoBackend
}
carFetchStart := time.Now()
pq := []string{path}
for i := 0; i < len(nodes); i++ {
if recordIfContextErr(resourceTypeCar, ctx, "fetchResourceWithLoop") {
return ctx.Err()
}
// sample request for mirroring
if p.config.MirrorFraction > rand.Float64() {
select {
case p.mirrorSamples <- mirroredPoolRequest{node: nodes[i], path: pq[0], key: aff}:
default:
}
}
old := pq[0]
err = p.fetchResourceAndUpdate(ctx, nodes[i], old, i, cb)
if err != nil && errors.Is(err, context.Canceled) {
return err
}
var epr = ErrPartialResponse{}
if err == nil {
pq = pq[1:]
if len(pq) == 0 {
durationMs := time.Since(carFetchStart).Milliseconds()
// TODO: how to account for total retrieved data
//fetchSpeedPerBlockMetric.Observe(float64(float64(len(blk.RawData())) / float64(durationMs)))
fetchDurationCarSuccessMetric.Observe(float64(durationMs))
return
} else if pq[0] == old {
continue
} else {
// TODO: potentially worth doing something smarter here based on what the current state
// of permanent vs temporary errors is.
// for now: reset i on partials so we also give them a chance to retry.
i = -1
}
} else if errors.As(err, &epr) {
if len(epr.StillNeed) == 0 {
// the error was ErrPartial, but no additional needs were specified treat as
// any other transient error.
continue
}
pq = pq[1:]
pq = append(pq, epr.StillNeed...)
if pq[0] == old {
continue
} else {
// TODO: potentially worth doing something smarter here based on what the current state
// of permanent vs temporary errors is.
// for now: reset i on partials so we also give them a chance to retry.
i = -1
}
}
}
fetchDurationCarFailureMetric.Observe(float64(time.Since(carFetchStart).Milliseconds()))
p.updateFetchKeyCoolDown(path)
// Saturn fetch failed after exhausting all retrieval attempts, we can return the error.
return
}
func (p *pool) fetchBlockAndUpdate(ctx context.Context, node string, c cid.Cid, attempt int) (blk blocks.Block, err error) {
blk, rm, err := p.doFetch(ctx, node, c, attempt)
if err != nil && errors.Is(err, context.Canceled) {
return nil, err
}
if err != nil {
goLogger.Debugw("fetch attempt failed", "from", node, "attempt", attempt, "of", c, "error", err)
}
err = p.commonUpdate(node, rm, err)
return
}
func (p *pool) fetchResourceAndUpdate(ctx context.Context, node string, path string, attempt int, cb DataCallback) (err error) {
rm, err := p.fetchResource(ctx, node, path, "application/vnd.ipld.car", attempt, cb)
if err != nil && errors.Is(err, context.Canceled) {
return err
}
if err != nil {
goLogger.Debugw("fetch attempt failed", "from", node, "attempt", attempt, "of", path, "error", err)
}
p.commonUpdate(node, rm, err)
return
}
func (p *pool) commonUpdate(node string, rm tieredhashing.ResponseMetrics, err error) (ferr error) {
p.lk.Lock()
defer p.lk.Unlock()
ferr = err
if err == nil && rm.Success {
p.th.RecordSuccess(node, rm)
if p.th.IsInitDone() {
p.poolInitDone.Do(func() {
poolEnoughObservationsForMainSetDurationMetric.Set(float64(time.Since(p.th.StartAt).Milliseconds()))
})
}
// Saturn fetch worked, we return the block.
return
}
fr := p.th.RecordFailure(node, rm)
if fr != nil {
poolRemovedFailureTotalMetric.WithLabelValues(string(fr.Tier), fr.Reason).Inc()
poolRemovedConnFailureTotalMetric.WithLabelValues(string(fr.Tier)).Add(float64(fr.ConnErrors))
poolRemovedReadFailureTotalMetric.WithLabelValues(string(fr.Tier)).Add(float64(fr.NetworkErrors))
poolRemovedNon2xxTotalMetric.WithLabelValues(string(fr.Tier)).Add(float64(fr.ResponseCodes))
if fr.MainToUnknownChange != 0 || fr.UnknownToMainChange != 0 {
poolTierChangeMetric.WithLabelValues(tierMainToUnknown).Set(float64(fr.MainToUnknownChange))
poolTierChangeMetric.WithLabelValues(tierUnknownToMain).Set(float64(fr.UnknownToMainChange))
}
}
if p.th.DoRefresh() {
select {
case p.refresh <- struct{}{}:
default:
}
}
return
}