Repository navigation
fix(artwork): keep the storage sweep from deleting artwork being re-cached #5916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Quick104
wants to merge
3
commits into
main
Choose a base branch
from
fix/artwork-sweep-reuse-race
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+249
−3
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
dd2253e
fix(artwork): keep the storage sweep from deleting artwork being re-c…
Quick104 86bde0a
test(artwork): register re-caches on their own pool in the sweep race…
Quick104 a428f51
fix(artwork): bound the sweep's rollback and respect the GC grace period
Quick104 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
internal/metadata/artwork_storage_sweep_recache_db_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| package metadata | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "slices" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/Silo-Server/silo-server/internal/blobstore" | ||
| "github.com/Silo-Server/silo-server/internal/catalog" | ||
| ) | ||
|
|
||
| // recacheDuringSweepStorage lists one page and runs onDelete before recording | ||
| // a deletion, so a test can re-cache artwork while the sweep is deleting it. | ||
| type recacheDuringSweepStorage struct { | ||
| page []blobstore.ObjectInfo | ||
| onDelete func(keys []string) | ||
| deleted []string | ||
| } | ||
|
|
||
| func (s *recacheDuringSweepStorage) List(context.Context, string, string, int) ([]blobstore.ObjectInfo, string, error) { | ||
| return s.page, "", nil | ||
| } | ||
|
|
||
| func (s *recacheDuringSweepStorage) Delete(_ context.Context, keys []string) (int, error) { | ||
| if s.onDelete != nil { | ||
| s.onDelete(keys) | ||
| } | ||
| s.deleted = append(s.deleted, keys...) | ||
| return len(keys), nil | ||
| } | ||
|
|
||
| // separateArtworkRevisionTracker returns a tracker on its own connected pool, | ||
| // so a registration that times out can only have waited on the sweep's locks, | ||
| // not for a free connection. | ||
| func separateArtworkRevisionTracker(ctx context.Context, t *testing.T) *catalog.ArtworkRevisionTracker { | ||
| t.Helper() | ||
| pool := artworkRevisionGCTestPool(t) | ||
| if err := pool.Ping(ctx); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| return catalog.NewArtworkRevisionTracker(pool) | ||
| } | ||
|
|
||
| // TestArtworkSweepDeletionSerializesWithRecachingDB covers the image cache | ||
| // re-caching an unreferenced revision while the storage sweep deletes it. The | ||
| // revision is content-addressed, so the cache registers the same original path | ||
| // with the revision tracker, finds the objects already stored, skips the | ||
| // upload, and publishes the path. Deleting the objects after that leaves the | ||
| // catalog pointing at missing artwork. | ||
| func TestArtworkSweepDeletionSerializesWithRecachingDB(t *testing.T) { | ||
| pool := artworkRevisionGCTestPool(t) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
| tracker := separateArtworkRevisionTracker(ctx, t) | ||
|
|
||
| suffix := time.Now().UnixNano() | ||
| old := time.Now().Add(-72 * time.Hour) | ||
| // ladder returns an aged revision's original path and stored objects, and | ||
| // removes any revision row the test leaves for it. | ||
| ladder := func(name string) (string, []blobstore.ObjectInfo) { | ||
| dir := fmt.Sprintf("tmdb/movies/sweep-%d-%s/poster/", suffix, name) | ||
| t.Cleanup(func() { | ||
| _, _ = pool.Exec(context.Background(), | ||
| `DELETE FROM artwork_revision_gc_candidates WHERE original_path = $1`, dir+"original.rev.webp") | ||
| }) | ||
| return dir + "original.rev.webp", []blobstore.ObjectInfo{ | ||
| {Key: dir + "original.rev.webp", ModTime: old}, | ||
| {Key: dir + "w300.rev.webp", ModTime: old}, | ||
| } | ||
| } | ||
|
|
||
| // parkRevision tracks a revision as the GC leaves live artwork, last | ||
| // touched before the age floor; its reference has since gone. | ||
| parkRevision := func(original string) { | ||
| t.Helper() | ||
| if _, err := pool.Exec(ctx, ` | ||
| INSERT INTO artwork_revision_gc_candidates (original_path, image_type, not_before, next_attempt_at, updated_at) | ||
| VALUES ($1, 'poster', $2, NULL, $2)`, original, old); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| t.Run("a re-cache that starts during the delete waits for it", func(t *testing.T) { | ||
| untracked, untrackedObjects := ladder("untracked") | ||
| tracked, trackedObjects := ladder("tracked") | ||
| parkRevision(tracked) | ||
|
|
||
| recacheErrs := map[string]error{} | ||
| storage := &recacheDuringSweepStorage{ | ||
| page: append(untrackedObjects, trackedObjects...), | ||
| onDelete: func([]string) { | ||
| for _, original := range []string{untracked, tracked} { | ||
| waitCtx, stop := context.WithTimeout(ctx, 200*time.Millisecond) | ||
| recacheErrs[original] = tracker.TrackArtworkRevision(waitCtx, original, "poster", nil) | ||
| stop() | ||
| } | ||
| }, | ||
| } | ||
| if _, err := NewArtworkStorageSweeper(pool, storage).SweepPrefix(ctx, "tmdb/", "", 1); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(storage.deleted) != 4 { | ||
| t.Fatalf("deleted %v, want both unreferenced ladders", storage.deleted) | ||
| } | ||
| // Registration must wait until the objects are gone: only then does the | ||
| // cache find them missing and upload them again. | ||
| for original, err := range recacheErrs { | ||
| if !errors.Is(err, context.DeadlineExceeded) { | ||
| t.Errorf("re-cache of %s while the sweep was deleting it returned %v, want it to wait", original, err) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| t.Run("a revision re-cached after the reference check is kept", func(t *testing.T) { | ||
| original, objects := ladder("recached") | ||
| storage := &recacheDuringSweepStorage{page: objects} | ||
| sweeper := NewArtworkStorageSweeper(pool, storage) | ||
| lookup := sweeper.lookup | ||
| sweeper.lookup = func(ctx context.Context, paths []string) (map[string]struct{}, error) { | ||
| referenced, err := lookup(ctx, paths) | ||
| if err == nil && slices.Contains(paths, original) { | ||
| // The cache registers the revision and is about to publish it. | ||
| err = tracker.TrackArtworkRevision(ctx, original, "poster", nil) | ||
| } | ||
| return referenced, err | ||
| } | ||
| if _, err := sweeper.SweepPrefix(ctx, "tmdb/", "", 1); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(storage.deleted) != 0 { | ||
| t.Fatalf("deleted %v, a revision the cache just registered", storage.deleted) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("a revision the GC is deleting is left to it", func(t *testing.T) { | ||
| original, objects := ladder("collecting") | ||
| parkRevision(original) | ||
| // The GC holds the row while it deletes the revision's objects. | ||
| collecting, err := pool.Begin(ctx) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer func() { _ = collecting.Rollback(context.Background()) }() | ||
| if _, err := collecting.Exec(ctx, | ||
| `SELECT 1 FROM artwork_revision_gc_candidates WHERE original_path = $1 FOR UPDATE`, original); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| storage := &recacheDuringSweepStorage{page: objects} | ||
| if _, err := NewArtworkStorageSweeper(pool, storage).SweepPrefix(ctx, "tmdb/", "", 1); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(storage.deleted) != 0 { | ||
| t.Fatalf("deleted %v, a revision the GC holds", storage.deleted) | ||
| } | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.