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
/
caboose.go
377 lines (308 loc) · 12.6 KB
/
caboose.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
package caboose
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"time"
"github.com/filecoin-saturn/caboose/tieredhashing"
"github.com/willscott/go-requestcontext"
ipfsblockstore "github.com/ipfs/boxo/blockstore"
ipath "github.com/ipfs/boxo/coreiface/path"
gateway "github.com/ipfs/boxo/gateway"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
const (
SaturnEnvKey = "STRN_ENV_TAG"
)
type Config struct {
// OrchestratorEndpoint is the URL of the Saturn orchestrator.
OrchestratorEndpoint *url.URL
// OrchestratorClient is the HTTP client to use when communicating with the Saturn orchestrator.
OrchestratorClient *http.Client
// OrchestratorOverride replaces calls to the orchestrator with a fixed response.
OrchestratorOverride []tieredhashing.NodeInfo
// LoggingEndpoint is the URL of the logging endpoint where we submit logs pertaining to our Saturn retrieval requests.
LoggingEndpoint url.URL
// LoggingClient is the HTTP client to use when communicating with the logging endpoint.
LoggingClient *http.Client
// LoggingInterval is the interval at which we submit logs to the logging endpoint.
LoggingInterval time.Duration
// SaturnClient is the HTTP client to use when retrieving content from the Saturn network.
SaturnClient *http.Client
ExtraHeaders *http.Header
// DoValidation is used to determine if we should validate the blocks recieved from the Saturn network.
DoValidation bool
// If set, AffinityKey is used instead of the block CID as the key on the
// Saturn node pool to determine which Saturn node to retrieve the block from.
// NOTE: If gateway.ContentPathKey is present in request context,
// it will be used as AffinityKey automatically.
AffinityKey string
// PoolRefresh is the interval at which we refresh the pool of Saturn nodes.
PoolRefresh time.Duration
// MirrorFraction is what fraction of requests will be mirrored to another random node in order to track metrics / determine the current best nodes.
MirrorFraction float64
// MaxRetrievalAttempts determines the number of times we will attempt to retrieve a block from the Saturn network before failing.
MaxRetrievalAttempts int
// MaxFetchFailuresBeforeCoolDown is the maximum number of retrieval failures across the pool for a url before we auto-reject subsequent
// fetches of that url.
MaxFetchFailuresBeforeCoolDown int
// FetchKeyCoolDownDuration is duration of time a key will stay in the cool down cache
// before we start making retrieval attempts for it.
FetchKeyCoolDownDuration time.Duration
// SaturnNodeCoolOff is the cool off duration for a saturn node once we determine that we shouldn't be sending requests to it for a while.
SaturnNodeCoolOff time.Duration
TieredHashingOpts []tieredhashing.Option
ComplianceCidPeriod int64
}
const DefaultLoggingInterval = 5 * time.Second
const DefaultSaturnOrchestratorRequestTimeout = 30 * time.Second
const DefaultSaturnBlockRequestTimeout = 19 * time.Second
const DefaultSaturnCarRequestTimeout = 30 * time.Minute
const DefaultSaturnMirrorRequestTimeout = 30 * time.Second
// default retries before failure unless overridden by MaxRetrievalAttempts
const defaultMaxRetries = 2
// default percentage of requests to mirror for tracking how nodes perform unless overridden by MirrorFraction
const defaultMirrorFraction = 0.01
const maxBlockSize = 4194305 // 4 Mib + 1 byte
const DefaultOrchestratorEndpoint = "https://orchestrator.strn.pl/nodes?maxNodes=200"
const DefaultPoolRefreshInterval = 5 * time.Minute
// we cool off sending requests to Saturn for a cid for a certain duration
// if we've seen a certain number of failures for it already in a given duration.
// NOTE: before getting creative here, make sure you dont break end user flow
// described in https://github.com/ipni/storetheindex/pull/1344
const defaultMaxFetchFailures = 3 * defaultMaxRetries // this has to fail more than DefaultMaxRetries done for a single gateway request
const defaultFetchKeyCoolDownDuration = 1 * time.Minute // how long will a sane person wait and stare at blank screen with "retry later" error before hitting F5?
// we cool off sending requests to a Saturn node if it returns transient errors rather than immediately downvoting it;
// however, only upto a certain max number of cool-offs.
const defaultSaturnNodeCoolOff = 5 * time.Minute
// This represents, on average, how many requests caboose makes before requesting a compliance cid.
// Example: a period of 100 implies Caboose will on average make a compliance CID request once every 100 requests.
const DefaultComplianceCidPeriod = int64(100)
var ErrNotImplemented error = errors.New("not implemented")
var ErrNoBackend error = errors.New("no available saturn backend")
var ErrContentProviderNotFound error = errors.New("saturn failed to find content providers")
var ErrSaturnTimeout error = errors.New("saturn backend timed out")
type ErrSaturnTooManyRequests struct {
Node string
retryAfter time.Duration
}
func (e *ErrSaturnTooManyRequests) Error() string {
return fmt.Sprintf("saturn node %s returned Too Many Requests error, please retry after %s", e.Node, humanRetry(e.retryAfter))
}
func (e *ErrSaturnTooManyRequests) RetryAfter() time.Duration {
return e.retryAfter
}
type ErrCoolDown struct {
Cid cid.Cid
Path string
retryAfter time.Duration
}
func (e *ErrCoolDown) Error() string {
switch true {
case e.Cid != cid.Undef && e.Path != "":
return fmt.Sprintf("multiple saturn retrieval failures seen for CID %q and Path %q, please retry after %s", e.Cid, e.Path, humanRetry(e.retryAfter))
case e.Path != "":
return fmt.Sprintf("multiple saturn retrieval failures seen for Path %q, please retry after %s", e.Path, humanRetry(e.retryAfter))
case e.Cid != cid.Undef:
return fmt.Sprintf("multiple saturn retrieval failures seen for CID %q, please retry after %s", e.Cid, humanRetry(e.retryAfter))
default:
return fmt.Sprintf("multiple saturn retrieval failures for unknown CID/Path (BUG), please retry after %s", humanRetry(e.retryAfter))
}
}
func (e *ErrCoolDown) RetryAfter() time.Duration {
return e.retryAfter
}
func humanRetry(d time.Duration) string {
return d.Truncate(time.Second).String()
}
// ErrPartialResponse can be returned from a DataCallback to indicate that some of the requested resource
// was successfully fetched, and that instead of retrying the full resource, that there are
// one or more more specific resources that should be fetched (via StillNeed) to complete the request.
type ErrPartialResponse struct {
error
StillNeed []string
}
func (epr ErrPartialResponse) Error() string {
if epr.error != nil {
return fmt.Sprintf("partial response: %s", epr.error.Error())
}
return "caboose received a partial response"
}
// ErrInvalidResponse can be returned from a DataCallback to indicate that the data provided for the
// requested resource was explicitly 'incorrect' - that blocks not in the requested dag, or non-car-conforming
// data was returned.
type ErrInvalidResponse struct {
Message string
}
func (e ErrInvalidResponse) Error() string {
return e.Message
}
type Caboose struct {
config *Config
pool *pool
logger *logger
}
// DataCallback allows for extensible validation of path-retrieved data.
type DataCallback func(resource string, reader io.Reader) error
// NewCaboose sets up a caboose fetcher.
// Note: Caboose is NOT a persistent blockstore and does NOT have an in-memory cache.
// Every request will result in a remote network request.
func NewCaboose(config *Config) (*Caboose, error) {
if config.FetchKeyCoolDownDuration == 0 {
config.FetchKeyCoolDownDuration = defaultFetchKeyCoolDownDuration
}
if config.MaxFetchFailuresBeforeCoolDown == 0 {
config.MaxFetchFailuresBeforeCoolDown = defaultMaxFetchFailures
}
if config.SaturnNodeCoolOff == 0 {
config.SaturnNodeCoolOff = defaultSaturnNodeCoolOff
}
if config.MirrorFraction == 0 {
config.MirrorFraction = defaultMirrorFraction
}
if override := os.Getenv(BackendOverrideKey); len(override) > 0 {
var overrideNodes []tieredhashing.NodeInfo
err := json.Unmarshal([]byte(override), &overrideNodes)
if err != nil {
goLogger.Warnf("Error parsing BackendOverrideKey:", "err", err)
return nil, err
}
config.OrchestratorOverride = overrideNodes
}
c := Caboose{
config: config,
pool: newPool(config),
logger: newLogger(config),
}
c.pool.logger = c.logger
if c.config.SaturnClient == nil {
c.config.SaturnClient = &http.Client{
Timeout: DefaultSaturnCarRequestTimeout,
}
}
c.config.SaturnClient.Transport = otelhttp.NewTransport(c.config.SaturnClient.Transport)
if c.config.OrchestratorEndpoint == nil {
var err error
c.config.OrchestratorEndpoint, err = url.Parse(DefaultOrchestratorEndpoint)
if err != nil {
return nil, err
}
}
if c.config.ComplianceCidPeriod == 0 {
c.config.ComplianceCidPeriod = DefaultComplianceCidPeriod
}
if c.config.PoolRefresh == 0 {
c.config.PoolRefresh = DefaultPoolRefreshInterval
}
if c.config.MaxRetrievalAttempts == 0 {
c.config.MaxRetrievalAttempts = defaultMaxRetries
}
// start the pool
c.pool.Start()
return &c, nil
}
// Caboose is a blockstore.
var _ ipfsblockstore.Blockstore = (*Caboose)(nil)
func (c *Caboose) Close() {
c.pool.Close()
c.logger.Close()
}
// Fetch allows fetching car archives by a path of the form `/ipfs/<cid>[/path/to/file]`
func (c *Caboose) Fetch(ctx context.Context, path string, cb DataCallback) error {
traceID := requestcontext.IDFromContext(ctx)
tid, err := trace.TraceIDFromHex(traceID)
ctx, span := spanTrace(ctx, "Fetch", trace.WithAttributes(attribute.String("path", path)))
defer span.End()
if err == nil {
sc := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: tid,
SpanID: span.SpanContext().SpanID(),
Remote: true,
})
ctx = trace.ContextWithRemoteSpanContext(ctx, sc)
}
return c.pool.fetchResourceWith(ctx, path, cb, c.getAffinity(ctx))
}
func (c *Caboose) Has(ctx context.Context, it cid.Cid) (bool, error) {
ctx, span := spanTrace(ctx, "Has", trace.WithAttributes(attribute.Stringer("cid", it)))
defer span.End()
blk, err := c.pool.fetchBlockWith(ctx, it, c.getAffinity(ctx))
if err != nil {
return false, err
}
return blk != nil, nil
}
// for testing only
func (c *Caboose) GetPoolPerf() map[string]*tieredhashing.NodePerf {
return c.pool.th.GetPerf()
}
func (c *Caboose) Get(ctx context.Context, it cid.Cid) (blocks.Block, error) {
ctx, span := spanTrace(ctx, "Get", trace.WithAttributes(attribute.Stringer("cid", it)))
defer span.End()
blk, err := c.pool.fetchBlockWith(ctx, it, c.getAffinity(ctx))
if err != nil {
return nil, err
}
return blk, nil
}
// GetSize returns the CIDs mapped BlockSize
func (c *Caboose) GetSize(ctx context.Context, it cid.Cid) (int, error) {
ctx, span := spanTrace(ctx, "GetSize", trace.WithAttributes(attribute.Stringer("cid", it)))
defer span.End()
blk, err := c.pool.fetchBlockWith(ctx, it, c.getAffinity(ctx))
if err != nil {
return 0, err
}
return len(blk.RawData()), nil
}
func (c *Caboose) getAffinity(ctx context.Context) string {
// https://github.com/ipfs/bifrost-gateway/issues/53#issuecomment-1442732865
if affG := ctx.Value(gateway.ContentPathKey); affG != nil {
contentPath := affG.(ipath.Path).String()
// Using exact content path seems to work better for initial website loads
// because it groups all blocks related to the single file,
// but at the same time spreads files across multiple L1s
// which removes the risk of specific L1 becoming a cache hot spot for
// websites with huge DAG like /ipns/en.wikipedia-on-ipfs.org
return contentPath
/* TODO: if we ever want to revisit, and group per root CID of entire DAG:
const contentRootIdx = 2
if parts := strings.Split(contentPath, "/"); len(parts) > contentRootIdx {
// use top level contentRoot ('id' from /ipfs/id or /ipns/id) as affinity key
return parts[contentRootIdx]
}
*/
}
if affC := ctx.Value(c.config.AffinityKey); affC != nil {
return affC.(string)
}
return ""
}
// HashOnRead specifies if every read block should be
// rehashed to make sure it matches its CID.
func (c *Caboose) HashOnRead(enabled bool) {
c.config.DoValidation = enabled
}
/* Mutable blockstore methods */
func (c *Caboose) Put(context.Context, blocks.Block) error {
return ErrNotImplemented
}
func (c *Caboose) PutMany(context.Context, []blocks.Block) error {
return ErrNotImplemented
}
func (c *Caboose) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
return nil, ErrNotImplemented
}
func (c *Caboose) DeleteBlock(context.Context, cid.Cid) error {
return ErrNotImplemented
}
var _ ipfsblockstore.Blockstore = (*Caboose)(nil)