forked from application-research/estuary
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpinning.go
929 lines (779 loc) · 22.4 KB
/
pinning.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"sort"
"strconv"
"strings"
"time"
drpc "github.com/application-research/estuary/drpc"
"github.com/application-research/estuary/pinner"
"github.com/application-research/estuary/types"
"github.com/application-research/estuary/util"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-merkledag"
"github.com/labstack/echo/v4"
"github.com/libp2p/go-libp2p-core/peer"
"go.opentelemetry.io/otel/attribute"
trace "go.opentelemetry.io/otel/trace"
"golang.org/x/xerrors"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
func (cm *ContentManager) pinStatus(cont Content) (*types.IpfsPinStatus, error) {
cm.pinLk.Lock()
po, ok := cm.pinJobs[cont.ID]
cm.pinLk.Unlock()
if !ok {
var meta map[string]interface{}
if cont.PinMeta != "" {
if err := json.Unmarshal([]byte(cont.PinMeta), &meta); err != nil {
log.Warnf("content %d has invalid pinmeta: %s", cont, err)
}
}
ps := &types.IpfsPinStatus{
Requestid: fmt.Sprintf("%d", cont.ID),
Status: "pinning",
Created: cont.CreatedAt,
Pin: types.IpfsPin{
Cid: cont.Cid.CID.String(),
Name: cont.Name,
Meta: meta,
},
Delegates: cm.pinDelegatesForContent(cont),
Info: nil, // TODO: all sorts of extra info we could add...
}
if cont.Active {
ps.Status = "pinned"
}
if cont.Failed {
ps.Status = "failed"
}
return ps, nil
}
status := po.PinStatus()
status.Delegates = cm.pinDelegatesForContent(cont)
return status, nil
}
func (cm *ContentManager) pinDelegatesForContent(cont Content) []string {
if cont.Location == "local" {
var out []string
for _, a := range cm.Host.Addrs() {
out = append(out, fmt.Sprintf("%s/p2p/%s", a, cm.Host.ID()))
}
return out
} else {
ai, err := cm.addrInfoForShuttle(cont.Location)
if err != nil {
log.Errorf("failed to get address info for shuttle %q: %s", cont.Location, err)
return nil
}
if ai == nil {
log.Warnf("no address info for shuttle %s: %s", cont.Location, err)
return nil
}
var out []string
for _, a := range ai.Addrs {
out = append(out, fmt.Sprintf("%s/p2p/%s", a, ai.ID))
}
return out
}
}
func (s *Server) doPinning(ctx context.Context, op *pinner.PinningOperation, cb pinner.PinProgressCB) error {
ctx, span := s.tracer.Start(ctx, "doPinning")
defer span.End()
connectedToAtLeastOne := false
for _, pi := range op.Peers {
if err := s.Node.Host.Connect(ctx, pi); err != nil && s.Node.Host.ID() != pi.ID {
log.Warnf("failed to connect to origin node for pinning operation: %s", err)
} else {
// Check if it's trying to connect to itself since we only want to check if the
// the connection is between the host and the external/other peers.
connectedToAtLeastOne = true
}
}
// If it can't connect to any legitimate provider peers, then we fail the entire operation.
if !connectedToAtLeastOne {
log.Errorf("unable to connect to any of the provider peers for pinning operation")
return nil
}
bserv := blockservice.New(s.Node.Blockstore, s.Node.Bitswap)
dserv := merkledag.NewDAGService(bserv)
dsess := merkledag.NewSession(ctx, dserv)
if err := s.CM.addDatabaseTrackingToContent(ctx, op.ContId, dsess, s.Node.Blockstore, op.Obj, cb); err != nil {
return err
}
if op.MakeDeal {
s.CM.ToCheck <- op.ContId
}
if op.Replace > 0 {
if err := s.CM.RemoveContent(ctx, op.Replace, true); err != nil {
log.Infof("failed to remove content in replacement: %d", op.Replace)
}
}
// this provide call goes out immediately
if err := s.Node.FullRT.Provide(ctx, op.Obj, true); err != nil {
log.Warnf("provider broadcast failed: %s", err)
}
// this one adds to a queue
if err := s.Node.Provider.Provide(op.Obj); err != nil {
log.Warnf("providing failed: %s", err)
}
return nil
}
func (cm *ContentManager) refreshPinQueue() error {
var toPin []Content
if err := cm.DB.Find(&toPin, "active = false and pinning = true and not aggregate").Error; err != nil {
return err
}
// TODO: this doesnt persist the replacement directives, so a queued
// replacement, if ongoing during a restart of the node, will still
// complete the pin when the process comes back online, but it wont delete
// the old pin.
// Need to fix this, probably best option is just to add a 'replace' field
// to content, could be interesting to see the graph of replacements
// anyways
makeDeal := true
for _, c := range toPin {
if c.Location == "local" {
cm.addPinToQueue(c, nil, 0, makeDeal)
} else {
if err := cm.pinContentOnShuttle(context.TODO(), c, nil, 0, c.Location, makeDeal); err != nil {
log.Errorf("failed to send pin message to shuttle: %s", err)
time.Sleep(time.Millisecond * 100)
}
}
}
return nil
}
func (cm *ContentManager) pinContent(ctx context.Context, user uint, obj cid.Cid, name string, cols []*CollectionRef, peers []peer.AddrInfo, replace uint, meta map[string]interface{}, makeDeal bool) (*types.IpfsPinStatus, error) {
loc, err := cm.selectLocationForContent(ctx, obj, user)
if err != nil {
return nil, xerrors.Errorf("selecting location for content failed: %w", err)
}
var metab string
if meta != nil {
b, err := json.Marshal(meta)
if err != nil {
return nil, err
}
metab = string(b)
}
cont := Content{
Cid: util.DbCID{obj},
Name: name,
UserID: user,
Active: false,
Replication: cm.Replication,
Pinning: true,
PinMeta: metab,
Location: loc,
/*
Size int64 `json:"size"`
Offloaded bool `json:"offloaded"`
*/
}
if err := cm.DB.Create(&cont).Error; err != nil {
return nil, err
}
if len(cols) > 0 {
for _, c := range cols {
c.Content = cont.ID
}
if err := cm.DB.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "path"}, {Name: "collection"}},
DoUpdates: clause.AssignmentColumns([]string{"created_at", "content"}),
}).Create(cols).Error; err != nil {
return nil, err
}
}
if loc == "local" {
cm.addPinToQueue(cont, peers, replace, makeDeal)
} else {
if err := cm.pinContentOnShuttle(ctx, cont, peers, replace, loc, makeDeal); err != nil {
return nil, err
}
}
return cm.pinStatus(cont)
}
func (cm *ContentManager) addPinToQueue(cont Content, peers []peer.AddrInfo, replace uint, makeDeal bool) {
if cont.Location != "local" {
log.Errorf("calling addPinToQueue on non-local content")
}
op := &pinner.PinningOperation{
ContId: cont.ID,
UserId: cont.UserID,
Obj: cont.Cid.CID,
Name: cont.Name,
Peers: peers,
Started: cont.CreatedAt,
Status: "queued",
Replace: replace,
Location: cont.Location,
MakeDeal: makeDeal,
}
cm.pinLk.Lock()
// TODO: check if we are overwriting anything here
cm.pinJobs[cont.ID] = op
cm.pinLk.Unlock()
cm.pinMgr.Add(op)
}
func (cm *ContentManager) pinContentOnShuttle(ctx context.Context, cont Content, peers []peer.AddrInfo, replace uint, handle string, makeDeal bool) error {
ctx, span := cm.tracer.Start(ctx, "pinContentOnShuttle", trace.WithAttributes(
attribute.String("handle", handle),
attribute.String("CID", cont.Cid.CID.String()),
))
defer span.End()
if err := cm.sendShuttleCommand(ctx, handle, &drpc.Command{
Op: drpc.CMD_AddPin,
Params: drpc.CmdParams{
AddPin: &drpc.AddPin{
DBID: cont.ID,
UserId: cont.UserID,
Cid: cont.Cid.CID,
Peers: peers,
},
},
}); err != nil {
return err
}
op := &pinner.PinningOperation{
ContId: cont.ID,
UserId: cont.UserID,
Obj: cont.Cid.CID,
Name: cont.Name,
Peers: peers,
Started: cont.CreatedAt,
Status: "queued",
Replace: replace,
Location: handle,
MakeDeal: makeDeal,
}
cm.pinLk.Lock()
// TODO: check if we are overwriting anything here
cm.pinJobs[cont.ID] = op
cm.pinLk.Unlock()
return nil
}
func (cm *ContentManager) selectLocationForContent(ctx context.Context, obj cid.Cid, uid uint) (string, error) {
ctx, span := cm.tracer.Start(ctx, "selectLocation")
defer span.End()
var user User
if err := cm.DB.First(&user, "id = ?", uid).Error; err != nil {
return "", err
}
allShuttlesLowSpace := true
lowSpace := make(map[string]bool)
var activeShuttles []string
cm.shuttlesLk.Lock()
for d, sh := range cm.shuttles {
if !sh.private {
lowSpace[d] = sh.spaceLow
activeShuttles = append(activeShuttles, d)
} else {
allShuttlesLowSpace = false
}
}
cm.shuttlesLk.Unlock()
var shuttles []Shuttle
if err := cm.DB.Order("priority desc").Find(&shuttles, "handle in ? and open", activeShuttles).Error; err != nil {
return "", err
}
// prefer shuttles that are not low on blockstore space
sort.SliceStable(shuttles, func(i, j int) bool {
lsI := lowSpace[shuttles[i].Handle]
lsJ := lowSpace[shuttles[j].Handle]
if lsI == lsJ {
return false
}
return lsJ
})
if len(shuttles) == 0 {
//log.Info("no shuttles available for content to be delegated to")
if cm.localContentAddingDisabled {
return "", fmt.Errorf("no shuttles available and local content adding disabled")
}
return "local", nil
}
// TODO: take into account existing staging zones and their primary
// locations while choosing
ploc, err := cm.primaryStagingLocation(ctx, uid)
if err != nil {
return "", err
}
if ploc != "" {
if allShuttlesLowSpace || !lowSpace[ploc] {
for _, sh := range shuttles {
if sh.Handle == ploc {
return ploc, nil
}
}
}
// TODO: maybe we should just assign the pin to the preferred shuttle
// anyways, this could be the case where theres a small amount of
// downtime from rebooting or something
log.Warnf("preferred shuttle %q not online", ploc)
}
// since they are ordered by priority, just take the first
return shuttles[0].Handle, nil
}
func (cm *ContentManager) selectLocationForRetrieval(ctx context.Context, cont Content) (string, error) {
ctx, span := cm.tracer.Start(ctx, "selectLocationForRetrieval")
defer span.End()
var activeShuttles []string
cm.shuttlesLk.Lock()
for d, sh := range cm.shuttles {
if !sh.private {
activeShuttles = append(activeShuttles, d)
}
}
cm.shuttlesLk.Unlock()
var shuttles []Shuttle
if err := cm.DB.Order("priority desc").Find(&shuttles, "handle in ? and open", activeShuttles).Error; err != nil {
return "", err
}
if len(shuttles) == 0 {
if cm.localContentAddingDisabled {
return "", fmt.Errorf("no shuttles available and local content adding disabled")
}
return "local", nil
}
// prefer the shuttle the content is already on
for _, sh := range shuttles {
if sh.Handle == cont.Location {
return sh.Handle, nil
}
}
// since they are ordered by priority, just take the first
return shuttles[0].Handle, nil
}
func (cm *ContentManager) primaryStagingLocation(ctx context.Context, uid uint) (string, error) {
cm.bucketLk.Lock()
defer cm.bucketLk.Unlock()
zones, ok := cm.buckets[uid]
if !ok {
return "", nil
}
// TODO: maybe we could make this more complex, but for now, if we have a
// staging zone opened in a particular location, just keep using that one
for _, z := range zones {
return z.Location, nil
}
log.Warnf("empty staging zone set for user %d", uid)
return "", nil
}
// handleListPins godoc
// @Summary List all pinned objects
// @Description This endpoint lists all pinned objects
// @Tags pinning
// @Produce json
// @Failure 400 {object} util.HttpError
// @Failure 404 {object} util.HttpError
// @Failure 500 {object} util.HttpError
// @Router /pinning/pins [get]
func (s *Server) handleListPins(e echo.Context, u *User) error {
_, span := s.tracer.Start(e.Request().Context(), "handleListPins")
defer span.End()
qcids := e.QueryParam("cid")
qname := e.QueryParam("name")
qstatus := e.QueryParam("status")
qbefore := e.QueryParam("before")
qafter := e.QueryParam("after")
qlimit := e.QueryParam("limit")
qreqids := e.QueryParam("requestid")
q := s.DB.Model(Content{}).Where("user_id = ? and not aggregate", u.ID).Order("created_at desc")
if qcids != "" {
var cids []util.DbCID
for _, cstr := range strings.Split(qcids, ",") {
c, err := cid.Decode(cstr)
if err != nil {
return err
}
cids = append(cids, util.DbCID{c})
}
q = q.Where("cid in ?", cids)
}
if qname != "" {
q = q.Where("name = ?", qname)
}
if qbefore != "" {
beftime, err := time.Parse(time.RFC3339, qbefore)
if err != nil {
return err
}
q = q.Where("created_at <= ?", beftime)
}
if qafter != "" {
aftime, err := time.Parse(time.RFC3339, qafter)
if err != nil {
return err
}
q = q.Where("created_at > ?", aftime)
}
if qreqids != "" {
var ids []int
for _, rs := range strings.Split(qreqids, ",") {
id, err := strconv.Atoi(rs)
if err != nil {
return err
}
ids = append(ids, id)
}
q = q.Where("id in ?", ids)
}
var lim int
if qlimit != "" {
limit, err := strconv.Atoi(qlimit)
if err != nil {
return err
}
lim = limit
}
if lim == 0 {
lim = 500
}
var allowed map[string]bool
if qstatus != "" {
allowed = make(map[string]bool)
/*
- queued # pinning operation is waiting in the queue; additional info can be returned in info[status_details]
- pinning # pinning in progress; additional info can be returned in info[status_details]
- pinned # pinned successfully
- failed # pinning service was unable to finish pinning operation; additional info can be found in info[status_details]
*/
statuses := strings.Split(qstatus, ",")
for _, s := range statuses {
switch s {
case "queued", "pinning", "pinned", "failed":
allowed[s] = true
default:
return fmt.Errorf("unrecognized pin status in query: %q", s)
}
}
}
// certain sets of statuses we can use the database to filter for
oq, dblimit, err := filterForStatusQuery(q, allowed)
if err != nil {
return err
}
q = oq
if dblimit {
q = q.Limit(lim)
}
var contents []Content
if err := q.Scan(&contents).Error; err != nil {
return err
}
var out []*types.IpfsPinStatus
for _, c := range contents {
if lim > 0 && len(out) >= lim {
break
}
st, err := s.CM.pinStatus(c)
if err != nil {
return err
}
if allowed == nil || allowed[st.Status] {
out = append(out, st)
}
}
if len(out) == 0 {
out = make([]*types.IpfsPinStatus, 0)
}
return e.JSON(http.StatusOK, map[string]interface{}{
"count": len(out),
"results": out,
})
}
func filterForStatusQuery(q *gorm.DB, statuses map[string]bool) (*gorm.DB, bool, error) {
if len(statuses) == 0 || len(statuses) == 4 {
// if not filtering by status, we return *all* pins, in that case we can use the query to limit results
return q, true, nil
}
pinned := statuses["pinned"]
failed := statuses["failed"]
pinning := statuses["pinning"]
queued := statuses["queued"]
if len(statuses) == 1 {
switch {
case pinned:
return q.Where("active"), true, nil
case failed:
return q.Where("failed"), true, nil
default:
return q, false, nil
}
}
if len(statuses) == 2 {
if pinned && failed {
return q.Where("active or failed"), true, nil
}
if pinning && queued {
return q.Where("not active and not failed"), true, nil
}
// fallthrough to the rest of the logic
}
var canUseDBLimit bool = true
// If the query is trying to distinguish between pinning and queued, we cannot do that solely via a database query
if (statuses["queued"] && !statuses["pinning"]) || (statuses["pinning"] && !statuses["queued"]) {
canUseDBLimit = false
}
if !statuses["failed"] {
q = q.Where("not failed")
}
if !statuses["pinned"] {
q = q.Where("not active")
}
return q, canUseDBLimit, nil
}
/*
{
"cid": "QmCIDToBePinned",
"name": "PreciousData.pdf",
"origins":
[
"/ip4/203.0.113.142/tcp/4001/p2p/QmSourcePeerId",
"/ip4/203.0.113.114/udp/4001/quic/p2p/QmSourcePeerId"
],
"meta":
{
"app_id": "99986338-1113-4706-8302-4420da6158aa"
}
}
*/
// handleAddPin godoc
// @Summary Add and pin object
// @Description This endpoint adds a pin to the IPFS daemon.
// @Tags pinning
// @Produce json
// @in 200,400,default string Token "token"
// @Param cid path string true "cid"
// @Param name path string true "name"
// @Router /pinning/pins [post]
func (s *Server) handleAddPin(e echo.Context, u *User) error {
ctx := e.Request().Context()
if s.CM.contentAddingDisabled || u.StorageDisabled {
return &util.HttpError{
Code: http.StatusBadRequest,
Message: util.ERR_CONTENT_ADDING_DISABLED,
}
}
var pin types.IpfsPin
if err := e.Bind(&pin); err != nil {
return err
}
var cols []*CollectionRef
if c, ok := pin.Meta["collection"].(string); ok && c != "" {
var srchCol Collection
if err := s.DB.First(&srchCol, "uuid = ? and user_id = ?", c, u.ID).Error; err != nil {
return err
}
var colpath *string
colp, ok := pin.Meta["collectionPath"].(string)
if ok {
p, err := sanitizePath(colp)
if err != nil {
return err
}
colpath = &p
}
cols = []*CollectionRef{&CollectionRef{
Collection: srchCol.ID,
Path: colpath,
}}
}
var addrInfos []peer.AddrInfo
for _, p := range pin.Origins {
ai, err := peer.AddrInfoFromString(p)
if err != nil {
return err
}
addrInfos = append(addrInfos, *ai)
}
obj, err := cid.Decode(pin.Cid)
if err != nil {
return err
}
makeDeal := true
status, err := s.CM.pinContent(ctx, u.ID, obj, pin.Name, cols, addrInfos, 0, pin.Meta, makeDeal)
if err != nil {
return err
}
status.Pin.Meta = pin.Meta
return e.JSON(http.StatusAccepted, status)
}
// handleGetPin godoc
// @Summary Get a pinned objects
// @Description This endpoint returns a pinned object.
// @Tags pinning
// @Produce json
// @Param requestid path string true "cid"
// @Router /pinning/pins/{requestid} [get]
func (s *Server) handleGetPin(e echo.Context, u *User) error {
id, err := strconv.Atoi(e.Param("requestid"))
if err != nil {
return err
}
var content Content
if err := s.DB.First(&content, "id = ?", uint(id)).Error; err != nil {
return err
}
st, err := s.CM.pinStatus(content)
if err != nil {
return err
}
return e.JSON(http.StatusOK, st)
}
// handleReplacePin godoc
// @Summary Replace a pinned object
// @Description This endpoint replaces a pinned object.
// @Tags pinning
// @Produce json
// @Param id path string true "id"
// @Router /pinning/pins/{id} [post]
func (s *Server) handleReplacePin(e echo.Context, u *User) error {
if s.CM.contentAddingDisabled || u.StorageDisabled {
return &util.HttpError{
Code: http.StatusBadRequest,
Message: util.ERR_CONTENT_ADDING_DISABLED,
}
}
ctx := e.Request().Context()
id, err := strconv.Atoi(e.Param("requestid"))
if err != nil {
return err
}
var pin types.IpfsPin
if err := e.Bind(&pin); err != nil {
return err
}
var content Content
if err := s.DB.First(&content, "id = ?", id).Error; err != nil {
return err
}
if content.UserID != u.ID {
return &util.HttpError{
Code: http.StatusUnauthorized,
Message: util.ERR_NOT_AUTHORIZED,
}
}
var addrInfos []peer.AddrInfo
for _, p := range pin.Origins {
ai, err := peer.AddrInfoFromString(p)
if err != nil {
return err
}
addrInfos = append(addrInfos, *ai)
}
obj, err := cid.Decode(pin.Cid)
if err != nil {
return err
}
makeDeal := true
status, err := s.CM.pinContent(ctx, u.ID, obj, pin.Name, nil, addrInfos, uint(id), pin.Meta, makeDeal)
if err != nil {
return err
}
return e.JSON(http.StatusAccepted, status)
}
// handleDeletePin godoc
// @Summary Delete a pinned object
// @Description This endpoint deletes a pinned object.
// @Tags pinning
// @Produce json
// @Param requestid path string true "requestid"
// @Router /pinning/pins/{requestid} [delete]
func (s *Server) handleDeletePin(e echo.Context, u *User) error {
// TODO: need to cancel any in-progress pinning operation
ctx := e.Request().Context()
id, err := strconv.Atoi(e.Param("requestid"))
if err != nil {
return err
}
var content Content
if err := s.DB.First(&content, "id = ?", id).Error; err != nil {
return err
}
if content.UserID != u.ID {
return &util.HttpError{
Code: 401,
Message: util.ERR_NOT_AUTHORIZED,
}
}
// TODO: what if we delete a pin that was in progress?
if err := s.CM.unpinContent(ctx, uint(id)); err != nil {
return err
}
return e.NoContent(http.StatusAccepted)
}
func (cm *ContentManager) UpdatePinStatus(handle string, cont uint, status string) {
cm.pinLk.Lock()
op, ok := cm.pinJobs[cont]
cm.pinLk.Unlock()
if !ok {
log.Warnw("got pin status update for unknown content", "content", cont, "status", status, "shuttle", handle)
return
}
op.SetStatus(status)
if status == "failed" {
var c Content
if err := cm.DB.First(&c, "id = ?", cont).Error; err != nil {
log.Errorf("failed to look up content: %s", err)
return
}
if c.Active {
log.Errorf("got failed pin status message from shuttle %s where content(%d) was already active, refusing to do anything", handle, cont)
return
}
if err := cm.DB.Model(Content{}).Where("id = ?", cont).UpdateColumns(map[string]interface{}{
"active": false,
"pinning": false,
"failed": true,
}).Error; err != nil {
log.Errorf("failed to mark content as failed in database: %s", err)
}
}
}
func (cm *ContentManager) handlePinningComplete(ctx context.Context, handle string, pincomp *drpc.PinComplete) error {
ctx, span := cm.tracer.Start(ctx, "handlePinningComplete")
defer span.End()
var cont Content
if err := cm.DB.First(&cont, "id = ?", pincomp.DBID).Error; err != nil {
return xerrors.Errorf("got shuttle pin complete for unknown content %d (shuttle = %s): %w", pincomp.DBID, handle, err)
}
if cont.Active {
// content already active, no need to add objects, just update location
if err := cm.DB.Model(Content{}).Where("id = ?", cont.ID).UpdateColumns(map[string]interface{}{
"location": handle,
}).Error; err != nil {
return err
}
// TODO: should we recheck the staging zones?
return nil
}
if cont.Aggregate {
if err := cm.DB.Model(Content{}).Where("id = ?", cont.ID).UpdateColumns(map[string]interface{}{
"active": true,
"pinning": false,
"location": handle,
}).Error; err != nil {
return xerrors.Errorf("failed to update content in database: %w", err)
}
return nil
}
objects := make([]*Object, 0, len(pincomp.Objects))
for _, o := range pincomp.Objects {
objects = append(objects, &Object{
Cid: util.DbCID{o.Cid},
Size: o.Size,
})
}
if err := cm.addObjectsToDatabase(ctx, pincomp.DBID, nil, cid.Cid{}, objects, handle); err != nil {
return xerrors.Errorf("failed to add objects to database: %w", err)
}
cm.ToCheck <- cont.ID
return nil
}