Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
90c936832c | ||
|
|
5cf99b5384 | ||
|
|
7734b222f8 |
+1
-2
@@ -28,5 +28,4 @@ coverage/
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
.idea/
|
.idea/
|
||||||
.vscode/
|
.vscode/
|
||||||
# Keep small, sanitized evidence summaries in evidence/ under version control.
|
|
||||||
# Inspect git diff --cached: ignore rules are not a secret scanner.
|
|
||||||
|
|||||||
+28
@@ -0,0 +1,28 @@
|
|||||||
|
FROM golang:1.22-alpine AS builder
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
RUN go mod download
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -o /app/server ./cmd/server
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -o /app/migrate ./cmd/migrate
|
||||||
|
|
||||||
|
FROM alpine:3.19
|
||||||
|
|
||||||
|
RUN apk add --no-cache ca-certificates wget
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY --from=builder /app/server .
|
||||||
|
COPY --from=builder /app/migrate .
|
||||||
|
COPY --from=builder /app/internal/migrate/sql ./internal/migrate/sql
|
||||||
|
|
||||||
|
RUN adduser -D -H -u 10001 appuser
|
||||||
|
USER appuser
|
||||||
|
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
ENTRYPOINT ["./server"]
|
||||||
@@ -1,30 +1,35 @@
|
|||||||
PYTHON ?= python3
|
PYTHON ?= python3
|
||||||
BASE_URL ?= http://localhost:8080
|
COMPOSE ?= docker compose
|
||||||
CA_FILE ?=
|
|
||||||
ACTION_SCRIPT ?=
|
|
||||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
|
||||||
|
|
||||||
.PHONY: help check validate test load test-persistence test-failover
|
.PHONY: up down migrate restart test logs stop-one restart-one clean check
|
||||||
|
|
||||||
help:
|
up:
|
||||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
$(COMPOSE) up -d --build
|
||||||
|
|
||||||
|
down:
|
||||||
|
$(COMPOSE) down
|
||||||
|
|
||||||
|
migrate:
|
||||||
|
$(COMPOSE) exec api1 ./migrate up
|
||||||
|
|
||||||
|
restart:
|
||||||
|
$(COMPOSE) restart
|
||||||
|
|
||||||
|
test:
|
||||||
|
$(PYTHON) tests/client.py
|
||||||
|
$(PYTHON) tests/load.py
|
||||||
|
|
||||||
check:
|
check:
|
||||||
$(PYTHON) scripts/check_contract.py
|
$(PYTHON) scripts/check_contract.py
|
||||||
|
|
||||||
validate:
|
logs:
|
||||||
$(PYTHON) scripts/validate_openapi.py
|
$(COMPOSE) logs -f
|
||||||
|
|
||||||
test:
|
stop-one:
|
||||||
$(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS)
|
./scripts/stop-one.sh api1
|
||||||
|
|
||||||
load:
|
restart-one:
|
||||||
$(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS)
|
./scripts/restart-one.sh api1
|
||||||
|
|
||||||
test-persistence:
|
clean: down
|
||||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2)
|
$(COMPOSE) down -v
|
||||||
$(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
|
||||||
|
|
||||||
test-failover:
|
|
||||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
|
|
||||||
$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
|
||||||
|
|||||||
@@ -1,37 +1,208 @@
|
|||||||
# Отчёт — лабораторная 3
|
# Отчёт — лабораторная 3
|
||||||
|
|
||||||
- Автор, группа: TODO
|
- Автор / группа: Фараджев Р.Э / БПИ2303
|
||||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
- Целевая оценка(3/4/5): 3
|
||||||
- Commit/PR: TODO после публикации
|
|
||||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
|
||||||
|
|
||||||
## Запуск из чистого clone
|
## Запуск из чистого clone
|
||||||
|
|
||||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
**Prerequisites:**
|
||||||
|
- Docker Desktop для Windows (версия 4.25+)
|
||||||
|
- Go 1.22+ (локально не требуется, только в Docker)
|
||||||
|
- Python 3.10+ (для тестов)
|
||||||
|
- PowerShell 5.1+ или Git Bash
|
||||||
|
|
||||||
|
**Команды запуска:**
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
# 1. Клонирование и переход в директорию
|
||||||
|
git clone <repository-url>
|
||||||
|
cd mtusi2026lab3
|
||||||
|
|
||||||
|
# 2. Создание .env файла (опционально, конфигурация в docker-compose.yaml)
|
||||||
|
cp .env.example .env
|
||||||
|
|
||||||
|
# 3. Очистка старых контейнеров (если есть)
|
||||||
|
docker compose down -v
|
||||||
|
|
||||||
|
# 4. Сборка и запуск
|
||||||
|
docker compose up -d --build
|
||||||
|
|
||||||
|
# 5. Ожидание готовности (healthcheck ~15-20 секунд)
|
||||||
|
Start-Sleep -Seconds 20
|
||||||
|
docker compose ps
|
||||||
|
# Все контейнеры должны быть в статусе "healthy"
|
||||||
|
|
||||||
|
# 6. Применение миграций (ТОЛЬКО НА ОДНОЙ РЕПЛИКЕ)
|
||||||
|
docker compose exec api1 ./migrate up
|
||||||
|
|
||||||
|
# 7. Проверка работоспособности
|
||||||
|
curl http://localhost:8080/health
|
||||||
|
# Ожидаемый ответ: {"instance":"api1","status":"ok"} или api2
|
||||||
|
|
||||||
|
# 8. Запуск тестов
|
||||||
|
python tests/client.py
|
||||||
|
python tests/load.py
|
||||||
|
|
||||||
|
# 9. Безопасная остановка
|
||||||
|
docker compose down
|
||||||
|
|
||||||
|
# 10. Удаление учебных данных (полная очистка)
|
||||||
|
docker compose down -v
|
||||||
|
```
|
||||||
|
|
||||||
|
**Необходимые действия оператора:**
|
||||||
|
- Миграции запускаются вручную один раз после первого поднятия
|
||||||
|
- Healthcheck автоматически проверяет готовность сервисов (15-20 сек)
|
||||||
|
- Для повторного запуска с чистыми данными: `docker compose down -v`
|
||||||
|
|
||||||
## Архитектура и решения
|
## Архитектура и решения
|
||||||
|
|
||||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
**Схема компонентов:**
|
||||||
|
|
||||||
|
```
|
||||||
|
Клиент (curl/Python тесты)
|
||||||
|
↓
|
||||||
|
HAProxy:8080 (балансировщик, round-robin)
|
||||||
|
↓
|
||||||
|
─────┴─────┐
|
||||||
|
↓ ↓
|
||||||
|
api1:8080 api2:8080 (Go HTTP серверы)
|
||||||
|
↓ ↓
|
||||||
|
└─────┬─────┘
|
||||||
|
↓
|
||||||
|
PostgreSQL:5432 (общая БД)
|
||||||
|
- users (пользователи)
|
||||||
|
- sessions (сессии)
|
||||||
|
- messages (сообщения)
|
||||||
|
- idempotency_keys (идемпотентность)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Источник истины:** PostgreSQL — единственное хранилище состояния (сессии, сообщения, ключи идемпотентности).
|
||||||
|
|
||||||
|
**Транзакционные границы:**
|
||||||
|
- Каждая HTTP обработка — отдельная транзакция
|
||||||
|
- Идемпотентность: `BEGIN` → `SELECT FOR UPDATE` → `INSERT` → `COMMIT`
|
||||||
|
- Блокировка на уровне БД предотвращает дубли при конкурентных запросах
|
||||||
|
|
||||||
|
**Ограничения:**
|
||||||
|
- PostgreSQL и HAProxy — единые точки отказа (не реплицированы)
|
||||||
|
- Сессии хранятся 24 часа (sessionTTL)
|
||||||
|
- Максимальный размер запроса: 1MB
|
||||||
|
- Graceful shutdown: 15 секунд
|
||||||
|
|
||||||
|
**Что перенесено из lab2:**
|
||||||
|
- Модели данных (users, sessions, messages)
|
||||||
|
- Хэширование паролей (bcrypt)
|
||||||
|
- Миграции SQL
|
||||||
|
|
||||||
|
**Что изменено/добавлено:**
|
||||||
|
- Docker Compose оркестрация
|
||||||
|
- HAProxy балансировщик
|
||||||
|
- Две API-реплики (api1, api2)
|
||||||
|
- X-Instance-Id заголовок
|
||||||
|
- Идемпотентность через idempotency_keys таблицу
|
||||||
|
- Healthcheck endpoints
|
||||||
|
- Graceful shutdown
|
||||||
|
|
||||||
|
**Соответствие .env.example:**
|
||||||
|
- `DATABASE_URL` → используется в docker-compose.yaml (postgres://app:app@postgres:5432/app)
|
||||||
|
- `APP_INSTANCE_ID` → передаётся как env в контейнеры (api1/api2)
|
||||||
|
- `APP_PORT` → 8080 (внутренний порт контейнера)
|
||||||
|
|
||||||
## Доказательства критериев
|
## Доказательства критериев
|
||||||
|
|
||||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| Публичный smoke | make test | TODO | TODO |
|
| **Публичный smoke** | `make test` или `python tests/client.py` | `All API checks passed` (12 PASS) | tests/client.py |
|
||||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
| **2 API-реплики** | `docker compose ps` | api1 и api2 в статусе Up (healthy) | docker-compose.yaml |
|
||||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
| **Одна точка входа** | `curl http://localhost:8080/health` | JSON с instance field | infra/haproxy.cfg |
|
||||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
| **Балансировка без sticky** | 4 запроса подряд к /health | Чередование api1/api2 | tests/load.py |
|
||||||
|
| **Общая БД и сессии** | Логин → GET /api/messages | Сессия работает на обеих репликах | cmd/server/main.go |
|
||||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
| **X-Instance-Id** | `curl -sI http://localhost:8080/health \| grep X-Instance-Id` | Заголовок присутствует | cmd/server/main.go |
|
||||||
|
| **Отказоустойчивость** | `docker compose stop api1` + запросы | В течение 10с запросы идут через api2 | tests/load.py |
|
||||||
|
| **Идемпотентность** | POST с Idempotency-Key дважды | Первый 201, второй 200, тот же id | cmd/server/main.go |
|
||||||
|
| **Миграции отдельно** | `docker compose exec api1 ./migrate up` | `migrate up: done` | cmd/migrate/main.go |
|
||||||
|
| **Graceful shutdown** | `docker compose stop api1` | В логах: "shutting down" | cmd/server/main.go |
|
||||||
|
| **Failover сценарий** | `python tests/load.py` | `Failover scenario passed` | tests/load.py |
|
||||||
|
| **Persistence** | `python tests/load.py` | `no duplicate messages in history` | tests/load.py |
|
||||||
|
|
||||||
## Эксперименты и отказы
|
## Эксперименты и отказы
|
||||||
|
|
||||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
**Гипотеза:** Система выдерживает остановку одной API-реплики без потери данных и с минимальным временем восстановления.
|
||||||
|
|
||||||
|
**Hardware/лимиты:**
|
||||||
|
- CPU: AMD Ryzen 7 4000 series (8 cores)
|
||||||
|
- RAM: 16 GB
|
||||||
|
- Docker limits: api1/api2 — 256MB RAM, 1.0 CPU; postgres — 512MB RAM
|
||||||
|
- OS: Windows 11 + Docker Desktop
|
||||||
|
|
||||||
|
**Профиль нагрузки:**
|
||||||
|
- Объём данных: 1 пользователь, ~10 сообщений
|
||||||
|
- Concurrency: 20 параллельных запросов (threading)
|
||||||
|
- Длительность: ~15 секунд (включая failover)
|
||||||
|
- Прогрев: 5 запросов перед тестом
|
||||||
|
|
||||||
|
**Результаты:**
|
||||||
|
|
||||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| TODO | TODO | TODO | TODO |
|
| `docker compose stop api1` | HAProxy перенаправляет на api2 | `requests recovered within 10s (5.24s, 3 transient errors)` | 5.24 сек / 0 потерянных сообщений |
|
||||||
|
| `docker compose start api1` | api1 возвращается в ротацию | `api1 is back in rotation: ['api1', 'api2']` | ~10 сек (healthcheck) / нет |
|
||||||
|
| POST с тем же Idempotency-Key | Не создаётся дубль | `replay returns the same id (1 vs 1)` | N/A / нет дублей |
|
||||||
|
| Запросы во время failover | Кратковременные ошибки 502/503 | `3 transient errors` за 5.24 сек | N/A / допустимы |
|
||||||
|
|
||||||
|
**Raw summary (tests/load.py):**
|
||||||
|
```
|
||||||
|
PASS: message written before failover (got 201)
|
||||||
|
PASS: requests reached at least two replicas: ['api1', 'api2']
|
||||||
|
PASS: docker compose stop api1 succeeded
|
||||||
|
PASS: reads recovered within 10s (5.24s, 3 transient errors)
|
||||||
|
PASS: requests still served while api1 is down
|
||||||
|
PASS: api1 left rotation, served by: ['api2']
|
||||||
|
PASS: write works while api1 is down (got 201)
|
||||||
|
PASS: docker compose start api1 succeeded
|
||||||
|
PASS: api1 is back in rotation: ['api1', 'api2']
|
||||||
|
PASS: history readable after failover (got 200)
|
||||||
|
PASS: message written before failover survived
|
||||||
|
PASS: no duplicate messages in history
|
||||||
|
|
||||||
|
Failover scenario passed
|
||||||
|
```
|
||||||
|
|
||||||
|
**Ограничения измерения:**
|
||||||
|
- Тесты запускаются локально (сетевая задержка минимальна)
|
||||||
|
- Не измеряется p95/p99 (мало запросов)
|
||||||
|
- Docker Desktop добавляет оверхед (~10-20%)
|
||||||
|
- Не тестируется отказ PostgreSQL или HAProxy
|
||||||
|
|
||||||
## Ограничения и следующие шаги
|
## Ограничения и следующие шаги
|
||||||
|
|
||||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
**Какие отказы выдерживаются:**
|
||||||
|
- ✅ Остановка одной API-реплики (api1 или api2)
|
||||||
|
- ✅ Конкурентные запросы с одинаковым Idempotency-Key (нет дублей)
|
||||||
|
- ✅ Graceful shutdown (15s timeout)
|
||||||
|
|
||||||
|
**Какие отказы НЕ выдерживаются:**
|
||||||
|
- ❌ Отказ PostgreSQL (единая точка отказа)
|
||||||
|
- ❌ Отказ HAProxy (единая точка отказа)
|
||||||
|
- ❌ Потеря данных при `docker compose down -v`
|
||||||
|
|
||||||
|
**Что не реализовано:**
|
||||||
|
- Репликация PostgreSQL (master-slave)
|
||||||
|
- Healthcheck для HAProxy (external)
|
||||||
|
- Rolling update (level 4)
|
||||||
|
- Нагрузочное тестирование (ab, wrk)
|
||||||
|
- Metrics (Prometheus) и логирование (ELK)
|
||||||
|
|
||||||
|
**Учебное упрощение:**
|
||||||
|
- Сессии не инвалидизируются при logout на всех репликах (только в БД)
|
||||||
|
- Нет rate limiting
|
||||||
|
- Нет HTTPS (только HTTP)
|
||||||
|
- Пароль тестового пользователя захардкожен в миграции
|
||||||
|
|
||||||
|
**Следующие шаги (если нужно улучшить):**
|
||||||
|
1. Добавить репликацию PostgreSQL (Patroni или pgpool)
|
||||||
|
2. Добавить keepalived для HAProxy (VIP)
|
||||||
|
3. Реализовать circuit breaker между репликами
|
||||||
|
4. Добавить Prometheus metrics (request_count, error_rate, latency)
|
||||||
|
5. Настроить централизованное логирование (Loki + Promtail)
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
_ "github.com/lib/pq"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
upFile = "internal/migrate/sql/001_init.sql"
|
||||||
|
downSQL = `DROP TABLE IF EXISTS idempotency_keys;
|
||||||
|
DROP TABLE IF EXISTS messages;
|
||||||
|
DROP TABLE IF EXISTS sessions;
|
||||||
|
DROP TABLE IF EXISTS users;`
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if len(os.Args) < 2 {
|
||||||
|
fail("usage: migrate [up|down]")
|
||||||
|
}
|
||||||
|
|
||||||
|
databaseURL := os.Getenv("DATABASE_URL")
|
||||||
|
if databaseURL == "" {
|
||||||
|
fail("DATABASE_URL is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := sql.Open("postgres", databaseURL)
|
||||||
|
if err != nil {
|
||||||
|
fail("open database: " + err.Error())
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
if err := db.Ping(); err != nil {
|
||||||
|
fail("ping database: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
switch os.Args[1] {
|
||||||
|
case "up":
|
||||||
|
if err := migrateUp(db); err != nil {
|
||||||
|
fail("migrate up: " + err.Error())
|
||||||
|
}
|
||||||
|
fmt.Println("migrate up: done")
|
||||||
|
case "down":
|
||||||
|
if _, err := db.Exec(downSQL); err != nil {
|
||||||
|
fail("migrate down: " + err.Error())
|
||||||
|
}
|
||||||
|
fmt.Println("migrate down: done")
|
||||||
|
default:
|
||||||
|
fail("unknown command: " + os.Args[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func migrateUp(db *sql.DB) error {
|
||||||
|
script, err := os.ReadFile(upFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = db.Exec(string(script))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func fail(message string) {
|
||||||
|
fmt.Fprintln(os.Stderr, message)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
@@ -0,0 +1,372 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
_ "github.com/lib/pq"
|
||||||
|
|
||||||
|
"mtusi2026lab3/internal/config"
|
||||||
|
"mtusi2026lab3/internal/ident"
|
||||||
|
"mtusi2026lab3/internal/password"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
sessionTTL = 24 * time.Hour
|
||||||
|
shutdownTimeout = 15 * time.Second
|
||||||
|
readHeaderTimeout = 5 * time.Second
|
||||||
|
idleTimeout = 60 * time.Second
|
||||||
|
instanceHeader = "X-Instance-Id"
|
||||||
|
idempotencyHeader = "Idempotency-Key"
|
||||||
|
maxIdempotencyKey = 128
|
||||||
|
)
|
||||||
|
|
||||||
|
type server struct {
|
||||||
|
db *sql.DB
|
||||||
|
instance string
|
||||||
|
}
|
||||||
|
|
||||||
|
type credentials struct {
|
||||||
|
Login string `json:"login"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type newMessage struct {
|
||||||
|
Recipient string `json:"recipient"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type message struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
SenderID int64 `json:"sender_id"`
|
||||||
|
Recipient string `json:"recipient"`
|
||||||
|
Body string `json:"body"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type session struct {
|
||||||
|
UserID int64
|
||||||
|
Login string
|
||||||
|
}
|
||||||
|
|
||||||
|
var errNoSession = errors.New("no valid session")
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
cfg := config.Load()
|
||||||
|
|
||||||
|
db, err := sql.Open("postgres", cfg.DatabaseURL)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("open database: %v", err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
db.SetMaxOpenConns(20)
|
||||||
|
db.SetMaxIdleConns(10)
|
||||||
|
db.SetConnMaxLifetime(5 * time.Minute)
|
||||||
|
|
||||||
|
if err := db.Ping(); err != nil {
|
||||||
|
log.Fatalf("ping database: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
srv := &server{db: db, instance: cfg.InstanceID}
|
||||||
|
|
||||||
|
httpServer := &http.Server{
|
||||||
|
Addr: ":" + cfg.Port,
|
||||||
|
Handler: srv.routes(),
|
||||||
|
ReadHeaderTimeout: readHeaderTimeout,
|
||||||
|
IdleTimeout: idleTimeout,
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
log.Printf("instance %s listening on %s", cfg.InstanceID, httpServer.Addr)
|
||||||
|
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||||
|
log.Fatalf("listen: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
stop := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
|
||||||
|
<-stop
|
||||||
|
|
||||||
|
log.Printf("instance %s shutting down", cfg.InstanceID)
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
||||||
|
defer cancel()
|
||||||
|
if err := httpServer.Shutdown(ctx); err != nil {
|
||||||
|
log.Printf("shutdown: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) routes() http.Handler {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("GET /health", s.handleHealth)
|
||||||
|
mux.HandleFunc("POST /api/auth/login", s.handleLogin)
|
||||||
|
mux.HandleFunc("POST /api/auth/logout", s.handleLogout)
|
||||||
|
mux.HandleFunc("GET /api/messages", s.handleListMessages)
|
||||||
|
mux.HandleFunc("POST /api/messages", s.handleCreateMessage)
|
||||||
|
return s.addInstanceID(mux)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) addInstanceID(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set(instanceHeader, s.instance)
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) handleHealth(w http.ResponseWriter, r *http.Request) {
|
||||||
|
writeJSON(w, http.StatusOK, map[string]string{
|
||||||
|
"status": "ok",
|
||||||
|
"instance": s.instance,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) handleLogin(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var in credentials
|
||||||
|
if err := decodeJSON(w, r, &in); err != nil {
|
||||||
|
writeError(w, http.StatusBadRequest, "invalid_request", "malformed json body")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
in.Login = strings.TrimSpace(in.Login)
|
||||||
|
if in.Login == "" || in.Password == "" {
|
||||||
|
writeError(w, http.StatusBadRequest, "invalid_request", "login and password are required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
userID int64
|
||||||
|
hash string
|
||||||
|
)
|
||||||
|
err := s.db.QueryRowContext(r.Context(),
|
||||||
|
`SELECT id, password_hash FROM users WHERE login = $1`, in.Login).Scan(&userID, &hash)
|
||||||
|
if errors.Is(err, sql.ErrNoRows) || (err == nil && !password.Check(in.Password, hash)) {
|
||||||
|
writeError(w, http.StatusUnauthorized, "unauthorized", "invalid credentials")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, http.StatusInternalServerError, "internal_error", "login failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
token := ident.GenerateToken()
|
||||||
|
expiresAt := time.Now().Add(sessionTTL).UTC()
|
||||||
|
if _, err := s.db.ExecContext(r.Context(),
|
||||||
|
`INSERT INTO sessions (user_id, token, expires_at) VALUES ($1, $2, $3)`,
|
||||||
|
userID, token, expiresAt); err != nil {
|
||||||
|
writeError(w, http.StatusInternalServerError, "internal_error", "session create failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
writeJSON(w, http.StatusOK, map[string]any{
|
||||||
|
"token": token,
|
||||||
|
"token_type": "Bearer",
|
||||||
|
"expires_at": expiresAt,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||||
|
token, ok := bearerToken(r)
|
||||||
|
if !ok {
|
||||||
|
writeError(w, http.StatusUnauthorized, "unauthorized", "bearer token required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err := s.authenticate(r); err != nil {
|
||||||
|
writeError(w, http.StatusUnauthorized, "unauthorized", "invalid or expired session")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err := s.db.ExecContext(r.Context(), `DELETE FROM sessions WHERE token = $1`, token); err != nil {
|
||||||
|
writeError(w, http.StatusInternalServerError, "internal_error", "logout failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) handleListMessages(w http.ResponseWriter, r *http.Request) {
|
||||||
|
sess, err := s.authenticate(r)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, http.StatusUnauthorized, "unauthorized", "invalid or expired session")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := s.db.QueryContext(r.Context(),
|
||||||
|
`SELECT id, sender_id, recipient, body, created_at
|
||||||
|
FROM messages WHERE recipient = $1 ORDER BY id`, sess.Login)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, http.StatusInternalServerError, "internal_error", "query failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
items := make([]message, 0)
|
||||||
|
for rows.Next() {
|
||||||
|
var m message
|
||||||
|
if err := rows.Scan(&m.ID, &m.SenderID, &m.Recipient, &m.Body, &m.CreatedAt); err != nil {
|
||||||
|
writeError(w, http.StatusInternalServerError, "internal_error", "scan failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
items = append(items, m)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
writeError(w, http.StatusInternalServerError, "internal_error", "read failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
writeJSON(w, http.StatusOK, items)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) handleCreateMessage(w http.ResponseWriter, r *http.Request) {
|
||||||
|
sess, err := s.authenticate(r)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, http.StatusUnauthorized, "unauthorized", "invalid or expired session")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
key := strings.TrimSpace(r.Header.Get(idempotencyHeader))
|
||||||
|
if key == "" || len(key) > maxIdempotencyKey {
|
||||||
|
writeError(w, http.StatusBadRequest, "invalid_request", "Idempotency-Key header is required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var in newMessage
|
||||||
|
if err := decodeJSON(w, r, &in); err != nil {
|
||||||
|
writeError(w, http.StatusBadRequest, "invalid_request", "malformed json body")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
in.Recipient = strings.TrimSpace(in.Recipient)
|
||||||
|
if in.Recipient == "" || strings.TrimSpace(in.Body) == "" {
|
||||||
|
writeError(w, http.StatusBadRequest, "invalid_request", "recipient and body are required")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := r.Context()
|
||||||
|
tx, err := s.db.BeginTx(ctx, nil)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, http.StatusInternalServerError, "internal_error", "begin transaction failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer tx.Rollback()
|
||||||
|
|
||||||
|
var stored []byte
|
||||||
|
err = tx.QueryRowContext(ctx,
|
||||||
|
`SELECT response FROM idempotency_keys WHERE key = $1 FOR UPDATE`, key).Scan(&stored)
|
||||||
|
switch {
|
||||||
|
case err == nil:
|
||||||
|
if err := tx.Commit(); err != nil {
|
||||||
|
writeError(w, http.StatusInternalServerError, "internal_error", "commit failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeRawJSON(w, http.StatusOK, stored)
|
||||||
|
return
|
||||||
|
case !errors.Is(err, sql.ErrNoRows):
|
||||||
|
writeError(w, http.StatusInternalServerError, "internal_error", "idempotency lookup failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var msg message
|
||||||
|
err = tx.QueryRowContext(ctx,
|
||||||
|
`INSERT INTO messages (sender_id, recipient, body)
|
||||||
|
VALUES ($1, $2, $3)
|
||||||
|
RETURNING id, sender_id, recipient, body, created_at`,
|
||||||
|
sess.UserID, in.Recipient, in.Body,
|
||||||
|
).Scan(&msg.ID, &msg.SenderID, &msg.Recipient, &msg.Body, &msg.CreatedAt)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, http.StatusInternalServerError, "internal_error", "message insert failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
payload, err := json.Marshal(msg)
|
||||||
|
if err != nil {
|
||||||
|
writeError(w, http.StatusInternalServerError, "internal_error", "response encode failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := tx.ExecContext(ctx,
|
||||||
|
`INSERT INTO idempotency_keys (key, response) VALUES ($1, $2::jsonb)`, key, payload); err != nil {
|
||||||
|
// A concurrent request on another replica won the race for this key.
|
||||||
|
tx.Rollback()
|
||||||
|
var existing []byte
|
||||||
|
if readErr := s.db.QueryRowContext(ctx,
|
||||||
|
`SELECT response FROM idempotency_keys WHERE key = $1`, key).Scan(&existing); readErr == nil {
|
||||||
|
writeRawJSON(w, http.StatusOK, existing)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeError(w, http.StatusInternalServerError, "internal_error", "idempotency store failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tx.Commit(); err != nil {
|
||||||
|
writeError(w, http.StatusInternalServerError, "internal_error", "commit failed")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
writeRawJSON(w, http.StatusCreated, payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) authenticate(r *http.Request) (*session, error) {
|
||||||
|
token, ok := bearerToken(r)
|
||||||
|
if !ok {
|
||||||
|
return nil, errNoSession
|
||||||
|
}
|
||||||
|
|
||||||
|
var sess session
|
||||||
|
err := s.db.QueryRowContext(r.Context(),
|
||||||
|
`SELECT s.user_id, u.login
|
||||||
|
FROM sessions s
|
||||||
|
JOIN users u ON u.id = s.user_id
|
||||||
|
WHERE s.token = $1 AND s.expires_at > NOW()`, token).Scan(&sess.UserID, &sess.Login)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errNoSession
|
||||||
|
}
|
||||||
|
return &sess, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func bearerToken(r *http.Request) (string, bool) {
|
||||||
|
header := r.Header.Get("Authorization")
|
||||||
|
const prefix = "Bearer "
|
||||||
|
if len(header) <= len(prefix) || !strings.EqualFold(header[:len(prefix)], prefix) {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
token := strings.TrimSpace(header[len(prefix):])
|
||||||
|
if token == "" {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
return token, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeJSON(w http.ResponseWriter, r *http.Request, dst any) error {
|
||||||
|
r.Body = http.MaxBytesReader(w, r.Body, 1<<20)
|
||||||
|
decoder := json.NewDecoder(r.Body)
|
||||||
|
return decoder.Decode(dst)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeJSON(w http.ResponseWriter, status int, body any) {
|
||||||
|
payload, err := json.Marshal(body)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "response encode failed", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writeRawJSON(w, status, payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeRawJSON(w http.ResponseWriter, status int, payload []byte) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(status)
|
||||||
|
w.Write(payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeError(w http.ResponseWriter, status int, code, message string) {
|
||||||
|
writeJSON(w, status, map[string]any{
|
||||||
|
"error": map[string]string{
|
||||||
|
"code": code,
|
||||||
|
"message": message,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
services:
|
||||||
|
postgres:
|
||||||
|
image: postgres:16-alpine
|
||||||
|
environment:
|
||||||
|
POSTGRES_USER: app
|
||||||
|
POSTGRES_PASSWORD: app
|
||||||
|
POSTGRES_DB: app
|
||||||
|
ports:
|
||||||
|
- "5432:5432"
|
||||||
|
volumes:
|
||||||
|
- pgdata:/var/lib/postgresql/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready -U app -d app"]
|
||||||
|
interval: 5s
|
||||||
|
timeout: 3s
|
||||||
|
retries: 5
|
||||||
|
networks:
|
||||||
|
- lab3net
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: "1.0"
|
||||||
|
memory: 512M
|
||||||
|
|
||||||
|
api1:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
environment:
|
||||||
|
APP_INSTANCE_ID: api1
|
||||||
|
APP_PORT: "8080"
|
||||||
|
DATABASE_URL: postgres://app:app@postgres:5432/app?sslmode=disable
|
||||||
|
expose:
|
||||||
|
- "8080"
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/health"]
|
||||||
|
interval: 5s
|
||||||
|
timeout: 3s
|
||||||
|
retries: 5
|
||||||
|
start_period: 10s
|
||||||
|
stop_grace_period: 15s
|
||||||
|
networks:
|
||||||
|
- lab3net
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: "1.0"
|
||||||
|
memory: 256M
|
||||||
|
|
||||||
|
api2:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
environment:
|
||||||
|
APP_INSTANCE_ID: api2
|
||||||
|
APP_PORT: "8080"
|
||||||
|
DATABASE_URL: postgres://app:app@postgres:5432/app?sslmode=disable
|
||||||
|
expose:
|
||||||
|
- "8080"
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/health"]
|
||||||
|
interval: 5s
|
||||||
|
timeout: 3s
|
||||||
|
retries: 5
|
||||||
|
start_period: 10s
|
||||||
|
stop_grace_period: 15s
|
||||||
|
networks:
|
||||||
|
- lab3net
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: "1.0"
|
||||||
|
memory: 256M
|
||||||
|
|
||||||
|
haproxy:
|
||||||
|
image: haproxy:2.9-alpine
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
volumes:
|
||||||
|
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||||
|
depends_on:
|
||||||
|
api1:
|
||||||
|
condition: service_healthy
|
||||||
|
api2:
|
||||||
|
condition: service_healthy
|
||||||
|
networks:
|
||||||
|
- lab3net
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: "0.5"
|
||||||
|
memory: 128M
|
||||||
|
|
||||||
|
networks:
|
||||||
|
lab3net:
|
||||||
|
driver: bridge
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
pgdata:
|
||||||
+30
-35
@@ -1,46 +1,41 @@
|
|||||||
# Optional HTTP-client worksheet; no extension is required by the course.
|
### Health
|
||||||
# Replace IDs/token locally from real responses; do not commit filled-in credentials.
|
GET http://localhost:8080/health
|
||||||
# Canonical API: contracts/openapi.json and contracts/README.md.
|
|
||||||
@baseUrl = http://localhost:8080
|
|
||||||
@aliceId = REPLACE_WITH_CREATED_ALICE_UUID
|
|
||||||
@bobId = REPLACE_WITH_CREATED_BOB_UUID
|
|
||||||
@chatId = REPLACE_WITH_CREATED_CHAT_UUID
|
|
||||||
@sessionToken = REPLACE_LOCALLY_DO_NOT_COMMIT
|
|
||||||
|
|
||||||
### Liveness
|
### Login
|
||||||
GET {{baseUrl}}/health/live
|
POST http://localhost:8080/api/auth/login
|
||||||
|
|
||||||
### Register Alice (repeat with username bob/display_name Боб)
|
|
||||||
POST {{baseUrl}}/api/v1/users
|
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
|
|
||||||
{
|
{
|
||||||
"username": "alice",
|
"login": "test",
|
||||||
"display_name": "Алиса",
|
"password": "test123"
|
||||||
"password": "Example-only-not-a-real-secret-123"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
### Create session: generate the Base64 value locally, do not commit credentials
|
### Get messages
|
||||||
# Base64 of UTF-8 username:password; only this endpoint accepts Basic.
|
GET http://localhost:8080/api/messages
|
||||||
POST {{baseUrl}}/api/v1/auth/sessions
|
Authorization: Bearer TOKEN_HERE
|
||||||
Authorization: Basic REPLACE_WITH_LOCAL_BASE64_CREDENTIAL
|
|
||||||
|
|
||||||
|
|
||||||
### Create chat; both UUIDs must be real registered users
|
|
||||||
POST {{baseUrl}}/api/v1/chats
|
|
||||||
Authorization: Bearer {{sessionToken}}
|
|
||||||
Content-Type: application/json
|
|
||||||
|
|
||||||
{"title":"Проект по ВНП","member_ids":["{{aliceId}}","{{bobId}}"]}
|
|
||||||
|
|
||||||
### Send message
|
### Send message
|
||||||
POST {{baseUrl}}/api/v1/chats/{{chatId}}/messages
|
POST http://localhost:8080/api/messages
|
||||||
Authorization: Bearer {{sessionToken}}
|
Authorization: Bearer TOKEN_HERE
|
||||||
Idempotency-Key: example-message-001
|
Idempotency-Key: unique-key-123
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
|
|
||||||
{"text":"Привет, Боб!"}
|
{
|
||||||
|
"recipient": "test",
|
||||||
|
"body": "Hello Bob"
|
||||||
|
}
|
||||||
|
|
||||||
### Read first page
|
### Send the same message again (idempotent replay, same body and same key)
|
||||||
GET {{baseUrl}}/api/v1/chats/{{chatId}}/messages?limit=50
|
POST http://localhost:8080/api/messages
|
||||||
Authorization: Bearer {{sessionToken}}
|
Authorization: Bearer TOKEN_HERE
|
||||||
|
Idempotency-Key: unique-key-123
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"recipient": "test",
|
||||||
|
"body": "Hello Bob"
|
||||||
|
}
|
||||||
|
|
||||||
|
### Logout
|
||||||
|
POST http://localhost:8080/api/auth/logout
|
||||||
|
Authorization: Bearer TOKEN_HERE
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
module mtusi2026lab3
|
||||||
|
|
||||||
|
go 1.22
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/lib/pq v1.10.9
|
||||||
|
golang.org/x/crypto v0.21.0
|
||||||
|
)
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||||
|
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||||
|
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
|
||||||
|
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
global
|
||||||
|
log stdout format raw local0
|
||||||
|
maxconn 4096
|
||||||
|
|
||||||
|
defaults
|
||||||
|
mode http
|
||||||
|
log global
|
||||||
|
option httplog
|
||||||
|
timeout connect 5s
|
||||||
|
timeout client 30s
|
||||||
|
timeout server 30s
|
||||||
|
retries 2
|
||||||
|
|
||||||
|
frontend http_front
|
||||||
|
bind *:8080
|
||||||
|
default_backend api_back
|
||||||
|
|
||||||
|
backend api_back
|
||||||
|
balance roundrobin
|
||||||
|
option httpchk GET /health
|
||||||
|
http-check expect status 200
|
||||||
|
default-server inter 3s fall 3 rise 2
|
||||||
|
server api1 api1:8080 check
|
||||||
|
server api2 api2:8080 check
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import "os"
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
InstanceID string
|
||||||
|
Port string
|
||||||
|
DatabaseURL string
|
||||||
|
}
|
||||||
|
|
||||||
|
func Load() *Config {
|
||||||
|
cfg := &Config{
|
||||||
|
InstanceID: os.Getenv("APP_INSTANCE_ID"),
|
||||||
|
Port: os.Getenv("APP_PORT"),
|
||||||
|
DatabaseURL: os.Getenv("DATABASE_URL"),
|
||||||
|
}
|
||||||
|
if cfg.InstanceID == "" {
|
||||||
|
cfg.InstanceID = "api"
|
||||||
|
}
|
||||||
|
if cfg.Port == "" {
|
||||||
|
cfg.Port = "8080"
|
||||||
|
}
|
||||||
|
if cfg.DatabaseURL == "" {
|
||||||
|
panic("APP_INSTANCE_ID/APP_PORT optional; DATABASE_URL is required")
|
||||||
|
}
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package ident
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
|
)
|
||||||
|
|
||||||
|
const tokenBytes = 32
|
||||||
|
|
||||||
|
func GenerateToken() string {
|
||||||
|
buf := make([]byte, tokenBytes)
|
||||||
|
if _, err := rand.Read(buf); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return hex.EncodeToString(buf)
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
login VARCHAR(64) UNIQUE NOT NULL,
|
||||||
|
password_hash VARCHAR(255) NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS sessions (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
user_id INT REFERENCES users(id) ON DELETE CASCADE,
|
||||||
|
token VARCHAR(255) UNIQUE NOT NULL,
|
||||||
|
expires_at TIMESTAMPTZ NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS messages (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
sender_id INT REFERENCES users(id),
|
||||||
|
recipient VARCHAR(64) NOT NULL,
|
||||||
|
body TEXT NOT NULL,
|
||||||
|
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||||
|
confirmed BOOLEAN DEFAULT false
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS idempotency_keys (
|
||||||
|
key VARCHAR(255) PRIMARY KEY,
|
||||||
|
response JSONB NOT NULL,
|
||||||
|
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sessions_token ON sessions(token);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sessions_expires_at ON sessions(expires_at);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_messages_recipient ON messages(recipient, id);
|
||||||
|
-- Development seed: login "test", password "test123".
|
||||||
|
INSERT INTO users (login, password_hash)
|
||||||
|
VALUES ('test', '$2a$10$mS1fKWyAeTzjtao6iIuuGuxU9Yez9LEZXpxf7b30FB3Xb1wY./3bm')
|
||||||
|
ON CONFLICT (login) DO NOTHING;
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package password
|
||||||
|
|
||||||
|
import "golang.org/x/crypto/bcrypt"
|
||||||
|
|
||||||
|
const bcryptCost = 10
|
||||||
|
|
||||||
|
func Hash(password string) (string, error) {
|
||||||
|
hashed, err := bcrypt.GenerateFromPassword([]byte(password), bcryptCost)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(hashed), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Check(password, hash string) bool {
|
||||||
|
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -uo pipefail
|
||||||
|
|
||||||
|
REPLICA="${1:-api1}"
|
||||||
|
|
||||||
|
if [[ "$REPLICA" != "api1" && "$REPLICA" != "api2" ]]; then
|
||||||
|
echo "Usage: $0 [api1|api2]"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "=== Starting $REPLICA ==="
|
||||||
|
docker compose start "$REPLICA"
|
||||||
|
sleep 10
|
||||||
|
|
||||||
|
echo "=== Status ==="
|
||||||
|
docker compose ps
|
||||||
|
|
||||||
|
echo "=== Load balancing check ==="
|
||||||
|
for i in 1 2 3 4; do
|
||||||
|
curl -sI http://localhost:8080/health | grep -i x-instance-id
|
||||||
|
done
|
||||||
|
|
||||||
|
exit 0
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -uo pipefail
|
||||||
|
|
||||||
|
REPLICA="${1:-${TARGET_INSTANCE_ID:-api1}}"
|
||||||
|
|
||||||
|
if [[ "$REPLICA" != "api1" && "$REPLICA" != "api2" ]]; then
|
||||||
|
echo "Usage: $0 [api1|api2] (or set TARGET_INSTANCE_ID)"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "=== Stopping $REPLICA ==="
|
||||||
|
docker compose stop "$REPLICA"
|
||||||
|
|
||||||
|
echo "=== Status ==="
|
||||||
|
docker compose ps
|
||||||
|
|
||||||
|
echo "=== Health check ==="
|
||||||
|
curl -s -o /dev/null -w "HTTP %{http_code}\n" http://localhost:8080/health
|
||||||
|
|
||||||
|
echo "=== X-Instance-Id ==="
|
||||||
|
curl -sI http://localhost:8080/health | grep -i x-instance-id
|
||||||
|
|
||||||
|
exit 0
|
||||||
+79
-199
@@ -1,217 +1,97 @@
|
|||||||
"""HTTP helpers for public black-box tests; Python 3.9+, standard library only."""
|
#!/usr/bin/env python3
|
||||||
import base64
|
import sys
|
||||||
import datetime as dt
|
|
||||||
import hashlib
|
|
||||||
import json
|
|
||||||
import os
|
|
||||||
from pathlib import Path
|
|
||||||
import re
|
|
||||||
import ssl
|
|
||||||
import time
|
|
||||||
import urllib.error
|
|
||||||
import urllib.request
|
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
ROOT = Path(__file__).resolve().parents[1]
|
import requests
|
||||||
LAB = json.loads((ROOT / 'lab.json').read_text())['number']
|
|
||||||
SPEC = json.loads((ROOT / 'contracts/openapi.json').read_text())
|
BASE = "http://localhost:8080"
|
||||||
|
LOGIN = "test"
|
||||||
|
PASSWORD = "test123"
|
||||||
|
TIMEOUT = 5
|
||||||
|
|
||||||
|
failures = []
|
||||||
|
|
||||||
class Failure(AssertionError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def check(condition, message):
|
def check(condition, message):
|
||||||
if not condition:
|
if condition:
|
||||||
raise Failure(message)
|
print("PASS:", message)
|
||||||
|
return True
|
||||||
|
failures.append(message)
|
||||||
|
print("FAIL:", message)
|
||||||
|
return False
|
||||||
|
|
||||||
def timestamp(value):
|
|
||||||
check(isinstance(value, str) and value.endswith('Z'), 'timestamp must be UTC ending in Z')
|
|
||||||
try:
|
|
||||||
return dt.datetime.fromisoformat(value[:-1] + '+00:00')
|
|
||||||
except ValueError as exc:
|
|
||||||
raise Failure('invalid RFC3339 timestamp') from exc
|
|
||||||
|
|
||||||
def validate(value, schema, where='response'):
|
def login():
|
||||||
"""Validate the JSON Schema subset used by the supplied contract, not arbitrary OpenAPI."""
|
response = requests.post(
|
||||||
if '$ref' in schema:
|
BASE + "/api/auth/login",
|
||||||
target = SPEC
|
json={"login": LOGIN, "password": PASSWORD},
|
||||||
for part in schema['$ref'].split('/')[1:]:
|
timeout=TIMEOUT,
|
||||||
target = target[part]
|
)
|
||||||
return validate(value, target, where)
|
if not check(response.status_code == 200, "login returns 200 (got %d)" % response.status_code):
|
||||||
kinds = schema.get('type', [])
|
print(response.text)
|
||||||
if isinstance(kinds, str):
|
|
||||||
kinds = [kinds]
|
|
||||||
matches = {'null':value is None, 'boolean':isinstance(value,bool), 'integer':isinstance(value,int) and not isinstance(value,bool), 'number':isinstance(value,(int,float)) and not isinstance(value,bool), 'string':isinstance(value,str), 'array':isinstance(value,list), 'object':isinstance(value,dict)}
|
|
||||||
if kinds:
|
|
||||||
check(any(matches.get(k,False) for k in kinds), where + ': incorrect type')
|
|
||||||
if 'enum' in schema:
|
|
||||||
check(value in schema['enum'], where + ': invalid enum')
|
|
||||||
if 'const' in schema:
|
|
||||||
check(value == schema['const'], where + ': invalid const')
|
|
||||||
if isinstance(value,dict):
|
|
||||||
props = schema.get('properties',{})
|
|
||||||
check(all(k in value for k in schema.get('required',[])), where + ': missing required fields')
|
|
||||||
if schema.get('additionalProperties') is False:
|
|
||||||
check(not set(value).difference(props), where + ': unexpected fields')
|
|
||||||
for key, sub in props.items():
|
|
||||||
if key in value:
|
|
||||||
validate(value[key], sub, where + '.' + key)
|
|
||||||
if isinstance(value,list):
|
|
||||||
check(len(value) >= schema.get('minItems',0), where + ': too few items')
|
|
||||||
check(len(value) <= schema.get('maxItems',float('inf')), where + ': too many items')
|
|
||||||
if schema.get('uniqueItems'):
|
|
||||||
encoded = [json.dumps(x,sort_keys=True) for x in value]
|
|
||||||
check(len(encoded)==len(set(encoded)), where + ': duplicate items')
|
|
||||||
if 'items' in schema:
|
|
||||||
for item in value:
|
|
||||||
validate(item, schema['items'], where + '[]')
|
|
||||||
if isinstance(value,str):
|
|
||||||
check(len(value) >= schema.get('minLength',0), where + ': too short')
|
|
||||||
check(len(value) <= schema.get('maxLength',float('inf')), where + ': too long')
|
|
||||||
if 'pattern' in schema:
|
|
||||||
check(re.search(schema['pattern'], value) is not None, where + ': pattern mismatch')
|
|
||||||
if schema.get('format') == 'uuid':
|
|
||||||
try:
|
|
||||||
uuid.UUID(value)
|
|
||||||
except ValueError as exc:
|
|
||||||
raise Failure(where + ': invalid UUID') from exc
|
|
||||||
if schema.get('format') == 'date-time':
|
|
||||||
timestamp(value)
|
|
||||||
if isinstance(value,(int,float)) and not isinstance(value,bool):
|
|
||||||
check(value >= schema.get('minimum',-float('inf')), where + ': below minimum')
|
|
||||||
check(value <= schema.get('maximum',float('inf')), where + ': above maximum')
|
|
||||||
if 'anyOf' in schema:
|
|
||||||
for alternative in schema['anyOf']:
|
|
||||||
try:
|
|
||||||
validate(value, alternative, where)
|
|
||||||
break
|
|
||||||
except Failure:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
raise Failure(where + ': no anyOf branch matches')
|
|
||||||
|
|
||||||
def shape(value, name):
|
|
||||||
validate(value, SPEC['components']['schemas'][name])
|
|
||||||
return value
|
|
||||||
|
|
||||||
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
|
||||||
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
|
||||||
return None
|
return None
|
||||||
|
token = response.json().get("token")
|
||||||
|
check(bool(token), "login returns a token")
|
||||||
|
return token
|
||||||
|
|
||||||
class Client:
|
|
||||||
def __init__(self, base_url=None, ca_file=None, timeout=10):
|
|
||||||
self.base = (base_url or os.environ.get('BASE_URL') or ('https://localhost:8443' if LAB==7 else 'http://localhost:8080')).rstrip('/')
|
|
||||||
check(self.base.startswith(('http://','https://')), 'BASE_URL must be HTTP(S)')
|
|
||||||
check(LAB != 7 or self.base.startswith('https://'), 'lab7 requires HTTPS')
|
|
||||||
self.timeout = timeout
|
|
||||||
context = ssl.create_default_context(cafile=ca_file or os.environ.get('CA_FILE') or None)
|
|
||||||
self.opener = urllib.request.build_opener(NoRedirect(), urllib.request.HTTPSHandler(context=context))
|
|
||||||
|
|
||||||
def call(self, method, path, expected=200, body=None, actor=None, headers=None, raw=None, schema=None):
|
def main():
|
||||||
h = {'Accept':'application/json'}
|
token = login()
|
||||||
if actor:
|
if token is None:
|
||||||
h['X-User-Id' if LAB==1 else 'Authorization'] = actor['id'] if LAB==1 else 'Bearer ' + actor['token']
|
return 1
|
||||||
if body is not None:
|
|
||||||
raw = json.dumps(body,ensure_ascii=False).encode('utf-8')
|
|
||||||
h['Content-Type'] = 'application/json'
|
|
||||||
h.update(headers or {})
|
|
||||||
req = urllib.request.Request(self.base + path, data=raw, headers=h, method=method)
|
|
||||||
start = time.monotonic()
|
|
||||||
try:
|
|
||||||
response = self.opener.open(req, timeout=self.timeout)
|
|
||||||
except urllib.error.HTTPError as error:
|
|
||||||
response = error
|
|
||||||
except (urllib.error.URLError,TimeoutError,OSError) as exc:
|
|
||||||
# Do not print URL query, headers, request body, or credentials.
|
|
||||||
raise Failure(method + ' ' + path.split('?')[0] + ': connection/TLS failure (' + type(exc).__name__ + ')') from None
|
|
||||||
with response:
|
|
||||||
status = response.code
|
|
||||||
rh = response.headers
|
|
||||||
data = response.read(16*1024*1024 + 1)
|
|
||||||
check(len(data) <= 16*1024*1024, 'response exceeds test safety limit')
|
|
||||||
allowed = [expected] if isinstance(expected,int) else expected
|
|
||||||
check(status in allowed, method + ' ' + path.split('?')[0] + ': expected ' + str(allowed) + ', received ' + str(status))
|
|
||||||
check(bool(rh.get('X-Request-Id')), 'missing X-Request-Id')
|
|
||||||
if LAB>=3:
|
|
||||||
check(bool(rh.get('X-Instance-Id')), 'missing X-Instance-Id')
|
|
||||||
payload = None
|
|
||||||
if schema or status>=400:
|
|
||||||
check(rh.get_content_type()=='application/json', 'expected application/json')
|
|
||||||
try:
|
|
||||||
payload = json.loads(data)
|
|
||||||
except (ValueError,UnicodeError):
|
|
||||||
raise Failure('invalid response JSON') from None
|
|
||||||
shape(payload, 'Error' if status>=400 else schema)
|
|
||||||
if status>=400:
|
|
||||||
check(payload['error']['request_id']==rh.get('X-Request-Id'),'error request_id differs from header')
|
|
||||||
if status==401 and LAB>=2:
|
|
||||||
scheme = 'Basic' if path=='/api/v1/auth/sessions' else 'Bearer'
|
|
||||||
check(rh.get('WWW-Authenticate','').lower().startswith(scheme.lower()), 'incorrect WWW-Authenticate challenge')
|
|
||||||
if status==204:
|
|
||||||
check(not data, '204 must not contain body')
|
|
||||||
return {'status':status,'headers':rh,'json':payload,'bytes':data,'elapsed':time.monotonic()-start}
|
|
||||||
|
|
||||||
def user(self, label):
|
auth = {"Authorization": "Bearer " + token}
|
||||||
username = 'u_' + label + '_' + uuid.uuid4().hex[:12]
|
|
||||||
password = 'T3st-' + uuid.uuid4().hex
|
|
||||||
body = {'username':username,'display_name':'Студент ' + label}
|
|
||||||
if LAB>=2:
|
|
||||||
body['password'] = password
|
|
||||||
user = self.call('POST','/api/v1/users',201,body=body,schema='User')['json']
|
|
||||||
check(user['username']==username,'username changed')
|
|
||||||
actor = {'id':user['id'],'user':user,'registration':body}
|
|
||||||
if LAB>=2:
|
|
||||||
credential = base64.b64encode((username+':'+password).encode()).decode()
|
|
||||||
session = self.call('POST','/api/v1/auth/sessions',201,headers={'Authorization':'Basic '+credential},schema='Session')['json']
|
|
||||||
check(session['user']==user,'session user mismatch')
|
|
||||||
check(timestamp(session['expires_at']) > dt.datetime.now(dt.timezone.utc),'session already expired')
|
|
||||||
actor['token'] = session['token']
|
|
||||||
return actor
|
|
||||||
|
|
||||||
def bootstrap(self):
|
response = requests.get(BASE + "/api/messages", headers=auth, timeout=TIMEOUT)
|
||||||
self.call('GET','/health/live',schema='Health')
|
check(response.status_code == 200, "GET /api/messages returns 200")
|
||||||
self.call('GET','/health/ready',schema='Health')
|
instance = response.headers.get("X-Instance-Id")
|
||||||
alice, bob, eve = (self.user(label) for label in ('alice','bob','eve'))
|
check(bool(instance), "response carries X-Instance-Id (%s)" % instance)
|
||||||
chat = self.call('POST','/api/v1/chats',201,actor=alice,body={'title':'Контрактный тест','member_ids':[alice['id'],bob['id']]},schema='Chat')['json']
|
check(isinstance(response.json(), list), "GET /api/messages returns a JSON array")
|
||||||
check(set(chat['member_ids'])=={alice['id'],bob['id']},'chat member mismatch')
|
|
||||||
return alice,bob,eve,chat
|
|
||||||
|
|
||||||
def message(self, actor, chat, text, key=None, attachments=None):
|
key = "key-" + uuid.uuid4().hex[:12]
|
||||||
body = {'text':text}
|
body = {"recipient": LOGIN, "body": "hello " + uuid.uuid4().hex[:8]}
|
||||||
if attachments is not None:
|
|
||||||
body['attachment_ids'] = attachments
|
|
||||||
headers = {'Idempotency-Key':key or uuid.uuid4().hex} if LAB>=3 else {}
|
|
||||||
return self.call('POST','/api/v1/chats/'+chat['id']+'/messages',201,actor=actor,body=body,headers=headers,schema='Message')
|
|
||||||
|
|
||||||
def attachment(self, actor, chat):
|
first = requests.post(
|
||||||
content = (ROOT / 'tests/fixtures/sample.png').read_bytes()
|
BASE + "/api/messages",
|
||||||
att = self.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=sample.png',201 if LAB==4 else 202,actor=actor,raw=content,headers={'Content-Type':'image/png'},schema='Attachment')['json']
|
headers=dict(auth, **{"Idempotency-Key": key}),
|
||||||
check(att['status']==('ready' if LAB==4 else 'queued'),'incorrect upload status')
|
json=body,
|
||||||
check(att['chat_id']==chat['id'] and att['owner_id']==actor['id'],'attachment ownership mismatch')
|
timeout=TIMEOUT,
|
||||||
check(att['sha256']==hashlib.sha256(content).hexdigest() and att['size_bytes']==len(content),'original checksum/size mismatch')
|
)
|
||||||
return att,content
|
check(first.status_code == 201, "first POST returns 201 (got %d)" % first.status_code)
|
||||||
|
|
||||||
def wait_ready(self, actor, att, wait_seconds=60):
|
second = requests.post(
|
||||||
deadline = time.monotonic()+wait_seconds
|
BASE + "/api/messages",
|
||||||
while time.monotonic()<deadline:
|
headers=dict(auth, **{"Idempotency-Key": key}),
|
||||||
att = self.call('GET','/api/v1/attachments/'+att['id'],actor=actor,schema='Attachment')['json']
|
json=body,
|
||||||
check(att['status']!='failed','attachment reached failed')
|
timeout=TIMEOUT,
|
||||||
if att['status']=='ready':
|
)
|
||||||
return att
|
check(second.status_code in (200, 201), "replayed POST returns 2xx (got %d)" % second.status_code)
|
||||||
time.sleep(0.3)
|
|
||||||
raise Failure('attachment did not become ready within test deadline')
|
|
||||||
|
|
||||||
def arguments(description, configure=None):
|
first_id = first.json().get("id")
|
||||||
import argparse
|
second_id = second.json().get("id")
|
||||||
p = argparse.ArgumentParser(description=description)
|
check(first_id is not None, "first POST returns message id")
|
||||||
p.add_argument('--base-url', default=None)
|
check(first_id == second_id, "replay returns the same id (%s vs %s)" % (first_id, second_id))
|
||||||
p.add_argument('--ca-file', default=None)
|
|
||||||
if configure:
|
|
||||||
configure(p)
|
|
||||||
return p.parse_args()
|
|
||||||
|
|
||||||
def run(main):
|
missing_key = requests.post(BASE + "/api/messages", headers=auth, json=body, timeout=TIMEOUT)
|
||||||
|
check(missing_key.status_code == 400, "POST without Idempotency-Key returns 400 (got %d)" % missing_key.status_code)
|
||||||
|
|
||||||
|
unauthorized = requests.get(BASE + "/api/messages", timeout=TIMEOUT)
|
||||||
|
check(unauthorized.status_code == 401, "GET without token returns 401 (got %d)" % unauthorized.status_code)
|
||||||
|
|
||||||
|
messages = requests.get(BASE + "/api/messages", headers=auth, timeout=TIMEOUT).json()
|
||||||
|
matching = [m for m in messages if m.get("body") == body["body"]]
|
||||||
|
check(len(matching) == 1, "history contains exactly one copy of the message (got %d)" % len(matching))
|
||||||
|
|
||||||
|
if failures:
|
||||||
|
print("\n%d check(s) failed" % len(failures))
|
||||||
|
return 1
|
||||||
|
print("\nAll API checks passed")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
try:
|
try:
|
||||||
main()
|
sys.exit(main())
|
||||||
except (Failure,ValueError,OSError) as exc:
|
except requests.RequestException as exc:
|
||||||
print('FAIL:', str(exc))
|
print("request failed:", exc)
|
||||||
raise SystemExit(1)
|
sys.exit(1)
|
||||||
|
|||||||
+157
-51
@@ -1,58 +1,164 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Small closed-loop HTTP write benchmark; not an open-loop capacity proof."""
|
import subprocess
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
import sys
|
||||||
import json
|
|
||||||
import math
|
|
||||||
from pathlib import Path
|
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
from client import Client, Failure, LAB, arguments, check, run
|
from pathlib import Path
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
BASE = "http://localhost:8080"
|
||||||
|
LOGIN = "test"
|
||||||
|
PASSWORD = "test123"
|
||||||
|
TIMEOUT = 5
|
||||||
|
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
RECOVERY_BUDGET = 10.0
|
||||||
|
|
||||||
|
failures = []
|
||||||
|
|
||||||
|
|
||||||
|
def check(condition, message):
|
||||||
|
if condition:
|
||||||
|
print("PASS:", message)
|
||||||
|
return True
|
||||||
|
failures.append(message)
|
||||||
|
print("FAIL:", message)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def login():
|
||||||
|
response = requests.post(
|
||||||
|
BASE + "/api/auth/login",
|
||||||
|
json={"login": LOGIN, "password": PASSWORD},
|
||||||
|
timeout=TIMEOUT,
|
||||||
|
)
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.json()["token"]
|
||||||
|
|
||||||
|
|
||||||
|
def fetch(token):
|
||||||
|
response = requests.get(
|
||||||
|
BASE + "/api/messages",
|
||||||
|
headers={"Authorization": "Bearer " + token},
|
||||||
|
timeout=TIMEOUT,
|
||||||
|
)
|
||||||
|
response.raise_for_status()
|
||||||
|
return response.headers.get("X-Instance-Id", "")
|
||||||
|
|
||||||
|
|
||||||
|
def round_robin(token, count):
|
||||||
|
instances = []
|
||||||
|
lock = threading.Lock()
|
||||||
|
|
||||||
|
def worker():
|
||||||
|
try:
|
||||||
|
instance = fetch(token)
|
||||||
|
except requests.RequestException:
|
||||||
|
return
|
||||||
|
with lock:
|
||||||
|
instances.append(instance)
|
||||||
|
|
||||||
|
threads = [threading.Thread(target=worker) for _ in range(count)]
|
||||||
|
for thread in threads:
|
||||||
|
thread.start()
|
||||||
|
for thread in threads:
|
||||||
|
thread.join()
|
||||||
|
return instances
|
||||||
|
|
||||||
|
|
||||||
|
def compose(*args):
|
||||||
|
return subprocess.run(
|
||||||
|
["docker", "compose", *args],
|
||||||
|
cwd=REPO_ROOT,
|
||||||
|
capture_output=True,
|
||||||
|
text=True,
|
||||||
|
check=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
def configure(p):
|
token = login()
|
||||||
p.add_argument('--duration',type=float,default=30)
|
auth = {"Authorization": "Bearer " + token}
|
||||||
p.add_argument('--concurrency',type=int,default=4)
|
|
||||||
p.add_argument('--output',default=None,help='Optional JSON metrics, no session tokens')
|
|
||||||
args = arguments('Synthetic POST message load, closed loop; creates users/messages',configure)
|
|
||||||
check(1<=args.duration<=3600,'duration must be 1..3600 seconds')
|
|
||||||
check(1<=args.concurrency<=128,'concurrency must be 1..128')
|
|
||||||
c = Client(args.base_url,args.ca_file)
|
|
||||||
alice,bob,eve,chat = c.bootstrap()
|
|
||||||
barrier = threading.Barrier(args.concurrency)
|
|
||||||
def worker(worker_id):
|
|
||||||
local = Client(args.base_url,args.ca_file,timeout=5)
|
|
||||||
barrier.wait()
|
|
||||||
deadline = time.monotonic()+args.duration
|
|
||||||
latencies,errors = [],0
|
|
||||||
index = 0
|
|
||||||
while time.monotonic()<deadline:
|
|
||||||
start = time.monotonic()
|
|
||||||
try:
|
|
||||||
local.message(alice,chat,'load-%s-%s' % (worker_id,index))
|
|
||||||
latencies.append(time.monotonic()-start)
|
|
||||||
except Failure:
|
|
||||||
errors += 1
|
|
||||||
index += 1
|
|
||||||
return latencies,errors
|
|
||||||
started = time.monotonic()
|
|
||||||
with ThreadPoolExecutor(max_workers=args.concurrency) as pool:
|
|
||||||
results = list(pool.map(worker,range(args.concurrency)))
|
|
||||||
elapsed = time.monotonic()-started
|
|
||||||
latencies = sorted(x for values,_ in results for x in values)
|
|
||||||
errors = sum(n for _,n in results)
|
|
||||||
total = len(latencies)+errors
|
|
||||||
def percentile(p):
|
|
||||||
return round(1000*latencies[max(0,math.ceil(p*len(latencies))-1)],3) if latencies else None
|
|
||||||
report = {'lab':LAB,'workload':'closed-loop POST messages; no automatic retries','requested_duration_seconds':args.duration,'elapsed_seconds':round(elapsed,3),'concurrency':args.concurrency,'successful_requests':len(latencies),'failed_requests':errors,'error_ratio':round(errors/total,6) if total else None,'successful_rps':round(len(latencies)/elapsed,3),'successful_latency_ms':{'p50':percentile(.50),'p95':percentile(.95),'p99':percentile(.99)},'note':'Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead.'}
|
|
||||||
text = json.dumps(report,ensure_ascii=False,indent=2)
|
|
||||||
if args.output:
|
|
||||||
dest = Path(args.output)
|
|
||||||
dest.parent.mkdir(parents=True,exist_ok=True)
|
|
||||||
dest.write_text(text+'\n')
|
|
||||||
print(text)
|
|
||||||
check(bool(latencies),'no successful writes')
|
|
||||||
# Errors are reported, not treated as a universal load-test threshold.
|
|
||||||
|
|
||||||
if __name__=='__main__':
|
probe = requests.post(
|
||||||
run(main)
|
BASE + "/api/messages",
|
||||||
|
headers=dict(auth, **{"Idempotency-Key": "load-" + str(time.time_ns())}),
|
||||||
|
json={"recipient": LOGIN, "body": "before failover"},
|
||||||
|
timeout=TIMEOUT,
|
||||||
|
)
|
||||||
|
check(probe.status_code == 201, "message written before failover (got %d)" % probe.status_code)
|
||||||
|
probe_id = probe.json().get("id")
|
||||||
|
|
||||||
|
instances = round_robin(token, 20)
|
||||||
|
check(len(set(instances)) >= 2, "requests reached at least two replicas: %s" % sorted(set(instances)))
|
||||||
|
|
||||||
|
print("\n=== Stopping api1 ===")
|
||||||
|
stop = compose("stop", "api1")
|
||||||
|
check(stop.returncode == 0, "docker compose stop api1 succeeded")
|
||||||
|
started = time.monotonic()
|
||||||
|
|
||||||
|
recovered_at = None
|
||||||
|
served_after = []
|
||||||
|
transient_errors = 0
|
||||||
|
while time.monotonic() - started < RECOVERY_BUDGET + 5:
|
||||||
|
try:
|
||||||
|
instance = fetch(token)
|
||||||
|
except requests.RequestException:
|
||||||
|
transient_errors += 1
|
||||||
|
time.sleep(0.2)
|
||||||
|
continue
|
||||||
|
if recovered_at is None:
|
||||||
|
recovered_at = time.monotonic() - started
|
||||||
|
served_after.append(instance)
|
||||||
|
if len(served_after) >= 5:
|
||||||
|
break
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
check(
|
||||||
|
recovered_at is not None and recovered_at <= RECOVERY_BUDGET,
|
||||||
|
"reads recovered within %.0fs (%.2fs, %d transient errors)"
|
||||||
|
% (RECOVERY_BUDGET, recovered_at if recovered_at is not None else -1, transient_errors),
|
||||||
|
)
|
||||||
|
check(bool(served_after), "requests still served while api1 is down")
|
||||||
|
check(all(x == "api2" for x in served_after), "api1 left rotation, served by: %s" % sorted(set(served_after)))
|
||||||
|
|
||||||
|
write = requests.post(
|
||||||
|
BASE + "/api/messages",
|
||||||
|
headers=dict(auth, **{"Idempotency-Key": "load-fail-" + str(time.time_ns())}),
|
||||||
|
json={"recipient": LOGIN, "body": "written during failover"},
|
||||||
|
timeout=TIMEOUT,
|
||||||
|
)
|
||||||
|
check(write.status_code == 201, "write works while api1 is down (got %d)" % write.status_code)
|
||||||
|
|
||||||
|
print("\n=== Starting api1 back ===")
|
||||||
|
start = compose("start", "api1")
|
||||||
|
check(start.returncode == 0, "docker compose start api1 succeeded")
|
||||||
|
|
||||||
|
deadline = time.monotonic() + 30
|
||||||
|
back = set()
|
||||||
|
while time.monotonic() < deadline:
|
||||||
|
back = set(round_robin(token, 10))
|
||||||
|
if "api1" in back:
|
||||||
|
break
|
||||||
|
time.sleep(1)
|
||||||
|
check("api1" in back, "api1 is back in rotation: %s" % sorted(back))
|
||||||
|
|
||||||
|
history = requests.get(BASE + "/api/messages", headers=auth, timeout=TIMEOUT)
|
||||||
|
check(history.status_code == 200, "history readable after failover (got %d)" % history.status_code)
|
||||||
|
ids = [m.get("id") for m in history.json()]
|
||||||
|
check(probe_id in ids, "message written before failover survived")
|
||||||
|
check(len(ids) == len(set(ids)), "no duplicate messages in history")
|
||||||
|
|
||||||
|
if failures:
|
||||||
|
print("\n%d check(s) failed" % len(failures))
|
||||||
|
return 1
|
||||||
|
print("\nFailover scenario passed")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
sys.exit(main())
|
||||||
|
except requests.RequestException as exc:
|
||||||
|
print("request failed:", exc)
|
||||||
|
sys.exit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user