diff --git a/Dockerfile b/Dockerfile index ff27469a..efd6b0d8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ COPY go.mod go.sum ./ RUN go mod download COPY . . RUN go build -p=1 -o /out/messenger ./cmd/messenger -RUN go build -p=1 -o /out/lab4experiment ./cmd/lab4experiment +RUN go build -p=1 -o /out/lab5experiment ./cmd/lab5experiment FROM build AS test @@ -35,7 +35,7 @@ FROM alpine:3.24.2 RUN addgroup -S app && adduser -S -G app -u 10001 app COPY --from=build --chown=app:app /out/messenger /app/messenger -COPY --from=build --chown=app:app /out/lab4experiment /app/lab4experiment +COPY --from=build --chown=app:app /out/lab5experiment /app/lab5experiment USER app WORKDIR /app diff --git a/compose.yaml b/compose.yaml index f1c370aa..b2137b4f 100644 --- a/compose.yaml +++ b/compose.yaml @@ -34,6 +34,32 @@ services: restart: unless-stopped networks: [data] + redis-broker: + image: redis:8.2.2-alpine + command: + - redis-server + - --appendonly + - "yes" + - --appendfsync + - always + - --maxmemory-policy + - noeviction + - --requirepass + - ${REDIS_BROKER_PASSWORD} + environment: + REDIS_BROKER_PASSWORD: ${REDIS_BROKER_PASSWORD} + volumes: + - redis_broker_data:/data + healthcheck: + test: ["CMD-SHELL", "redis-cli --no-auth-warning -a \"$$REDIS_BROKER_PASSWORD\" ping | grep -q PONG"] + interval: 5s + timeout: 3s + retries: 20 + cpus: ${REDIS_BROKER_CPUS} + mem_limit: ${REDIS_BROKER_MEMORY} + restart: unless-stopped + networks: [data] + s3: build: context: . @@ -147,6 +173,7 @@ services: ATTACHMENT_MAX_PIXELS: ${ATTACHMENT_MAX_PIXELS} ATTACHMENT_BODY_MEMORY_BYTES: ${ATTACHMENT_BODY_MEMORY_BYTES} ATTACHMENT_DECODE_MEMORY_BYTES: ${ATTACHMENT_DECODE_MEMORY_BYTES} + PIPELINE_VERSION: ${PIPELINE_VERSION} expose: - "8080" depends_on: @@ -170,6 +197,103 @@ services: restart: unless-stopped networks: [application, data] + dispatcher: + image: ${API_IMAGE:-mtusi-messenger:lab5} + command: ["/app/messenger", "dispatch"] + environment: + DATABASE_URL: ${DATABASE_URL} + DB_MIN_CONNS: ${DB_MIN_CONNS} + DB_MAX_CONNS: ${DB_MAX_CONNS} + DB_MAX_CONNECTION_AGE_SECONDS: ${DB_MAX_CONNECTION_AGE_SECONDS} + DB_MAX_CONNECTION_IDLE_SECONDS: ${DB_MAX_CONNECTION_IDLE_SECONDS} + DB_HEALTH_CHECK_SECONDS: ${DB_HEALTH_CHECK_SECONDS} + DEPENDENCY_TIMEOUT_SECONDS: ${DEPENDENCY_TIMEOUT_SECONDS} + BROKER_URL: ${BROKER_URL} + BROKER_STREAM: ${BROKER_STREAM} + BROKER_CONSUMER_GROUP: ${BROKER_CONSUMER_GROUP} + BROKER_FAILED_STREAM: ${BROKER_FAILED_STREAM} + BROKER_FAILED_MAX_LENGTH: ${BROKER_FAILED_MAX_LENGTH} + OUTBOX_BATCH_SIZE: ${OUTBOX_BATCH_SIZE} + OUTBOX_LEASE_SECONDS: ${OUTBOX_LEASE_SECONDS} + OUTBOX_POLL_SECONDS: ${OUTBOX_POLL_SECONDS} + OUTBOX_RECOVERY_SECONDS: ${OUTBOX_RECOVERY_SECONDS} + OUTBOX_RETRY_SECONDS: ${OUTBOX_RETRY_SECONDS} + PIPELINE_METRICS_HOST: 0.0.0.0 + PIPELINE_METRICS_PORT: 9091 + expose: + - "9091" + depends_on: + migrate: + condition: service_completed_successfully + redis-broker: + condition: service_healthy + healthcheck: + test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:9091/health/live || exit 1"] + interval: 5s + timeout: 2s + retries: 20 + cpus: ${DISPATCHER_CPUS} + mem_limit: ${DISPATCHER_MEMORY} + restart: unless-stopped + networks: [data, monitoring] + + worker: + image: ${API_IMAGE:-mtusi-messenger:lab5} + command: ["/app/messenger", "worker"] + environment: + DATABASE_URL: ${DATABASE_URL} + DB_MIN_CONNS: ${DB_MIN_CONNS} + DB_MAX_CONNS: ${DB_MAX_CONNS} + DB_MAX_CONNECTION_AGE_SECONDS: ${DB_MAX_CONNECTION_AGE_SECONDS} + DB_MAX_CONNECTION_IDLE_SECONDS: ${DB_MAX_CONNECTION_IDLE_SECONDS} + DB_HEALTH_CHECK_SECONDS: ${DB_HEALTH_CHECK_SECONDS} + DEPENDENCY_TIMEOUT_SECONDS: ${DEPENDENCY_TIMEOUT_SECONDS} + S3_ENDPOINT: ${S3_ENDPOINT} + S3_REGION: ${S3_REGION} + S3_BUCKET: ${S3_BUCKET} + S3_ACCESS_KEY: ${S3_ACCESS_KEY} + S3_SECRET_KEY: ${S3_SECRET_KEY} + S3_USE_TLS: ${S3_USE_TLS} + S3_TIMEOUT_SECONDS: ${S3_TIMEOUT_SECONDS} + BROKER_URL: ${BROKER_URL} + BROKER_STREAM: ${BROKER_STREAM} + BROKER_CONSUMER_GROUP: ${BROKER_CONSUMER_GROUP} + BROKER_FAILED_STREAM: ${BROKER_FAILED_STREAM} + BROKER_FAILED_MAX_LENGTH: ${BROKER_FAILED_MAX_LENGTH} + WORKER_CONCURRENCY: ${WORKER_CONCURRENCY} + WORKER_PREFETCH: ${WORKER_PREFETCH} + WORKER_LEASE_SECONDS: ${WORKER_LEASE_SECONDS} + WORKER_PROCESSING_TIMEOUT_SECONDS: ${WORKER_PROCESSING_TIMEOUT_SECONDS} + WORKER_READ_BLOCK_SECONDS: ${WORKER_READ_BLOCK_SECONDS} + WORKER_RECLAIM_SECONDS: ${WORKER_RECLAIM_SECONDS} + WORKER_MAX_ATTEMPTS: ${WORKER_MAX_ATTEMPTS} + WORKER_RETRY_BASE_SECONDS: ${WORKER_RETRY_BASE_SECONDS} + WORKER_RETRY_MAX_SECONDS: ${WORKER_RETRY_MAX_SECONDS} + ATTACHMENT_MAX_BYTES: ${ATTACHMENT_MAX_BYTES} + ATTACHMENT_MAX_PIXELS: ${ATTACHMENT_MAX_PIXELS} + WORKER_MEMORY_BYTES: ${WORKER_MEMORY_BYTES} + PIPELINE_METRICS_HOST: 0.0.0.0 + PIPELINE_METRICS_PORT: 9091 + expose: + - "9091" + depends_on: + migrate: + condition: service_completed_successfully + redis-broker: + condition: service_healthy + s3-bootstrap: + condition: service_completed_successfully + healthcheck: + test: ["CMD-SHELL", "wget -q -O /dev/null http://127.0.0.1:9091/health/live || exit 1"] + interval: 5s + timeout: 2s + retries: 20 + cpus: ${WORKER_CPUS} + mem_limit: ${WORKER_MEMORY} + stop_grace_period: 8s + restart: unless-stopped + networks: [data, monitoring] + nginx: build: context: . @@ -222,6 +346,55 @@ services: mem_limit: ${TEST_MEMORY} networks: [data] + lab5-experiment: + profiles: ["experiments"] + image: ${API_IMAGE:-mtusi-messenger:lab5} + command: ["/app/lab5experiment"] + environment: + DATABASE_URL: ${DATABASE_URL} + DB_MIN_CONNS: ${DB_MIN_CONNS} + DB_MAX_CONNS: ${DB_MAX_CONNS} + DB_MAX_CONNECTION_AGE_SECONDS: ${DB_MAX_CONNECTION_AGE_SECONDS} + DB_MAX_CONNECTION_IDLE_SECONDS: ${DB_MAX_CONNECTION_IDLE_SECONDS} + DB_HEALTH_CHECK_SECONDS: ${DB_HEALTH_CHECK_SECONDS} + DEPENDENCY_TIMEOUT_SECONDS: ${DEPENDENCY_TIMEOUT_SECONDS} + S3_ENDPOINT: ${S3_ENDPOINT} + S3_REGION: ${S3_REGION} + S3_BUCKET: ${S3_BUCKET} + S3_ACCESS_KEY: ${S3_ACCESS_KEY} + S3_SECRET_KEY: ${S3_SECRET_KEY} + S3_USE_TLS: ${S3_USE_TLS} + S3_TIMEOUT_SECONDS: ${S3_TIMEOUT_SECONDS} + BROKER_URL: ${BROKER_URL} + BROKER_STREAM: ${BROKER_STREAM} + BROKER_CONSUMER_GROUP: ${BROKER_CONSUMER_GROUP} + BROKER_FAILED_STREAM: ${BROKER_FAILED_STREAM} + BROKER_FAILED_MAX_LENGTH: ${BROKER_FAILED_MAX_LENGTH} + WORKER_CONCURRENCY: 1 + WORKER_PREFETCH: 32 + WORKER_LEASE_SECONDS: 2 + WORKER_PROCESSING_TIMEOUT_SECONDS: 1 + WORKER_READ_BLOCK_SECONDS: 1 + WORKER_RECLAIM_SECONDS: 2 + WORKER_MAX_ATTEMPTS: ${WORKER_MAX_ATTEMPTS} + WORKER_RETRY_BASE_SECONDS: 1 + WORKER_RETRY_MAX_SECONDS: 4 + ATTACHMENT_MAX_BYTES: ${ATTACHMENT_MAX_BYTES} + ATTACHMENT_MAX_PIXELS: ${ATTACHMENT_MAX_PIXELS} + WORKER_MEMORY_BYTES: ${WORKER_MEMORY_BYTES} + PIPELINE_METRICS_HOST: 0.0.0.0 + PIPELINE_METRICS_PORT: 9091 + depends_on: + migrate: + condition: service_completed_successfully + redis-broker: + condition: service_healthy + s3-bootstrap: + condition: service_completed_successfully + cpus: ${TEST_CPUS} + mem_limit: ${TEST_MEMORY} + networks: [application, data] + postgres-exporter: image: quay.io/prometheuscommunity/postgres-exporter:v0.20.1 environment: @@ -251,6 +424,20 @@ services: restart: unless-stopped networks: [data, monitoring] + redis-broker-exporter: + image: oliver006/redis_exporter:v1.89.0-alpine + environment: + REDIS_ADDR: redis://redis-broker:6379 + REDIS_USER: default + REDIS_PASSWORD: ${REDIS_BROKER_PASSWORD} + depends_on: + redis-broker: + condition: service_healthy + cpus: ${EXPORTER_CPUS} + mem_limit: ${EXPORTER_MEMORY} + restart: unless-stopped + networks: [data, monitoring] + nginx-exporter: image: nginx/nginx-prometheus-exporter:1.5.1 command: ["--nginx.scrape-uri=http://nginx:8081/stub_status"] @@ -285,6 +472,9 @@ services: - nginx-exporter - postgres-exporter - valkey-exporter + - redis-broker-exporter + - dispatcher + - worker - s3 cpus: ${PROMETHEUS_CPUS} mem_limit: ${PROMETHEUS_MEMORY} @@ -314,6 +504,7 @@ services: volumes: postgres_data: valkey_data: + redis_broker_data: prometheus_data: grafana_data: s3_data: diff --git a/deploy/compose.images.yaml b/deploy/compose.images.yaml index 4251b210..7d983198 100644 --- a/deploy/compose.images.yaml +++ b/deploy/compose.images.yaml @@ -5,6 +5,12 @@ services: migrate: image: ${API_IMAGE:?API_IMAGE is required} + dispatcher: + image: ${API_IMAGE:?API_IMAGE is required} + + worker: + image: ${API_IMAGE:?API_IMAGE is required} + nginx: image: ${NGINX_IMAGE:?NGINX_IMAGE is required} diff --git a/lab5.env.example b/lab5.env.example index 5ad34dcb..c9d7b28f 100644 --- a/lab5.env.example +++ b/lab5.env.example @@ -26,6 +26,32 @@ DATABASE_URL=postgres://messenger:local-postgres-password@postgres:5432/messenge VALKEY_PASSWORD=local-valkey-password CACHE_URL=redis://default:local-valkey-password@valkey:6379/0 +# Redis Streams используется только как устойчивый брокер заданий. +REDIS_BROKER_PASSWORD=local-redis-broker-password +BROKER_URL=redis://default:local-redis-broker-password@redis-broker:6379/0 +BROKER_STREAM=attachment-jobs +BROKER_CONSUMER_GROUP=attachment-workers +BROKER_FAILED_STREAM=attachment-jobs-failed +BROKER_FAILED_MAX_LENGTH=10000 + +# Версия pipeline, outbox и ограниченные повторы worker. +PIPELINE_VERSION=1 +OUTBOX_BATCH_SIZE=32 +OUTBOX_LEASE_SECONDS=15 +OUTBOX_POLL_SECONDS=1 +OUTBOX_RECOVERY_SECONDS=10 +OUTBOX_RETRY_SECONDS=2 +WORKER_CONCURRENCY=2 +WORKER_PREFETCH=4 +WORKER_LEASE_SECONDS=30 +WORKER_PROCESSING_TIMEOUT_SECONDS=20 +WORKER_READ_BLOCK_SECONDS=2 +WORKER_RECLAIM_SECONDS=5 +WORKER_MAX_ATTEMPTS=5 +WORKER_RETRY_BASE_SECONDS=1 +WORKER_RETRY_MAX_SECONDS=30 +WORKER_MEMORY_BYTES=738197504 + # Административные данные MinIO используются только при bootstrap. MINIO_ROOT_USER=local-minio-admin MINIO_ROOT_PASSWORD=local-minio-admin-password @@ -91,6 +117,12 @@ POSTGRES_CPUS=2 POSTGRES_MEMORY=1g VALKEY_CPUS=1 VALKEY_MEMORY=256m +REDIS_BROKER_CPUS=1 +REDIS_BROKER_MEMORY=256m +DISPATCHER_CPUS=0.5 +DISPATCHER_MEMORY=128m +WORKER_CPUS=2 +WORKER_MEMORY=1g S3_CPUS=1 S3_MEMORY=512m S3_BOOTSTRAP_CPUS=0.25 diff --git a/monitoring/grafana/dashboards/messenger.json b/monitoring/grafana/dashboards/messenger.json index 0478e84b..a4d0a5b0 100644 --- a/monitoring/grafana/dashboards/messenger.json +++ b/monitoring/grafana/dashboards/messenger.json @@ -1,143 +1,638 @@ { - "annotations": {"list": []}, + "annotations": { + "list": [] + }, "editable": false, "graphTooltip": 1, "panels": [ { - "type": "timeseries", "title": "API requests per second", "id": 1, - "gridPos": {"h": 8, "w": 12, "x": 0, "y": 0}, - "targets": [{"expr": "sum(rate(messenger_http_requests_total[1m]))", "legendFormat": "total", "refId": "A"}] - }, - { - "type": "timeseries", "title": "API RPS by replica", "id": 2, - "gridPos": {"h": 8, "w": 12, "x": 12, "y": 0}, - "targets": [{"expr": "sum by (instance) (rate(messenger_http_requests_total[1m]))", "legendFormat": "{{instance}}", "refId": "A"}] - }, - { - "type": "timeseries", "title": "API responses by status class", "id": 3, - "gridPos": {"h": 8, "w": 12, "x": 0, "y": 8}, - "targets": [{"expr": "sum by (status_class) (rate(messenger_http_requests_total[1m]))", "legendFormat": "{{status_class}}", "refId": "A"}] - }, - { - "type": "timeseries", "title": "Healthy API replicas", "id": 4, - "gridPos": {"h": 8, "w": 12, "x": 12, "y": 8}, - "targets": [{"expr": "sum(up{job=\"messenger\"})", "legendFormat": "replicas", "refId": "A"}] - }, - { - "type": "timeseries", "title": "API latency p95 by replica and route", "id": 5, - "gridPos": {"h": 8, "w": 24, "x": 0, "y": 16}, - "fieldConfig": {"defaults": {"unit": "s"}, "overrides": []}, - "targets": [{"expr": "histogram_quantile(0.95, sum by (le, instance, route) (rate(messenger_http_request_duration_seconds_bucket[5m])))", "legendFormat": "{{instance}} {{route}}", "refId": "A"}] - }, - { - "type": "timeseries", "title": "API in flight", "id": 6, - "gridPos": {"h": 8, "w": 8, "x": 0, "y": 24}, - "targets": [{"expr": "messenger_http_requests_in_flight", "legendFormat": "{{instance}}", "refId": "A"}] - }, - { - "type": "timeseries", "title": "Go heap by replica", "id": 7, - "gridPos": {"h": 8, "w": 8, "x": 8, "y": 24}, - "fieldConfig": {"defaults": {"unit": "bytes"}, "overrides": []}, - "targets": [{"expr": "go_memstats_heap_alloc_bytes{job=\"messenger\"}", "legendFormat": "{{instance}}", "refId": "A"}] - }, - { - "type": "timeseries", "title": "Idempotency replays", "id": 8, - "gridPos": {"h": 8, "w": 8, "x": 16, "y": 24}, - "targets": [{"expr": "sum(rate(messenger_http_idempotency_replays_total[1m]))", "legendFormat": "replays/s", "refId": "A"}] - }, - { - "type": "timeseries", "title": "Nginx connections and requests", "id": 9, - "gridPos": {"h": 8, "w": 12, "x": 0, "y": 32}, + "type": "timeseries", + "title": "API requests per second", + "id": 1, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 0 + }, "targets": [ - {"expr": "nginx_connections_active", "legendFormat": "active connections", "refId": "A"}, - {"expr": "rate(nginx_http_requests_total[1m])", "legendFormat": "requests/s", "refId": "B"} + { + "expr": "sum(rate(messenger_http_requests_total[1m]))", + "legendFormat": "total", + "refId": "A" + } ] }, { - "type": "timeseries", "title": "PostgreSQL connections", "id": 10, - "gridPos": {"h": 8, "w": 12, "x": 12, "y": 32}, - "targets": [{"expr": "sum(pg_stat_activity_count{datname=\"messenger\"})", "legendFormat": "connections", "refId": "A"}] - }, - { - "type": "timeseries", "title": "PostgreSQL transactions", "id": 11, - "gridPos": {"h": 8, "w": 12, "x": 0, "y": 40}, + "type": "timeseries", + "title": "API RPS by replica", + "id": 2, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 0 + }, "targets": [ - {"expr": "rate(pg_stat_database_xact_commit{datname=\"messenger\"}[1m])", "legendFormat": "commit/s", "refId": "A"}, - {"expr": "rate(pg_stat_database_xact_rollback{datname=\"messenger\"}[1m])", "legendFormat": "rollback/s", "refId": "B"} + { + "expr": "sum by (instance) (rate(messenger_http_requests_total[1m]))", + "legendFormat": "{{instance}}", + "refId": "A" + } ] }, { - "type": "timeseries", "title": "PostgreSQL database size", "id": 12, - "gridPos": {"h": 8, "w": 12, "x": 12, "y": 40}, - "fieldConfig": {"defaults": {"unit": "bytes"}, "overrides": []}, - "targets": [{"expr": "pg_database_size_bytes{datname=\"messenger\"}", "legendFormat": "database", "refId": "A"}] - }, - { - "type": "timeseries", "title": "Valkey memory", "id": 13, - "gridPos": {"h": 8, "w": 12, "x": 0, "y": 48}, - "fieldConfig": {"defaults": {"unit": "bytes"}, "overrides": []}, - "targets": [{"expr": "redis_memory_used_bytes", "legendFormat": "used", "refId": "A"}] - }, - { - "type": "timeseries", "title": "Valkey keys and commands", "id": 14, - "gridPos": {"h": 8, "w": 12, "x": 12, "y": 48}, + "type": "timeseries", + "title": "API responses by status class", + "id": 3, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 8 + }, "targets": [ - {"expr": "sum(redis_db_keys)", "legendFormat": "keys", "refId": "A"}, - {"expr": "rate(redis_commands_processed_total[1m])", "legendFormat": "commands/s", "refId": "B"} + { + "expr": "sum by (status_class) (rate(messenger_http_requests_total[1m]))", + "legendFormat": "{{status_class}}", + "refId": "A" + } ] }, { - "type": "timeseries", "title": "Attachment uploads by result", "id": 15, - "gridPos": {"h": 8, "w": 12, "x": 0, "y": 56}, - "targets": [{"expr": "sum by (result) (rate(messenger_attachment_uploads_total[1m]))", "legendFormat": "{{result}}", "refId": "A"}] - }, - { - "type": "timeseries", "title": "Attachment upload latency p95", "id": 16, - "gridPos": {"h": 8, "w": 12, "x": 12, "y": 56}, - "fieldConfig": {"defaults": {"unit": "s"}, "overrides": []}, - "targets": [{"expr": "histogram_quantile(0.95, sum by (le) (rate(messenger_attachment_upload_duration_seconds_bucket[5m])))", "legendFormat": "p95", "refId": "A"}] - }, - { - "type": "timeseries", "title": "Attachment upload traffic", "id": 17, - "gridPos": {"h": 8, "w": 8, "x": 0, "y": 64}, - "fieldConfig": {"defaults": {"unit": "Bps"}, "overrides": []}, - "targets": [{"expr": "sum(rate(messenger_attachment_upload_bytes_total[1m]))", "legendFormat": "bytes/s", "refId": "A"}] - }, - { - "type": "timeseries", "title": "MinIO objects", "id": 18, - "gridPos": {"h": 8, "w": 8, "x": 8, "y": 64}, - "targets": [{"expr": "sum(minio_cluster_usage_object_total)", "legendFormat": "objects", "refId": "A"}] - }, - { - "type": "timeseries", "title": "MinIO requests and errors", "id": 19, - "gridPos": {"h": 8, "w": 8, "x": 16, "y": 64}, + "type": "timeseries", + "title": "Healthy API replicas", + "id": 4, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 8 + }, "targets": [ - {"expr": "sum(rate(minio_s3_requests_total[1m]))", "legendFormat": "requests/s", "refId": "A"}, - {"expr": "sum(rate(minio_s3_requests_errors_total[1m]))", "legendFormat": "errors/s", "refId": "B"} + { + "expr": "sum(up{job=\"messenger\"})", + "legendFormat": "replicas", + "refId": "A" + } ] }, { - "type": "timeseries", "title": "MinIO availability", "id": 20, - "gridPos": {"h": 8, "w": 12, "x": 0, "y": 72}, - "targets": [{"expr": "up{job=\"minio\"}", "legendFormat": "up", "refId": "A"}] + "type": "timeseries", + "title": "API latency p95 by replica and route", + "id": 5, + "gridPos": { + "h": 8, + "w": 24, + "x": 0, + "y": 16 + }, + "fieldConfig": { + "defaults": { + "unit": "s" + }, + "overrides": [] + }, + "targets": [ + { + "expr": "histogram_quantile(0.95, sum by (le, instance, route) (rate(messenger_http_request_duration_seconds_bucket[5m])))", + "legendFormat": "{{instance}} {{route}}", + "refId": "A" + } + ] }, { - "type": "timeseries", "title": "MinIO storage", "id": 21, - "gridPos": {"h": 8, "w": 12, "x": 12, "y": 72}, - "fieldConfig": {"defaults": {"unit": "bytes"}, "overrides": []}, + "type": "timeseries", + "title": "API in flight", + "id": 6, + "gridPos": { + "h": 8, + "w": 8, + "x": 0, + "y": 24 + }, "targets": [ - {"expr": "sum(minio_cluster_usage_total_bytes)", "legendFormat": "used", "refId": "A"}, - {"expr": "sum(minio_cluster_capacity_usable_free_bytes)", "legendFormat": "free", "refId": "B"} + { + "expr": "messenger_http_requests_in_flight", + "legendFormat": "{{instance}}", + "refId": "A" + } + ] + }, + { + "type": "timeseries", + "title": "Go heap by replica", + "id": 7, + "gridPos": { + "h": 8, + "w": 8, + "x": 8, + "y": 24 + }, + "fieldConfig": { + "defaults": { + "unit": "bytes" + }, + "overrides": [] + }, + "targets": [ + { + "expr": "go_memstats_heap_alloc_bytes{job=\"messenger\"}", + "legendFormat": "{{instance}}", + "refId": "A" + } + ] + }, + { + "type": "timeseries", + "title": "Idempotency replays", + "id": 8, + "gridPos": { + "h": 8, + "w": 8, + "x": 16, + "y": 24 + }, + "targets": [ + { + "expr": "sum(rate(messenger_http_idempotency_replays_total[1m]))", + "legendFormat": "replays/s", + "refId": "A" + } + ] + }, + { + "type": "timeseries", + "title": "Nginx connections and requests", + "id": 9, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 32 + }, + "targets": [ + { + "expr": "nginx_connections_active", + "legendFormat": "active connections", + "refId": "A" + }, + { + "expr": "rate(nginx_http_requests_total[1m])", + "legendFormat": "requests/s", + "refId": "B" + } + ] + }, + { + "type": "timeseries", + "title": "PostgreSQL connections", + "id": 10, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 32 + }, + "targets": [ + { + "expr": "sum(pg_stat_activity_count{datname=\"messenger\"})", + "legendFormat": "connections", + "refId": "A" + } + ] + }, + { + "type": "timeseries", + "title": "PostgreSQL transactions", + "id": 11, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 40 + }, + "targets": [ + { + "expr": "rate(pg_stat_database_xact_commit{datname=\"messenger\"}[1m])", + "legendFormat": "commit/s", + "refId": "A" + }, + { + "expr": "rate(pg_stat_database_xact_rollback{datname=\"messenger\"}[1m])", + "legendFormat": "rollback/s", + "refId": "B" + } + ] + }, + { + "type": "timeseries", + "title": "PostgreSQL database size", + "id": 12, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 40 + }, + "fieldConfig": { + "defaults": { + "unit": "bytes" + }, + "overrides": [] + }, + "targets": [ + { + "expr": "pg_database_size_bytes{datname=\"messenger\"}", + "legendFormat": "database", + "refId": "A" + } + ] + }, + { + "type": "timeseries", + "title": "Valkey memory", + "id": 13, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 48 + }, + "fieldConfig": { + "defaults": { + "unit": "bytes" + }, + "overrides": [] + }, + "targets": [ + { + "expr": "redis_memory_used_bytes{job=\"valkey\"}", + "legendFormat": "used", + "refId": "A" + } + ] + }, + { + "type": "timeseries", + "title": "Valkey keys and commands", + "id": 14, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 48 + }, + "targets": [ + { + "expr": "sum(redis_db_keys{job=\"valkey\"})", + "legendFormat": "keys", + "refId": "A" + }, + { + "expr": "rate(redis_commands_processed_total{job=\"valkey\"}[1m])", + "legendFormat": "commands/s", + "refId": "B" + } + ] + }, + { + "type": "timeseries", + "title": "Attachment uploads by result", + "id": 15, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 56 + }, + "targets": [ + { + "expr": "sum by (result) (rate(messenger_attachment_uploads_total[1m]))", + "legendFormat": "{{result}}", + "refId": "A" + } + ] + }, + { + "type": "timeseries", + "title": "Attachment upload latency p95", + "id": 16, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 56 + }, + "fieldConfig": { + "defaults": { + "unit": "s" + }, + "overrides": [] + }, + "targets": [ + { + "expr": "histogram_quantile(0.95, sum by (le) (rate(messenger_attachment_upload_duration_seconds_bucket[5m])))", + "legendFormat": "p95", + "refId": "A" + } + ] + }, + { + "type": "timeseries", + "title": "Attachment upload traffic", + "id": 17, + "gridPos": { + "h": 8, + "w": 8, + "x": 0, + "y": 64 + }, + "fieldConfig": { + "defaults": { + "unit": "Bps" + }, + "overrides": [] + }, + "targets": [ + { + "expr": "sum(rate(messenger_attachment_upload_bytes_total[1m]))", + "legendFormat": "bytes/s", + "refId": "A" + } + ] + }, + { + "type": "timeseries", + "title": "MinIO objects", + "id": 18, + "gridPos": { + "h": 8, + "w": 8, + "x": 8, + "y": 64 + }, + "targets": [ + { + "expr": "sum(minio_cluster_usage_object_total)", + "legendFormat": "objects", + "refId": "A" + } + ] + }, + { + "type": "timeseries", + "title": "MinIO requests and errors", + "id": 19, + "gridPos": { + "h": 8, + "w": 8, + "x": 16, + "y": 64 + }, + "targets": [ + { + "expr": "sum(rate(minio_s3_requests_total[1m]))", + "legendFormat": "requests/s", + "refId": "A" + }, + { + "expr": "sum(rate(minio_s3_requests_errors_total[1m]))", + "legendFormat": "errors/s", + "refId": "B" + } + ] + }, + { + "type": "timeseries", + "title": "MinIO availability", + "id": 20, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 72 + }, + "targets": [ + { + "expr": "up{job=\"minio\"}", + "legendFormat": "up", + "refId": "A" + } + ] + }, + { + "type": "timeseries", + "title": "MinIO storage", + "id": 21, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 72 + }, + "fieldConfig": { + "defaults": { + "unit": "bytes" + }, + "overrides": [] + }, + "targets": [ + { + "expr": "sum(minio_cluster_usage_total_bytes)", + "legendFormat": "used", + "refId": "A" + }, + { + "expr": "sum(minio_cluster_capacity_usable_free_bytes)", + "legendFormat": "free", + "refId": "B" + } + ] + }, + { + "type": "timeseries", + "title": "Attachment pipeline throughput", + "id": 22, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 80 + }, + "fieldConfig": { + "defaults": { + "unit": "ops" + }, + "overrides": [] + }, + "targets": [ + { + "expr": "sum by (result) (rate(messenger_attachment_worker_jobs_total[1m]))", + "legendFormat": "{{result}}", + "refId": "A" + } + ] + }, + { + "type": "timeseries", + "title": "Attachment worker latency and concurrency", + "id": 23, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 80 + }, + "fieldConfig": { + "defaults": {}, + "overrides": [] + }, + "targets": [ + { + "expr": "histogram_quantile(0.95, sum by (le) (rate(messenger_attachment_worker_job_duration_seconds_bucket[5m])))", + "legendFormat": "p95", + "refId": "A" + }, + { + "expr": "sum(messenger_attachment_worker_jobs_in_flight)", + "legendFormat": "in flight", + "refId": "B" + } + ] + }, + { + "type": "timeseries", + "title": "Attachment retries and reclaimed deliveries", + "id": 24, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 88 + }, + "fieldConfig": { + "defaults": { + "unit": "ops" + }, + "overrides": [] + }, + "targets": [ + { + "expr": "sum(rate(messenger_attachment_worker_retries_total[1m]))", + "legendFormat": "retries", + "refId": "A" + }, + { + "expr": "sum(rate(messenger_attachment_worker_reclaimed_deliveries_total[1m]))", + "legendFormat": "reclaimed", + "refId": "B" + } + ] + }, + { + "type": "timeseries", + "title": "Transactional outbox", + "id": 25, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 88 + }, + "fieldConfig": { + "defaults": { + "unit": "ops" + }, + "overrides": [] + }, + "targets": [ + { + "expr": "sum(rate(messenger_outbox_published_total[1m]))", + "legendFormat": "published", + "refId": "A" + }, + { + "expr": "sum(rate(messenger_outbox_publish_failures_total[1m]))", + "legendFormat": "publish failures", + "refId": "B" + }, + { + "expr": "sum(rate(messenger_outbox_recovered_leases_total[1m]))", + "legendFormat": "recovered leases", + "refId": "C" + } + ] + }, + { + "type": "timeseries", + "title": "Redis broker memory", + "id": 26, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 96 + }, + "fieldConfig": { + "defaults": { + "unit": "bytes" + }, + "overrides": [] + }, + "targets": [ + { + "expr": "redis_memory_used_bytes{job=\"redis-broker\"}", + "legendFormat": "used", + "refId": "A" + } + ] + }, + { + "type": "timeseries", + "title": "Redis broker commands and keys", + "id": 27, + "gridPos": { + "h": 8, + "w": 12, + "x": 12, + "y": 96 + }, + "fieldConfig": { + "defaults": {}, + "overrides": [] + }, + "targets": [ + { + "expr": "rate(redis_commands_processed_total{job=\"redis-broker\"}[1m])", + "legendFormat": "commands", + "refId": "A" + }, + { + "expr": "sum(redis_db_keys{job=\"redis-broker\"})", + "legendFormat": "keys", + "refId": "B" + } ] } ], "refresh": "5s", "schemaVersion": 41, - "tags": ["highload", "lab4", "nginx", "failover"], - "templating": {"list": []}, - "time": {"from": "now-15m", "to": "now"}, + "tags": [ + "highload", + "lab5", + "nginx", + "failover" + ], + "templating": { + "list": [] + }, + "time": { + "from": "now-15m", + "to": "now" + }, "timezone": "browser", - "title": "Messenger lab4 overview", - "uid": "messenger-overview", - "version": 3 + "title": "Messenger lab5 overview", + "uid": "messenger-lab5", + "version": 2 } diff --git a/monitoring/prometheus.yml b/monitoring/prometheus.yml index 83721c48..1869184e 100644 --- a/monitoring/prometheus.yml +++ b/monitoring/prometheus.yml @@ -22,6 +22,21 @@ scrape_configs: static_configs: - targets: ["valkey-exporter:9121"] + - job_name: redis-broker + static_configs: + - targets: ["redis-broker-exporter:9121"] + + - job_name: attachment-worker + dns_sd_configs: + - names: ["worker"] + type: A + port: 9091 + refresh_interval: 2s + + - job_name: outbox-dispatcher + static_configs: + - targets: ["dispatcher:9091"] + - job_name: minio metrics_path: /minio/v2/metrics/cluster static_configs: diff --git a/scripts/restart.sh b/scripts/restart.sh index a724583d..d5872516 100755 --- a/scripts/restart.sh +++ b/scripts/restart.sh @@ -2,8 +2,8 @@ set -eu -docker compose stop nginx api valkey postgres s3 -docker compose start postgres valkey s3 +docker compose stop nginx api worker dispatcher redis-broker valkey postgres s3 +docker compose start postgres valkey redis-broker s3 docker compose run --rm s3-bootstrap docker compose run --rm migrate docker compose up -d --wait --wait-timeout 90 diff --git a/scripts/rolling-update.sh b/scripts/rolling-update.sh index d02dc02d..695e3f8f 100755 --- a/scripts/rolling-update.sh +++ b/scripts/rolling-update.sh @@ -150,7 +150,7 @@ fi cp "$upstream_file" "$upstream_backup" compose pull api nginx swagger prometheus grafana s3 s3-bootstrap -compose up -d --no-build --wait --wait-timeout 120 postgres valkey s3 +compose up -d --no-build --wait --wait-timeout 120 postgres valkey redis-broker s3 compose run --rm --no-deps s3-bootstrap compose run --rm --no-deps migrate @@ -208,7 +208,7 @@ for container in $candidate_pool; do done smoke_check -compose up -d --no-build --no-deps swagger postgres-exporter valkey-exporter nginx-exporter prometheus grafana +compose up -d --no-build --no-deps dispatcher worker swagger postgres-exporter valkey-exporter redis-broker-exporter nginx-exporter prometheus grafana printf '%s\n' "$candidate_pool" > "$active_containers" cp "$manifest" "$current_manifest"