lab5: асинхронная очередь, worker и идемпотентный pipeline

API принимает 202 после durable staging и TX intent (job+outbox); отдельные relay и worker публикуют/обрабатывают задания с publisher confirm, lease, retry/backoff, DLQ и replay. Добавлены версии pipeline, варианты thumbnail/crop/optimized, presign в verified staging, status-aware sweeper и admission control.
This commit is contained in:
Vladislav Kunetskiy
2026-10-30 22:36:52 +03:00
parent 6c5ba026f1
commit 4bdcac3dfa
95 changed files with 8466 additions and 764 deletions
+57 -70
View File
@@ -15,73 +15,71 @@ import (
"mtusi-messenger/internal/service/attachment/mocks"
)
func TestSweepReconcilesPendingAndOrphanObjects(t *testing.T) {
repo := mocks.NewMockRepository(t)
func TestSweepAppliesStatusAwareRetention(t *testing.T) {
repo := mocks.NewMockSweepRepository(t)
locker := mocks.NewMockLockStore(t)
store := newFakeStore()
stale := time.Now().UTC().Add(-time.Hour)
stale := time.Now().UTC().Add(-2 * time.Hour)
presign := domain.Attachment{
ID: uuid.New(), ObjectKey: "staging/presign/chat/" + uuid.NewString(),
Status: domain.AttachmentPending, CreatedAt: stale,
ID: uuid.New(), StagingKey: "staging/presign/chat/" + uuid.NewString(),
Status: domain.AttachmentPending,
}
proxyRecoverable := domain.Attachment{
ID: uuid.New(), ObjectKey: "attachments/chat/" + uuid.NewString(),
SizeBytes: 3, SHA256: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
Status: domain.AttachmentPending, CreatedAt: stale,
staleStaging := domain.StaleStaging{
AttachmentID: uuid.New(), StagingKey: "staging/uploads/chat/ready", Status: domain.AttachmentReady,
}
proxyMissing := domain.Attachment{
ID: uuid.New(), ObjectKey: "attachments/chat/" + uuid.NewString(),
Status: domain.AttachmentPending, CreatedAt: stale,
}
readyKey := "attachments/chat/ready"
activeStaging := "staging/uploads/chat/active"
readyKey := "attachments/chat/ready/original"
variantKey := "attachments/chat/ready/variants/v1/thumbnail.jpg"
store.objects[presign.ObjectKey] = storedObject{data: []byte("x"), lastModified: stale}
store.objects[proxyRecoverable.ObjectKey] = storedObject{
data: []byte("abc"),
lastModified: stale,
metadata: map[string]string{"sha256": proxyRecoverable.SHA256},
for _, key := range []string{
presign.StagingKey, staleStaging.StagingKey, activeStaging, readyKey, variantKey,
"attachments/chat/orphan", "staging/orphan",
} {
store.objects[key] = storedObject{data: []byte("x"), lastModified: stale}
}
store.objects[readyKey] = storedObject{data: []byte("ready"), lastModified: stale}
store.objects["attachments/chat/orphan"] = storedObject{data: []byte("orphan"), lastModified: stale}
store.objects["staging/abandoned"] = storedObject{data: []byte("stale"), lastModified: stale}
store.objects["staging/fresh"] = storedObject{data: []byte("fresh"), lastModified: time.Now().UTC()}
store.objects["staging/fresh"] = storedObject{data: []byte("x"), lastModified: time.Now().UTC()}
repo.EXPECT().ListStalePending(mock.Anything, mock.Anything, int32(100)).
Return([]domain.Attachment{presign, proxyRecoverable, proxyMissing}, nil).Once()
repo.EXPECT().ListPendingPresign(mock.Anything, mock.Anything, 100).
Return([]domain.Attachment{presign}, nil).Once()
repo.EXPECT().DeletePending(mock.Anything, presign.ID).Return(nil).Once()
repo.EXPECT().MarkReady(mock.Anything, proxyRecoverable.ID, proxyRecoverable.ObjectKey).
Return(domain.Attachment{ID: proxyRecoverable.ID, Status: domain.AttachmentReady}, nil).Once()
repo.EXPECT().DeletePending(mock.Anything, proxyMissing.ID).Return(nil).Once()
repo.EXPECT().ListKeys(mock.Anything, int32(1000), int32(0)).
Return([]domain.AttachmentKey{
{ID: proxyRecoverable.ID, ObjectKey: proxyRecoverable.ObjectKey, Status: domain.AttachmentReady},
{ID: uuid.New(), ObjectKey: readyKey, Status: domain.AttachmentReady},
}, nil).Once()
locker.EXPECT().AcquireSweepLock(mock.Anything).
Return(func() {}, true, nil).Once()
repo.EXPECT().ListStaleStaging(mock.Anything, mock.Anything, mock.Anything, 100).
Return([]domain.StaleStaging{staleStaging}, nil).Once()
repo.EXPECT().MarkStagingDeleted(mock.Anything, staleStaging.AttachmentID, staleStaging.StagingKey).
Return(nil).Once()
repo.EXPECT().ListReferencedKeys(mock.Anything, "", 1000).
Return([]string{activeStaging, readyKey, variantKey}, nil).Once()
locker.EXPECT().AcquireSweepLock(mock.Anything).Return(func() {}, true, nil).Once()
sweeper := attachment.NewSweeper(repo, store, locker, time.Minute, time.Hour, slog.Default())
sweeper := attachment.NewSweeper(
repo, store, locker,
10*time.Minute, time.Hour, 24*time.Hour, time.Minute, slog.Default(),
)
stats, err := sweeper.SweepOnce(t.Context())
require.NoError(t, err)
require.Equal(t, 1, stats.Recovered)
require.Equal(t, 2, stats.RowsDeleted)
require.Equal(t, 3, stats.ObjectsDeleted)
require.Equal(t, 1, stats.RowsDeleted)
require.Equal(t, 1, stats.StagingCleared)
require.Equal(t, 4, stats.ObjectsDeleted)
require.NotContains(t, store.objects, presign.ObjectKey)
require.NotContains(t, store.objects, presign.StagingKey)
require.NotContains(t, store.objects, staleStaging.StagingKey)
require.NotContains(t, store.objects, "attachments/chat/orphan")
require.NotContains(t, store.objects, "staging/abandoned")
require.Contains(t, store.objects, proxyRecoverable.ObjectKey)
require.NotContains(t, store.objects, "staging/orphan")
require.Contains(t, store.objects, activeStaging)
require.Contains(t, store.objects, readyKey)
require.Contains(t, store.objects, variantKey)
require.Contains(t, store.objects, "staging/fresh")
}
func TestSweepSkipsWhenAnotherReplicaHoldsTheLock(t *testing.T) {
repo := mocks.NewMockRepository(t)
repo := mocks.NewMockSweepRepository(t)
locker := mocks.NewMockLockStore(t)
sweeper := attachment.NewSweeper(repo, newFakeStore(), locker, time.Minute, time.Hour, slog.Default())
sweeper := attachment.NewSweeper(
repo, newFakeStore(), locker,
time.Minute, time.Hour, 24*time.Hour, time.Minute, slog.Default(),
)
locker.EXPECT().AcquireSweepLock(mock.Anything).Return(nil, false, nil).Once()
@@ -90,37 +88,26 @@ func TestSweepSkipsWhenAnotherReplicaHoldsTheLock(t *testing.T) {
require.Zero(t, stats)
}
func TestSweepKeepsObjectWhenMarkReadyFails(t *testing.T) {
repo := mocks.NewMockRepository(t)
func TestSweepAbortsObjectCollectionWhenKeysCannotBeRead(t *testing.T) {
repo := mocks.NewMockSweepRepository(t)
locker := mocks.NewMockLockStore(t)
store := newFakeStore()
stale := time.Now().UTC().Add(-time.Hour)
row := domain.Attachment{
ID: uuid.New(), ObjectKey: "attachments/chat/" + uuid.NewString(),
SizeBytes: 3, SHA256: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
Status: domain.AttachmentPending, CreatedAt: stale,
}
orphan := "attachments/chat/orphan"
store.objects[orphan] = storedObject{data: []byte("x"), lastModified: time.Now().UTC().Add(-2 * time.Hour)}
store.objects[row.ObjectKey] = storedObject{
data: []byte("abc"),
lastModified: stale,
metadata: map[string]string{"sha256": row.SHA256},
}
repo.EXPECT().ListStalePending(mock.Anything, mock.Anything, int32(100)).
Return([]domain.Attachment{row}, nil).Once()
repo.EXPECT().MarkReady(mock.Anything, row.ID, row.ObjectKey).
Return(domain.Attachment{}, context.DeadlineExceeded).Once()
repo.EXPECT().ListKeys(mock.Anything, int32(1000), int32(0)).
Return([]domain.AttachmentKey{{ID: row.ID, ObjectKey: row.ObjectKey, Status: domain.AttachmentPending}}, nil).Once()
repo.EXPECT().ListPendingPresign(mock.Anything, mock.Anything, 100).Return(nil, nil).Once()
repo.EXPECT().ListStaleStaging(mock.Anything, mock.Anything, mock.Anything, 100).Return(nil, nil).Once()
repo.EXPECT().ListReferencedKeys(mock.Anything, "", 1000).
Return(nil, context.DeadlineExceeded).Once()
locker.EXPECT().AcquireSweepLock(mock.Anything).Return(func() {}, true, nil).Once()
sweeper := attachment.NewSweeper(repo, store, locker, time.Minute, time.Hour, slog.Default())
sweeper := attachment.NewSweeper(
repo, store, locker,
time.Minute, time.Hour, 24*time.Hour, time.Minute, slog.Default(),
)
stats, err := sweeper.SweepOnce(t.Context())
require.NoError(t, err)
require.Zero(t, stats.Recovered)
require.Zero(t, stats.RowsDeleted)
require.Contains(t, store.objects, row.ObjectKey)
_, err := sweeper.SweepOnce(t.Context())
require.ErrorIs(t, err, context.DeadlineExceeded)
require.Contains(t, store.objects, orphan, "fail-closed: no deletion when the key set is unknown")
}