Доработайте публикацию и сверку вложений по итогам ревью
This commit is contained in:
@@ -45,6 +45,11 @@ type Reconciler struct {
|
||||
grace time.Duration
|
||||
}
|
||||
|
||||
type ReconcileOptions struct {
|
||||
Apply bool
|
||||
Deep bool
|
||||
}
|
||||
|
||||
func NewReconciler(db *pgxpool.Pool, blobs Blobs, grace time.Duration) (*Reconciler, error) {
|
||||
if db == nil || blobs == nil {
|
||||
return nil, errors.New("reconcile: database and blobs are required")
|
||||
@@ -61,10 +66,18 @@ func NewReconciler(db *pgxpool.Pool, blobs Blobs, grace time.Duration) (*Reconci
|
||||
// Run streams findings and keeps at most one page of metadata in memory.
|
||||
// Dry runs only read storage. Each apply candidate is checked again under the
|
||||
// publisher's transaction lock; no scan snapshot authorizes a later mutation.
|
||||
func (r *Reconciler) Run(ctx context.Context, apply, deep bool, emit func(Finding)) (stats Stats, err error) {
|
||||
func (r *Reconciler) Run(ctx context.Context, options ReconcileOptions, emit func(Finding)) (stats Stats, err error) {
|
||||
started := time.Now()
|
||||
defer func() { stats.ElapsedMS = time.Since(started).Milliseconds() }()
|
||||
run := reconcileRun{r: r, ctx: ctx, apply: apply, deep: deep, emit: emit, stats: &stats, cutoff: started.Add(-r.grace)}
|
||||
run := reconcileRun{
|
||||
r: r,
|
||||
ctx: ctx,
|
||||
apply: options.Apply,
|
||||
deep: options.Deep,
|
||||
emit: emit,
|
||||
stats: &stats,
|
||||
cutoff: started.Add(-r.grace),
|
||||
}
|
||||
for _, prefix := range []string{"staging/", "blobs/"} {
|
||||
if err = run.objects(prefix); err != nil {
|
||||
return stats, err
|
||||
@@ -203,7 +216,8 @@ func (s *reconcileRun) knownFiles(page []Object) (map[string]bool, error) {
|
||||
return known, nil
|
||||
}
|
||||
s.stats.SQLQueries++
|
||||
rows, err := s.r.db.Query(s.ctx, `SELECT hash FROM files WHERE hash=ANY($1::bytea[])`, hashes)
|
||||
rows, err := s.r.db.Query(s.ctx, `
|
||||
SELECT hash FROM files WHERE hash = ANY($1::bytea[])`, hashes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -235,7 +249,8 @@ func (s *reconcileRun) orphan(obj Object, known bool) (*Finding, error) {
|
||||
op.ctx = opCtx
|
||||
var exists bool
|
||||
s.stats.SQLQueries++
|
||||
if err := tx.QueryRow(opCtx, `SELECT EXISTS(SELECT 1 FROM files WHERE hash=$1)`, hash).Scan(&exists); err != nil {
|
||||
if err := tx.QueryRow(opCtx, `
|
||||
SELECT EXISTS (SELECT 1 FROM files WHERE hash = $1)`, hash).Scan(&exists); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if exists {
|
||||
@@ -304,7 +319,7 @@ func (s *reconcileRun) locked(hash []byte, fn func(context.Context, pgx.Tx) (*Fi
|
||||
type reconcileFile struct {
|
||||
hash []byte
|
||||
size int64
|
||||
status string
|
||||
status FileStatus
|
||||
created time.Time
|
||||
referenced bool
|
||||
}
|
||||
@@ -316,9 +331,13 @@ func (s *reconcileRun) files() error {
|
||||
return err
|
||||
}
|
||||
s.stats.SQLQueries++
|
||||
rows, err := s.r.db.Query(s.ctx, `SELECT f.hash,f.size_bytes,f.status,f.created_at,
|
||||
EXISTS(SELECT 1 FROM attachments a WHERE a.file_hash=f.hash)
|
||||
FROM files f WHERE f.hash>$1 ORDER BY f.hash LIMIT 500`, after)
|
||||
rows, err := s.r.db.Query(s.ctx, `
|
||||
SELECT f.hash, f.size_bytes, f.status, f.created_at,
|
||||
EXISTS (SELECT 1 FROM attachments a WHERE a.file_hash = f.hash)
|
||||
FROM files f
|
||||
WHERE f.hash > $1
|
||||
ORDER BY f.hash
|
||||
LIMIT $2`, after, reconcilePageSize)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -346,30 +365,9 @@ func (s *reconcileRun) files() error {
|
||||
if !s.candidate(f) {
|
||||
continue
|
||||
}
|
||||
var finding *Finding
|
||||
if s.apply {
|
||||
finding, err = s.locked(f.hash, func(opCtx context.Context, tx pgx.Tx) (*Finding, error) {
|
||||
op := *s
|
||||
op.ctx = opCtx
|
||||
var current reconcileFile
|
||||
s.stats.SQLQueries++
|
||||
err := tx.QueryRow(opCtx, `SELECT f.hash,f.size_bytes,f.status,f.created_at,
|
||||
EXISTS(SELECT 1 FROM attachments a WHERE a.file_hash=f.hash)
|
||||
FROM files f WHERE f.hash=$1 FOR UPDATE OF f`, f.hash).
|
||||
Scan(¤t.hash, ¤t.size, ¤t.status, ¤t.created, ¤t.referenced)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !s.candidate(current) {
|
||||
return nil, nil
|
||||
}
|
||||
return op.inspect(current, tx)
|
||||
})
|
||||
} else {
|
||||
finding, err = s.inspect(f, nil)
|
||||
finding, err := s.inspect(f)
|
||||
if err == nil && s.apply && finding != nil && finding.Action != "report" {
|
||||
finding, err = s.repairFile(f.hash)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("reconcile file %s: %w", BlobKey(f.hash), err)
|
||||
@@ -379,17 +377,66 @@ func (s *reconcileRun) files() error {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *reconcileRun) repairFile(hash []byte) (*Finding, error) {
|
||||
return s.locked(hash, func(opCtx context.Context, tx pgx.Tx) (*Finding, error) {
|
||||
op := *s
|
||||
op.ctx = opCtx
|
||||
var current reconcileFile
|
||||
s.stats.SQLQueries++
|
||||
err := tx.QueryRow(opCtx, `
|
||||
SELECT f.hash, f.size_bytes, f.status, f.created_at,
|
||||
EXISTS (SELECT 1 FROM attachments a WHERE a.file_hash = f.hash)
|
||||
FROM files f
|
||||
WHERE f.hash = $1
|
||||
FOR UPDATE OF f`, hash).
|
||||
Scan(¤t.hash, ¤t.size, ¤t.status, ¤t.created, ¤t.referenced)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !op.candidate(current) {
|
||||
return nil, nil
|
||||
}
|
||||
finding, err := op.inspect(current)
|
||||
if err != nil || finding == nil {
|
||||
return finding, err
|
||||
}
|
||||
switch finding.Action {
|
||||
case "delete_file":
|
||||
// Delete bytes first. A failed SQL commit leaves an unreferenced
|
||||
// missing row that the next pass can safely finish collecting.
|
||||
if err := op.delete(finding.Key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.stats.SQLQueries++
|
||||
_, err = tx.Exec(opCtx, `DELETE FROM files WHERE hash = $1`, hash)
|
||||
case "mark_failed":
|
||||
s.stats.SQLQueries++
|
||||
_, err = tx.Exec(opCtx, `UPDATE files SET status = $2 WHERE hash = $1`, hash, FileFailed)
|
||||
default:
|
||||
return finding, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
finding.Applied = true
|
||||
return finding, nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *reconcileRun) candidate(f reconcileFile) bool {
|
||||
if !s.old(f.created) {
|
||||
return false
|
||||
}
|
||||
if !f.referenced {
|
||||
return f.status == "pending" || f.status == "ready"
|
||||
return f.status == FilePending || f.status == FileReady
|
||||
}
|
||||
return f.status == "ready" || f.status == "failed"
|
||||
return f.status == FileReady || f.status == FileFailed
|
||||
}
|
||||
|
||||
func (s *reconcileRun) inspect(f reconcileFile, tx pgx.Tx) (*Finding, error) {
|
||||
func (s *reconcileRun) inspect(f reconcileFile) (*Finding, error) {
|
||||
key := BlobKey(f.hash)
|
||||
if !f.referenced {
|
||||
// A publication that rolled back can leave fresh bytes behind an old
|
||||
@@ -401,36 +448,14 @@ func (s *reconcileRun) inspect(f reconcileFile, tx pgx.Tx) (*Finding, error) {
|
||||
if err == nil && !s.old(obj.Modified) {
|
||||
return nil, nil
|
||||
}
|
||||
finding := &Finding{Kind: "unreferenced_" + f.status, Key: key, Action: "delete_file"}
|
||||
if !s.apply {
|
||||
return finding, nil
|
||||
}
|
||||
// S3 must succeed first. A failed SQL commit then leaves a missing,
|
||||
// unreferenced row that the next run can safely finish collecting.
|
||||
if err := s.delete(key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.stats.SQLQueries++
|
||||
if _, err := tx.Exec(s.ctx, `DELETE FROM files WHERE hash=$1`, f.hash); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
finding.Applied = true
|
||||
return finding, nil
|
||||
return &Finding{Kind: "unreferenced_" + string(f.status), Key: key, Action: "delete_file"}, nil
|
||||
}
|
||||
obj, err := s.stat(key)
|
||||
if errors.Is(err, ErrObjectMissing) {
|
||||
if f.status == "failed" {
|
||||
if f.status == FileFailed {
|
||||
return nil, nil
|
||||
}
|
||||
finding := &Finding{Kind: "missing_blob", Key: key, Action: "mark_failed"}
|
||||
if s.apply {
|
||||
s.stats.SQLQueries++
|
||||
if _, err = tx.Exec(s.ctx, `UPDATE files SET status='failed' WHERE hash=$1`, f.hash); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
finding.Applied = true
|
||||
}
|
||||
return finding, nil
|
||||
return &Finding{Kind: "missing_blob", Key: key, Action: "mark_failed"}, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -449,7 +474,7 @@ func (s *reconcileRun) inspect(f reconcileFile, tx pgx.Tx) (*Finding, error) {
|
||||
}
|
||||
// A failed file stays failed even when the key reappears. Recovery requires
|
||||
// an explicit publisher; a shallow HEAD cannot prove content integrity.
|
||||
if f.status == "failed" {
|
||||
if f.status == FileFailed {
|
||||
return &Finding{Kind: "failed_present", Key: key, Action: "report"}, nil
|
||||
}
|
||||
return nil, nil
|
||||
|
||||
Reference in New Issue
Block a user