lab5: закрытие остаточных замечаний
Validate tRNS semantics and bind PLTE entry count to the IHDR bit depth so malformed PNGs are rejected with 415 instead of failing in the worker. Refuse replay of an older failed version while a newer pipeline job exists, preventing loss of the shared staging source, and retire version-stale queued jobs as superseded instead of republishing them forever.
This commit is contained in:
@@ -578,6 +578,23 @@ func (r *PipelineRepository) ClaimJob(
|
||||
|
||||
switch {
|
||||
case job.PipelineVersion < attachment.PipelineVersion:
|
||||
// A queued job left behind by an older version (for example a
|
||||
// replay accepted before the newer version committed) can never
|
||||
// make progress. Retire it instead of acknowledging and
|
||||
// republishing it through reconciliation forever.
|
||||
if job.Status == domain.JobQueued {
|
||||
if _, err := tx.Exec(ctx, `
|
||||
UPDATE attachment_jobs
|
||||
SET status = 'superseded', updated_at = now(), completed_at = now()
|
||||
WHERE job_id = $1 AND status = 'queued'`, job.ID); err != nil {
|
||||
return fmt.Errorf("supersede version-stale job: %w", mapError(err))
|
||||
}
|
||||
|
||||
if err := insertEventTx(ctx, tx, job.ID, domain.EventSuperseded, job.Attempt, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
result.Outcome = domain.ClaimVersionStale
|
||||
|
||||
return nil
|
||||
@@ -1455,6 +1472,24 @@ func (r *PipelineRepository) ReplayJob(
|
||||
return fmt.Errorf("%w: job is superseded by a newer pipeline version", domain.ErrConflict)
|
||||
}
|
||||
|
||||
// A newer version may still be unpublished (attachment.pipeline_version
|
||||
// changes only when it commits), so compare against job rows too. An
|
||||
// older replay must not compete with a newer job for the same staging
|
||||
// source or return the attachment to an older state.
|
||||
var newerJob bool
|
||||
|
||||
if err := tx.QueryRow(ctx, `
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM attachment_jobs
|
||||
WHERE attachment_id = $1 AND pipeline_version > $2
|
||||
)`, job.AttachmentID, job.PipelineVersion).Scan(&newerJob); err != nil {
|
||||
return fmt.Errorf("check newer pipeline version: %w", mapError(err))
|
||||
}
|
||||
|
||||
if newerJob {
|
||||
return fmt.Errorf("%w: a newer pipeline version already has a job", domain.ErrConflict)
|
||||
}
|
||||
|
||||
nextAttempt := job.Attempt + 1
|
||||
|
||||
_, err = tx.Exec(ctx, `
|
||||
|
||||
@@ -413,6 +413,90 @@ func (s *PipelineSuite) TestReplayNewerFailedVersionKeepsReadyServingVersion() {
|
||||
s.Nil(current.ErrorCode)
|
||||
}
|
||||
|
||||
// Replaying an older failed version is refused while a newer job exists,
|
||||
// even before that newer version becomes the published serving version.
|
||||
func (s *PipelineSuite) TestReplayOlderFailedVersionRefusedWhenNewerJobExists() {
|
||||
alice := s.createUser("alice")
|
||||
chat := s.createChat(alice)
|
||||
repo := s.store.Pipeline()
|
||||
|
||||
draft, job := s.attachmentDraft(chat, alice, 1)
|
||||
_, err := repo.CreateQueued(s.T().Context(), draft, job, domain.AdmissionLimits{})
|
||||
s.Require().NoError(err)
|
||||
|
||||
claim, err := repo.ClaimJob(s.T().Context(), domain.NewJobMessage(job), "lease-1", time.Minute)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(domain.ClaimAcquired, claim.Outcome)
|
||||
|
||||
failed, err := repo.FailJob(s.T().Context(), job.ID, 1, "lease-1", "invalid_image")
|
||||
s.Require().NoError(err)
|
||||
s.True(failed)
|
||||
|
||||
reprocessID, err := uuid.NewV7()
|
||||
s.Require().NoError(err)
|
||||
|
||||
_, err = repo.Reprocess(s.T().Context(), draft.ID, 2, 5, reprocessID)
|
||||
s.Require().NoError(err)
|
||||
|
||||
_, err = repo.ReplayJob(s.T().Context(), job.ID, 1)
|
||||
s.Require().ErrorIs(err, domain.ErrConflict)
|
||||
|
||||
var v1Status string
|
||||
s.Require().NoError(s.pool.QueryRow(s.T().Context(),
|
||||
`SELECT status FROM attachment_jobs WHERE job_id = $1`, job.ID).Scan(&v1Status))
|
||||
s.Equal(string(domain.JobFailed), v1Status)
|
||||
}
|
||||
|
||||
// A queued job for an older version is retired on claim instead of being
|
||||
// acknowledged and republished by reconciliation forever.
|
||||
func (s *PipelineSuite) TestClaimRetiresVersionStaleQueuedJob() {
|
||||
alice := s.createUser("alice")
|
||||
chat := s.createChat(alice)
|
||||
repo := s.store.Pipeline()
|
||||
|
||||
draft, job := s.attachmentDraft(chat, alice, 1)
|
||||
_, err := repo.CreateQueued(s.T().Context(), draft, job, domain.AdmissionLimits{})
|
||||
s.Require().NoError(err)
|
||||
|
||||
reprocessID, err := uuid.NewV7()
|
||||
s.Require().NoError(err)
|
||||
|
||||
reprocess, err := repo.Reprocess(s.T().Context(), draft.ID, 2, 5, reprocessID)
|
||||
s.Require().NoError(err)
|
||||
|
||||
claim, err := repo.ClaimJob(s.T().Context(), domain.NewJobMessage(reprocess), "lease-2", time.Minute)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(domain.ClaimAcquired, claim.Outcome)
|
||||
|
||||
variants := []domain.Variant{
|
||||
{
|
||||
Name: domain.VariantThumbnail, ContentType: "image/jpeg", SizeBytes: 11,
|
||||
Width: 256, Height: 160, SHA256: strings.Repeat("d", 64),
|
||||
ObjectKey: domain.VariantObjectKey(chat.ID, draft.ID, 2, domain.VariantThumbnail),
|
||||
},
|
||||
}
|
||||
|
||||
ok, err := repo.CompleteJob(s.T().Context(), reprocess.ID, 1, "lease-2", variants)
|
||||
s.Require().NoError(err)
|
||||
s.True(ok)
|
||||
|
||||
// Simulate a legacy/racing replay that left the old version queued.
|
||||
_, err = s.pool.Exec(s.T().Context(), `
|
||||
UPDATE attachment_jobs
|
||||
SET status = 'queued', attempt = 1, last_error_code = NULL
|
||||
WHERE job_id = $1`, job.ID)
|
||||
s.Require().NoError(err)
|
||||
|
||||
stale, err := repo.ClaimJob(s.T().Context(), domain.NewJobMessage(job), "lease-3", time.Minute)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(domain.ClaimVersionStale, stale.Outcome)
|
||||
|
||||
var status string
|
||||
s.Require().NoError(s.pool.QueryRow(s.T().Context(),
|
||||
`SELECT status FROM attachment_jobs WHERE job_id = $1`, job.ID).Scan(&status))
|
||||
s.Equal(string(domain.JobSuperseded), status)
|
||||
}
|
||||
|
||||
func (s *PipelineSuite) TestReconcileQueuedJobsRepublishesLostIntent() {
|
||||
alice := s.createUser("alice")
|
||||
chat := s.createChat(alice)
|
||||
|
||||
Reference in New Issue
Block a user