fix: harden attachment cleanup

This commit is contained in:
2026-10-09 13:27:58 +03:00
parent f8c31de7c0
commit 3fde1c324c
8 changed files with 134 additions and 24 deletions
+8 -6
View File
@@ -327,6 +327,9 @@ func runConsistency(arguments []string, output io.Writer) error {
if err != nil {
return err
}
if _, err := pool.Exec(ctx, `UPDATE attachments SET status = 'pending', ready_at = NULL WHERE id = $1`, abandoned.ID); err != nil {
return fmt.Errorf("prepare abandoned pending attachment: %w", err)
}
keys, err := attachmentKeys(ctx, pool, []string{unused.ID, inUse.ID, abandoned.ID})
if err != nil {
return err
@@ -358,8 +361,7 @@ func runConsistency(arguments []string, output io.Writer) error {
attachmentExists(ctx, pool, abandoned.ID)
if _, err := pool.Exec(ctx, `
UPDATE attachments
SET created_at = clock_timestamp() - INTERVAL '2 hours',
ready_at = clock_timestamp() - INTERVAL '2 hours'
SET created_at = clock_timestamp() - INTERVAL '2 hours'
WHERE id = $1`, abandoned.ID); err != nil {
return fmt.Errorf("age abandoned attachment: %w", err)
}
@@ -394,7 +396,7 @@ func runConsistency(arguments []string, output io.Writer) error {
FreshApply: freshRun, DryRun: dryRunResult, Apply: applyResult, RepeatedApply: repeatedResult,
FreshPreserved: freshPreserved, OrphanDeleted: !objectExists(ctx, objects, orphanKey),
UnusedDeleted: !attachmentExists(ctx, pool, unused.ID), InUsePreserved: attachmentExists(ctx, pool, inUse.ID),
AbandonedDeleted: !attachmentExists(ctx, pool, abandoned.ID) && !objectExists(ctx, objects, keys[abandoned.ID]),
AbandonedDeleted: !attachmentExists(ctx, pool, abandoned.ID) && !objectExists(ctx, objects, keys[abandoned.ID]),
TotalDurationMillis: time.Since(started).Milliseconds(),
}
if !result.FreshPreserved || !result.OrphanDeleted || !result.UnusedDeleted || !result.InUsePreserved || !result.AbandonedDeleted ||
@@ -414,7 +416,7 @@ func hasAnomalies(report reconcile.Report) bool {
return report.FreshOrphanObjects != 0 || report.OldOrphanObjects != 0 ||
report.FreshMissingObjects != 0 || report.ReferencedMissingObjects != 0 ||
report.OldUnreferencedMetadata != 0 || report.OldPendingAttachments != 0 ||
report.OldUnreferencedAttachments != 0
report.OldUnreferencedAttachments != 0 || report.DeletingAttachments != 0
}
func attachmentKeys(ctx context.Context, pool *pgxpool.Pool, ids []string) (map[string]string, error) {
@@ -580,11 +582,11 @@ func waitForReplicas(ctx context.Context, prometheusURL string, expected int) er
}
func activeReplicasQuery() string {
return `count(timestamp(process_resident_memory_bytes{job="messenger"}) > time() - 10)`
return `count(last_over_time(process_resident_memory_bytes{job="messenger"}[10s]))`
}
func freshMetricQuery(metric string) string {
return fmt.Sprintf(`sum(%[1]s{job="messenger"} and on(instance) (timestamp(%[1]s{job="messenger"}) > time() - 10))`, metric)
return fmt.Sprintf(`sum(last_over_time(%s{job="messenger"}[10s]))`, metric)
}
func prometheusValue(ctx context.Context, baseURL, query string) (int64, error) {