-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinmgr.go
291 lines (236 loc) · 5.42 KB
/
pinmgr.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
package pinner
import (
"context"
"fmt"
"sync"
"time"
"github.com/application-research/go-pinmgr/types"
"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/pkg/errors"
)
var log = logging.Logger("pinner")
type PinFunc func(context.Context, *PinningOperation, PinProgressCB) error
type PinProgressCB func(int64)
type PinStatusFunc func(uint, string)
func NewPinManager(pinfunc PinFunc, scf PinStatusFunc, opts *PinManagerOpts) *PinManager {
if scf == nil {
scf = func(uint, string) {}
}
if opts == nil {
opts = DefaultOpts
}
return &PinManager{
pinQueue: make(map[uint][]*PinningOperation),
activePins: make(map[uint]int),
pinQueueIn: make(chan *PinningOperation, 64),
pinQueueOut: make(chan *PinningOperation),
pinComplete: make(chan *PinningOperation, 64),
RunPinFunc: pinfunc,
StatusChangeFunc: scf,
maxActivePerUser: opts.MaxActivePerUser,
}
}
var DefaultOpts = &PinManagerOpts{
MaxActivePerUser: 15,
}
type PinManagerOpts struct {
MaxActivePerUser int
}
type PinManager struct {
pinQueueIn chan *PinningOperation
pinQueueOut chan *PinningOperation
pinComplete chan *PinningOperation
pinQueue map[uint][]*PinningOperation
activePins map[uint]int
pinQueueLk sync.Mutex
RunPinFunc PinFunc
StatusChangeFunc func(uint, string)
maxActivePerUser int
}
// TODO: some of these fields are overkill for the generalized pin manager
// thing, but are still in use by the primary estuary node. Should probably
// find a way to decouple this better
type PinningOperation struct {
Obj cid.Cid
Name string
Peers []peer.AddrInfo
Meta map[string]interface{}
Status types.PinningStatus
UserId uint
ContId uint
Replace uint
LastUpdate time.Time
Started time.Time
NumFetched int
SizeFetched int64
FetchErr error
EndTime time.Time
Location string
SkipLimiter bool
lk sync.Mutex
MakeDeal bool
}
func (po *PinningOperation) fail(err error) {
po.lk.Lock()
po.FetchErr = err
po.EndTime = time.Now()
po.Status = "failed"
po.LastUpdate = time.Now()
po.lk.Unlock()
}
func (po *PinningOperation) complete() {
po.lk.Lock()
defer po.lk.Unlock()
po.EndTime = time.Now()
po.LastUpdate = time.Now()
po.Status = "pinned"
}
func (po *PinningOperation) SetStatus(st types.PinningStatus) {
po.lk.Lock()
defer po.lk.Unlock()
po.Status = st
po.LastUpdate = time.Now()
}
func (po *PinningOperation) PinStatus() *types.IpfsPinStatus {
po.lk.Lock()
defer po.lk.Unlock()
return &types.IpfsPinStatus{
Requestid: fmt.Sprint(po.ContId),
Status: po.Status,
Created: po.Started,
Pin: types.IpfsPin{
Cid: po.Obj.String(),
Name: po.Name,
Meta: po.Meta,
},
/* Ref: https://github.com/ipfs/go-pinning-service-http-client/issues/12
Info: map[string]interface{}{
"obj_fetched": po.NumFetched,
"size_fetched": po.SizeFetched,
},
*/
}
}
func (pm *PinManager) PinQueueSize() int {
var count int
pm.pinQueueLk.Lock()
defer pm.pinQueueLk.Unlock()
for _, pq := range pm.pinQueue {
count += len(pq)
}
return count
}
func (pm *PinManager) Add(op *PinningOperation) {
go func() {
pm.pinQueueIn <- op
}()
}
var maxTimeout = 24 * time.Hour
func (pm *PinManager) doPinning(op *PinningOperation) error {
ctx, cancel := context.WithTimeout(context.Background(), maxTimeout)
defer cancel()
op.SetStatus("pinning")
pm.StatusChangeFunc(op.ContId, "pinning")
if err := pm.RunPinFunc(ctx, op, func(size int64) {
op.lk.Lock()
defer op.lk.Unlock()
op.NumFetched++
op.SizeFetched += size
}); err != nil {
op.fail(err)
pm.StatusChangeFunc(op.ContId, "failed")
return errors.Wrap(err, "RunPinFunc failed")
}
op.complete()
pm.StatusChangeFunc(op.ContId, "pinned")
return nil
}
func (pm *PinManager) popNextPinOp() *PinningOperation {
if len(pm.pinQueue) == 0 {
return nil
}
var minCount int = 10000
var user uint
for u := range pm.pinQueue {
active := pm.activePins[u]
if active < minCount {
minCount = active
user = u
}
}
_, ok := pm.pinQueue[0]
if ok {
user = 0
}
if minCount >= pm.maxActivePerUser && user != 0 {
return nil
}
pq := pm.pinQueue[user]
next := pq[0]
if len(pq) == 1 {
delete(pm.pinQueue, user)
} else {
pm.pinQueue[user] = pq[1:]
}
return next
}
func (pm *PinManager) enqueuePinOp(po *PinningOperation) {
u := po.UserId
if po.SkipLimiter {
u = 0
}
q := pm.pinQueue[u]
pm.pinQueue[u] = append(q, po)
}
func (pm *PinManager) Run(workers int) {
for i := 0; i < workers; i++ {
go pm.pinWorker()
}
var next *PinningOperation
var send chan *PinningOperation
next = pm.popNextPinOp()
if next != nil {
send = pm.pinQueueOut
}
for {
select {
case op := <-pm.pinQueueIn:
if next == nil {
next = op
send = pm.pinQueueOut
} else {
pm.pinQueueLk.Lock()
pm.enqueuePinOp(op)
pm.pinQueueLk.Unlock()
}
case send <- next:
pm.pinQueueLk.Lock()
pm.activePins[next.UserId]++
next = pm.popNextPinOp()
if next == nil {
send = nil
}
pm.pinQueueLk.Unlock()
case op := <-pm.pinComplete:
pm.pinQueueLk.Lock()
pm.activePins[op.UserId]--
if next == nil {
next = pm.popNextPinOp()
if next != nil {
send = pm.pinQueueOut
}
}
pm.pinQueueLk.Unlock()
}
}
}
func (pm *PinManager) pinWorker() {
for op := range pm.pinQueueOut {
if err := pm.doPinning(op); err != nil {
log.Errorf("pinning queue error: %+v", err)
}
pm.pinComplete <- op
}
}