Merge pull request #16790 from zyyw/issue-16747-improve-gc-log

fix: improve GC log message
This commit is contained in:
Shengwen YU 2022-05-07 09:16:06 +08:00 committed by GitHub
commit 2dceffe457
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -243,16 +243,18 @@ func (gc *GarbageCollector) sweep(ctx job.Context) error {
sweepSize := int64(0) sweepSize := int64(0)
blobCnt := 0 blobCnt := 0
mfCnt := 0 mfCnt := 0
for _, blob := range gc.deleteSet { total := len(gc.deleteSet)
for i, blob := range gc.deleteSet {
idx := i + 1
// set the status firstly, if the blob is updated by any HEAD/PUT request, it should be fail and skip. // set the status firstly, if the blob is updated by any HEAD/PUT request, it should be fail and skip.
blob.Status = blobModels.StatusDeleting blob.Status = blobModels.StatusDeleting
count, err := gc.blobMgr.UpdateBlobStatus(ctx.SystemContext(), blob) count, err := gc.blobMgr.UpdateBlobStatus(ctx.SystemContext(), blob)
if err != nil { if err != nil {
gc.logger.Errorf("failed to mark gc candidate deleting, skip: %s, %s", blob.Digest, blob.Status) gc.logger.Errorf("[%d/%d] failed to mark gc candidate deleting, skip: %s, %s", idx, total, blob.Digest, blob.Status)
continue continue
} }
if count == 0 { if count == 0 {
gc.logger.Warningf("no blob found to mark gc candidate deleting, ID:%d, digest:%s", blob.ID, blob.Digest) gc.logger.Warningf("[%d/%d] no blob found to mark gc candidate deleting, ID:%d, digest:%s", idx, total, blob.ID, blob.Digest)
continue continue
} }
@ -261,44 +263,44 @@ func (gc *GarbageCollector) sweep(ctx job.Context) error {
if _, exist := gc.trashedArts[blob.Digest]; exist && blob.IsManifest() { if _, exist := gc.trashedArts[blob.Digest]; exist && blob.IsManifest() {
for _, art := range gc.trashedArts[blob.Digest] { for _, art := range gc.trashedArts[blob.Digest] {
// Harbor cannot know the existing tags in the backend from its database, so let the v2 DELETE manifest to remove all of them. // Harbor cannot know the existing tags in the backend from its database, so let the v2 DELETE manifest to remove all of them.
gc.logger.Infof("delete the manifest with registry v2 API: %s, %s, %s", gc.logger.Infof("[%d/%d] delete the manifest with registry v2 API: %s, %s, %s",
art.RepositoryName, blob.ContentType, blob.Digest) idx, total, art.RepositoryName, blob.ContentType, blob.Digest)
if err := v2DeleteManifest(gc.logger, art.RepositoryName, blob.Digest); err != nil { if err := v2DeleteManifest(gc.logger, art.RepositoryName, blob.Digest); err != nil {
gc.logger.Errorf("failed to delete manifest with v2 API, %s, %s, %v", art.RepositoryName, blob.Digest, err) gc.logger.Errorf("[%d/%d] failed to delete manifest with v2 API, %s, %s, %v", idx, total, art.RepositoryName, blob.Digest, err)
if err := ignoreNotFound(func() error { if err := ignoreNotFound(func() error {
return gc.markDeleteFailed(ctx, blob) return gc.markDeleteFailed(ctx, blob)
}); err != nil { }); err != nil {
gc.logger.Errorf("failed to call gc.markDeleteFailed() after v2DeleteManifest() error out: %s, %v", blob.Digest, err) gc.logger.Errorf("[%d/%d] failed to call gc.markDeleteFailed() after v2DeleteManifest() error out: %s, %v", idx, total, blob.Digest, err)
return err return err
} }
skippedBlob = true skippedBlob = true
continue continue
} }
// for manifest, it has to delete the revisions folder of each repository // for manifest, it has to delete the revisions folder of each repository
gc.logger.Infof("delete manifest from storage: %s", blob.Digest) gc.logger.Infof("[%d/%d] delete manifest from storage: %s", idx, total, blob.Digest)
if err := retry.Retry(func() error { if err := retry.Retry(func() error {
return ignoreNotFound(func() error { return ignoreNotFound(func() error {
return gc.registryCtlClient.DeleteManifest(art.RepositoryName, blob.Digest) return gc.registryCtlClient.DeleteManifest(art.RepositoryName, blob.Digest)
}) })
}, retry.Callback(func(err error, sleep time.Duration) { }, retry.Callback(func(err error, sleep time.Duration) {
gc.logger.Infof("failed to exec DeleteManifest, error: %v, will retry again after: %s", err, sleep) gc.logger.Infof("[%d/%d] failed to exec DeleteManifest, error: %v, will retry again after: %s", idx, total, err, sleep)
})); err != nil { })); err != nil {
gc.logger.Errorf("failed to remove manifest from storage: %s, %s, errMsg=%v", art.RepositoryName, blob.Digest, err) gc.logger.Errorf("[%d/%d] failed to remove manifest from storage: %s, %s, errMsg=%v", idx, total, art.RepositoryName, blob.Digest, err)
if err := ignoreNotFound(func() error { if err := ignoreNotFound(func() error {
return gc.markDeleteFailed(ctx, blob) return gc.markDeleteFailed(ctx, blob)
}); err != nil { }); err != nil {
gc.logger.Errorf("failed to call gc.markDeleteFailed() after gc.registryCtlClient.DeleteManifest() error out: %s, %s, %v", art.RepositoryName, blob.Digest, err) gc.logger.Errorf("[%d/%d] failed to call gc.markDeleteFailed() after gc.registryCtlClient.DeleteManifest() error out: %s, %s, %v", idx, total, art.RepositoryName, blob.Digest, err)
return err return err
} }
skippedBlob = true skippedBlob = true
continue continue
} }
gc.logger.Infof("delete artifact trash record from database: %d, %s, %s", art.ID, art.RepositoryName, art.Digest) gc.logger.Infof("[%d/%d] delete artifact trash record from database: %d, %s, %s", idx, total, art.ID, art.RepositoryName, art.Digest)
if err := ignoreNotFound(func() error { if err := ignoreNotFound(func() error {
return gc.artrashMgr.Delete(ctx.SystemContext(), art.ID) return gc.artrashMgr.Delete(ctx.SystemContext(), art.ID)
}); err != nil { }); err != nil {
gc.logger.Errorf("failed to call gc.artrashMgr.Delete(): %v, errMsg=%v", art.ID, err) gc.logger.Errorf("[%d/%d] failed to call gc.artrashMgr.Delete(): %v, errMsg=%v", idx, total, art.ID, err)
return err return err
} }
} }
@ -312,19 +314,19 @@ func (gc *GarbageCollector) sweep(ctx job.Context) error {
// delete all of blobs, which include config, layer and manifest // delete all of blobs, which include config, layer and manifest
// for the foreign layer, as it's not stored in the storage, no need to call the delete api and count size, but still have to delete the DB record. // for the foreign layer, as it's not stored in the storage, no need to call the delete api and count size, but still have to delete the DB record.
if !blob.IsForeignLayer() { if !blob.IsForeignLayer() {
gc.logger.Infof("delete blob from storage: %s", blob.Digest) gc.logger.Infof("[%d/%d] delete blob from storage: %s", idx, total, blob.Digest)
if err := retry.Retry(func() error { if err := retry.Retry(func() error {
return ignoreNotFound(func() error { return ignoreNotFound(func() error {
return gc.registryCtlClient.DeleteBlob(blob.Digest) return gc.registryCtlClient.DeleteBlob(blob.Digest)
}) })
}, retry.Callback(func(err error, sleep time.Duration) { }, retry.Callback(func(err error, sleep time.Duration) {
gc.logger.Infof("failed to exec DeleteBlob, error: %v, will retry again after: %s", err, sleep) gc.logger.Infof("[%d/%d] failed to exec DeleteBlob, error: %v, will retry again after: %s", idx, total, err, sleep)
})); err != nil { })); err != nil {
gc.logger.Errorf("failed to delete blob from storage: %s, %s, errMsg=%v", blob.Digest, blob.Status, err) gc.logger.Errorf("[%d/%d] failed to delete blob from storage: %s, %s, errMsg=%v", idx, total, blob.Digest, blob.Status, err)
if err := ignoreNotFound(func() error { if err := ignoreNotFound(func() error {
return gc.markDeleteFailed(ctx, blob) return gc.markDeleteFailed(ctx, blob)
}); err != nil { }); err != nil {
gc.logger.Errorf("failed to call gc.markDeleteFailed() after gc.registryCtlClient.DeleteBlob() error out: %s, %v", blob.Digest, err) gc.logger.Errorf("[%d/%d] failed to call gc.markDeleteFailed() after gc.registryCtlClient.DeleteBlob() error out: %s, %v", idx, total, blob.Digest, err)
return err return err
} }
continue continue
@ -332,15 +334,15 @@ func (gc *GarbageCollector) sweep(ctx job.Context) error {
sweepSize = sweepSize + blob.Size sweepSize = sweepSize + blob.Size
} }
gc.logger.Infof("delete blob record from database: %d, %s", blob.ID, blob.Digest) gc.logger.Infof("[%d/%d] delete blob record from database: %d, %s", idx, total, blob.ID, blob.Digest)
if err := ignoreNotFound(func() error { if err := ignoreNotFound(func() error {
return gc.blobMgr.Delete(ctx.SystemContext(), blob.ID) return gc.blobMgr.Delete(ctx.SystemContext(), blob.ID)
}); err != nil { }); err != nil {
gc.logger.Errorf("failed to delete blob from database: %s, %s, errMsg=%v", blob.Digest, blob.Status, err) gc.logger.Errorf("[%d/%d] failed to delete blob from database: %s, %s, errMsg=%v", idx, total, blob.Digest, blob.Status, err)
if err := ignoreNotFound(func() error { if err := ignoreNotFound(func() error {
return gc.markDeleteFailed(ctx, blob) return gc.markDeleteFailed(ctx, blob)
}); err != nil { }); err != nil {
gc.logger.Errorf("failed to call gc.markDeleteFailed() after gc.blobMgr.Delete() error out, %d, %s %v", blob.ID, blob.Digest, err) gc.logger.Errorf("[%d/%d] failed to call gc.markDeleteFailed() after gc.blobMgr.Delete() error out, %d, %s %v", idx, total, blob.ID, blob.Digest, err)
return err return err
} }
return err return err