forked from mtusi2026/mtusi2026lab6
feat(reconciler): lab6 trace lease recovery and S3 cleanup
This commit is contained in:
@@ -7,8 +7,11 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
const advisoryLockKey int64 = 0x4D5455534904
|
||||
@@ -64,8 +67,12 @@ func (r *Reconciler) Run(ctx context.Context, interval time.Duration) {
|
||||
}
|
||||
|
||||
func (r *Reconciler) tick(ctx context.Context) {
|
||||
ctx, span := telemetry.Tracer("reconciler").Start(ctx, "reconciler.tick")
|
||||
defer span.End()
|
||||
|
||||
acquired, release, err := r.acquireLock(ctx)
|
||||
if err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка получения блокировки: %v", err)
|
||||
return
|
||||
}
|
||||
@@ -100,9 +107,10 @@ func (r *Reconciler) acquireLock(ctx context.Context) (bool, func(), error) {
|
||||
}
|
||||
|
||||
release := func() {
|
||||
unlockCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
unlockCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
|
||||
defer cancel()
|
||||
if _, err := conn.Exec(unlockCtx, `SELECT pg_advisory_unlock($1)`, advisoryLockKey); err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка снятия блокировки: %v", err)
|
||||
}
|
||||
conn.Release()
|
||||
@@ -111,8 +119,12 @@ func (r *Reconciler) acquireLock(ctx context.Context) (bool, func(), error) {
|
||||
}
|
||||
|
||||
func (r *Reconciler) sweepExpiredLeases(ctx context.Context) int {
|
||||
ctx, span := telemetry.Tracer("reconciler").Start(ctx, "reconciler.leases")
|
||||
defer span.End()
|
||||
|
||||
expired, err := r.attachments.ListExpiredLeases(ctx, r.batchSize)
|
||||
if err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка получения истёкших аренд: %v", err)
|
||||
return 0
|
||||
}
|
||||
@@ -120,6 +132,7 @@ func (r *Reconciler) sweepExpiredLeases(ctx context.Context) int {
|
||||
recovered := 0
|
||||
for _, a := range expired {
|
||||
if err := r.attachments.ResetLease(ctx, a.ID); err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка сброса аренды для %s: %v", a.ID, err)
|
||||
continue
|
||||
}
|
||||
@@ -137,6 +150,7 @@ func (r *Reconciler) sweepExpiredLeases(ctx context.Context) int {
|
||||
CreatedAt: time.Now().UTC(),
|
||||
})
|
||||
if err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка постановки в очередь восстановленной задачи %s: %v", a.ID, err)
|
||||
continue
|
||||
}
|
||||
@@ -147,8 +161,12 @@ func (r *Reconciler) sweepExpiredLeases(ctx context.Context) int {
|
||||
|
||||
// Чистим staging в S3 для готовых задач
|
||||
func (r *Reconciler) sweepStaging(ctx context.Context) int {
|
||||
ctx, span := telemetry.Tracer("reconciler").Start(ctx, "reconciler.staging")
|
||||
defer span.End()
|
||||
|
||||
toCleanup, err := r.attachments.ListStagingToCleanup(ctx, r.batchSize)
|
||||
if err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка получения списка staging для очистки: %v", err)
|
||||
return 0
|
||||
}
|
||||
@@ -156,10 +174,12 @@ func (r *Reconciler) sweepStaging(ctx context.Context) int {
|
||||
deleted := 0
|
||||
for _, a := range toCleanup {
|
||||
if err := r.storage.Delete(ctx, a.StagingKey); err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка удаления объекта staging %s: %v", a.StagingKey, err)
|
||||
continue
|
||||
}
|
||||
if err := r.attachments.ClearStagingKey(ctx, a.ID); err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка очистки ключа staging %s: %v", a.ID, err)
|
||||
continue
|
||||
}
|
||||
@@ -170,8 +190,12 @@ func (r *Reconciler) sweepStaging(ctx context.Context) int {
|
||||
|
||||
// Удаляем старые сиротские объекты (без записи в БД)
|
||||
func (r *Reconciler) sweepOrphans(ctx context.Context) int {
|
||||
ctx, span := telemetry.Tracer("reconciler").Start(ctx, "reconciler.orphans")
|
||||
defer span.End()
|
||||
|
||||
objects, err := r.storage.List(ctx, "chats/")
|
||||
if err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка получения списка объектов: %v", err)
|
||||
return 0
|
||||
}
|
||||
@@ -192,6 +216,7 @@ func (r *Reconciler) sweepOrphans(ctx context.Context) int {
|
||||
|
||||
existing, err := r.attachments.ExistingKeys(ctx, keys)
|
||||
if err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка получения существующих ключей: %v", err)
|
||||
return 0
|
||||
}
|
||||
@@ -207,6 +232,7 @@ func (r *Reconciler) sweepOrphans(ctx context.Context) int {
|
||||
continue
|
||||
}
|
||||
if err := r.storage.Delete(ctx, k); err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка удаления сиротского объекта %s: %v", k, err)
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user