forked from mtusi2026/mtusi2026lab4
S3-вложения и сверка Postgresql с MinIO
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
// Сверка PostgreSQL и bucket.
|
||||
// Удаляются только старые расхождения: объект без строки и строка без объекта,
|
||||
package reconcile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"messenger/internal/blob"
|
||||
"messenger/internal/store"
|
||||
)
|
||||
|
||||
// Meta - чтение и безопасное удаление метаданных. Удаление само проверяет ссылки и возраст.
|
||||
type Meta interface {
|
||||
AttachmentRefs(ctx context.Context) ([]store.AttachmentRef, error)
|
||||
DeleteUnreferencedAttachment(ctx context.Context, id string, olderThan time.Time) (bool, error)
|
||||
}
|
||||
|
||||
// Summary - наблюдаемый результат одного прохода.
|
||||
type Summary struct {
|
||||
Mode string `json:"mode"`
|
||||
GraceSeconds int `json:"grace_seconds"`
|
||||
Cutoff string `json:"cutoff"`
|
||||
DBRows int `json:"db_rows"`
|
||||
S3Objects int `json:"s3_objects"`
|
||||
ListPages int `json:"list_pages"`
|
||||
Pairs int `json:"pairs"`
|
||||
OrphanFresh int `json:"orphan_fresh"`
|
||||
OrphanStale int `json:"orphan_stale"`
|
||||
MissingFresh int `json:"missing_fresh"`
|
||||
MissingReferenced int `json:"missing_referenced"`
|
||||
MissingStaleUnreferenced int `json:"missing_stale_unreferenced"`
|
||||
DeletedObjects int `json:"deleted_objects"`
|
||||
DeletedRows int `json:"deleted_rows"`
|
||||
DurationMs int64 `json:"duration_ms"`
|
||||
}
|
||||
|
||||
// Run сравнивает множество ключей. Стоимость - один листинг bucket и один запрос метаданных,
|
||||
// без HEAD на каждый объект.
|
||||
func Run(ctx context.Context, meta Meta, objects blob.Store, grace time.Duration, apply bool) (Summary, error) {
|
||||
start := time.Now()
|
||||
if grace < 0 {
|
||||
grace = 0
|
||||
}
|
||||
cutoff := time.Now().UTC().Add(-grace)
|
||||
rows, err := meta.AttachmentRefs(ctx)
|
||||
if err != nil {
|
||||
return Summary{}, err
|
||||
}
|
||||
listed, pages, err := objects.List(ctx)
|
||||
if err != nil {
|
||||
return Summary{}, err
|
||||
}
|
||||
byKey := make(map[string]store.AttachmentRef, len(rows))
|
||||
for _, row := range rows {
|
||||
byKey[row.ObjectKey] = row
|
||||
}
|
||||
seen := make(map[string]struct{}, len(listed))
|
||||
sum := Summary{
|
||||
Mode: "dry-run",
|
||||
GraceSeconds: int(grace / time.Second),
|
||||
Cutoff: cutoff.Format(time.RFC3339),
|
||||
DBRows: len(rows),
|
||||
S3Objects: len(listed),
|
||||
ListPages: pages,
|
||||
}
|
||||
if apply {
|
||||
sum.Mode = "apply"
|
||||
}
|
||||
for _, obj := range listed {
|
||||
seen[obj.Key] = struct{}{}
|
||||
if _, ok := byKey[obj.Key]; ok {
|
||||
sum.Pairs++
|
||||
continue
|
||||
}
|
||||
if !obj.Modified.UTC().Before(cutoff) {
|
||||
sum.OrphanFresh++
|
||||
continue
|
||||
}
|
||||
sum.OrphanStale++
|
||||
if !apply {
|
||||
continue
|
||||
}
|
||||
if err := objects.Delete(ctx, obj.Key); err != nil {
|
||||
return Summary{}, err
|
||||
}
|
||||
sum.DeletedObjects++
|
||||
}
|
||||
for _, row := range rows {
|
||||
if _, ok := seen[row.ObjectKey]; ok {
|
||||
continue
|
||||
}
|
||||
if row.Referenced {
|
||||
sum.MissingReferenced++
|
||||
continue
|
||||
}
|
||||
if !row.CreatedAt.UTC().Before(cutoff) {
|
||||
sum.MissingFresh++
|
||||
continue
|
||||
}
|
||||
sum.MissingStaleUnreferenced++
|
||||
if !apply {
|
||||
continue
|
||||
}
|
||||
deleted, err := meta.DeleteUnreferencedAttachment(ctx, row.ID, cutoff)
|
||||
if err != nil {
|
||||
return Summary{}, err
|
||||
}
|
||||
if deleted {
|
||||
sum.DeletedRows++
|
||||
}
|
||||
}
|
||||
sum.DurationMs = time.Since(start).Milliseconds()
|
||||
return sum, nil
|
||||
}
|
||||
Reference in New Issue
Block a user