mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-12-07 14:09:47 +00:00
The package cleanup routine checks every container version for whether it is referenced by a multi-platform manifest, which appears to be a performance problem indicated by CPU profiling collected in #9358 on SQLite systems. This PR removes that check completely, which isn't necessary since #4698 added a much more performant mass-cleanup of these dangling platform versions. May fix #9358 completely, but it leaves fundamental scalability concerns with SQLite due to long-running transactions. The transactions will be shorter with this change. Requires end-user testing to confirm if sufficiently fixed. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [x] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. <!--start release-notes-assistant--> ## Release notes <!--URL:https://codeberg.org/forgejo/forgejo--> - Bug fixes - [PR](https://codeberg.org/forgejo/forgejo/pulls/10297): <!--number 10297 --><!--line 0 --><!--description cmVkdWNlIHJ1bnRpbWUgb2YgY29udGFpbmVyIGNsZWFudXAgYnkgcmVseWluZyBvbiBtYXNzIGRpZ2VzdCBjbGVhbnVw-->reduce runtime of container cleanup by relying on mass digest cleanup<!--description--> <!--end release-notes-assistant--> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10297 Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net> Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
98 lines
2.5 KiB
Go
98 lines
2.5 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package container
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
packages_model "forgejo.org/models/packages"
|
|
container_model "forgejo.org/models/packages/container"
|
|
"forgejo.org/modules/optional"
|
|
packages_service "forgejo.org/services/packages"
|
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
)
|
|
|
|
// Cleanup removes expired container data
|
|
func Cleanup(ctx context.Context, olderThan time.Duration) error {
|
|
if err := cleanupExpiredBlobUploads(ctx, olderThan); err != nil {
|
|
return err
|
|
}
|
|
if err := CleanupSHA256(ctx, olderThan); err != nil {
|
|
return err
|
|
}
|
|
return cleanupExpiredUploadedBlobs(ctx, olderThan)
|
|
}
|
|
|
|
// cleanupExpiredBlobUploads removes expired blob uploads
|
|
func cleanupExpiredBlobUploads(ctx context.Context, olderThan time.Duration) error {
|
|
pbus, err := packages_model.FindExpiredBlobUploads(ctx, olderThan)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, pbu := range pbus {
|
|
if err := RemoveBlobUploadByID(ctx, pbu.ID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// cleanupExpiredUploadedBlobs removes expired uploaded blobs not referenced by a manifest
|
|
func cleanupExpiredUploadedBlobs(ctx context.Context, olderThan time.Duration) error {
|
|
pfs, err := container_model.SearchExpiredUploadedBlobs(ctx, olderThan)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, pf := range pfs {
|
|
if err := packages_service.DeletePackageFile(ctx, pf); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
pvs, _, err := packages_model.SearchVersions(ctx, &packages_model.PackageSearchOptions{
|
|
Type: packages_model.TypeContainer,
|
|
Version: packages_model.SearchValue{
|
|
ExactMatch: true,
|
|
Value: container_model.UploadVersion,
|
|
},
|
|
IsInternal: optional.Some(true),
|
|
HasFiles: optional.Some(false),
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, pv := range pvs {
|
|
if err := packages_model.DeleteAllProperties(ctx, packages_model.PropertyTypeVersion, pv.ID); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := packages_model.DeleteVersionByID(ctx, pv.ID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func ShouldBeSkipped(pv *packages_model.PackageVersion) bool {
|
|
// Always skip the "latest" tag
|
|
if pv.LowerVersion == "latest" {
|
|
return true
|
|
}
|
|
|
|
// Check if the version is a digest (or untagged)
|
|
if digest.Digest(pv.LowerVersion).Validate() == nil {
|
|
// Don't apply PackageCleanupRules to image versions that aren't tags; rely on `CleanupSHA256` to do mass
|
|
// cleanup of them once they're dangling due to not being referenced.
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|