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
/
Copy pathfetcher.go
505 lines (440 loc) · 17.7 KB
/
fetcher.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
package caboose
import (
"context"
"errors"
"fmt"
"hash/crc32"
"io"
"net/http"
"net/http/httptrace"
"os"
"strconv"
"strings"
"time"
"github.com/filecoin-saturn/caboose/tieredhashing"
"github.com/willscott/go-requestcontext"
"go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"github.com/google/uuid"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
servertiming "github.com/mitchellh/go-server-timing"
"github.com/tcnksm/go-httpstat"
)
var saturnReqTmpl = "/ipfs/%s?format=raw"
const (
saturnNodeIdKey = "Saturn-Node-Id"
saturnTransferIdKey = "Saturn-Transfer-Id"
saturnCacheHitKey = "Saturn-Cache-Status"
saturnCacheHit = "HIT"
saturnRetryAfterKey = "Retry-After"
resourceTypeCar = "car"
resourceTypeBlock = "block"
)
var (
// 256 Kib to 8Gib
carSizes = []float64{262144, 524288, 1.048576e+06, 2.097152e+06, 4.194304e+06,
8.388608e+06, 1.6777216e+07, 3.3554432e+07, 6.7108864e+07, 1.34217728e+08,
2.68435456e+08, 5.36870912e+08, 1.073741824e+09, 2.147483648e+09, 4.294967296e+09, 8.589934592e+09}
carSizesStr = []string{"256.0 Kib", "512.0 Kib", "1.0 Mib", "2.0 Mib",
"4.0 Mib", "8.0 Mib", "16.0 Mib", "32.0 Mib", "64.0 Mib", "128.0 Mib", "256.0 Mib", "512.0 Mib", "1.0 Gib", "2.0 Gib", "4.0 Gib", "8.0 Gib"}
)
// doFetch attempts to fetch a block from a given Saturn endpoint. It sends the retrieval logs to the logging endpoint upon a successful or failed attempt.
func (p *pool) doFetch(ctx context.Context, from string, c cid.Cid, attempt int) (b blocks.Block, rm tieredhashing.ResponseMetrics, e error) {
reqUrl := fmt.Sprintf(saturnReqTmpl, c)
rm, e = p.fetchResource(ctx, from, reqUrl, "application/vnd.ipld.raw", attempt, func(rsrc string, r io.Reader) error {
block, err := io.ReadAll(io.LimitReader(r, maxBlockSize))
if err != nil {
switch {
case err == io.EOF && len(block) >= maxBlockSize:
// we don't expect to see this error any time soon, but if IPFS
// ecosystem ever starts allowing bigger blocks, this message will save
// multiple people collective man-months in debugging ;-)
return fmt.Errorf("strn responded with a block bigger than maxBlockSize=%d", maxBlockSize-1)
case err == io.EOF:
// This is fine :-)
// Zero-length block may be valid (example: bafkreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku)
// We accept this as non-error and let it go over CID validation later.
default:
return fmt.Errorf("unable to read strn response body: %w", err)
}
}
if p.config.DoValidation {
nc, err := c.Prefix().Sum(block)
if err != nil {
return blocks.ErrWrongHash
}
if !nc.Equals(c) {
return blocks.ErrWrongHash
}
}
b, e = blocks.NewBlockWithCid(block, c)
if e != nil {
return e
}
return nil
})
return
}
// TODO Refactor to use a metrics collector that separates the collection of metrics from the actual fetching
func (p *pool) fetchResource(ctx context.Context, from string, resource string, mime string, attempt int, cb DataCallback) (rm tieredhashing.ResponseMetrics, err error) {
rm = tieredhashing.ResponseMetrics{}
resourceType := resourceTypeCar
if mime == "application/vnd.ipld.raw" {
resourceType = resourceTypeBlock
}
// if the context is already cancelled, there's nothing we can do here.
if ce := ctx.Err(); ce != nil {
fetchRequestContextErrorTotalMetric.WithLabelValues(resourceType, fmt.Sprintf("%t", errors.Is(ce, context.Canceled)), "fetchResource-init").Add(1)
return rm, ce
}
p.lk.RLock()
isCore := p.th.IsCore(from)
p.lk.RUnlock()
ctx, span := spanTrace(ctx, "Pool.FetchResource", trace.WithAttributes(attribute.String("from", from), attribute.String("of", resource), attribute.String("mime", mime), attribute.Bool("core", isCore)))
defer span.End()
requestId := uuid.NewString()
goLogger.Debugw("doing fetch", "from", from, "of", resource, "mime", mime, "requestId", requestId)
contextRequestId := requestcontext.IDFromContext(ctx)
start := time.Now()
response_success_end := time.Now()
fb := time.Unix(0, 0)
code := 0
proto := "unknown"
respReq := &http.Request{}
received := 0
reqUrl := ""
if strings.Contains(from, "://") {
reqUrl = fmt.Sprintf("%s%s", from, resource)
} else {
reqUrl = fmt.Sprintf("https://%s%s", from, resource)
}
var respHeader http.Header
saturnNodeId := ""
saturnTransferId := ""
isCacheHit := false
networkError := ""
verificationError := ""
isBlockRequest := false
if mime == "application/vnd.ipld.raw" {
isBlockRequest = true
}
// Get our timing header builder from the context
timing := servertiming.FromContext(ctx)
var timingMetric *servertiming.Metric
if timing != nil {
timingMetric = timing.NewMetric("fetch").Start()
defer timingMetric.Stop()
}
var result httpstat.Result
defer func() {
// do nothing if this is because of a cancelled context
if err != nil && errors.Is(err, context.Canceled) {
return
}
if respHeader != nil {
saturnNodeId = respHeader.Get(saturnNodeIdKey)
saturnTransferId = respHeader.Get(saturnTransferIdKey)
cacheHit := respHeader.Get(saturnCacheHitKey)
if cacheHit == saturnCacheHit {
isCacheHit = true
}
}
durationSecs := time.Since(start).Seconds()
goLogger.Debugw("fetch result", "from", from, "of", resource, "status", code, "size", received, "duration", durationSecs, "attempt", attempt, "error", err,
"proto", proto)
fetchResponseCodeMetric.WithLabelValues(resourceType, fmt.Sprintf("%d", code)).Add(1)
var ttfbMs int64
if err == nil && received > 0 {
ttfbMs = fb.Sub(start).Milliseconds()
cacheStatus := getCacheStatus(isCacheHit)
if isBlockRequest {
fetchSizeBlockMetric.Observe(float64(received))
} else {
fetchSizeCarMetric.WithLabelValues("success").Observe(float64(received))
}
durationMs := response_success_end.Sub(start).Milliseconds()
fetchSpeedPerPeerSuccessMetric.WithLabelValues(resourceType, cacheStatus).Observe(float64(received) / float64(durationMs))
fetchCacheCountSuccessTotalMetric.WithLabelValues(resourceType, cacheStatus).Add(1)
// track individual block metrics separately
if isBlockRequest {
fetchTTFBPerBlockPerPeerSuccessMetric.WithLabelValues(cacheStatus).Observe(float64(ttfbMs))
fetchDurationPerBlockPerPeerSuccessMetric.WithLabelValues(cacheStatus).Observe(float64(response_success_end.Sub(start).Milliseconds()))
} else {
ci := 0
for index, value := range carSizes {
if float64(received) < value {
ci = index
break
}
}
carSizeStr := carSizesStr[ci]
fetchTTFBPerCARPerPeerSuccessMetric.WithLabelValues(cacheStatus, carSizeStr).Observe(float64(ttfbMs))
fetchDurationPerCarPerPeerSuccessMetric.WithLabelValues(cacheStatus).Observe(float64(response_success_end.Sub(start).Milliseconds()))
}
// update L1 server timings
updateSuccessServerTimingMetrics(respHeader.Values(servertiming.HeaderKey), resourceType, isCacheHit, durationMs, ttfbMs, received)
} else {
if isBlockRequest {
fetchDurationPerBlockPerPeerFailureMetric.Observe(float64(time.Since(start).Milliseconds()))
} else {
fetchDurationPerCarPerPeerFailureMetric.Observe(float64(time.Since(start).Milliseconds()))
fetchSizeCarMetric.WithLabelValues("failure").Observe(float64(received))
}
if code == http.StatusBadGateway || code == http.StatusGatewayTimeout {
updateLassie5xxTime(respHeader.Values(servertiming.HeaderKey), resourceType)
}
}
if err == nil || !errors.Is(err, context.Canceled) {
if p.logger != nil {
p.logger.queue <- log{
CacheHit: isCacheHit,
URL: reqUrl,
StartTime: start,
NumBytesSent: received,
RequestDurationSec: durationSecs,
RequestID: saturnTransferId,
HTTPStatusCode: code,
HTTPProtocol: proto,
TTFBMS: int(ttfbMs),
// my address
Range: "",
Referrer: respReq.Referer(),
UserAgent: respReq.UserAgent(),
NodeId: saturnNodeId,
NodeIpAddress: from,
IfNetworkError: networkError,
VerificationError: verificationError,
}
}
}
}()
// TODO: Ideally, we would have additional "PerRequestInactivityTimeout"
// which is the amount of time without any NEW data from the server, but
// that can be added later. We need both because a slow trickle of data
// could take a large amount of time.
requestTimeout := time.Duration(DefaultSaturnCarRequestTimeout.Milliseconds()/int64(attempt+1)) * time.Millisecond
if isBlockRequest {
requestTimeout = time.Duration(DefaultSaturnBlockRequestTimeout.Milliseconds()/int64(attempt+1)) * time.Millisecond
}
reqCtx, cancel := context.WithTimeout(ctx, requestTimeout)
defer cancel()
clientTrace := otelhttptrace.NewClientTrace(reqCtx)
subReqCtx := httptrace.WithClientTrace(reqCtx, clientTrace)
req, err := http.NewRequestWithContext(subReqCtx, http.MethodGet, reqUrl, nil)
if err != nil {
if recordIfContextErr(resourceType, reqCtx, "build-http-request") {
return rm, reqCtx.Err()
}
return rm, err
}
req.Header.Add("Accept", mime)
if contextRequestId != "" {
req.Header.Add("X-Request-Id", contextRequestId)
}
if p.config.ExtraHeaders != nil {
for k, vs := range *p.config.ExtraHeaders {
for _, v := range vs {
req.Header.Add(k, v)
}
}
}
agent := req.Header.Get("User-Agent")
req.Header.Set("User-Agent", os.Getenv(SaturnEnvKey)+"/"+agent)
//trace
req = req.WithContext(httpstat.WithHTTPStat(req.Context(), &result))
var resp *http.Response
saturnCallsTotalMetric.WithLabelValues(resourceType).Add(1)
startReq := time.Now()
resp, err = p.config.SaturnClient.Do(req)
if err != nil {
if recordIfContextErr(resourceType, reqCtx, "send-http-request") {
if errors.Is(err, context.Canceled) {
return rm, reqCtx.Err()
}
}
if errors.Is(err, context.DeadlineExceeded) {
saturnConnectionFailureTotalMetric.WithLabelValues(resourceType, "timeout").Add(1)
} else {
saturnConnectionFailureTotalMetric.WithLabelValues(resourceType, "non-timeout").Add(1)
}
networkError = err.Error()
rm.ConnFailure = true
return rm, fmt.Errorf("http request failed: %w", err)
}
respHeader = resp.Header
headerTTFBPerPeerMetric.WithLabelValues(resourceType, getCacheStatus(respHeader.Get(saturnCacheHitKey) == saturnCacheHit)).Observe(float64(time.Since(startReq).Milliseconds()))
defer resp.Body.Close()
code = resp.StatusCode
rm.ResponseCode = code
proto = resp.Proto
respReq = resp.Request
if timing != nil && respHeader.Get(saturnCacheHitKey) != saturnCacheHit {
timingHeaders := respHeader.Values(servertiming.HeaderKey)
for _, th := range timingHeaders {
if subReqTiming, err := servertiming.ParseHeader(th); err == nil {
for _, m := range subReqTiming.Metrics {
// forward upstream
m.Extra["attempt"] = fmt.Sprintf("%d", attempt)
m.Extra["subreq"] = subReqID(from, resource)
timing.Add(m)
// record the otel span
_, subSpan := otel.Tracer("caboose/l1").Start(ctx, m.Name, trace.WithTimestamp(time.Now().Add(m.Duration*-1)))
for eK, eV := range m.Extra {
subSpan.SetAttributes(attribute.String(eK, eV))
}
subSpan.End()
}
}
}
}
if resp.StatusCode != http.StatusOK {
_, _ = io.Copy(io.Discard, resp.Body)
saturnCallsFailureTotalMetric.WithLabelValues(resourceType, "non-2xx", fmt.Sprintf("%d", resp.StatusCode)).Add(1)
if resp.StatusCode == http.StatusTooManyRequests {
var retryAfter time.Duration
if strnRetryHint := respHeader.Get(saturnRetryAfterKey); strnRetryHint != "" {
seconds, err := strconv.ParseInt(strnRetryHint, 10, 64)
if err == nil {
retryAfter = time.Duration(seconds) * time.Second
}
}
if retryAfter == 0 {
retryAfter = p.config.SaturnNodeCoolOff
}
return rm, fmt.Errorf("http error from strn: %d, err=%w", resp.StatusCode, &ErrSaturnTooManyRequests{retryAfter: retryAfter, Node: from})
}
// empty body so it can be re-used.
if resp.StatusCode == http.StatusGatewayTimeout {
return rm, fmt.Errorf("http error from strn: %d, err=%w", resp.StatusCode, ErrSaturnTimeout)
}
// This should only be 502, but L1s were not translating 404 from Lassie, so we have to support both for now.
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusBadGateway {
return rm, fmt.Errorf("http error from strn: %d, err=%w", resp.StatusCode, ErrContentProviderNotFound)
}
return rm, fmt.Errorf("http error from strn: %d", resp.StatusCode)
}
if respHeader.Get(saturnCacheHitKey) == saturnCacheHit {
isCacheHit = true
}
wrapped := TrackingReader{resp.Body, time.Time{}, 0}
err = cb(resource, &wrapped)
received = wrapped.len
if err != nil {
var target = ErrInvalidResponse{}
if errors.As(err, &target) {
verificationError = err.Error()
goLogger.Errorw("failed to read response; verification error", "err", err.Error())
} else {
networkError = err.Error()
goLogger.Errorw("failed to read response; no verification error", "err", err.Error())
}
if strings.Contains(err.Error(), "empty car") {
emptyCarErrorTotalMetric.WithLabelValues(resourceType).Inc()
}
if recordIfContextErr(resourceType, reqCtx, "read-http-response") {
if errors.Is(err, context.Canceled) {
return rm, reqCtx.Err()
}
}
if errors.Is(err, context.DeadlineExceeded) {
saturnCallsFailureTotalMetric.WithLabelValues(resourceType, fmt.Sprintf("failed-response-read-timeout-%s", getCacheStatus(isCacheHit)),
fmt.Sprintf("%d", code)).Add(1)
} else {
saturnCallsFailureTotalMetric.WithLabelValues(resourceType, fmt.Sprintf("failed-response-read-%s", getCacheStatus(isCacheHit)), fmt.Sprintf("%d", code)).Add(1)
}
rm.NetworkError = true
return rm, err
}
// We don't drain the response as that causes the go-routine to block everytime which leaks go-routines.
fb = wrapped.firstByte
response_success_end = time.Now()
// trace-metrics
// request life-cycle metrics
fetchRequestSuccessTimeTraceMetric.WithLabelValues(resourceType, getCacheStatus(isCacheHit), "tcp_connection").Observe(float64(result.TCPConnection.Milliseconds()))
fetchRequestSuccessTimeTraceMetric.WithLabelValues(resourceType, getCacheStatus(isCacheHit), "tls_handshake").Observe(float64(result.TLSHandshake.Milliseconds()))
fetchRequestSuccessTimeTraceMetric.WithLabelValues(resourceType, getCacheStatus(isCacheHit), "wait_after_request_sent_for_header").Observe(float64(result.ServerProcessing.Milliseconds()))
fetchRequestSuccessTimeTraceMetric.WithLabelValues(resourceType, getCacheStatus(isCacheHit), "name_lookup").
Observe(float64(result.NameLookup.Milliseconds()))
fetchRequestSuccessTimeTraceMetric.WithLabelValues(resourceType, getCacheStatus(isCacheHit), "connect").
Observe(float64(result.Connect.Milliseconds()))
fetchRequestSuccessTimeTraceMetric.WithLabelValues(resourceType, getCacheStatus(isCacheHit), "pre_transfer").
Observe(float64(result.Pretransfer.Milliseconds()))
fetchRequestSuccessTimeTraceMetric.WithLabelValues(resourceType, getCacheStatus(isCacheHit), "start_transfer").
Observe(float64(result.StartTransfer.Milliseconds()))
rm.TTFBMs = float64(wrapped.firstByte.Sub(start).Milliseconds())
rm.Success = true
rm.SpeedPerMs = float64(received) / float64(response_success_end.Sub(start).Milliseconds())
saturnCallsSuccessTotalMetric.WithLabelValues(resourceType, getCacheStatus(isCacheHit)).Add(1)
return rm, nil
}
func recordIfContextErr(resourceType string, ctx context.Context, requestState string) bool {
if ce := ctx.Err(); ce != nil {
fetchRequestContextErrorTotalMetric.WithLabelValues(resourceType, fmt.Sprintf("%t", errors.Is(ce, context.Canceled)), requestState).Add(1)
return true
}
return false
}
func updateLassie5xxTime(timingHeaders []string, resourceType string) {
if len(timingHeaders) == 0 {
goLogger.Debug("no timing headers in request response.")
return
}
for _, th := range timingHeaders {
if subReqTiming, err := servertiming.ParseHeader(th); err == nil {
for _, m := range subReqTiming.Metrics {
switch m.Name {
case "shim_lassie_headers":
if m.Duration.Milliseconds() != 0 {
lassie5XXTimeMetric.WithLabelValues(resourceType).Observe(float64(m.Duration.Milliseconds()))
}
return
default:
}
}
}
}
}
// todo: refactor for dryness
func updateSuccessServerTimingMetrics(timingHeaders []string, resourceType string, isCacheHit bool, totalTimeMs, ttfbMs int64, recieved int) {
if len(timingHeaders) == 0 {
goLogger.Debug("no timing headers in request response.")
return
}
for _, th := range timingHeaders {
if subReqTiming, err := servertiming.ParseHeader(th); err == nil {
for _, m := range subReqTiming.Metrics {
switch m.Name {
case "shim_lassie_headers":
if m.Duration.Milliseconds() != 0 && !isCacheHit {
fetchDurationPerPeerSuccessCacheMissTotalLassieMetric.WithLabelValues(resourceType).Observe(float64(m.Duration.Milliseconds()))
}
case "nginx":
// sanity checks
if totalTimeMs != 0 && ttfbMs != 0 && m.Duration.Milliseconds() != 0 {
fetchDurationPerPeerSuccessTotalL1NodeMetric.WithLabelValues(resourceType, getCacheStatus(isCacheHit)).Observe(float64(m.Duration.Milliseconds()))
networkTimeMs := totalTimeMs - m.Duration.Milliseconds()
if networkTimeMs > 0 {
s := float64(recieved) / float64(networkTimeMs)
fetchNetworkSpeedPerPeerSuccessMetric.WithLabelValues(resourceType).Observe(s)
}
networkLatencyMs := ttfbMs - m.Duration.Milliseconds()
fetchNetworkLatencyPeerSuccessMetric.WithLabelValues(resourceType).Observe(float64(networkLatencyMs))
}
default:
}
}
}
}
}
func getCacheStatus(isCacheHit bool) string {
if isCacheHit {
return "Cache-hit"
}
return "Cache-miss"
}
func subReqID(host, rsrc string) string {
return fmt.Sprintf("%x", crc32.ChecksumIEEE([]byte(host+rsrc)))
}