forked from mtusi2026/mtusi2026lab6
119 lines
3.5 KiB
Go
119 lines
3.5 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"highload/internal/database/sqlc"
|
|
"highload/internal/storage"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
type CleanupPolicy struct {
|
|
Now time.Time
|
|
Grace time.Duration
|
|
Retention time.Duration
|
|
DryRun bool
|
|
}
|
|
|
|
// CleanupObject synchronizes with upload, finalization and operator replay.
|
|
// All candidate metadata is rechecked while holding the relevant lock.
|
|
func (r *AttachmentRepository) CleanupObject(ctx context.Context, obj storage.ObjectInfo, policy CleanupPolicy, deleteObject func(context.Context, string) error) (bool, error) {
|
|
txCtx, cancel := context.WithTimeout(ctx, attachmentTransactionTimeout)
|
|
defer cancel()
|
|
tx, err := r.pool.Begin(txCtx)
|
|
if err != nil {
|
|
return false, convertDBError(err)
|
|
}
|
|
defer rollbackTx(ctx, tx)
|
|
q := r.queries.WithTx(tx)
|
|
locked, err := q.TryLockAttachmentObjectKey(txCtx, obj.Key)
|
|
if err != nil || !locked {
|
|
return false, err
|
|
}
|
|
|
|
staging, err := q.GetAttachmentByStagingKey(txCtx, obj.Key)
|
|
keepObject := false
|
|
switch {
|
|
case err == nil:
|
|
version := staging.PipelineVersion
|
|
_, err = q.LockAttachmentJobForPipeline(txCtx, sqlc.LockAttachmentJobForPipelineParams{
|
|
AttachmentID: staging.ID, PipelineVersion: staging.PipelineVersion,
|
|
})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
staging, err = q.GetAttachmentByID(txCtx, staging.ID)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if staging.PipelineVersion != version || staging.StagingKey.String != obj.Key ||
|
|
(staging.Status == "ready" && staging.ObjectKey.String == obj.Key) {
|
|
return false, nil
|
|
}
|
|
if staging.Status != "ready" && (staging.Status != "failed" || staging.UpdatedAt.Time.Add(policy.Retention).After(policy.Now)) {
|
|
return false, nil
|
|
}
|
|
// An upgrade can stage the original published by the previous version.
|
|
keepObject, err = q.IsPublishedOriginalKey(txCtx, obj.Key)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
case !errors.Is(err, pgx.ErrNoRows):
|
|
return false, err
|
|
default:
|
|
if obj.LastModified.IsZero() || obj.LastModified.Add(policy.Grace).After(policy.Now) {
|
|
return false, nil
|
|
}
|
|
if strings.HasPrefix(obj.Key, "results/") {
|
|
// Keys include the job version and the generating lease.
|
|
parts := strings.Split(obj.Key, "/")
|
|
if len(parts) != 5 {
|
|
return false, nil
|
|
}
|
|
id, idErr := getUUIDFromString(parts[1])
|
|
version, versionErr := strconv.ParseInt(strings.TrimPrefix(parts[2], "v"), 10, 32)
|
|
generation, generationErr := strconv.ParseInt(parts[3], 10, 32)
|
|
if idErr != nil || versionErr != nil || generationErr != nil || version < 1 || generation < 1 {
|
|
return false, nil
|
|
}
|
|
job, err := q.LockAttachmentJobForPipeline(txCtx, sqlc.LockAttachmentJobForPipelineParams{
|
|
AttachmentID: *id, PipelineVersion: int32(version),
|
|
})
|
|
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
|
return false, err
|
|
}
|
|
if err == nil && job.LeaseGeneration == int32(generation) && job.LeaseUntil.Valid {
|
|
return false, nil
|
|
}
|
|
}
|
|
exists, err := q.AttachmentExistsByObjectKey(txCtx, obj.Key)
|
|
if err != nil || exists {
|
|
return false, err
|
|
}
|
|
}
|
|
if policy.DryRun {
|
|
return true, nil
|
|
}
|
|
if !keepObject {
|
|
if err := deleteObject(txCtx, obj.Key); err != nil && !errors.Is(err, storage.ErrObjectNotFound) {
|
|
return false, err
|
|
}
|
|
}
|
|
if staging.StagingKey.Valid {
|
|
if _, err = q.ClearStagingReference(txCtx, sqlc.ClearStagingReferenceParams{
|
|
ID: staging.ID, StagingKey: staging.StagingKey,
|
|
}); err != nil {
|
|
return false, err
|
|
}
|
|
}
|
|
if err := tx.Commit(txCtx); err != nil {
|
|
return false, err
|
|
}
|
|
return !keepObject, nil
|
|
}
|