From f4a445e0193edae654e1493c78777830c7a86d9b Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Fri, 17 Jun 2022 17:07:10 +0200 Subject: [PATCH] pintracker: fix status objects missing or having wrong fields The operation tracker was not setting some fields correctly when producing PinInfo objects. Additionally, recover operations were submitted with empty pin objects, which resulted in the status for pins sent on recover operations to be missing fields. --- pintracker/optracker/operationtracker.go | 19 +++++++++++++------ pintracker/stateless/stateless.go | 16 ++++++++++++++-- pintracker/stateless/stateless_test.go | 2 +- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/pintracker/optracker/operationtracker.go b/pintracker/optracker/operationtracker.go index b9495907..ffca373c 100644 --- a/pintracker/optracker/operationtracker.go +++ b/pintracker/optracker/operationtracker.go @@ -154,9 +154,12 @@ func (opt *OperationTracker) SetError(ctx context.Context, c api.Cid, err error) func (opt *OperationTracker) unsafePinInfo(ctx context.Context, op *Operation, ipfs api.IPFSID) api.PinInfo { if op == nil { return api.PinInfo{ - Cid: api.CidUndef, - Peer: opt.pid, - Name: "", + Cid: api.CidUndef, + Name: "", + Peer: opt.pid, + Origins: nil, + //Created: 0, + Metadata: nil, PinInfoShort: api.PinInfoShort{ PeerName: opt.peerName, IPFS: "", @@ -169,9 +172,13 @@ func (opt *OperationTracker) unsafePinInfo(ctx context.Context, op *Operation, i } } return api.PinInfo{ - Cid: op.Cid(), - Peer: opt.pid, - Name: op.Pin().Name, + Cid: op.Cid(), + Name: op.Pin().Name, + Peer: opt.pid, + Allocations: op.Pin().Allocations, + Origins: op.Pin().Origins, + Created: op.Pin().Timestamp, + Metadata: op.Pin().Metadata, PinInfoShort: api.PinInfoShort{ PeerName: opt.peerName, IPFS: ipfs.ID, diff --git a/pintracker/stateless/stateless.go b/pintracker/stateless/stateless.go index d08695b6..587b4791 100644 --- a/pintracker/stateless/stateless.go +++ b/pintracker/stateless/stateless.go @@ -612,11 +612,23 @@ func (spt *Tracker) Recover(ctx context.Context, c api.Cid) (api.PinInfo, error) } func (spt *Tracker) recoverWithPinInfo(ctx context.Context, pi api.PinInfo) (api.PinInfo, error) { - var err error + st, err := spt.getState(ctx) + if err != nil { + logger.Error(err) + return api.PinInfo{}, err + } + + var pin api.Pin + switch pi.Status { case api.TrackerStatusPinError, api.TrackerStatusUnexpectedlyUnpinned: + pin, err = st.Get(ctx, pi.Cid) + if err != nil { // ignore error - in case pin was removed while recovering + logger.Warn(err) + return spt.Status(ctx, pi.Cid), nil + } logger.Infof("Restarting pin operation for %s", pi.Cid) - err = spt.enqueue(ctx, api.PinCid(pi.Cid), optracker.OperationPin) + err = spt.enqueue(ctx, pin, optracker.OperationPin) case api.TrackerStatusUnpinError: logger.Infof("Restarting unpin operation for %s", pi.Cid) err = spt.enqueue(ctx, api.PinCid(pi.Cid), optracker.OperationUnpin) diff --git a/pintracker/stateless/stateless_test.go b/pintracker/stateless/stateless_test.go index c1e098d3..24ecc9b7 100644 --- a/pintracker/stateless/stateless_test.go +++ b/pintracker/stateless/stateless_test.go @@ -513,7 +513,7 @@ func TestAttemptCountAndPriority(t *testing.T) { normalPin2 := api.PinWithOpts(test.Cid4, pinOpts) errPin := api.PinWithOpts(pinErrCid, pinOpts) - spt := testStatelessPinTracker(t, normalPin, normalPin2) + spt := testStatelessPinTracker(t, normalPin, normalPin2, errPin) defer spt.Shutdown(ctx) st := spt.Status(ctx, test.Cid1)