Проверяйте ссылки на объекты хранилища по страницам
This commit is contained in:
@@ -499,6 +499,75 @@ func TestReconciler(t *testing.T) {
|
||||
t.Fatalf("per-object SQL lookup: %+v %v", stats, err)
|
||||
}
|
||||
})
|
||||
t.Run("published variants and active staging sources are checked by page", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
const count = 100
|
||||
old := time.Now().Add(-2 * time.Hour)
|
||||
digest := strings.Repeat("0", 64)
|
||||
hash := f.file("shared original", "ready", 2*time.Hour, false, true)
|
||||
ids := make([]uuid.UUID, count)
|
||||
for i := range ids {
|
||||
id := uuid.New()
|
||||
ids[i] = id
|
||||
f.b.add("staging/"+id.String(), []byte("x"), old)
|
||||
for _, name := range []string{"thumbnail", "crop", "optimized"} {
|
||||
key := fmt.Sprintf("variants/%s/1/%s/%s", id, name, digest)
|
||||
f.b.add(key, []byte("x"), old)
|
||||
}
|
||||
}
|
||||
f.exec(`INSERT INTO attachments(id,chat_id,owner_id,filename,file_hash,created_at,target_version,published_version)
|
||||
SELECT id,$2,$3,'batch.png',$4,now(),2,1 FROM unnest($1::uuid[]) id`, ids, f.chat, f.user, hash)
|
||||
f.exec(`INSERT INTO attachment_jobs(attachment_id,pipeline_version,source_key,state)
|
||||
SELECT id,1,'staging/'||id,'ready' FROM attachments UNION ALL
|
||||
SELECT id,2,'staging/'||id,'queued' FROM attachments`)
|
||||
f.exec(`INSERT INTO attachment_variants(attachment_id,pipeline_version,variant,object_key,mime_type,width,height,size_bytes,sha256)
|
||||
SELECT id,1,name,'variants/'||id||'/1/'||name||'/'||$1,
|
||||
CASE WHEN name='optimized' THEN 'image/webp' ELSE 'image/jpeg' END,1,1,1,decode($1,'hex')
|
||||
FROM attachments CROSS JOIN unnest(ARRAY['thumbnail','crop','optimized']) name`, digest)
|
||||
stats, _, err := f.run(true, false)
|
||||
t.Logf("%d published variants and %d active staging sources: %+v", 3*count, count, stats)
|
||||
if err != nil || stats.Findings != 0 || stats.ScannedObjects != 4*count+1 || stats.SQLQueries > 6 {
|
||||
t.Fatalf("expected page-batched lookups to preserve all referenced objects: %+v, %v", stats, err)
|
||||
}
|
||||
})
|
||||
t.Run("new job protects a variant after the page lookup", func(t *testing.T) {
|
||||
f := reconcileSetup(t, db)
|
||||
hash := f.file("pending source", "pending", 2*time.Hour, true, false)
|
||||
var id uuid.UUID
|
||||
if err := f.db.QueryRow(ctx, `SELECT id FROM attachments WHERE file_hash=$1`, hash).Scan(&id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key := fmt.Sprintf("variants/%s/1/thumbnail/%064x", id, 0)
|
||||
f.b.add(key, []byte("x"), time.Now().Add(-2*time.Hour))
|
||||
tx, err := f.db.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
keyHash := sha256.Sum256([]byte(key))
|
||||
if err := attachment.LockHash(ctx, tx, keyHash[:]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r, err := attachment.NewReconciler(f.db, f.b, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := r.Run(ctx, attachment.ReconcileOptions{Apply: true}, nil)
|
||||
done <- err
|
||||
}()
|
||||
waitReconcileLock(t, ctx, f.db)
|
||||
if _, err := tx.Exec(ctx, `INSERT INTO attachment_jobs(attachment_id, source_key) VALUES ($1,$2)`, id, "staging/"+id.String()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := <-done; err != nil || !f.b.has(key) {
|
||||
t.Fatalf("variant required by new job was lost: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func waitReconcileLock(t *testing.T, ctx context.Context, db *pgxpool.Pool) {
|
||||
|
||||
Reference in New Issue
Block a user