forked from mtusi2026/mtusi2026lab6
Compare commits
5
Commits
0b237618c0
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8704718f35 | ||
|
|
8c8a0302c9 | ||
|
|
323d1e9294 | ||
|
|
c915547449 | ||
|
|
f23855f786 |
@@ -60,7 +60,7 @@ API ─────────────────────────
|
||||
Grafana
|
||||
```
|
||||
|
||||
Приложения экспортируют OTLP только в Collector. Они не знают адреса Loki и Tempo. Collector выполняет `memory_limiter` до `batch`, ограничивает очереди exporter и прекращает retry через 30 секунд. Недоступность telemetry backend поэтому не блокирует бизнес-запросы и не вызывает неограниченный рост памяти.
|
||||
Приложения экспортируют OTLP только в Collector. Они не знают адреса Loki и Tempo. Collector выполняет `memory_limiter` до `batch`, ограничивает очереди exporter и прекращает retry через 30 секунд. Evidence проверяет queue size/capacity, failures и drops отдельно для `otlp_grpc/tempo` и `otlp_http/loki`, а память — по пиковому RSS за двухминутное окно. Недоступность telemetry backend поэтому не блокирует бизнес-запросы и не вызывает неограниченный рост памяти.
|
||||
|
||||
Prometheus, Loki, Tempo и Grafana используют отдельные volumes. Retention локальных logs/traces — 7 суток; Prometheus хранит 7 суток. Collector является транспортом и процессором, а не долговременным хранилищем.
|
||||
|
||||
@@ -107,7 +107,8 @@ Grafana provisioning создаёт Prometheus, Loki и Tempo без ручно
|
||||
| --- | --- |
|
||||
| HTTP requests | `messenger_http_requests_total{method,route,status_class}`, requests |
|
||||
| HTTP duration | `messenger_http_request_duration_seconds_bucket`, seconds |
|
||||
| Jobs processed / failed | `messenger_attachment_worker_jobs_total{job_type,result}`, jobs |
|
||||
| Delivery outcomes | `messenger_attachment_worker_jobs_total{job_type,result}`, deliveries |
|
||||
| Terminal job transitions | `messenger_attachment_terminal_transitions_total{status}`, first durable outcome per job |
|
||||
| Job duration | `messenger_attachment_worker_job_duration_seconds_bucket`, seconds |
|
||||
| Time to ready | `messenger_attachment_time_to_ready_seconds_bucket`, seconds |
|
||||
| Queue states | `messenger_attachment_jobs_count{status}`, jobs |
|
||||
@@ -137,7 +138,7 @@ error budget = 0.5%
|
||||
|
||||
### Время готовности вложений
|
||||
|
||||
SLI — доля завершённых attachment jobs, достигших `ready` не позднее 30 секунд. `failed` и ready позже 30 секунд являются bad events.
|
||||
SLI — доля уникальных durable attachment jobs, чей первый terminal outcome равен `ready` и наступил не позднее 30 секунд после acceptance. Время acceptance назначает PostgreSQL при создании job, поэтому начало и конец интервала принадлежат одному clock domain. PostgreSQL записывает первый outcome в append-only ledger в той же транзакции, что и переход job. Миграция переносит уже завершённые jobs с `accepted_at = created_at` и `terminal_at = updated_at`. Повторная delivery и ручной replay не меняют уже учтённый результат: job, впервые завершившаяся `failed`, остаётся bad event даже после успешного replay.
|
||||
|
||||
```text
|
||||
bad = failed + ready later than 30s
|
||||
@@ -147,6 +148,8 @@ SLO = 99% за 30 дней
|
||||
error budget = 1%
|
||||
```
|
||||
|
||||
`messenger_attachment_terminal_transitions_total` и cumulative buckets `messenger_attachment_time_to_ready_seconds_bucket` экспортируются из ledger. Bucket `le="+Inf"` является durable количеством первых outcomes `ready` и используется как знаменатель ready jobs.
|
||||
|
||||
30 секунд отделяют нормальную асинхронную обработку от видимой пользователю задержки и существенно превышают обычное время небольшого синтетического изображения.
|
||||
|
||||
### Multi-window burn-rate alerts
|
||||
@@ -226,14 +229,14 @@ make test-lab6
|
||||
|
||||
## Результаты прогона
|
||||
|
||||
Полный прогон `2026-11-01` сохранён локально в `evidence/runtime/lab6-20261101T135321Z` и прошёл schema validation без ошибок. Обезличенная сводка включена в `evidence/lab6-results.json`. Время обнаружения является верхней границей: сценарий опрашивает alert после выдержки заданной длительности отказа.
|
||||
Полный прогон `2026-11-04` сохранён локально в `evidence/runtime/lab6-20261104T174049Z` и прошёл schema validation без ошибок. Обезличенная сводка включена в `evidence/lab6-results.json`. Время обнаружения является верхней границей: сценарий опрашивает alert после выдержки заданной длительности отказа.
|
||||
|
||||
| Инцидент | Наблюдение | Обнаружение | Восстановление |
|
||||
| --- | --- | ---: | ---: |
|
||||
| Worker, короткий | `AttachmentWorkerUnavailable` firing, очередь затем обработана | не более 76 с | alert resolved через 15 с, полное наблюдение 36 с |
|
||||
| PostgreSQL | operational 5xx и fast burn-rate firing, 4 DB error traces связаны с логами | не более 76 с | operational alert resolved через 127 с, API стабилен через 148 с |
|
||||
| Worker, длительный | oldest queued age рос; backlog, fast и slow burn alerts firing | не более 180 с | alert resolved через 15 с, backlog обработан за 65 с |
|
||||
| Loki и Tempo | Collector использовал ограниченные retry/queue, бизнес-путь продолжил работу | 30 с отказа | `/health/ready` вернул 200, backends healthy через 31 с |
|
||||
| Worker, короткий | `AttachmentWorkerUnavailable` firing, очередь затем обработана | не более 76 с | alert resolved через 13 с, полное наблюдение 33 с |
|
||||
| PostgreSQL | operational 5xx и fast burn-rate firing; сохранены 3 DB error traces, один связан с API-логом | не более 78 с | operational alert resolved через 134 с, API стабилен через 154 с |
|
||||
| Worker, длительный | oldest queued age рос; backlog, fast и slow burn alerts firing | не более 181 с | alert resolved через 20 с, backlog обработан за 91 с |
|
||||
| Loki и Tempo | очереди выросли до 15/512 и 16/512, затем дренировались до нуля; peak RSS 226.7 MB | 30 с отказа | `/health/ready` вернул 200, backends healthy через 31 с |
|
||||
|
||||
Перед первым fault список firing alerts был пуст. Burn-rate alerts после инцидента закономерно остаются активными дольше оперативных alerts: их длинные окна сохраняют уже потраченный error budget. Это не считается ложным срабатыванием следующей фазы.
|
||||
|
||||
|
||||
+43
-22
@@ -1,46 +1,46 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"captured_at": "2026-11-01T14:05:43Z",
|
||||
"captured_at": "2026-11-04T17:53:52Z",
|
||||
"baseline": {
|
||||
"api_request_rate_per_second": 1.1485206460733426,
|
||||
"api_request_rate_per_second": 1.014325,
|
||||
"firing_alerts": 0,
|
||||
"traces_by_service": {
|
||||
"messenger-api": 39,
|
||||
"messenger-worker": 23,
|
||||
"messenger-dispatcher": 22
|
||||
"messenger-api": 100,
|
||||
"messenger-worker": 72,
|
||||
"messenger-dispatcher": 64
|
||||
},
|
||||
"logs_by_service": {
|
||||
"messenger-api": 162,
|
||||
"messenger-worker": 40,
|
||||
"messenger-api": 207,
|
||||
"messenger-worker": 41,
|
||||
"messenger-dispatcher": 1
|
||||
},
|
||||
"api_worker_shared_traces": 3,
|
||||
"traces_with_correlated_logs": 37
|
||||
"api_worker_shared_traces": 22,
|
||||
"traces_with_correlated_logs": 101
|
||||
},
|
||||
"incidents": [
|
||||
{
|
||||
"name": "worker_unavailable_short",
|
||||
"injected_duration_seconds": 76,
|
||||
"alert_detection_upper_bound_seconds": 76,
|
||||
"alert_resolution_seconds": 15,
|
||||
"recovery_observation_seconds": 36
|
||||
"alert_resolution_seconds": 13,
|
||||
"recovery_observation_seconds": 33
|
||||
},
|
||||
{
|
||||
"name": "postgres_unavailable",
|
||||
"injected_duration_seconds": 76,
|
||||
"alert_detection_upper_bound_seconds": 76,
|
||||
"operational_alert_resolution_seconds": 127,
|
||||
"recovery_observation_seconds": 148,
|
||||
"injected_duration_seconds": 78,
|
||||
"alert_detection_upper_bound_seconds": 78,
|
||||
"operational_alert_resolution_seconds": 134,
|
||||
"recovery_observation_seconds": 154,
|
||||
"fast_burn_alert_observed": true,
|
||||
"database_error_traces": 4,
|
||||
"database_error_traces_with_logs": 4
|
||||
"database_error_traces": 3,
|
||||
"database_error_traces_with_logs": 1
|
||||
},
|
||||
{
|
||||
"name": "worker_unavailable_long",
|
||||
"injected_duration_seconds": 180,
|
||||
"alert_detection_upper_bound_seconds": 180,
|
||||
"alert_resolution_seconds": 15,
|
||||
"recovery_observation_seconds": 65,
|
||||
"injected_duration_seconds": 181,
|
||||
"alert_detection_upper_bound_seconds": 181,
|
||||
"alert_resolution_seconds": 20,
|
||||
"recovery_observation_seconds": 91,
|
||||
"fast_burn_alert_observed": true,
|
||||
"slow_burn_alert_observed": true
|
||||
},
|
||||
@@ -48,7 +48,28 @@
|
||||
"name": "telemetry_backends_unavailable",
|
||||
"injected_duration_seconds": 30,
|
||||
"api_ready_http_status": 200,
|
||||
"recovery_observation_seconds": 31
|
||||
"recovery_observation_seconds": 31,
|
||||
"exporters": {
|
||||
"otlp_grpc/tempo": {
|
||||
"capacity": 512,
|
||||
"degraded_queue_size": 15,
|
||||
"recovered_queue_size": 0,
|
||||
"export_failures": 16,
|
||||
"dropped_records": 0
|
||||
},
|
||||
"otlp_http/loki": {
|
||||
"capacity": 512,
|
||||
"degraded_queue_size": 16,
|
||||
"recovered_queue_size": 0,
|
||||
"export_failures": 8,
|
||||
"dropped_records": 0
|
||||
}
|
||||
},
|
||||
"collector_peak_rss_bytes": {
|
||||
"baseline": 225923072,
|
||||
"degraded": 226549760,
|
||||
"recovered": 226746368
|
||||
}
|
||||
}
|
||||
],
|
||||
"validation": {
|
||||
|
||||
@@ -58,6 +58,13 @@ service:
|
||||
telemetry:
|
||||
logs:
|
||||
level: info
|
||||
metrics:
|
||||
readers:
|
||||
- pull:
|
||||
exporter:
|
||||
prometheus:
|
||||
host: 0.0.0.0
|
||||
port: 8888
|
||||
pipelines:
|
||||
metrics:
|
||||
receivers: [otlp]
|
||||
|
||||
@@ -17,7 +17,6 @@ type Metrics struct {
|
||||
workerInFlight metric.Int64UpDownCounter
|
||||
workerJobs metric.Int64Counter
|
||||
workerDuration metric.Float64Histogram
|
||||
workerTimeToReady metric.Float64Histogram
|
||||
workerRetries metric.Int64Counter
|
||||
workerReclaimed metric.Int64Counter
|
||||
outboxPublished metric.Int64Counter
|
||||
@@ -39,10 +38,6 @@ func NewMetrics(meter metric.Meter, handler http.Handler, workerCapacity int) *M
|
||||
metric.WithDescription("Attachment job delivery processing duration."), metric.WithUnit("s"),
|
||||
metric.WithExplicitBucketBoundaries(workerDurationBuckets...),
|
||||
)
|
||||
workerTimeToReady, _ := meter.Float64Histogram("messenger_attachment_time_to_ready",
|
||||
metric.WithDescription("Elapsed time from durable job creation to ready state."), metric.WithUnit("s"),
|
||||
metric.WithExplicitBucketBoundaries(workerDurationBuckets...),
|
||||
)
|
||||
workerRetries, _ := meter.Int64Counter("messenger_attachment_worker_retries", metric.WithDescription("Attachment jobs scheduled for another attempt."))
|
||||
workerReclaimed, _ := meter.Int64Counter("messenger_attachment_worker_reclaimed_deliveries", metric.WithDescription("Redis Stream deliveries reclaimed after their idle timeout."))
|
||||
outboxPublished, _ := meter.Int64Counter("messenger_outbox_published", metric.WithDescription("Outbox events confirmed by Redis Streams."))
|
||||
@@ -59,8 +54,8 @@ func NewMetrics(meter metric.Meter, handler http.Handler, workerCapacity int) *M
|
||||
}
|
||||
return &Metrics{
|
||||
meter: meter, workerInFlight: workerInFlight, workerJobs: workerJobs,
|
||||
workerDuration: workerDuration, workerTimeToReady: workerTimeToReady,
|
||||
workerRetries: workerRetries, workerReclaimed: workerReclaimed,
|
||||
workerDuration: workerDuration,
|
||||
workerRetries: workerRetries, workerReclaimed: workerReclaimed,
|
||||
outboxPublished: outboxPublished, outboxPublishFailure: outboxPublishFailure,
|
||||
outboxRecovered: outboxRecovered, handler: handler,
|
||||
}
|
||||
@@ -81,10 +76,6 @@ func (m *Metrics) ObserveFinished(ctx context.Context, result string, duration t
|
||||
m.workerDuration.Record(ctx, duration.Seconds(), attributes)
|
||||
}
|
||||
|
||||
func (m *Metrics) ObserveTimeToReady(ctx context.Context, duration time.Duration) {
|
||||
m.workerTimeToReady.Record(ctx, duration.Seconds(), metric.WithAttributes(attribute.String("job_type", "attachment.process")))
|
||||
}
|
||||
|
||||
func (m *Metrics) ObserveRetry(ctx context.Context) {
|
||||
m.workerRetries.Add(ctx, 1)
|
||||
}
|
||||
|
||||
@@ -58,7 +58,6 @@ type ImageProcessor interface {
|
||||
type Metrics interface {
|
||||
ObserveStarted(context.Context)
|
||||
ObserveFinished(context.Context, string, time.Duration)
|
||||
ObserveTimeToReady(context.Context, time.Duration)
|
||||
ObserveRetry(context.Context)
|
||||
ObserveReclaimed(context.Context, int)
|
||||
}
|
||||
@@ -290,7 +289,6 @@ func (w *Worker) ProcessDelivery(ctx context.Context, delivery redisstream.Deliv
|
||||
return err
|
||||
}
|
||||
result = "ready"
|
||||
w.metrics.ObserveTimeToReady(ctx, w.now().Sub(envelope.CreatedAt))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -350,7 +348,7 @@ func (w *Worker) resolveClaim(
|
||||
if job.ErrorCode != nil {
|
||||
code = *job.ErrorCode
|
||||
}
|
||||
return "failed", delivery.ToFailed(ctx, redisstream.FailedDelivery{Code: code, FailedAt: w.now()})
|
||||
return "duplicate_failed", delivery.ToFailed(ctx, redisstream.FailedDelivery{Code: code, FailedAt: w.now()})
|
||||
default:
|
||||
return "transient_error", fmt.Errorf("claim attachment job: %w", claimErr)
|
||||
}
|
||||
@@ -538,6 +536,5 @@ type nopMetrics struct{}
|
||||
|
||||
func (nopMetrics) ObserveStarted(context.Context) {}
|
||||
func (nopMetrics) ObserveFinished(context.Context, string, time.Duration) {}
|
||||
func (nopMetrics) ObserveTimeToReady(context.Context, time.Duration) {}
|
||||
func (nopMetrics) ObserveRetry(context.Context) {}
|
||||
func (nopMetrics) ObserveReclaimed(context.Context, int) {}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -14,7 +13,9 @@ import (
|
||||
"unicode/utf8"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/messenger/attachment"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/model"
|
||||
@@ -133,6 +134,9 @@ func (s *Server) attachmentContent(w http.ResponseWriter, r *http.Request) *apiE
|
||||
w.Header().Set("Content-Disposition", mime.FormatMediaType("inline", map[string]string{"filename": content.Attachment.Filename}))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if _, err := io.Copy(w, content.Body); err != nil {
|
||||
span := trace.SpanFromContext(r.Context())
|
||||
span.RecordError(err)
|
||||
span.SetStatus(codes.Error, "response body write failed")
|
||||
s.logger.WarnContext(r.Context(), "attachment download interrupted", "event", "attachment_download_interrupted", "attachment_id", content.Attachment.ID, "error", err)
|
||||
}
|
||||
return nil
|
||||
@@ -153,22 +157,6 @@ func attachmentVariant(query url.Values) (string, bool) {
|
||||
}
|
||||
}
|
||||
|
||||
var traceparentPattern = regexp.MustCompile(`^00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$`)
|
||||
|
||||
func traceparent(value string) string {
|
||||
if traceparentPattern.MatchString(value) {
|
||||
return value
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func tracestate(value string) string {
|
||||
if len(value) <= 512 {
|
||||
return value
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func attachmentIDFromRequest(r *http.Request) (string, *apiError) {
|
||||
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
|
||||
attachmentID, valid := canonicalID(parts[3])
|
||||
|
||||
@@ -37,9 +37,6 @@ func (s *Store) QueueAttachmentWithJobAndOutbox(
|
||||
return model.Attachment{}, model.ErrInvalidInput
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
if job.CreatedAt.IsZero() {
|
||||
job.CreatedAt = now
|
||||
}
|
||||
if job.AvailableAt.IsZero() {
|
||||
job.AvailableAt = now
|
||||
}
|
||||
@@ -67,8 +64,7 @@ func (s *Store) QueueAttachmentWithJobAndOutbox(
|
||||
|
||||
job.AttachmentID = attachmentID
|
||||
job.Status = model.AttachmentJobQueued
|
||||
job.UpdatedAt = job.CreatedAt
|
||||
if err := insertAttachmentJob(ctx, tx, job); err != nil {
|
||||
if err := insertAttachmentJob(ctx, tx, &job); err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
if err := insertAttachmentJobOutbox(ctx, tx, job, traceparent, tracestate); err != nil {
|
||||
@@ -92,7 +88,6 @@ func (s *Store) QueueAttachmentPipelineVersion(
|
||||
}
|
||||
job.Attempt = 1
|
||||
now := time.Now().UTC()
|
||||
job.CreatedAt = now
|
||||
job.AvailableAt = now
|
||||
|
||||
tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{})
|
||||
@@ -114,7 +109,6 @@ func (s *Store) QueueAttachmentPipelineVersion(
|
||||
}
|
||||
job.AttachmentID = attachment.ID
|
||||
job.Status = model.AttachmentJobQueued
|
||||
job.UpdatedAt = now
|
||||
const queueAttachment = `
|
||||
UPDATE attachments
|
||||
SET status = 'queued', pipeline_version = $2, staging_key = object_key,
|
||||
@@ -124,7 +118,7 @@ func (s *Store) QueueAttachmentPipelineVersion(
|
||||
if err := scanAttachment(tx.QueryRow(ctx, queueAttachment, attachment.ID, job.PipelineVersion), &attachment); err != nil {
|
||||
return model.Attachment{}, fmt.Errorf("queue attachment pipeline version: %w", err)
|
||||
}
|
||||
if err := insertAttachmentJob(ctx, tx, job); err != nil {
|
||||
if err := insertAttachmentJob(ctx, tx, &job); err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
if err := insertAttachmentJobOutbox(ctx, tx, job, traceparent, tracestate); err != nil {
|
||||
@@ -163,15 +157,16 @@ func (s *Store) alreadyQueuedAttachment(
|
||||
return publicAttachment(attachment), nil
|
||||
}
|
||||
|
||||
func insertAttachmentJob(ctx context.Context, tx pgx.Tx, job model.AttachmentJob) error {
|
||||
func insertAttachmentJob(ctx context.Context, tx pgx.Tx, job *model.AttachmentJob) error {
|
||||
const query = `
|
||||
INSERT INTO attachment_jobs (
|
||||
id, attachment_id, pipeline_version, attempt, status, available_at, created_at, updated_at
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, 'queued', $5, $6, $6)`
|
||||
if _, err := tx.Exec(
|
||||
ctx, query, job.ID, job.AttachmentID, job.PipelineVersion, job.Attempt, job.AvailableAt, job.CreatedAt,
|
||||
); err != nil {
|
||||
VALUES ($1, $2, $3, $4, 'queued', $5, statement_timestamp(), statement_timestamp())
|
||||
RETURNING created_at, updated_at`
|
||||
if err := tx.QueryRow(
|
||||
ctx, query, job.ID, job.AttachmentID, job.PipelineVersion, job.Attempt, job.AvailableAt,
|
||||
).Scan(&job.CreatedAt, &job.UpdatedAt); err != nil {
|
||||
return fmt.Errorf("insert attachment job: %w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -417,6 +412,9 @@ func (s *Store) CompleteAttachmentJob(
|
||||
if _, err := tx.Exec(ctx, completeAttachment, attachment.ID, job.PipelineVersion, originalKey); err != nil {
|
||||
return "", fmt.Errorf("complete attachment: %w", err)
|
||||
}
|
||||
if err := insertAttachmentTerminalEvent(ctx, tx, job, model.AttachmentJobReady); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return "", fmt.Errorf("commit completed attachment job: %w", err)
|
||||
}
|
||||
@@ -527,12 +525,26 @@ func (s *Store) FailAttachmentJob(
|
||||
if _, err := tx.Exec(ctx, failAttachment, attachment.ID, job.PipelineVersion, errorCode); err != nil {
|
||||
return "", fmt.Errorf("fail attachment: %w", err)
|
||||
}
|
||||
if err := insertAttachmentTerminalEvent(ctx, tx, job, model.AttachmentJobFailed); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return "", fmt.Errorf("commit failed attachment job: %w", err)
|
||||
}
|
||||
return model.JobMutationApplied, nil
|
||||
}
|
||||
|
||||
func insertAttachmentTerminalEvent(ctx context.Context, tx pgx.Tx, job model.AttachmentJob, status string) error {
|
||||
const query = `
|
||||
INSERT INTO attachment_terminal_events (job_id, status, accepted_at)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (job_id) DO NOTHING`
|
||||
if _, err := tx.Exec(ctx, query, job.ID, status, job.CreatedAt); err != nil {
|
||||
return fmt.Errorf("insert attachment terminal event: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func lockJobMutation(
|
||||
ctx context.Context,
|
||||
tx pgx.Tx,
|
||||
|
||||
@@ -89,9 +89,7 @@ func (s *Store) RecoverExpiredAttachmentJobs(
|
||||
if _, err := tx.Exec(ctx, requeueAttachment, attachment.ID, job.PipelineVersion); err != nil {
|
||||
return model.RecoveryResult{}, fmt.Errorf("requeue attachment after expired lease: %w", err)
|
||||
}
|
||||
eventJob := job
|
||||
eventJob.CreatedAt = time.Now().UTC()
|
||||
if err := insertAttachmentJobOutbox(ctx, tx, eventJob, "", ""); err != nil {
|
||||
if err := insertAttachmentJobOutbox(ctx, tx, job, "", ""); err != nil {
|
||||
return model.RecoveryResult{}, err
|
||||
}
|
||||
recovered.Requeued++
|
||||
@@ -118,6 +116,9 @@ func markExpiredJobFailed(
|
||||
if _, err := tx.Exec(ctx, failJob, job.ID, errorCode); err != nil {
|
||||
return fmt.Errorf("fail expired attachment job: %w", err)
|
||||
}
|
||||
if err := insertAttachmentTerminalEvent(ctx, tx, job, model.AttachmentJobFailed); err != nil {
|
||||
return err
|
||||
}
|
||||
if !failAttachment {
|
||||
return nil
|
||||
}
|
||||
@@ -198,9 +199,7 @@ func (s *Store) ReplayFailedAttachment(
|
||||
); err != nil {
|
||||
return model.AttachmentJob{}, fmt.Errorf("audit attachment replay: %w", err)
|
||||
}
|
||||
eventJob := job
|
||||
eventJob.CreatedAt = time.Now().UTC()
|
||||
if err := insertAttachmentJobOutbox(ctx, tx, eventJob, "", ""); err != nil {
|
||||
if err := insertAttachmentJobOutbox(ctx, tx, job, "", ""); err != nil {
|
||||
return model.AttachmentJob{}, err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
@@ -19,6 +20,7 @@ import (
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/model"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/postgres"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/reconcile"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab6/migrations"
|
||||
)
|
||||
|
||||
type PostgresSuite struct {
|
||||
@@ -210,6 +212,7 @@ func (s *PostgresSuite) TestAttachmentJobLifecycleAndOutboxFencing() {
|
||||
result, err := s.store.CompleteAttachmentJob(context.Background(), lease, variants)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.JobMutationApplied, result)
|
||||
s.assertTerminalEvent(job.ID, model.AttachmentJobReady, lease.Job.CreatedAt)
|
||||
result, err = s.store.CompleteAttachmentJob(context.Background(), lease, variants)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.JobMutationAlreadyComplete, result)
|
||||
@@ -252,6 +255,130 @@ func (s *PostgresSuite) TestAttachmentJobLifecycleAndOutboxFencing() {
|
||||
s.Equal(model.JobMutationStale, result)
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) TestAttachmentJobCreationUsesDatabaseClock() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
attachmentID := uuid.NewString()
|
||||
stagingKey := "staging/" + attachmentID + "/original"
|
||||
pending, err := s.store.CreatePendingAttachment(context.Background(), model.Attachment{
|
||||
ID: attachmentID, ChatID: chatID, OwnerID: owner.ID, Filename: "future.png",
|
||||
ContentType: "image/png", SizeBytes: 128, SHA256: strings.Repeat("f", 64), ObjectKey: stagingKey,
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
future := time.Now().Add(24 * time.Hour)
|
||||
job := model.AttachmentJob{
|
||||
ID: uuid.NewString(), AttachmentID: attachmentID, PipelineVersion: 1,
|
||||
Attempt: 1, CreatedAt: future,
|
||||
}
|
||||
_, err = s.store.QueueAttachmentWithJobAndOutbox(context.Background(), pending.ID, stagingKey, job, "", "")
|
||||
s.Require().NoError(err)
|
||||
stored, err := s.store.AttachmentJob(context.Background(), job.ID)
|
||||
s.Require().NoError(err)
|
||||
s.True(stored.CreatedAt.Before(future))
|
||||
|
||||
lease, err := s.store.ClaimAttachmentJob(
|
||||
context.Background(), job.ID, attachmentID, 1, 1, "worker-clock", time.Minute,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
result, err := s.store.CompleteAttachmentJob(context.Background(), lease, completedVariants(attachmentID, 1))
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.JobMutationApplied, result)
|
||||
s.assertTerminalEvent(job.ID, model.AttachmentJobReady, stored.CreatedAt)
|
||||
s.assertOutboxCreationTime(job.ID, 1, stored.CreatedAt)
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) TestTerminalLedgerMigrationBackfillsExistingJob() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
replayedAttachment, replayedJob := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
||||
skewedAttachment, skewedJob := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
||||
replayedAcceptedAt := time.Now().Add(-2 * time.Minute).UTC().Truncate(time.Microsecond)
|
||||
firstFailureAt := replayedAcceptedAt.Add(20 * time.Second)
|
||||
replayedReadyAt := firstFailureAt.Add(time.Minute)
|
||||
skewedTerminalAt := time.Now().Add(-time.Minute).UTC().Truncate(time.Microsecond)
|
||||
skewedCreatedAt := skewedTerminalAt.Add(time.Minute)
|
||||
tx, err := s.pool.Begin(context.Background())
|
||||
s.Require().NoError(err)
|
||||
defer func() { s.Require().NoError(tx.Rollback(context.Background())) }()
|
||||
_, err = tx.Exec(
|
||||
context.Background(),
|
||||
`UPDATE attachment_jobs SET status = 'ready', attempt = 2, error_code = NULL, created_at = $2, updated_at = $3
|
||||
WHERE id = $1`,
|
||||
replayedJob.ID,
|
||||
replayedAcceptedAt,
|
||||
replayedReadyAt,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
_, err = tx.Exec(
|
||||
context.Background(),
|
||||
`UPDATE attachments SET status = 'ready', error_code = NULL, ready_at = $2
|
||||
WHERE id = $1`,
|
||||
replayedAttachment.ID,
|
||||
replayedReadyAt,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
_, err = tx.Exec(
|
||||
context.Background(),
|
||||
`INSERT INTO attachment_replay_audit (
|
||||
id, job_id, attachment_id, previous_attempt, new_attempt, actor, reason, created_at
|
||||
) VALUES ($1, $2, $3, 1, 2, 'legacy-operator', 'legacy replay', $4)`,
|
||||
uuid.NewString(),
|
||||
replayedJob.ID,
|
||||
replayedAttachment.ID,
|
||||
firstFailureAt,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
_, err = tx.Exec(
|
||||
context.Background(),
|
||||
`UPDATE attachment_jobs SET status = 'failed', error_code = 'legacy_failure', created_at = $2, updated_at = $3
|
||||
WHERE id = $1`,
|
||||
skewedJob.ID,
|
||||
skewedCreatedAt,
|
||||
skewedTerminalAt,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
_, err = tx.Exec(
|
||||
context.Background(),
|
||||
`UPDATE attachments SET status = 'failed', error_code = 'legacy_failure', ready_at = NULL
|
||||
WHERE id = $1`,
|
||||
skewedAttachment.ID,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
_, err = tx.Exec(context.Background(), `ALTER INDEX attachment_terminal_events_status_terminal_at_idx RENAME TO attachment_terminal_events_status_terminal_at_idx_old`)
|
||||
s.Require().NoError(err)
|
||||
_, err = tx.Exec(context.Background(), `ALTER TABLE attachment_terminal_events RENAME TO attachment_terminal_events_old`)
|
||||
s.Require().NoError(err)
|
||||
migration, err := migrations.FS.ReadFile("0008_attachment_terminal_events.sql")
|
||||
s.Require().NoError(err)
|
||||
_, err = tx.Exec(context.Background(), string(migration), pgx.QueryExecModeSimpleProtocol)
|
||||
s.Require().NoError(err)
|
||||
|
||||
s.assertTerminalEventInTx(tx, replayedJob.ID, model.AttachmentJobFailed, replayedAcceptedAt, firstFailureAt)
|
||||
s.assertTerminalEventInTx(tx, skewedJob.ID, model.AttachmentJobFailed, skewedTerminalAt, skewedTerminalAt)
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) assertTerminalEventInTx(
|
||||
tx pgx.Tx,
|
||||
jobID string,
|
||||
status string,
|
||||
acceptedAt time.Time,
|
||||
terminalAt time.Time,
|
||||
) {
|
||||
s.T().Helper()
|
||||
var (
|
||||
actualStatus string
|
||||
actualAcceptedAt time.Time
|
||||
actualTerminalAt time.Time
|
||||
)
|
||||
err := tx.QueryRow(
|
||||
context.Background(),
|
||||
`SELECT status, accepted_at, terminal_at FROM attachment_terminal_events WHERE job_id = $1`,
|
||||
jobID,
|
||||
).Scan(&actualStatus, &actualAcceptedAt, &actualTerminalAt)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(status, actualStatus)
|
||||
s.True(acceptedAt.Equal(actualAcceptedAt))
|
||||
s.True(terminalAt.Equal(actualTerminalAt))
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) TestPublishedOutboxRetentionIsBatched() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
_, firstJob := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
||||
@@ -295,6 +422,8 @@ func (s *PostgresSuite) TestPublishedOutboxRetentionIsBatched() {
|
||||
func (s *PostgresSuite) TestFailedAttachmentReplayIsAudited() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
attachment, job := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
||||
original, err := s.store.AttachmentJob(context.Background(), job.ID)
|
||||
s.Require().NoError(err)
|
||||
lease, err := s.store.ClaimAttachmentJob(
|
||||
context.Background(), job.ID, attachment.ID, job.PipelineVersion, job.Attempt, "worker-1", time.Minute,
|
||||
)
|
||||
@@ -302,6 +431,7 @@ func (s *PostgresSuite) TestFailedAttachmentReplayIsAudited() {
|
||||
result, err := s.store.FailAttachmentJob(context.Background(), lease, "invalid_image")
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.JobMutationApplied, result)
|
||||
firstTerminalAt := s.assertTerminalEvent(job.ID, model.AttachmentJobFailed, original.CreatedAt)
|
||||
|
||||
_, _, err = s.store.CreateMessage(
|
||||
context.Background(), chatID, owner.ID, "failed attachment",
|
||||
@@ -320,6 +450,18 @@ func (s *PostgresSuite) TestFailedAttachmentReplayIsAudited() {
|
||||
s.Require().NoError(err)
|
||||
s.Equal(2, replayed.Attempt)
|
||||
s.Equal(model.AttachmentJobQueued, replayed.Status)
|
||||
s.Equal(original.CreatedAt, replayed.CreatedAt)
|
||||
s.assertOutboxCreationTime(job.ID, 2, original.CreatedAt)
|
||||
replayedLease, err := s.store.ClaimAttachmentJob(
|
||||
context.Background(), job.ID, attachment.ID, replayed.PipelineVersion, replayed.Attempt, "worker-2", time.Minute,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
result, err = s.store.CompleteAttachmentJob(
|
||||
context.Background(), replayedLease, completedVariants(attachment.ID, replayed.PipelineVersion),
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.JobMutationApplied, result)
|
||||
s.Equal(firstTerminalAt, s.assertTerminalEvent(job.ID, model.AttachmentJobFailed, original.CreatedAt))
|
||||
|
||||
var auditCount int
|
||||
err = s.pool.QueryRow(
|
||||
@@ -336,6 +478,8 @@ func (s *PostgresSuite) TestExpiredLeaseRecoveryFencesOldWorker() {
|
||||
context.Background(), job.ID, attachment.ID, job.PipelineVersion, job.Attempt, "worker-1", time.Minute,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
original, err := s.store.AttachmentJob(context.Background(), job.ID)
|
||||
s.Require().NoError(err)
|
||||
_, err = s.pool.Exec(
|
||||
context.Background(),
|
||||
`UPDATE attachment_jobs SET lease_until = clock_timestamp() - INTERVAL '1 second' WHERE id = $1`,
|
||||
@@ -351,12 +495,82 @@ func (s *PostgresSuite) TestExpiredLeaseRecoveryFencesOldWorker() {
|
||||
s.Require().NoError(err)
|
||||
s.Equal(2, current.Attempt)
|
||||
s.Equal(model.AttachmentJobQueued, current.Status)
|
||||
s.Equal(original.CreatedAt, current.CreatedAt)
|
||||
s.assertOutboxCreationTime(job.ID, 2, original.CreatedAt)
|
||||
|
||||
result, err := s.store.CompleteAttachmentJob(context.Background(), lease, completedVariants(attachment.ID, 1))
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.JobMutationStale, result)
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) TestExpiredLeaseRecoveryFailsExhaustedJobOnce() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
attachment, job := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
||||
original, err := s.store.AttachmentJob(context.Background(), job.ID)
|
||||
s.Require().NoError(err)
|
||||
_, err = s.store.ClaimAttachmentJob(
|
||||
context.Background(), job.ID, attachment.ID, job.PipelineVersion, job.Attempt, "worker-1", time.Minute,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
_, err = s.pool.Exec(
|
||||
context.Background(),
|
||||
`UPDATE attachment_jobs SET lease_until = clock_timestamp() - INTERVAL '1 second' WHERE id = $1`,
|
||||
job.ID,
|
||||
)
|
||||
s.Require().NoError(err)
|
||||
|
||||
recovered, err := s.store.RecoverExpiredAttachmentJobs(context.Background(), 10, 1, time.Now())
|
||||
s.Require().NoError(err)
|
||||
s.Zero(recovered.Requeued)
|
||||
s.Equal(1, recovered.Failed)
|
||||
firstTerminalAt := s.assertTerminalEvent(job.ID, model.AttachmentJobFailed, original.CreatedAt)
|
||||
|
||||
recovered, err = s.store.RecoverExpiredAttachmentJobs(context.Background(), 10, 1, time.Now())
|
||||
s.Require().NoError(err)
|
||||
s.Zero(recovered.Requeued)
|
||||
s.Zero(recovered.Failed)
|
||||
current, err := s.store.AttachmentJob(context.Background(), job.ID)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(model.AttachmentJobFailed, current.Status)
|
||||
s.Equal(firstTerminalAt, s.assertTerminalEvent(job.ID, model.AttachmentJobFailed, original.CreatedAt))
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) assertTerminalEvent(jobID string, status string, acceptedAt time.Time) time.Time {
|
||||
s.T().Helper()
|
||||
var (
|
||||
actualStatus string
|
||||
actualAcceptedAt time.Time
|
||||
terminalAt time.Time
|
||||
count int
|
||||
)
|
||||
err := s.pool.QueryRow(
|
||||
context.Background(),
|
||||
`SELECT min(status), min(accepted_at), min(terminal_at), count(*)
|
||||
FROM attachment_terminal_events WHERE job_id = $1`,
|
||||
jobID,
|
||||
).Scan(&actualStatus, &actualAcceptedAt, &terminalAt, &count)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(1, count)
|
||||
s.Equal(status, actualStatus)
|
||||
s.True(acceptedAt.Equal(actualAcceptedAt), "expected %s, got %s", acceptedAt, actualAcceptedAt)
|
||||
return terminalAt
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) assertOutboxCreationTime(jobID string, attempt int, expected time.Time) {
|
||||
s.T().Helper()
|
||||
var payload []byte
|
||||
err := s.pool.QueryRow(
|
||||
context.Background(),
|
||||
`SELECT payload FROM outbox_events WHERE job_id = $1 AND attempt = $2`,
|
||||
jobID,
|
||||
attempt,
|
||||
).Scan(&payload)
|
||||
s.Require().NoError(err)
|
||||
var message model.AttachmentJobMessage
|
||||
s.Require().NoError(json.Unmarshal(payload, &message))
|
||||
s.True(expected.Equal(message.CreatedAt), "expected %s, got %s", expected, message.CreatedAt)
|
||||
}
|
||||
|
||||
func (s *PostgresSuite) TestCompleteAttachmentJobRejectsIncompleteOrMismatchedVariants() {
|
||||
owner, chatID := createOwnerAndChat(s.T(), s.store)
|
||||
attachment, job := createQueuedAttachment(s.T(), s.store, owner.ID, chatID)
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
CREATE TABLE attachment_terminal_events (
|
||||
job_id UUID PRIMARY KEY,
|
||||
status TEXT NOT NULL,
|
||||
accepted_at TIMESTAMPTZ NOT NULL,
|
||||
terminal_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
|
||||
CONSTRAINT attachment_terminal_events_status CHECK (status IN ('ready', 'failed')),
|
||||
CONSTRAINT attachment_terminal_events_time CHECK (terminal_at >= accepted_at)
|
||||
);
|
||||
|
||||
CREATE INDEX attachment_terminal_events_status_terminal_at_idx
|
||||
ON attachment_terminal_events (status, terminal_at);
|
||||
|
||||
WITH first_replay AS (
|
||||
SELECT job_id, MIN(created_at) AS created_at
|
||||
FROM attachment_replay_audit
|
||||
GROUP BY job_id
|
||||
)
|
||||
INSERT INTO attachment_terminal_events (job_id, status, accepted_at, terminal_at)
|
||||
SELECT jobs.id, 'failed', LEAST(jobs.created_at, first_replay.created_at), first_replay.created_at
|
||||
FROM attachment_jobs jobs
|
||||
JOIN first_replay ON first_replay.job_id = jobs.id
|
||||
ON CONFLICT (job_id) DO NOTHING;
|
||||
|
||||
INSERT INTO attachment_terminal_events (job_id, status, accepted_at, terminal_at)
|
||||
SELECT jobs.id, jobs.status, LEAST(jobs.created_at, jobs.updated_at), jobs.updated_at
|
||||
FROM attachment_jobs jobs
|
||||
WHERE jobs.status IN ('ready', 'failed')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM attachment_replay_audit replay
|
||||
WHERE replay.job_id = jobs.id
|
||||
)
|
||||
ON CONFLICT (job_id) DO NOTHING;
|
||||
@@ -1010,7 +1010,7 @@
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"expr": "1 - (clamp_min((sum(rate(messenger_attachment_time_to_ready_seconds_count[5m])) or vector(0)) - (sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le=\"30\"}[5m])) or vector(0)), 0) + (sum(rate(messenger_attachment_worker_jobs_total{result=\"failed\"}[5m])) or vector(0))) / clamp_min((sum(rate(messenger_attachment_time_to_ready_seconds_count[5m])) or vector(0)) + (sum(rate(messenger_attachment_worker_jobs_total{result=\"failed\"}[5m])) or vector(0)), 0.000001)",
|
||||
"expr": "1 - (clamp_min((sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le=\"+Inf\"}[5m])) or vector(0)) - (sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le=\"30\"}[5m])) or vector(0)), 0) + (sum(rate(messenger_attachment_terminal_transitions_total{status=\"failed\"}[5m])) or vector(0))) / clamp_min((sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le=\"+Inf\"}[5m])) or vector(0)) + (sum(rate(messenger_attachment_terminal_transitions_total{status=\"failed\"}[5m])) or vector(0)), 0.000001)",
|
||||
"legendFormat": "ready in 30s",
|
||||
"refId": "A"
|
||||
}
|
||||
@@ -1045,17 +1045,17 @@
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"expr": "(clamp_min((sum(rate(messenger_attachment_time_to_ready_seconds_count[5m])) or vector(0)) - (sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le=\"30\"}[5m])) or vector(0)), 0) + (sum(rate(messenger_attachment_worker_jobs_total{result=\"failed\"}[5m])) or vector(0))) / clamp_min((sum(rate(messenger_attachment_time_to_ready_seconds_count[5m])) or vector(0)) + (sum(rate(messenger_attachment_worker_jobs_total{result=\"failed\"}[5m])) or vector(0)), 0.000001) / 0.01",
|
||||
"expr": "(clamp_min((sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le=\"+Inf\"}[5m])) or vector(0)) - (sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le=\"30\"}[5m])) or vector(0)), 0) + (sum(rate(messenger_attachment_terminal_transitions_total{status=\"failed\"}[5m])) or vector(0))) / clamp_min((sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le=\"+Inf\"}[5m])) or vector(0)) + (sum(rate(messenger_attachment_terminal_transitions_total{status=\"failed\"}[5m])) or vector(0)), 0.000001) / 0.01",
|
||||
"legendFormat": "5m burn",
|
||||
"refId": "A"
|
||||
},
|
||||
{
|
||||
"expr": "(clamp_min((sum(rate(messenger_attachment_time_to_ready_seconds_count[1h])) or vector(0)) - (sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le=\"30\"}[1h])) or vector(0)), 0) + (sum(rate(messenger_attachment_worker_jobs_total{result=\"failed\"}[1h])) or vector(0))) / clamp_min((sum(rate(messenger_attachment_time_to_ready_seconds_count[1h])) or vector(0)) + (sum(rate(messenger_attachment_worker_jobs_total{result=\"failed\"}[1h])) or vector(0)), 0.000001) / 0.01",
|
||||
"expr": "(clamp_min((sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le=\"+Inf\"}[1h])) or vector(0)) - (sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le=\"30\"}[1h])) or vector(0)), 0) + (sum(rate(messenger_attachment_terminal_transitions_total{status=\"failed\"}[1h])) or vector(0))) / clamp_min((sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le=\"+Inf\"}[1h])) or vector(0)) + (sum(rate(messenger_attachment_terminal_transitions_total{status=\"failed\"}[1h])) or vector(0)), 0.000001) / 0.01",
|
||||
"legendFormat": "1h burn",
|
||||
"refId": "B"
|
||||
},
|
||||
{
|
||||
"expr": "(clamp_min((sum(rate(messenger_attachment_time_to_ready_seconds_count[6h])) or vector(0)) - (sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le=\"30\"}[6h])) or vector(0)), 0) + (sum(rate(messenger_attachment_worker_jobs_total{result=\"failed\"}[6h])) or vector(0))) / clamp_min((sum(rate(messenger_attachment_time_to_ready_seconds_count[6h])) or vector(0)) + (sum(rate(messenger_attachment_worker_jobs_total{result=\"failed\"}[6h])) or vector(0)), 0.000001) / 0.01",
|
||||
"expr": "(clamp_min((sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le=\"+Inf\"}[6h])) or vector(0)) - (sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le=\"30\"}[6h])) or vector(0)), 0) + (sum(rate(messenger_attachment_terminal_transitions_total{status=\"failed\"}[6h])) or vector(0))) / clamp_min((sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le=\"+Inf\"}[6h])) or vector(0)) + (sum(rate(messenger_attachment_terminal_transitions_total{status=\"failed\"}[6h])) or vector(0)), 0.000001) / 0.01",
|
||||
"legendFormat": "6h burn",
|
||||
"refId": "C"
|
||||
}
|
||||
@@ -1410,12 +1410,12 @@
|
||||
},
|
||||
"targets": [
|
||||
{
|
||||
"expr": "sum by (exporter) (rate(otelcol_exporter_send_failed_spans_total[5m]))",
|
||||
"expr": "sum by (exporter) (rate(otelcol_exporter_send_failed_spans[5m]))",
|
||||
"legendFormat": "{{exporter}} spans failed/s",
|
||||
"refId": "A"
|
||||
},
|
||||
{
|
||||
"expr": "sum by (exporter) (rate(otelcol_exporter_send_failed_log_records_total[5m]))",
|
||||
"expr": "sum by (exporter) (rate(otelcol_exporter_send_failed_log_records[5m]))",
|
||||
"legendFormat": "{{exporter}} logs failed/s",
|
||||
"refId": "B"
|
||||
},
|
||||
|
||||
@@ -32,9 +32,11 @@ datasources:
|
||||
maxLines: 1000
|
||||
derivedFields:
|
||||
- name: TraceID
|
||||
matcherRegex: 'trace_id[=:" ]+([0-9a-f]{32})'
|
||||
matcherType: label
|
||||
matcherRegex: trace_id
|
||||
datasourceUid: tempo
|
||||
url: '$${__value.raw}'
|
||||
urlDisplayLabel: View trace
|
||||
|
||||
- name: Tempo
|
||||
type: tempo
|
||||
|
||||
@@ -37,3 +37,61 @@ messenger_outbox:
|
||||
- oldest_pending_age_seconds:
|
||||
usage: GAUGE
|
||||
description: Age of the oldest unpublished outbox event.
|
||||
|
||||
messenger_attachment_terminal_transitions:
|
||||
query: |
|
||||
WITH statuses(status) AS (
|
||||
VALUES ('ready'), ('failed')
|
||||
)
|
||||
SELECT
|
||||
statuses.status,
|
||||
COUNT(events.job_id)::bigint AS total
|
||||
FROM statuses
|
||||
LEFT JOIN attachment_terminal_events events ON events.status = statuses.status
|
||||
GROUP BY statuses.status
|
||||
metrics:
|
||||
- status:
|
||||
usage: LABEL
|
||||
description: First terminal status recorded for a durable attachment job.
|
||||
- total:
|
||||
usage: COUNTER
|
||||
description: Durable attachment jobs whose first terminal outcome has this status.
|
||||
|
||||
messenger_attachment_time_to_ready_seconds:
|
||||
query: |
|
||||
WITH bounds(le, upper_bound) AS (
|
||||
VALUES
|
||||
('0.05', 0.05::double precision),
|
||||
('0.1', 0.1::double precision),
|
||||
('0.25', 0.25::double precision),
|
||||
('0.5', 0.5::double precision),
|
||||
('1', 1::double precision),
|
||||
('2.5', 2.5::double precision),
|
||||
('5', 5::double precision),
|
||||
('10', 10::double precision),
|
||||
('15', 15::double precision),
|
||||
('20', 20::double precision),
|
||||
('30', 30::double precision),
|
||||
('60', 60::double precision),
|
||||
('+Inf', NULL::double precision)
|
||||
)
|
||||
SELECT
|
||||
bounds.le,
|
||||
COUNT(events.job_id) FILTER (
|
||||
WHERE events.status = 'ready'
|
||||
AND (
|
||||
bounds.upper_bound IS NULL
|
||||
OR EXTRACT(EPOCH FROM (events.terminal_at - events.accepted_at)) <= bounds.upper_bound
|
||||
)
|
||||
)::bigint AS bucket
|
||||
FROM bounds
|
||||
LEFT JOIN attachment_terminal_events events ON events.status = 'ready'
|
||||
GROUP BY bounds.le, bounds.upper_bound
|
||||
ORDER BY bounds.upper_bound NULLS LAST
|
||||
metrics:
|
||||
- le:
|
||||
usage: LABEL
|
||||
description: Upper bound of the durable time-to-ready bucket in seconds.
|
||||
- bucket:
|
||||
usage: COUNTER
|
||||
description: Durable attachment jobs reaching ready within the bucket bound.
|
||||
|
||||
@@ -68,29 +68,29 @@ groups:
|
||||
expr: |
|
||||
(
|
||||
clamp_min(
|
||||
(sum(rate(messenger_attachment_time_to_ready_seconds_count[5m])) or vector(0))
|
||||
(sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le="+Inf"}[5m])) or vector(0))
|
||||
- (sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le="30"}[5m])) or vector(0)),
|
||||
0
|
||||
)
|
||||
+ (sum(rate(messenger_attachment_worker_jobs_total{result="failed"}[5m])) or vector(0))
|
||||
+ (sum(rate(messenger_attachment_terminal_transitions_total{status="failed"}[5m])) or vector(0))
|
||||
)
|
||||
/ clamp_min(
|
||||
(sum(rate(messenger_attachment_time_to_ready_seconds_count[5m])) or vector(0))
|
||||
+ (sum(rate(messenger_attachment_worker_jobs_total{result="failed"}[5m])) or vector(0)),
|
||||
(sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le="+Inf"}[5m])) or vector(0))
|
||||
+ (sum(rate(messenger_attachment_terminal_transitions_total{status="failed"}[5m])) or vector(0)),
|
||||
0.000001
|
||||
) > 0.144
|
||||
and
|
||||
(
|
||||
clamp_min(
|
||||
(sum(rate(messenger_attachment_time_to_ready_seconds_count[1h])) or vector(0))
|
||||
(sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le="+Inf"}[1h])) or vector(0))
|
||||
- (sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le="30"}[1h])) or vector(0)),
|
||||
0
|
||||
)
|
||||
+ (sum(rate(messenger_attachment_worker_jobs_total{result="failed"}[1h])) or vector(0))
|
||||
+ (sum(rate(messenger_attachment_terminal_transitions_total{status="failed"}[1h])) or vector(0))
|
||||
)
|
||||
/ clamp_min(
|
||||
(sum(rate(messenger_attachment_time_to_ready_seconds_count[1h])) or vector(0))
|
||||
+ (sum(rate(messenger_attachment_worker_jobs_total{result="failed"}[1h])) or vector(0)),
|
||||
(sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le="+Inf"}[1h])) or vector(0))
|
||||
+ (sum(rate(messenger_attachment_terminal_transitions_total{status="failed"}[1h])) or vector(0)),
|
||||
0.000001
|
||||
) > 0.144
|
||||
for: 30s
|
||||
@@ -107,29 +107,29 @@ groups:
|
||||
expr: |
|
||||
(
|
||||
clamp_min(
|
||||
(sum(rate(messenger_attachment_time_to_ready_seconds_count[30m])) or vector(0))
|
||||
(sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le="+Inf"}[30m])) or vector(0))
|
||||
- (sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le="30"}[30m])) or vector(0)),
|
||||
0
|
||||
)
|
||||
+ (sum(rate(messenger_attachment_worker_jobs_total{result="failed"}[30m])) or vector(0))
|
||||
+ (sum(rate(messenger_attachment_terminal_transitions_total{status="failed"}[30m])) or vector(0))
|
||||
)
|
||||
/ clamp_min(
|
||||
(sum(rate(messenger_attachment_time_to_ready_seconds_count[30m])) or vector(0))
|
||||
+ (sum(rate(messenger_attachment_worker_jobs_total{result="failed"}[30m])) or vector(0)),
|
||||
(sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le="+Inf"}[30m])) or vector(0))
|
||||
+ (sum(rate(messenger_attachment_terminal_transitions_total{status="failed"}[30m])) or vector(0)),
|
||||
0.000001
|
||||
) > 0.06
|
||||
and
|
||||
(
|
||||
clamp_min(
|
||||
(sum(rate(messenger_attachment_time_to_ready_seconds_count[6h])) or vector(0))
|
||||
(sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le="+Inf"}[6h])) or vector(0))
|
||||
- (sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le="30"}[6h])) or vector(0)),
|
||||
0
|
||||
)
|
||||
+ (sum(rate(messenger_attachment_worker_jobs_total{result="failed"}[6h])) or vector(0))
|
||||
+ (sum(rate(messenger_attachment_terminal_transitions_total{status="failed"}[6h])) or vector(0))
|
||||
)
|
||||
/ clamp_min(
|
||||
(sum(rate(messenger_attachment_time_to_ready_seconds_count[6h])) or vector(0))
|
||||
+ (sum(rate(messenger_attachment_worker_jobs_total{result="failed"}[6h])) or vector(0)),
|
||||
(sum(rate(messenger_attachment_time_to_ready_seconds_bucket{le="+Inf"}[6h])) or vector(0))
|
||||
+ (sum(rate(messenger_attachment_terminal_transitions_total{status="failed"}[6h])) or vector(0)),
|
||||
0.000001
|
||||
) > 0.06
|
||||
for: 1m
|
||||
|
||||
@@ -2,11 +2,12 @@
|
||||
|
||||
## Цель и сигналы
|
||||
|
||||
Объект контроля — время от durable acceptance изображения до состояния `ready`. Цель — не менее 99% jobs завершаются `ready` не позднее 30 секунд за скользящие 30 дней. `failed` и завершение позже 30 секунд расходуют error budget.
|
||||
Объект контроля — первый terminal outcome каждой уникальной durable job. Цель — не менее 99% jobs впервые завершаются `ready` не позднее 30 секунд после acceptance за скользящие 30 дней. Первый `failed` и первое завершение `ready` позже 30 секунд расходуют error budget; replay не переписывает уже записанный outcome.
|
||||
|
||||
Основные сигналы:
|
||||
|
||||
- `messenger_attachment_worker_jobs_total{job_type,result}` — исходы worker;
|
||||
- `messenger_attachment_worker_jobs_total{job_type,result}` — исходы доставок worker;
|
||||
- `messenger_attachment_terminal_transitions_total{status}` — первые terminal outcomes из append-only PostgreSQL ledger;
|
||||
- `messenger_attachment_worker_job_duration_seconds_bucket{job_type,result}` — время обработки;
|
||||
- `messenger_attachment_time_to_ready_seconds_bucket` — end-to-end время;
|
||||
- `messenger_attachment_jobs_count{status}` — состояния jobs в PostgreSQL;
|
||||
|
||||
@@ -18,7 +18,12 @@ QUERIES = {
|
||||
"worker_capacity": 'sum(messenger_attachment_worker_capacity)',
|
||||
"pending_outbox": 'sum(messenger_outbox_pending)',
|
||||
"worker_up": 'min(up{job="attachment-worker"})',
|
||||
"collector_export_failures_5m": 'sum(increase(otelcol_exporter_send_failed_spans_total[5m])) + sum(increase(otelcol_exporter_send_failed_log_records_total[5m]))',
|
||||
"collector_queue_size": 'max by (exporter) (otelcol_exporter_queue_size)',
|
||||
"collector_queue_capacity": 'max by (exporter) (otelcol_exporter_queue_capacity)',
|
||||
"collector_export_failures_total": 'sum by (exporter) (otelcol_exporter_send_failed_spans) or sum by (exporter) (otelcol_exporter_send_failed_log_records) or on (exporter) (0 * max by (exporter) (otelcol_exporter_queue_capacity))',
|
||||
"collector_dropped_records_total": 'sum by (exporter) (otelcol_exporter_enqueue_failed_spans) or sum by (exporter) (otelcol_exporter_enqueue_failed_log_records) or on (exporter) (0 * max by (exporter) (otelcol_exporter_queue_capacity))',
|
||||
"collector_memory_bytes": 'max(otelcol_process_memory_rss) or max(process_resident_memory_bytes{job="otel-collector"})',
|
||||
"collector_memory_peak_bytes": 'max(max_over_time(otelcol_process_memory_rss[2m])) or max(max_over_time(process_resident_memory_bytes{job="otel-collector"}[2m]))',
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@@ -14,6 +15,105 @@ def check(condition, message, errors):
|
||||
errors.append(message)
|
||||
|
||||
|
||||
def query_scalar(snapshot, name, source, errors):
|
||||
query = snapshot.get("queries", {}).get(name, {})
|
||||
check("error" not in query, source + " query failed: " + name, errors)
|
||||
series = query.get("series", [])
|
||||
check(len(series) == 1, source + " query must return one series: " + name, errors)
|
||||
if len(series) != 1:
|
||||
return None
|
||||
try:
|
||||
value = float(series[0]["value"])
|
||||
except (KeyError, TypeError, ValueError):
|
||||
check(False, source + " query returned a non-numeric value: " + name, errors)
|
||||
return None
|
||||
check(math.isfinite(value), source + " query returned a non-finite value: " + name, errors)
|
||||
return value if math.isfinite(value) else None
|
||||
|
||||
|
||||
def query_exporters(snapshot, name, source, errors):
|
||||
expected = {"otlp_grpc/tempo", "otlp_http/loki"}
|
||||
query = snapshot.get("queries", {}).get(name, {})
|
||||
check("error" not in query, source + " query failed: " + name, errors)
|
||||
values = {}
|
||||
for series in query.get("series", []):
|
||||
exporter = series.get("labels", {}).get("exporter")
|
||||
check(exporter in expected, source + " query returned unexpected exporter: " + name, errors)
|
||||
check(exporter not in values, source + " query returned duplicate exporter: " + name, errors)
|
||||
try:
|
||||
value = float(series["value"])
|
||||
except (KeyError, TypeError, ValueError):
|
||||
check(False, source + " query returned a non-numeric value: " + name, errors)
|
||||
continue
|
||||
check(math.isfinite(value), source + " query returned a non-finite value: " + name, errors)
|
||||
if exporter in expected and exporter not in values and math.isfinite(value):
|
||||
values[exporter] = value
|
||||
check(set(values) == expected, source + " query must return the exact exporter set: " + name, errors)
|
||||
return values if set(values) == expected else None
|
||||
|
||||
|
||||
def validate_telemetry_outage(directory, errors):
|
||||
phase_names = ("baseline", "degraded", "recovered")
|
||||
exporter_metric_names = (
|
||||
"collector_queue_size",
|
||||
"collector_queue_capacity",
|
||||
"collector_export_failures_total",
|
||||
"collector_dropped_records_total",
|
||||
)
|
||||
scalar_metric_names = (
|
||||
"collector_memory_bytes",
|
||||
"collector_memory_peak_bytes",
|
||||
)
|
||||
phases = {}
|
||||
for phase in phase_names:
|
||||
path = directory / (phase + ".json")
|
||||
check(path.exists(), str(path) + " is missing", errors)
|
||||
if not path.exists():
|
||||
return
|
||||
snapshot = load(path)
|
||||
phases[phase] = {
|
||||
**{name: query_exporters(snapshot, name, path.name, errors) for name in exporter_metric_names},
|
||||
**{name: query_scalar(snapshot, name, path.name, errors) for name in scalar_metric_names},
|
||||
}
|
||||
if any(value is None for values in phases.values() for value in values.values()):
|
||||
return
|
||||
|
||||
for phase, values in phases.items():
|
||||
for exporter in ("otlp_grpc/tempo", "otlp_http/loki"):
|
||||
capacity = values["collector_queue_capacity"][exporter]
|
||||
size = values["collector_queue_size"][exporter]
|
||||
check(capacity > 0, phase + " collector queue capacity is not positive: " + exporter, errors)
|
||||
check(0 <= size <= capacity, phase + " collector queue size is outside capacity: " + exporter, errors)
|
||||
check(values["collector_memory_bytes"] > 0, phase + " collector memory is not positive", errors)
|
||||
check(values["collector_memory_peak_bytes"] > 0, phase + " collector peak memory is not positive", errors)
|
||||
|
||||
baseline = phases["baseline"]
|
||||
degraded = phases["degraded"]
|
||||
recovered = phases["recovered"]
|
||||
for exporter in ("otlp_grpc/tempo", "otlp_http/loki"):
|
||||
for name in ("collector_export_failures_total", "collector_dropped_records_total"):
|
||||
check(degraded[name][exporter] >= baseline[name][exporter], name + " decreased during outage: " + exporter, errors)
|
||||
check(recovered[name][exporter] >= degraded[name][exporter], name + " decreased after recovery: " + exporter, errors)
|
||||
outage_observed = (
|
||||
degraded["collector_queue_size"][exporter] > baseline["collector_queue_size"][exporter]
|
||||
or recovered["collector_export_failures_total"][exporter] > baseline["collector_export_failures_total"][exporter]
|
||||
or recovered["collector_dropped_records_total"][exporter] > baseline["collector_dropped_records_total"][exporter]
|
||||
)
|
||||
check(outage_observed, "telemetry outage was not observed for exporter: " + exporter, errors)
|
||||
drained_limit = max(
|
||||
baseline["collector_queue_size"][exporter],
|
||||
recovered["collector_queue_capacity"][exporter] * 0.05,
|
||||
)
|
||||
check(
|
||||
recovered["collector_queue_size"][exporter] <= drained_limit,
|
||||
"collector queue did not drain after recovery: " + exporter,
|
||||
errors,
|
||||
)
|
||||
memory_limit = max(baseline["collector_memory_peak_bytes"] * 1.5, baseline["collector_memory_peak_bytes"] + 64 * 1024 * 1024)
|
||||
check(degraded["collector_memory_peak_bytes"] <= memory_limit, "collector peak memory grew beyond the outage bound", errors)
|
||||
check(recovered["collector_memory_peak_bytes"] <= memory_limit, "collector peak memory remained above the recovery bound", errors)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Validate generated Lab 6 incident evidence")
|
||||
parser.add_argument("directory")
|
||||
@@ -77,6 +177,7 @@ def main():
|
||||
check(terminal.get("ready", 0) == traffic.get("attachments_accepted", -1), incident + " did not finish every attachment", errors)
|
||||
check(terminal.get("failed", 0) == 0 and terminal.get("timeout", 0) == 0, incident + " left unsuccessful attachments", errors)
|
||||
check(summary.get("api_ready_http_status_during_outage") == 200, incident + " broke API readiness", errors)
|
||||
validate_telemetry_outage(summary_path.parent, errors)
|
||||
|
||||
if args.require_alerts and incident in ("worker_unavailable", "postgres_unavailable"):
|
||||
for name in ("alert-firing.json", "alert-resolved.json"):
|
||||
|
||||
Reference in New Issue
Block a user