feat(reconciler): lab5 recover expired leases and cleanup staging

This commit is contained in:
Голиков Maximillian Антон
2026-11-11 12:40:53 +03:00
parent f748c09e10
commit d40b339906
4 changed files with 141 additions and 63 deletions
+60 -20
View File
@@ -2,6 +2,7 @@ package service
import (
"context"
"encoding/json"
"log"
"time"
@@ -15,6 +16,7 @@ const advisoryLockKey int64 = 0x4D5455534904
type Reconciler struct {
attachments domain.AttachmentRepository
storage domain.ObjectStorage
outbox domain.OutboxRepository
pool *pgxpool.Pool
gracePeriod time.Duration
batchSize int
@@ -23,6 +25,7 @@ type Reconciler struct {
func NewReconciler(
attachments domain.AttachmentRepository,
storage domain.ObjectStorage,
outbox domain.OutboxRepository,
pool *pgxpool.Pool,
gracePeriod time.Duration,
) *Reconciler {
@@ -32,6 +35,7 @@ func NewReconciler(
return &Reconciler{
attachments: attachments,
storage: storage,
outbox: outbox,
pool: pool,
gracePeriod: gracePeriod,
batchSize: 100,
@@ -45,14 +49,13 @@ func (r *Reconciler) Run(ctx context.Context, interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
log.Printf("reconciler: started, interval=%s grace=%s", interval, r.gracePeriod)
log.Printf("reconciler: запущен (interval=%s, grace=%s)", interval, r.gracePeriod)
r.tick(ctx)
for {
select {
case <-ctx.Done():
log.Println("reconciler: stopped")
log.Println("reconciler: остановлен")
return
case <-ticker.C:
r.tick(ctx)
@@ -63,7 +66,7 @@ func (r *Reconciler) Run(ctx context.Context, interval time.Duration) {
func (r *Reconciler) tick(ctx context.Context) {
acquired, release, err := r.acquireLock(ctx)
if err != nil {
log.Printf("reconciler: acquire lock: %v", err)
log.Printf("reconciler: ошибка получения блокировки: %v", err)
return
}
if !acquired {
@@ -71,11 +74,12 @@ func (r *Reconciler) tick(ctx context.Context) {
}
defer release()
pendingDeleted := r.sweepPending(ctx)
leasesReset := r.sweepExpiredLeases(ctx)
stagingDeleted := r.sweepStaging(ctx)
orphansDeleted := r.sweepOrphans(ctx)
if pendingDeleted > 0 || orphansDeleted > 0 {
log.Printf("reconciler: sweep done pending=%d orphans=%d", pendingDeleted, orphansDeleted)
if leasesReset > 0 || stagingDeleted > 0 || orphansDeleted > 0 {
log.Printf("reconciler: очистка завершена (восстановлено_задач=%d, удалено_staging=%d, удалено_сирот=%d)", leasesReset, stagingDeleted, orphansDeleted)
}
}
@@ -99,29 +103,64 @@ func (r *Reconciler) acquireLock(ctx context.Context) (bool, func(), error) {
unlockCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if _, err := conn.Exec(unlockCtx, `SELECT pg_advisory_unlock($1)`, advisoryLockKey); err != nil {
log.Printf("reconciler: unlock: %v", err)
log.Printf("reconciler: ошибка снятия блокировки: %v", err)
}
conn.Release()
}
return true, release, nil
}
func (r *Reconciler) sweepPending(ctx context.Context) int {
cutoff := time.Now().UTC().Add(-r.gracePeriod)
stale, err := r.attachments.ListStalePending(ctx, cutoff, r.batchSize)
func (r *Reconciler) sweepExpiredLeases(ctx context.Context) int {
expired, err := r.attachments.ListExpiredLeases(ctx, r.batchSize)
if err != nil {
log.Printf("reconciler: list stale pending: %v", err)
log.Printf("reconciler: ошибка получения истёкших аренд: %v", err)
return 0
}
recovered := 0
for _, a := range expired {
if err := r.attachments.ResetLease(ctx, a.ID); err != nil {
log.Printf("reconciler: ошибка сброса аренды для %s: %v", a.ID, err)
continue
}
payload, _ := json.Marshal(domain.JobMessage{
AttachmentID: a.ID,
PipelineVersion: a.PipelineVersion,
Attempt: a.Attempts + 1,
})
err := r.outbox.EnqueueDirect(ctx, &domain.OutboxEntry{
AggregateType: domain.OutboxAggregateAttachment,
AggregateID: a.ID,
EventType: domain.OutboxEventProcess,
Payload: payload,
CreatedAt: time.Now().UTC(),
})
if err != nil {
log.Printf("reconciler: ошибка постановки в очередь восстановленной задачи %s: %v", a.ID, err)
continue
}
recovered++
}
return recovered
}
// Чистим staging в S3 для готовых задач
func (r *Reconciler) sweepStaging(ctx context.Context) int {
toCleanup, err := r.attachments.ListStagingToCleanup(ctx, r.batchSize)
if err != nil {
log.Printf("reconciler: ошибка получения списка staging для очистки: %v", err)
return 0
}
deleted := 0
for _, a := range stale {
if err := r.storage.Delete(ctx, a.ObjectKey); err != nil {
log.Printf("reconciler: delete object %s: %v", a.ObjectKey, err)
for _, a := range toCleanup {
if err := r.storage.Delete(ctx, a.StagingKey); err != nil {
log.Printf("reconciler: ошибка удаления объекта staging %s: %v", a.StagingKey, err)
continue
}
if err := r.attachments.DeletePending(ctx, a.ID); err != nil {
log.Printf("reconciler: delete pending row %s: %v", a.ID, err)
if err := r.attachments.ClearStagingKey(ctx, a.ID); err != nil {
log.Printf("reconciler: ошибка очистки ключа staging %s: %v", a.ID, err)
continue
}
deleted++
@@ -129,10 +168,11 @@ func (r *Reconciler) sweepPending(ctx context.Context) int {
return deleted
}
// Удаляем старые сиротские объекты (без записи в БД)
func (r *Reconciler) sweepOrphans(ctx context.Context) int {
objects, err := r.storage.List(ctx, "chats/")
if err != nil {
log.Printf("reconciler: list objects: %v", err)
log.Printf("reconciler: ошибка получения списка объектов: %v", err)
return 0
}
if len(objects) == 0 {
@@ -152,7 +192,7 @@ func (r *Reconciler) sweepOrphans(ctx context.Context) int {
existing, err := r.attachments.ExistingKeys(ctx, keys)
if err != nil {
log.Printf("reconciler: existing keys: %v", err)
log.Printf("reconciler: ошибка получения существующих ключей: %v", err)
return 0
}
@@ -167,7 +207,7 @@ func (r *Reconciler) sweepOrphans(ctx context.Context) int {
continue
}
if err := r.storage.Delete(ctx, k); err != nil {
log.Printf("reconciler: delete orphan %s: %v", k, err)
log.Printf("reconciler: ошибка удаления сиротского объекта %s: %v", k, err)
continue
}
deleted++