Skip to content

Commit 075cc8f

Browse files
BogdanStirbatlidel
andauthored
refactor(pinmfs): log error if pre-existing pin failed (#8056)
* pinmfs: log error if pin failed * refactor: log pre-existing remote mfs pins This cleans up debug logging and ensures we use %q for data read from remote services. We log pre-existing pins for MFS root and print ERROR if pin for current root has status "failed", otherwise, we log to debug. Co-authored-by: Marcin Rataj <lidel@lidel.org>
1 parent b0fda5c commit 075cc8f

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

cmd/ipfs/pinmfs.go

+20-14
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ func pinAllMFS(ctx context.Context, node pinMFSNode, cfg *config.Config, rootCid
138138
for svcName_, svcConfig_ := range cfg.Pinning.RemoteServices {
139139
// skip services where MFS is not enabled
140140
svcName, svcConfig := svcName_, svcConfig_
141-
mfslog.Debugf("pinning considering service %s for mfs pinning", svcName)
141+
mfslog.Debugf("pinning MFS root considering service %q", svcName)
142142
if !svcConfig.Policies.MFS.Enable {
143-
mfslog.Debugf("pinning service %s is not enabled", svcName)
143+
mfslog.Debugf("pinning service %q is not enabled", svcName)
144144
ch <- lastPin{}
145145
continue
146146
}
@@ -153,7 +153,7 @@ func pinAllMFS(ctx context.Context, node pinMFSNode, cfg *config.Config, rootCid
153153
repinInterval, err = time.ParseDuration(svcConfig.Policies.MFS.RepinInterval)
154154
if err != nil {
155155
select {
156-
case errCh <- fmt.Errorf("remote pinning service %s has invalid MFS.RepinInterval (%v)", svcName, err):
156+
case errCh <- fmt.Errorf("remote pinning service %q has invalid MFS.RepinInterval (%v)", svcName, err):
157157
case <-ctx.Done():
158158
}
159159
ch <- lastPin{}
@@ -165,20 +165,20 @@ func pinAllMFS(ctx context.Context, node pinMFSNode, cfg *config.Config, rootCid
165165
if last, ok := lastPins[svcName]; ok {
166166
if last.ServiceConfig == svcConfig && (last.CID == rootCid || time.Since(last.Time) < repinInterval) {
167167
if last.CID == rootCid {
168-
mfslog.Debugf("pinning MFS root to %s: pin for %s exists since %s, skipping", svcName, rootCid, last.Time.String())
168+
mfslog.Debugf("pinning MFS root to %q: pin for %q exists since %s, skipping", svcName, rootCid, last.Time.String())
169169
} else {
170-
mfslog.Debugf("pinning MFS root to %s: skipped due to MFS.RepinInterval=%s (remaining: %s)", svcName, repinInterval.String(), (repinInterval - time.Since(last.Time)).String())
170+
mfslog.Debugf("pinning MFS root to %q: skipped due to MFS.RepinInterval=%s (remaining: %s)", svcName, repinInterval.String(), (repinInterval - time.Since(last.Time)).String())
171171
}
172172
ch <- lastPin{}
173173
continue
174174
}
175175
}
176176

177-
mfslog.Debugf("pinning MFS root %s to %s", rootCid, svcName)
177+
mfslog.Debugf("pinning MFS root %q to %q", rootCid, svcName)
178178
go func() {
179179
if r, err := pinMFS(ctx, node, rootCid, svcName, svcConfig); err != nil {
180180
select {
181-
case errCh <- fmt.Errorf("pinning MFS root %s to %s (%v)", rootCid, svcName, err):
181+
case errCh <- fmt.Errorf("pinning MFS root %q to %q (%v)", rootCid, svcName, err):
182182
case <-ctx.Done():
183183
}
184184
ch <- lastPin{}
@@ -212,12 +212,18 @@ func pinMFS(
212212
pinStatuses := []pinclient.Status{pinclient.StatusQueued, pinclient.StatusPinning, pinclient.StatusPinned, pinclient.StatusFailed}
213213
lsPinCh, lsErrCh := c.Ls(ctx, pinclient.PinOpts.FilterName(pinName), pinclient.PinOpts.FilterStatus(pinStatuses...))
214214
existingRequestID := "" // is there any pre-existing MFS pin with pinName (for any CID)?
215-
alreadyPinned := false // is CID for current MFS already pinned?
215+
pinning := false // is CID for current MFS already being pinned?
216216
pinTime := time.Now().UTC()
217+
pinStatusMsg := "pinning to %q: received pre-existing %q status for %q (requestid=%q)"
217218
for ps := range lsPinCh {
218219
existingRequestID = ps.GetRequestId()
220+
if ps.GetPin().GetCid() == cid && ps.GetStatus() == pinclient.StatusFailed {
221+
mfslog.Errorf(pinStatusMsg, svcName, pinclient.StatusFailed, cid, existingRequestID)
222+
} else {
223+
mfslog.Debugf(pinStatusMsg, svcName, ps.GetStatus(), ps.GetPin().GetCid(), existingRequestID)
224+
}
219225
if ps.GetPin().GetCid() == cid && ps.GetStatus() != pinclient.StatusFailed {
220-
alreadyPinned = true
226+
pinning = true
221227
pinTime = ps.GetCreated().UTC()
222228
break
223229
}
@@ -228,9 +234,9 @@ func pinMFS(
228234
return lastPin{}, fmt.Errorf("error while listing remote pins: %v", err)
229235
}
230236

231-
// CID of the current MFS root is already pinned, nothing to do
232-
if alreadyPinned {
233-
mfslog.Debugf("pinning MFS to %s: pin for %s exists since %s, skipping", svcName, cid, pinTime.String())
237+
// CID of the current MFS root is already being pinned, nothing to do
238+
if pinning {
239+
mfslog.Debugf("pinning MFS to %q: pin for %q exists since %s, skipping", svcName, cid, pinTime.String())
234240
return lastPin{Time: pinTime, ServiceName: svcName, ServiceConfig: svcConfig, CID: cid}, nil
235241
}
236242

@@ -250,13 +256,13 @@ func pinMFS(
250256

251257
// Create or replace pin for MFS root
252258
if existingRequestID != "" {
253-
mfslog.Debugf("pinning to %s: replacing existing MFS root pin with %s", svcName, cid)
259+
mfslog.Debugf("pinning to %q: replacing existing MFS root pin with %q", svcName, cid)
254260
_, err := c.Replace(ctx, existingRequestID, cid, addOpts...)
255261
if err != nil {
256262
return lastPin{}, err
257263
}
258264
} else {
259-
mfslog.Debugf("pinning to %s: creating a new MFS root pin for %s", svcName, cid)
265+
mfslog.Debugf("pinning to %q: creating a new MFS root pin for %q", svcName, cid)
260266
_, err := c.Add(ctx, cid, addOpts...)
261267
if err != nil {
262268
return lastPin{}, err

cmd/ipfs/pinmfs_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
config "github.com/ipfs/go-ipfs-config"
1111
ipld "github.com/ipfs/go-ipld-format"
12-
"github.com/ipfs/go-merkledag"
12+
merkledag "github.com/ipfs/go-merkledag"
1313
"github.com/libp2p/go-libp2p-core/host"
1414
peer "github.com/libp2p/go-libp2p-core/peer"
1515
)
@@ -149,7 +149,7 @@ func TestPinMFSService(t *testing.T) {
149149
},
150150
},
151151
}
152-
testPinMFSServiceWithError(t, cfg_invalid_interval, "remote pinning service invalid_interval has invalid MFS.RepinInterval")
152+
testPinMFSServiceWithError(t, cfg_invalid_interval, "remote pinning service \"invalid_interval\" has invalid MFS.RepinInterval")
153153
testPinMFSServiceWithError(t, cfg_valid_unnamed, "error while listing remote pins: empty response from remote pinning service")
154154
testPinMFSServiceWithError(t, cfg_valid_named, "error while listing remote pins: empty response from remote pinning service")
155155
}

0 commit comments

Comments
 (0)