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
366 lines (309 loc) · 11.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
package caboose
import (
"context"
"errors"
"fmt"
"hash/crc32"
"io"
"net/http"
"net/http/httptrace"
"os"
"strconv"
"strings"
"time"
"go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"github.com/google/uuid"
servertiming "github.com/mitchellh/go-server-timing"
"github.com/tcnksm/go-httpstat"
)
const (
UserAgentTag = "STRN_ENV_TAG"
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"}
)
// 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 *Node, resource string, mime string, attempt int, cb DataCallback) (err error) {
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 {
return ce
}
p.ActiveNodes.lk.RLock()
isCore := p.ActiveNodes.IsCore(from)
p.ActiveNodes.lk.RUnlock()
ctx, span := spanTrace(ctx, "Pool.FetchResource", trace.WithAttributes(attribute.String("from", from.URL), 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)
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.URL, "://") {
reqUrl = fmt.Sprintf("%s%s", from.URL, resource)
} else {
reqUrl = fmt.Sprintf("https://%s%s", from.URL, 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 {
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 {
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()))
}
} else {
if !isBlockRequest {
fetchDurationPerCarPerPeerFailureMetric.Observe(float64(time.Since(start).Milliseconds()))
fetchSizeCarMetric.WithLabelValues("failure").Observe(float64(received))
}
}
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.URL,
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 := DefaultCarRequestTimeout
if isBlockRequest {
requestTimeout = DefaultBlockRequestTimeout
}
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 isCtxError(reqCtx) {
return reqCtx.Err()
}
return err
}
req.Header.Add("Accept", mime)
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(UserAgentTag)+"/"+agent)
//trace
req = req.WithContext(httpstat.WithHTTPStat(req.Context(), &result))
var resp *http.Response
saturnCallsTotalMetric.WithLabelValues(resourceType).Add(1)
resp, err = p.config.Client.Do(req)
if err != nil {
if isCtxError(reqCtx) {
if errors.Is(err, context.Canceled) {
return 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()
return fmt.Errorf("http request failed: %w", err)
}
respHeader = resp.Header
defer resp.Body.Close()
code = resp.StatusCode
proto = resp.Proto
respReq = resp.Request
if timing != nil {
timingHeaders := respHeader.Values(servertiming.HeaderKey)
for _, th := range timingHeaders {
if subReqTiming, err := servertiming.ParseHeader(th); err == nil {
for _, m := range subReqTiming.Metrics {
m.Extra["attempt"] = fmt.Sprintf("%d", attempt)
m.Extra["subreq"] = subReqID(from.URL, resource)
timing.Add(m)
}
}
}
}
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.CoolOff
}
return fmt.Errorf("http error from strn: %d, err=%w", resp.StatusCode, &ErrTooManyRequests{retryAfter: retryAfter, Node: from.URL})
}
// empty body so it can be re-used.
if resp.StatusCode == http.StatusGatewayTimeout {
return fmt.Errorf("http error from strn: %d, err=%w", resp.StatusCode, ErrTimeout)
}
// 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 fmt.Errorf("http error from strn: %d, err=%w", resp.StatusCode, ErrContentProviderNotFound)
}
return 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 {
if isCtxError(reqCtx) {
if errors.Is(err, context.Canceled) {
return 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)
}
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())
}
return err
}
fb = wrapped.firstByte
response_success_end = time.Now()
// trace-metrics
// request life-cycle metrics
saturnCallsSuccessTotalMetric.WithLabelValues(resourceType, getCacheStatus(isCacheHit)).Add(1)
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()))
from.RecordSuccess(start, float64(wrapped.firstByte.Sub(start).Milliseconds()), float64(received)/float64(response_success_end.Sub(start).Milliseconds()))
return nil
}
func isCtxError(ctx context.Context) bool {
if ce := ctx.Err(); ce != nil {
return true
}
return false
}
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)))
}