forked from mtusi2026/mtusi2026lab3
lab3: стенд HAProxy + реплики под compose, лимиты, хуки остановки и квиесценции
- compose.yaml: проект mtusi-lab3, HAProxy как единственная точка входа,
deploy.replicas ${API_REPLICAS:-2}, CPU/RAM-лимиты на каждый сервис,
stop_grace_period 20s, лимиты из переменных (API_CPUS_PER_REPLICA и т.д.),
loopback-порты PostgreSQL/Redis только для операторских проверок; у API
портов нет; container_name нигде не задан.
- infra/haproxy/haproxy.cfg: проба GET /internal/lb-check, inter 500ms/fall 2
(~1s обнаружение), server-template + DNS Docker вместо статического списка,
stats на 127.0.0.1:8404.
- Дрейн: SIGTERM -> реплика сразу перестаёт проходить пробу LB -> пауза
DRAIN_DELAY_MS -> закрытие сервера и пулов. Nest уничтожает провайдеров
раньше beforeApplicationShutdown, поэтому последовательность вынесена в
main.ts, а HealthService отвечает только за флаг not-ready.
- scripts/stop-one.sh (остановка ровно одной реплики по X-Instance-Id),
scripts/start-replica.sh (восстановление до API_REPLICAS), scripts/quiesce.sh
(остановка/возврат посторонних контейнеров хоста), restart.sh перезапускает
и LB, scripts/app-e2e.sh (host-side e2e через loopback-порты).
- Makefile: stand-up/down/status, stand-replica, wait-ready, quiesce-up/down,
test-e2e, test-failover-all, rolling-update, track1, evidence.
- Dockerfile: ARG/ENV BUILD_ID для доказательства rolling update.
Проверено: make test, make test-e2e, make test-persistence (restart.sh),
make test-failover (stop-one.sh, восстановление через start-replica.sh),
make test-dependencies; docker inspect подтверждает NanoCpus/Memory и
отсутствие опубликованных портов у api.
This commit is contained in:
@@ -30,3 +30,4 @@ coverage/
|
||||
.vscode/
|
||||
# Keep small, sanitized evidence summaries in evidence/ under version control.
|
||||
# Inspect git diff --cached: ignore rules are not a secret scanner.
|
||||
.lab3-quiesce.json
|
||||
|
||||
@@ -16,6 +16,11 @@ FROM node:25.7.0-slim AS runtime
|
||||
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
# BUILD_ID доказывается через GET /internal/metrics и меняется при rolling
|
||||
# update: он показывает, какая именно сборка отвечает на запрос.
|
||||
ARG BUILD_ID=unknown
|
||||
ENV BUILD_ID=$BUILD_ID
|
||||
|
||||
ENV NODE_ENV=production
|
||||
ENV APP_PORT=8080
|
||||
ENV DRAIN_DELAY_MS=5000
|
||||
|
||||
@@ -4,11 +4,23 @@ CA_FILE ?=
|
||||
ACTION_SCRIPT ?=
|
||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||
COMPOSE_ENV_FILE ?= .env
|
||||
COMPOSE = docker compose --env-file $(COMPOSE_ENV_FILE)
|
||||
LAB3 = $(PYTHON) scripts/lab3/lab3.py
|
||||
FAILOVER_RUNS ?= 3
|
||||
RATE ?= 200
|
||||
TRACK1_ARGS ?=
|
||||
|
||||
.PHONY: help check validate test load test-persistence test-failover test-dependencies
|
||||
.PHONY: help check validate test load test-persistence test-failover test-dependencies \
|
||||
stand-up stand-down stand-status stand-replica wait-ready quiesce-up quiesce-down \
|
||||
test-e2e test-failover-all rolling-update track1 evidence
|
||||
|
||||
help:
|
||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes; test-dependencies: stops/restarts Redis and PostgreSQL"
|
||||
@echo "stand-up/stand-down/stand-status: HAProxy + API x API_REPLICAS over shared PostgreSQL and Redis"
|
||||
@echo "stand-replica: restore one missing API replica; wait-ready: wait for /health/ready"
|
||||
@echo "quiesce-up/quiesce-down: stop and restore unrelated background containers of the host"
|
||||
@echo "test-e2e: app black-box suite against the stand; test-failover-all: failover for every replica in turn"
|
||||
@echo "rolling-update: replace replicas one by one under load; track1: full scaling-price series; evidence: rebuild aggregates and graphs"
|
||||
|
||||
check:
|
||||
$(PYTHON) scripts/check_contract.py
|
||||
@@ -32,3 +44,50 @@ test-failover:
|
||||
|
||||
test-dependencies:
|
||||
cd app && E2E_COMPOSE_DIR="$(CURDIR)" E2E_COMPOSE_ENV_FILE="$(COMPOSE_ENV_FILE)" npm run test:dependencies
|
||||
|
||||
stand-up:
|
||||
$(COMPOSE) up -d --build --wait
|
||||
|
||||
stand-down:
|
||||
$(COMPOSE) down
|
||||
|
||||
stand-status:
|
||||
$(COMPOSE) ps
|
||||
|
||||
stand-replica:
|
||||
scripts/start-replica.sh
|
||||
|
||||
wait-ready:
|
||||
@for attempt in $$(seq 1 60); do \
|
||||
if curl -fsS "$(BASE_URL)/health/ready" >/dev/null 2>&1; then \
|
||||
echo "stand is ready ($(BASE_URL))"; exit 0; \
|
||||
fi; \
|
||||
sleep 1; \
|
||||
done; \
|
||||
echo "stand did not become ready within 60s" >&2; exit 1
|
||||
|
||||
quiesce-up:
|
||||
scripts/quiesce.sh up
|
||||
|
||||
quiesce-down:
|
||||
scripts/quiesce.sh down
|
||||
|
||||
test-e2e:
|
||||
scripts/app-e2e.sh
|
||||
|
||||
test-failover-all:
|
||||
@for run in $$(seq 1 $(FAILOVER_RUNS)); do \
|
||||
echo "=== failover run $$run of $(FAILOVER_RUNS)"; \
|
||||
$(MAKE) test-failover ACTION_SCRIPT=scripts/stop-one.sh || exit 1; \
|
||||
scripts/start-replica.sh; \
|
||||
$(MAKE) wait-ready; \
|
||||
done
|
||||
|
||||
rolling-update:
|
||||
$(LAB3) rolling-update --under-load --rate $(RATE)
|
||||
|
||||
track1:
|
||||
$(LAB3) track1 $(TRACK1_ARGS)
|
||||
|
||||
evidence:
|
||||
$(LAB3) analyze
|
||||
|
||||
@@ -6,11 +6,9 @@ describe('HealthService', () => {
|
||||
it('reports liveness and readiness when dependencies are healthy', async () => {
|
||||
const database = { health: vi.fn().mockResolvedValue(undefined) };
|
||||
const redis = { health: vi.fn().mockResolvedValue(undefined) };
|
||||
const config = { get: vi.fn().mockReturnValue(0) };
|
||||
const service = new HealthService(
|
||||
database as never,
|
||||
redis as never,
|
||||
config as never,
|
||||
);
|
||||
expect(service.getLiveness()).toEqual({ status: 'ok' });
|
||||
await expect(service.getReadiness()).resolves.toEqual({ status: 'ok' });
|
||||
@@ -24,7 +22,6 @@ describe('HealthService', () => {
|
||||
const service = new HealthService(
|
||||
database as never,
|
||||
redis as never,
|
||||
{ get: vi.fn() } as never,
|
||||
);
|
||||
await expect(service.getReadiness()).rejects.toBeInstanceOf(
|
||||
ServiceUnavailableException,
|
||||
@@ -35,7 +32,6 @@ describe('HealthService', () => {
|
||||
const service = new HealthService(
|
||||
{ health: vi.fn() } as never,
|
||||
{ health: vi.fn() } as never,
|
||||
{ get: vi.fn() } as never,
|
||||
);
|
||||
service.setDraining();
|
||||
expect(service.getLiveness()).toEqual({ status: 'ok' });
|
||||
@@ -48,7 +44,6 @@ describe('HealthService', () => {
|
||||
const service = new HealthService(
|
||||
{ health: vi.fn().mockRejectedValue(new Error('down')) } as never,
|
||||
{ health: vi.fn().mockRejectedValue(new Error('down')) } as never,
|
||||
{ get: vi.fn() } as never,
|
||||
);
|
||||
expect(service.getLbCheck()).toEqual({ status: 'ok' });
|
||||
service.setDraining();
|
||||
|
||||
@@ -4,10 +4,8 @@ import {
|
||||
Module,
|
||||
ServiceUnavailableException,
|
||||
} from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { DatabaseClient } from '../database/database.client.js';
|
||||
import { RedisClient } from '../redis/redis.client.js';
|
||||
import type { AppConfig } from '../config/app-config.schema.js';
|
||||
|
||||
@Injectable()
|
||||
export class HealthService {
|
||||
@@ -15,7 +13,6 @@ export class HealthService {
|
||||
constructor(
|
||||
private readonly database: DatabaseClient,
|
||||
private readonly redis: RedisClient,
|
||||
private readonly config: ConfigService<AppConfig>,
|
||||
) {}
|
||||
getLiveness() {
|
||||
return { status: 'ok' as const };
|
||||
@@ -40,16 +37,13 @@ export class HealthService {
|
||||
}
|
||||
return { status: 'ok' as const };
|
||||
}
|
||||
// Called on SIGTERM before the drain delay: the replica turns not-ready for
|
||||
// both the LB probe and the operator probe while it finishes in-flight
|
||||
// requests. The delay itself lives in main.ts, because Nest destroys
|
||||
// providers (closing the pools) before this class could serve anyone.
|
||||
setDraining() {
|
||||
this.ready = false;
|
||||
}
|
||||
async beforeApplicationShutdown(signal?: string): Promise<void> {
|
||||
this.setDraining();
|
||||
const delay = this.config.get<number>('DRAIN_DELAY_MS') ?? 5000;
|
||||
if (signal && delay > 0) {
|
||||
await new Promise<void>((resolve) => setTimeout(resolve, delay));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Global()
|
||||
|
||||
+24
-1
@@ -7,6 +7,7 @@ import {
|
||||
NestFastifyApplication,
|
||||
} from '@nestjs/platform-fastify';
|
||||
import { AppModule } from './app.module.js';
|
||||
import { HealthService } from './health/health.service.js';
|
||||
import { MetricsService } from './metrics/metrics.service.js';
|
||||
import { RequestIdInterceptor } from './common/http/request-id.interceptor.js';
|
||||
|
||||
@@ -29,12 +30,34 @@ async function bootstrap(): Promise<void> {
|
||||
transform: true,
|
||||
}),
|
||||
);
|
||||
app.enableShutdownHooks();
|
||||
|
||||
const config = app.get(ConfigService);
|
||||
const port = config.getOrThrow<number>('APP_PORT');
|
||||
const host = config.getOrThrow<string>('APP_HOST');
|
||||
await app.listen(port, host);
|
||||
|
||||
// Drain before teardown. Nest's own shutdown sequence destroys providers
|
||||
// (closing the database pool and Redis) before `beforeApplicationShutdown`
|
||||
// runs, so relying on it would keep the replica listed in the load balancer
|
||||
// while it can no longer serve: SIGTERM must first turn the replica
|
||||
// not-ready for the LB probe, then wait DRAIN_DELAY_MS, and only then close
|
||||
// the server and the pools.
|
||||
const health = app.get(HealthService);
|
||||
const drainDelayMs = config.getOrThrow<number>('DRAIN_DELAY_MS');
|
||||
let shuttingDown = false;
|
||||
const shutdown = async (): Promise<void> => {
|
||||
if (shuttingDown) return;
|
||||
shuttingDown = true;
|
||||
health.setDraining();
|
||||
if (drainDelayMs > 0)
|
||||
await new Promise<void>((resolve) => setTimeout(resolve, drainDelayMs));
|
||||
await app.close();
|
||||
process.exit(0);
|
||||
};
|
||||
for (const signal of ['SIGTERM', 'SIGINT', 'SIGHUP'] as const)
|
||||
process.once(signal, () => {
|
||||
void shutdown();
|
||||
});
|
||||
}
|
||||
|
||||
await bootstrap();
|
||||
|
||||
+174
@@ -0,0 +1,174 @@
|
||||
# Стенд lab3: клиент -> HAProxy -> API x N -> общие PostgreSQL и Redis.
|
||||
# Одна команда запуска: make stand-up (docker compose --env-file .env up -d --build --wait)
|
||||
#
|
||||
# Проект запинен именем, чтобы имена контейнеров и labels были предсказуемы для
|
||||
# scripts/stop-one.sh; container_name нигде не задан, иначе масштабирование
|
||||
# одного Compose-service стало бы невозможным.
|
||||
name: mtusi-lab3
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:16.4-alpine
|
||||
# pg_stat_statements включён для трека 1 (агрегаты SQL по репликам);
|
||||
# max_connections поднят под N x DATABASE_POOL_MAX.
|
||||
command:
|
||||
[
|
||||
'postgres',
|
||||
'-c',
|
||||
'shared_preload_libraries=pg_stat_statements',
|
||||
'-c',
|
||||
'max_connections=120',
|
||||
'-c',
|
||||
'shared_buffers=128MB',
|
||||
'-c',
|
||||
'effective_cache_size=384MB',
|
||||
]
|
||||
environment:
|
||||
POSTGRES_DB: ${POSTGRES_DB:-messenger}
|
||||
POSTGRES_USER: ${POSTGRES_USER:-messenger}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}
|
||||
# Только loopback: операторские проверки (host-side npm run test:e2e,
|
||||
# psql в evidence). API-реплики портов не публикуют вовсе.
|
||||
ports:
|
||||
- '127.0.0.1:${POSTGRES_PUBLISHED_PORT:-5432}:5432'
|
||||
volumes:
|
||||
- postgres-data:/var/lib/postgresql/data
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '${PG_CPUS:-1.5}'
|
||||
memory: ${PG_MEMORY:-512m}
|
||||
healthcheck:
|
||||
test:
|
||||
[
|
||||
'CMD-SHELL',
|
||||
'pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}',
|
||||
]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
|
||||
redis:
|
||||
image: redis:7.4.2-alpine
|
||||
# appendonly: сессии и кэш переживают рестарт контейнера (проверка
|
||||
# сохранности с неизменным токеном). volatile-lru вытесняет только
|
||||
# ключи с TTL; в стенде у них ограниченный набор и объём.
|
||||
command:
|
||||
[
|
||||
'redis-server',
|
||||
'--appendonly',
|
||||
'yes',
|
||||
'--appendfsync',
|
||||
'everysec',
|
||||
'--maxmemory',
|
||||
'96mb',
|
||||
'--maxmemory-policy',
|
||||
'volatile-lru',
|
||||
]
|
||||
ports:
|
||||
- '127.0.0.1:${REDIS_PUBLISHED_PORT:-6379}:6379'
|
||||
volumes:
|
||||
- redis-data:/data
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '${REDIS_CPUS:-0.25}'
|
||||
memory: ${REDIS_MEMORY:-128m}
|
||||
healthcheck:
|
||||
test: ['CMD', 'redis-cli', 'ping']
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
|
||||
# Миграции выполняются один раз отдельным сервисом, а не гонкой реплик:
|
||||
# api стартует только после его успешного завершения.
|
||||
migrate:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
image: ${API_IMAGE:-mtusi-messenger:lab3}
|
||||
command: ['node', 'dist/database/migration.runner.js']
|
||||
env_file: [.env]
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '0.5'
|
||||
memory: 192m
|
||||
depends_on:
|
||||
postgres: { condition: service_healthy }
|
||||
redis: { condition: service_healthy }
|
||||
restart: 'no'
|
||||
|
||||
api:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
args:
|
||||
BUILD_ID: ${BUILD_ID:-unknown}
|
||||
image: ${API_IMAGE:-mtusi-messenger:lab3}
|
||||
# Портов нет намеренно: единственная точка входа — HAProxy.
|
||||
env_file: [.env]
|
||||
environment:
|
||||
CACHE_MODE: ${CACHE_MODE:-protected}
|
||||
CACHE_MISS_DELAY_MS: ${CACHE_MISS_DELAY_MS:-0}
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
# Дрейн длиннее обнаружения отказа LB (500 ms x fall 2 ≈ 1 s).
|
||||
DRAIN_DELAY_MS: ${DRAIN_DELAY_MS:-3000}
|
||||
deploy:
|
||||
replicas: ${API_REPLICAS:-2}
|
||||
resources:
|
||||
limits:
|
||||
cpus: '${API_CPUS_PER_REPLICA:-1.5}'
|
||||
memory: ${API_MEMORY_PER_REPLICA:-768m}
|
||||
stop_grace_period: 20s
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
migrate: { condition: service_completed_successfully }
|
||||
postgres: { condition: service_healthy }
|
||||
redis: { condition: service_healthy }
|
||||
healthcheck:
|
||||
test:
|
||||
[
|
||||
'CMD',
|
||||
'node',
|
||||
'-e',
|
||||
"fetch('http://127.0.0.1:'+ (process.env.APP_PORT||8080) + '/health/ready').then(r=>{if(!r.ok)process.exit(1)}).catch(()=>process.exit(1))",
|
||||
]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
|
||||
haproxy:
|
||||
image: haproxy:3.2-alpine
|
||||
ports:
|
||||
- '${APP_PUBLIC_PORT:-8080}:8080'
|
||||
# Страница статистики — интерфейс LB, публикуется только на loopback.
|
||||
- '127.0.0.1:${LB_STATS_PORT:-8404}:8404'
|
||||
volumes:
|
||||
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '${LB_CPUS:-0.25}'
|
||||
memory: ${LB_MEMORY:-128m}
|
||||
depends_on:
|
||||
api: { condition: service_healthy }
|
||||
|
||||
# Генератор нагрузки запускается только явно: --profile load
|
||||
loadgen:
|
||||
image: grafana/k6:0.54.0
|
||||
profiles: ['load']
|
||||
volumes:
|
||||
- ./scripts/load:/load:ro
|
||||
command: ['run', '/load/write-ladder.js']
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '${LOADGEN_CPUS:-1.5}'
|
||||
memory: ${LOADGEN_MEMORY:-256m}
|
||||
depends_on:
|
||||
haproxy: { condition: service_healthy }
|
||||
|
||||
volumes:
|
||||
postgres-data:
|
||||
redis-data:
|
||||
@@ -1,63 +0,0 @@
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:16.4-alpine
|
||||
environment:
|
||||
POSTGRES_DB: ${POSTGRES_DB:-messenger}
|
||||
POSTGRES_USER: ${POSTGRES_USER:-messenger}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}
|
||||
volumes:
|
||||
- postgres-data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
|
||||
redis:
|
||||
image: redis:7.4.2-alpine
|
||||
command: ["redis-server", "--appendonly", "yes", "--appendfsync", "everysec"]
|
||||
volumes:
|
||||
- redis-data:/data
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
|
||||
migrate:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
image: mtusi-messenger:lab3
|
||||
command: ["node", "dist/database/migration.runner.js"]
|
||||
env_file: [.env]
|
||||
depends_on:
|
||||
postgres: { condition: service_healthy }
|
||||
redis: { condition: service_healthy }
|
||||
restart: "no"
|
||||
|
||||
api:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
image: mtusi-messenger:lab3
|
||||
ports:
|
||||
- "${APP_PUBLIC_PORT:-8080}:${APP_PORT:-8080}"
|
||||
env_file: [.env]
|
||||
environment:
|
||||
CACHE_MODE: ${CACHE_MODE:-protected}
|
||||
CACHE_MISS_DELAY_MS: ${CACHE_MISS_DELAY_MS:-0}
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
depends_on:
|
||||
migrate: { condition: service_completed_successfully }
|
||||
postgres: { condition: service_healthy }
|
||||
redis: { condition: service_healthy }
|
||||
healthcheck:
|
||||
test: ["CMD", "node", "-e", "fetch('http://127.0.0.1:'+ (process.env.APP_PORT||8080) + '/health/ready').then(r=>{if(!r.ok)process.exit(1)}).catch(()=>process.exit(1))"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
|
||||
volumes:
|
||||
postgres-data:
|
||||
redis-data:
|
||||
@@ -0,0 +1,54 @@
|
||||
# HAProxy — единственная точка входа lab3.
|
||||
#
|
||||
# Проверка готовности backend: GET /internal/lb-check (не /health/ready):
|
||||
# недоступность общей БД/Redis не лечится выводом реплик из ротации, а вывод
|
||||
# всех реплик подменил бы ограниченный 503 API страницей ошибки LB.
|
||||
# inter 500 ms, fall 2 => максимальное время обнаружения отказа ≈ 1 s, что
|
||||
# короче окна дреина API (DRAIN_DELAY_MS=3000): узел уходит из ротации раньше,
|
||||
# чем перестаёт обслуживать.
|
||||
#
|
||||
# Реплики не перечислены статически: server-template + DNS Docker обновляет
|
||||
# список адресов при масштабировании (init-addr none — стартовать без адресов
|
||||
# и дождаться DNS, а не падать при пустом ответе).
|
||||
|
||||
global
|
||||
maxconn 8192
|
||||
log stdout format raw local0
|
||||
|
||||
defaults
|
||||
mode http
|
||||
option httplog
|
||||
timeout connect 500ms
|
||||
timeout client 35s
|
||||
timeout server 30s
|
||||
timeout queue 2s
|
||||
retries 3
|
||||
option redispatch
|
||||
retry-on conn-failure
|
||||
|
||||
resolvers docker
|
||||
nameserver dns1 127.0.0.11:53
|
||||
resolve_retries 3
|
||||
timeout resolve 1s
|
||||
timeout retry 1s
|
||||
hold valid 500ms
|
||||
hold nx 500ms
|
||||
hold other 1s
|
||||
|
||||
frontend http
|
||||
bind *:8080
|
||||
default_backend api
|
||||
|
||||
backend api
|
||||
balance roundrobin
|
||||
option httpchk GET /internal/lb-check
|
||||
http-check expect status 200
|
||||
default-server check inter 500ms fastinter 250ms downinter 1s fall 2 rise 2 maxconn 200
|
||||
server-template api 1-8 api:8080 resolvers docker init-addr none
|
||||
|
||||
listen stats
|
||||
bind *:8404
|
||||
stats enable
|
||||
stats uri /stats
|
||||
stats refresh 1s
|
||||
stats show-legends
|
||||
Executable
+19
@@ -0,0 +1,19 @@
|
||||
#!/bin/sh
|
||||
# Black-box набор приложения (app/test/*.e2e-spec.ts) против поднятого стенда.
|
||||
# Требует loopback-портов PostgreSQL и Redis из compose.yaml: API-реплики портов
|
||||
# не публикуют, а сам набор подключается к хранилищам напрямую для проверки
|
||||
# фактического состояния (уникальность пользователей, история сообщений).
|
||||
set -eu
|
||||
|
||||
env_file="${COMPOSE_ENV_FILE:-.env}"
|
||||
# shellcheck disable=SC1090
|
||||
set -a
|
||||
. "./$env_file"
|
||||
set +a
|
||||
|
||||
export API_BASE_URL="${API_BASE_URL:-http://127.0.0.1:${APP_PUBLIC_PORT:-8080}}"
|
||||
export DATABASE_URL="postgresql://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in $env_file}@127.0.0.1:${POSTGRES_PUBLISHED_PORT:-5432}/${POSTGRES_DB:-messenger}"
|
||||
export REDIS_URL="redis://127.0.0.1:${REDIS_PUBLISHED_PORT:-6379}"
|
||||
|
||||
cd "$(dirname "$0")/../app"
|
||||
exec npm run test:e2e
|
||||
Executable
+70
@@ -0,0 +1,70 @@
|
||||
#!/bin/sh
|
||||
# Готовит хост к измерениям: посторонние контейнеры (чужие БД, прокси, dev-серверы)
|
||||
# потребляют CPU/RAM и портят сравнение конфигураций стенда. Скрипт запоминает,
|
||||
# что именно он остановил, и возвращает только это: чужие контейнеры, поднятые
|
||||
# пользователем после `up`, скрипт не трогает.
|
||||
#
|
||||
# scripts/quiesce.sh up остановить фон перед серией
|
||||
# scripts/quiesce.sh down вернуть ровно остановленное
|
||||
# scripts/quiesce.sh status состояние фона и список остановленного
|
||||
#
|
||||
# Список переопределяется QUIESCE_CONTAINERS (имена через пробел), файл
|
||||
# состояния — QUIESCE_STATE_FILE.
|
||||
set -eu
|
||||
|
||||
state_file="${QUIESCE_STATE_FILE:-.lab3-quiesce.json}"
|
||||
containers="${QUIESCE_CONTAINERS:-omniroute lampa caddy redis valkey sql-lab-db-1 redis-lab-redis-1}"
|
||||
|
||||
is_running() {
|
||||
[ "$(docker inspect -f '{{.State.Running}}' "$1" 2>/dev/null || printf false)" = 'true' ]
|
||||
}
|
||||
|
||||
case "${1:-}" in
|
||||
up)
|
||||
if [ -f "$state_file" ]; then
|
||||
printf 'quiesce: %s exists, background is already quiesced (run down first)\n' "$state_file" >&2
|
||||
exit 1
|
||||
fi
|
||||
stopped=''
|
||||
for container in $containers; do
|
||||
if is_running "$container"; then
|
||||
docker stop "$container" >/dev/null
|
||||
stopped="$stopped $container"
|
||||
printf 'quiesce: stopped %s\n' "$container"
|
||||
fi
|
||||
done
|
||||
printf '%s\n' "$stopped" | tr ' ' '\n' | sed '/^$/d' |
|
||||
jq -Rn --arg at "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
|
||||
'{started_at: $at, stopped: [inputs]}' >"$state_file"
|
||||
printf 'quiesce: %s stopped %s\n' "$(basename "$state_file")" "$(tr -d '\n' <"$state_file")"
|
||||
;;
|
||||
down)
|
||||
if [ ! -f "$state_file" ]; then
|
||||
printf 'quiesce: no %s, nothing to restore\n' "$state_file" >&2
|
||||
exit 0
|
||||
fi
|
||||
for container in $(jq -r '.stopped[]' "$state_file"); do
|
||||
docker start "$container" >/dev/null
|
||||
printf 'quiesce: started %s\n' "$container"
|
||||
done
|
||||
rm -f "$state_file"
|
||||
;;
|
||||
status)
|
||||
if [ -f "$state_file" ]; then
|
||||
printf 'quiesced at %s: %s\n' \
|
||||
"$(jq -r '.started_at' "$state_file")" \
|
||||
"$(jq -r '.stopped | join(" ")' "$state_file")"
|
||||
else
|
||||
printf 'background is not quiesced\n'
|
||||
fi
|
||||
for container in $containers; do
|
||||
if docker inspect -f '{{.State.Status}}' "$container" >/dev/null 2>&1; then
|
||||
printf '%s\t%s\n' "$container" "$(docker inspect -f '{{.State.Status}}' "$container")"
|
||||
fi
|
||||
done
|
||||
;;
|
||||
*)
|
||||
printf 'usage: %s up|down|status\n' "$0" >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
+2
-2
@@ -4,5 +4,5 @@
|
||||
set -euo pipefail
|
||||
|
||||
compose=(docker compose --env-file .env)
|
||||
"${compose[@]}" restart postgres redis api
|
||||
"${compose[@]}" up -d --no-recreate --no-deps --wait postgres redis api
|
||||
"${compose[@]}" restart postgres redis api haproxy
|
||||
"${compose[@]}" up -d --no-recreate --no-deps --wait postgres redis api haproxy
|
||||
|
||||
Executable
+16
@@ -0,0 +1,16 @@
|
||||
#!/bin/sh
|
||||
# Восстанавливает недостающую API-реплику до объявленного числа реплик.
|
||||
# Вызывается после make test-failover (или после остановки реплики вручную):
|
||||
# Compose создаёт ровно ту реплику, которой не хватает, и не трогает остальные
|
||||
# (--no-deps: миграции и БД не перезапускаются).
|
||||
set -eu
|
||||
|
||||
env_file="${COMPOSE_ENV_FILE:-.env}"
|
||||
replicas="${API_REPLICAS:-}"
|
||||
if [ -z "$replicas" ] && [ -f "$env_file" ]; then
|
||||
replicas=$(sed -n 's/^API_REPLICAS=//p' "$env_file" | tail -n 1)
|
||||
fi
|
||||
replicas="${replicas:-2}"
|
||||
|
||||
exec docker compose --env-file "$env_file" up -d --no-deps \
|
||||
--scale "api=${replicas}" api
|
||||
Executable
+35
@@ -0,0 +1,35 @@
|
||||
#!/bin/sh
|
||||
# Останавливает ровно одну API-реплику стенда — ту, которую обслужила текущая
|
||||
# сессия. TARGET_INSTANCE_ID приходит из теста (значение X-Instance-Id ответа),
|
||||
# а в Docker hostname контейнера равен его короткому ID, поэтому сопоставление
|
||||
# однозначно. Ничего, кроме этой реплики, скрипт не трогает: ни LB, ни БД, ни
|
||||
# тома. Возврат управления — быстрый (SIGTERM -> дрейн DRAIN_DELAY_MS=3s ->
|
||||
# выход), останавливать реплику "навсегда" не требуется.
|
||||
set -eu
|
||||
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||
project="${LAB3_PROJECT:-mtusi-lab3}"
|
||||
|
||||
target=''
|
||||
for container in $(
|
||||
docker ps \
|
||||
--filter "label=com.docker.compose.project=${project}" \
|
||||
--filter 'label=com.docker.compose.service=api' \
|
||||
--format '{{.ID}}'
|
||||
); do
|
||||
case "$container" in
|
||||
"$TARGET_INSTANCE_ID"*)
|
||||
target="$container"
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ -z "$target" ]; then
|
||||
printf 'stop-one: no running api replica of project %s whose id starts with %s\n' \
|
||||
"$project" "$TARGET_INSTANCE_ID" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
printf 'stop-one: stopping api replica %s\n' "$target" >&2
|
||||
docker stop --time 10 "$target" >/dev/null
|
||||
Reference in New Issue
Block a user