-
Notifications
You must be signed in to change notification settings - Fork 17
/
retriever.go
292 lines (263 loc) · 9.42 KB
/
retriever.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
package retriever
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/benbjohnson/clock"
"github.com/dustin/go-humanize"
"github.com/filecoin-project/lassie/pkg/events"
"github.com/filecoin-project/lassie/pkg/retriever/combinators"
"github.com/filecoin-project/lassie/pkg/types"
"github.com/ipfs/go-cid"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipni/go-libipni/metadata"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multicodec"
)
var (
ErrRetrieverNotStarted = errors.New("retriever not started")
ErrDealProposalFailed = errors.New("deal proposal failed")
ErrNoCandidates = errors.New("no candidates")
ErrUnexpectedRetrieval = errors.New("unexpected active retrieval")
ErrHitRetrievalLimit = errors.New("hit retrieval limit")
ErrProposalCreationFailed = errors.New("proposal creation failed")
ErrRetrievalRegistrationFailed = errors.New("retrieval registration failed")
ErrRetrievalFailed = errors.New("retrieval failed")
ErrAllRetrievalsFailed = errors.New("all retrievals failed")
ErrConnectFailed = errors.New("unable to connect to provider")
ErrAllQueriesFailed = errors.New("all queries failed")
ErrRetrievalTimedOut = errors.New("retrieval timed out")
ErrRetrievalAlreadyRunning = errors.New("retrieval already running for CID")
)
type Session interface {
GetStorageProviderTimeout(storageProviderId peer.ID) time.Duration
FilterIndexerCandidate(candidate types.RetrievalCandidate) (bool, types.RetrievalCandidate)
RegisterRetrieval(retrievalId types.RetrievalID, cid cid.Cid, selector datamodel.Node) bool
AddToRetrieval(retrievalId types.RetrievalID, storageProviderIds []peer.ID) error
EndRetrieval(retrievalId types.RetrievalID) error
RecordConnectTime(storageProviderId peer.ID, connectTime time.Duration)
RecordFirstByteTime(storageProviderId peer.ID, firstByteTime time.Duration)
RecordFailure(retrievalId types.RetrievalID, storageProviderId peer.ID) error
RecordSuccess(storageProviderId peer.ID, bandwidthBytesPerSecond uint64)
ChooseNextProvider(peers []peer.ID, metadata []metadata.Protocol) int
}
type Retriever struct {
// Assumed immutable during operation
executor types.Retriever
eventManager *events.EventManager
session Session
clock clock.Clock
protocols []multicodec.Code
}
type eventStats struct {
failedCount int64
}
func NewRetriever(
ctx context.Context,
session Session,
candidateSource types.CandidateSource,
protocolRetrievers map[multicodec.Code]types.CandidateRetriever,
) (*Retriever, error) {
return NewRetrieverWithClock(ctx, session, candidateSource, protocolRetrievers, clock.New())
}
func NewRetrieverWithClock(
ctx context.Context,
session Session,
candidateSource types.CandidateSource,
protocolRetrievers map[multicodec.Code]types.CandidateRetriever,
clock clock.Clock,
) (*Retriever, error) {
retriever := &Retriever{
eventManager: events.NewEventManager(ctx),
session: session,
clock: clock,
}
retriever.protocols = []multicodec.Code{}
for protocol := range protocolRetrievers {
retriever.protocols = append(retriever.protocols, protocol)
}
retriever.executor = combinators.RetrieverWithCandidateFinder{
CandidateFinder: NewAssignableCandidateFinderWithClock(candidateSource, session.FilterIndexerCandidate, clock),
CandidateRetriever: combinators.SplitRetriever[multicodec.Code]{
AsyncCandidateSplitter: combinators.NewAsyncCandidateSplitter(retriever.protocols, NewProtocolSplitter),
CandidateRetrievers: protocolRetrievers,
CoordinationKind: types.RaceCoordination,
},
}
return retriever, nil
}
// Start will start the retriever events system
func (retriever *Retriever) Start() {
retriever.eventManager.Start()
}
// Stop will stop the retriever events system and return a channel that will be
// closed when shutdown has completed
func (retriever *Retriever) Stop() chan struct{} {
return retriever.eventManager.Stop()
}
// RegisterSubscriber registers a subscriber to receive all events fired during the
// process of making a retrieval, including the process of querying available
// storage providers to find compatible ones to attempt retrieval from.
func (retriever *Retriever) RegisterSubscriber(subscriber types.RetrievalEventSubscriber) func() {
return retriever.eventManager.RegisterSubscriber(subscriber)
}
// Retrieve attempts to retrieve the given CID using the configured
// CandidateSource to find storage providers that should have the CID.
func (retriever *Retriever) Retrieve(
ctx context.Context,
request types.RetrievalRequest,
eventsCB func(types.RetrievalEvent),
) (*types.RetrievalStats, error) {
ctx = types.RegisterRetrievalIDToContext(ctx, request.RetrievalID)
if !retriever.eventManager.IsStarted() {
return nil, ErrRetrieverNotStarted
}
if !retriever.session.RegisterRetrieval(request.RetrievalID, request.Root, request.GetSelector()) {
return nil, fmt.Errorf("%w: %s", ErrRetrievalAlreadyRunning, request.Root)
}
defer func() {
if err := retriever.session.EndRetrieval(request.RetrievalID); err != nil {
logger.Errorf("failed to end retrieval tracking for %s: %s", request.Root, err.Error())
}
}()
// setup the event handler to track progress
eventStats := &eventStats{}
onRetrievalEvent := makeOnRetrievalEvent(ctx,
retriever.eventManager,
retriever.session,
request.Root,
request.RetrievalID,
eventStats,
eventsCB,
)
descriptor, err := request.GetDescriptorString()
if err != nil {
return nil, err
}
descriptor = strings.TrimPrefix(descriptor, "/ipfs/"+request.Root.String())
// Emit a StartedFetch event signaling that the Lassie fetch has started
onRetrievalEvent(events.StartedFetch(retriever.clock.Now(), request.RetrievalID, request.Root, descriptor, request.GetSupportedProtocols(retriever.protocols)...))
// retrieve, note that we could get a successful retrieval
// (retrievalStats!=nil) _and_ also an error return because there may be
// multiple failures along the way, if we got a retrieval then we'll pretend
// to our caller that there was no error
retrievalStats, err := retriever.executor.Retrieve(
ctx,
request,
onRetrievalEvent,
)
// Emit a Finished event denoting that the entire fetch has finished
onRetrievalEvent(events.Finished(retriever.clock.Now(), request.RetrievalID, types.RetrievalCandidate{RootCid: request.Root}))
if err != nil && retrievalStats == nil {
return nil, err
}
// success
logger.Infof(
"Successfully retrieved from miner %s for %s\n"+
"\tDuration: %s\n"+
"\tBytes Received: %s\n"+
"\tTotal Payment: %s",
retrievalStats.StorageProviderId,
request.Root,
retrievalStats.Duration,
humanize.IBytes(retrievalStats.Size),
types.FIL(retrievalStats.TotalPayment),
)
return retrievalStats, nil
}
// Implement RetrievalSubscriber
func makeOnRetrievalEvent(
ctx context.Context,
eventManager *events.EventManager,
session Session,
retrievalCid cid.Cid,
retrievalId types.RetrievalID,
eventStats *eventStats,
eventsCb func(event types.RetrievalEvent),
) func(event types.RetrievalEvent) {
// this callback is only called in the main retrieval goroutine so is safe to
// modify local values (eventStats) without synchronization
return func(event types.RetrievalEvent) {
logEvent(event)
switch ret := event.(type) {
case events.CandidatesFilteredEvent:
handleCandidatesFilteredEvent(retrievalId, session, retrievalCid, ret)
case events.FailedRetrievalEvent:
handleFailureEvent(ctx, session, retrievalId, eventStats, ret)
}
eventManager.DispatchEvent(event)
if eventsCb != nil {
eventsCb(event)
}
}
}
// handleFailureEvent is called when a query _or_ retrieval fails
func handleFailureEvent(
ctx context.Context,
session Session,
retrievalId types.RetrievalID,
eventStats *eventStats,
event events.FailedRetrievalEvent,
) {
eventStats.failedCount++
logger.Warnf(
"Failed to retrieve from miner %s for %s: %s",
event.ProviderId(),
event.RootCid(),
event.ErrorMessage(),
)
}
func handleCandidatesFilteredEvent(
retrievalId types.RetrievalID,
session Session,
retrievalCid cid.Cid,
event events.CandidatesFilteredEvent,
) {
if len(event.Candidates()) > 0 {
ids := make([]peer.ID, 0)
for _, c := range event.Candidates() {
ids = append(ids, c.MinerPeer.ID)
}
if err := session.AddToRetrieval(retrievalId, ids); err != nil {
logger.Errorf("failed to add storage providers to tracked retrieval for %s: %s", retrievalCid, err.Error())
}
}
}
func logEvent(event types.RetrievalEvent) {
kv := make([]interface{}, 0)
logadd := func(kva ...interface{}) {
if len(kva)%2 != 0 {
panic("bad number of key/value arguments")
}
for i := 0; i < len(kva); i += 2 {
key, ok := kva[i].(string)
if !ok {
panic("expected string key")
}
kv = append(kv, key, kva[i+1])
}
}
logadd("code", event.Code(),
"rootCid", event.RootCid(),
"storageProviderId", events.Identifier(event))
switch tevent := event.(type) {
case events.EventWithCandidates:
var cands = strings.Builder{}
for i, c := range tevent.Candidates() {
cands.WriteString(c.MinerPeer.ID.String())
if i < len(tevent.Candidates())-1 {
cands.WriteString(", ")
}
}
logadd("candidates", cands.String())
case events.BlockReceivedEvent:
logadd("bytes", tevent.ByteCount())
case events.FailedEvent:
logadd("errorMessage", tevent.ErrorMessage())
case events.SucceededEvent:
logadd("receivedSize", tevent.ReceivedBytesSize())
}
logger.Debugw("retrieval-event", kv...)
}