forked from application-research/estuary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrieval.go
162 lines (136 loc) · 4.26 KB
/
retrieval.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
package main
import (
"context"
"fmt"
"math/rand"
"time"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-fil-markets/retrievalmarket"
"github.com/ipfs/go-cid"
"github.com/whyrusleeping/estuary/filclient"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"gorm.io/gorm"
)
func (s *Server) retrievalAsksForContent(ctx context.Context, contid uint) (map[address.Address]*retrievalmarket.QueryResponse, error) {
ctx, span := s.tracer.Start(ctx, "retrievalAsksForContent", trace.WithAttributes(
attribute.Int("content", int(contid)),
))
defer span.End()
var content Content
if err := s.DB.First(&content, "id = ?", contid).Error; err != nil {
return nil, err
}
var deals []contentDeal
if err := s.DB.Find(&deals, "content = ? and deal_id > 0", contid).Error; err != nil {
return nil, err
}
fmt.Printf("looking at %d deals for content: %s\n", len(deals), content.Cid.CID)
out := make(map[address.Address]*retrievalmarket.QueryResponse)
for _, d := range deals {
maddr, err := d.MinerAddr()
if err != nil {
return nil, err
}
resp, err := s.FilClient.RetrievalQuery(ctx, maddr, content.Cid.CID)
if err != nil {
s.CM.recordRetrievalFailure(&retrievalFailureRecord{
Miner: maddr.String(),
Phase: "query",
Message: err.Error(),
})
log.Errorf("failed to query miner %s: %s", maddr, err)
continue
}
out[maddr] = resp
}
return out, nil
}
type retrievalFailureRecord struct {
gorm.Model
Miner string
Phase string
Message string
}
func (cm *ContentManager) recordRetrievalFailure(rfr *retrievalFailureRecord) error {
return cm.DB.Create(rfr).Error
}
func (s *Server) retrieveContent(ctx context.Context, contid uint) error {
ctx, span := s.tracer.Start(ctx, "retrieveContent", trace.WithAttributes(
attribute.Int("content", int(contid)),
))
defer span.End()
content, err := s.CM.getContent(contid)
if err != nil {
return err
}
asks, err := s.retrievalAsksForContent(ctx, contid)
if err != nil {
return err
}
fmt.Println("Got asks: ", len(asks))
if len(asks) == 0 {
return fmt.Errorf("no retrieval asks for content")
}
for m, ask := range asks {
if err := s.CM.tryRetrieve(ctx, m, content.Cid.CID, ask); err != nil {
log.Errorw("failed to retrieve content", "miner", m, "content", content.Cid.CID, "err", err)
s.CM.recordRetrievalFailure(&retrievalFailureRecord{
Miner: m.String(),
Phase: "retrieval",
Message: err.Error(),
})
continue
}
return nil
}
return nil
}
func (cm *ContentManager) tryRetrieve(ctx context.Context, maddr address.Address, c cid.Cid, ask *retrievalmarket.QueryResponse) error {
proposal := &retrievalmarket.DealProposal{
PayloadCID: c,
ID: retrievalmarket.DealID(rand.Int63n(1000000) + 100000),
Params: retrievalmarket.Params{
Selector: nil,
PieceCID: nil,
PricePerByte: ask.MinPricePerByte,
PaymentInterval: ask.MaxPaymentInterval,
PaymentIntervalIncrease: ask.MaxPaymentIntervalIncrease,
UnsealPrice: ask.UnsealPrice,
},
}
stats, err := cm.FilClient.RetrieveContent(ctx, maddr, proposal)
if err != nil {
return err
}
cm.recordRetrievalSuccess(c, maddr, stats)
return nil
}
type retrievalSuccessRecord struct {
ID uint `gorm:"primarykey" json:"-"`
CreatedAt time.Time `json:"createdAt"`
PropCid dbCID `json:"propCid"`
Miner string `json:"miner"`
Peer string `json:"peer"`
Size uint64 `json:"size"`
DurationMs int64 `json:"durationMs"`
AverageSpeed uint64 `json:"averageSpeed"`
TotalPayment string `json:"totalPayment"`
NumPayments int `json:"numPayments"`
AskPrice string `json:"askPrice"`
}
func (cm *ContentManager) recordRetrievalSuccess(cc cid.Cid, m address.Address, rstats *filclient.RetrievalStats) {
if err := cm.DB.Create(&retrievalSuccessRecord{
PropCid: dbCID{cc},
Miner: m.String(),
Peer: rstats.Peer.String(),
Size: rstats.Size,
DurationMs: rstats.Duration.Milliseconds(),
AverageSpeed: rstats.AverageSpeed,
TotalPayment: rstats.TotalPayment.String(),
NumPayments: rstats.NumPayments,
AskPrice: rstats.AskPrice.String(),
}).Error; err != nil {
log.Errorf("failed to write retrieval success record: %s", err)
}
}