Реализация лабораторной 3: балансировка, отказ API-реплики, idempotency
- 2 API-реплики (api-1, api-2) с уникальным INSTANCE_ID - Nginx load balancer с health-check и failover - PostgreSQL источник истины, Valkey кэш сессий - Idempotency-Key в заголовке, replay возвращает исходное сообщение - X-Instance-Id в каждом ответе для идентификации реплики - /version и /info эндпоинты - Housekeeping-loop для очистки истёкших сессий и ключей - stop-one.sh/cmd для failover-теста, restart.sh/cmd для persistence - Исправлены импорты config/models/api/store/sessions - Добавлены миграции 004 (idempotency), 005 (indexes), 006 (message_id) - Отчёт с доказательствами критериев уровня 3 и 4 - Все тесты проходят: smoke, 26 student tests, failover, persistence, degradation
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
#!/bin/sh
|
||||
# Generate the local, synthetic stand's secrets and .env.
|
||||
# Values are written to ./secrets (git-ignored, mode 0600) and never printed.
|
||||
# Re-running is safe: existing files are kept unless FORCE=1.
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
PYTHON="${PYTHON:-python}"
|
||||
SECRETS_DIR="${SECRETS_DIR:-secrets}"
|
||||
|
||||
if [ ! -f .env ]; then
|
||||
cp .env.example .env
|
||||
printf '%s\n' "created .env from .env.example"
|
||||
fi
|
||||
|
||||
mkdir -p "$SECRETS_DIR"
|
||||
chmod 700 "$SECRETS_DIR" 2>/dev/null || true
|
||||
|
||||
umask 077
|
||||
|
||||
random_password() {
|
||||
"$PYTHON" -c 'import secrets; print(secrets.token_hex(24))'
|
||||
}
|
||||
|
||||
write_if_missing() {
|
||||
path="$SECRETS_DIR/$1"
|
||||
if [ -f "$path" ] && [ "${FORCE:-0}" != "1" ]; then
|
||||
return 0
|
||||
fi
|
||||
printf '%s' "$2" > "$path"
|
||||
chmod 600 "$path" 2>/dev/null || true
|
||||
}
|
||||
|
||||
if [ -f "$SECRETS_DIR/database_url" ] && [ -f "$SECRETS_DIR/cache_url" ] && [ "${FORCE:-0}" != "1" ]; then
|
||||
printf '%s\n' "secrets already present in $SECRETS_DIR (FORCE=1 to regenerate)"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
postgres_password="$(random_password)"
|
||||
valkey_password="$(random_password)"
|
||||
|
||||
write_if_missing postgres_password "$postgres_password"
|
||||
write_if_missing valkey_password "$valkey_password"
|
||||
write_if_missing database_url "postgresql://messenger:${postgres_password}@postgres:5432/messenger"
|
||||
write_if_missing cache_url "redis://:${valkey_password}@valkey:6379/0"
|
||||
|
||||
printf '%s\n' "wrote secrets to $SECRETS_DIR: postgres_password, valkey_password, database_url, cache_url"
|
||||
printf '%s\n' "next: docker compose --env-file .env up -d --build"
|
||||
@@ -8,8 +8,8 @@ import re
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
def main():
|
||||
lab = json.loads((ROOT/'lab.json').read_text())['number']
|
||||
spec = json.loads((ROOT/'contracts/openapi.json').read_text())
|
||||
lab = json.loads((ROOT/'lab.json').read_text(encoding='utf-8'))['number']
|
||||
spec = json.loads((ROOT/'contracts/openapi.json').read_text(encoding='utf-8'))
|
||||
assert spec['openapi']=='3.1.0'
|
||||
assert spec['info']['version']=='1.%d.0'%lab
|
||||
operation_ids = set()
|
||||
@@ -41,11 +41,11 @@ def main():
|
||||
assert ('/api/v1/auth/sessions' in spec['paths']) == (lab>=2)
|
||||
assert ('/api/v1/attachments/{attachment_id}' in spec['paths']) == (lab>=4)
|
||||
for file in list((ROOT/'tests').glob('*.py'))+list((ROOT/'scripts').glob('*.py')):
|
||||
ast.parse(file.read_text(),filename=str(file))
|
||||
ast.parse(file.read_text(encoding='utf-8'),filename=str(file))
|
||||
for file in ROOT.rglob('*.json'):
|
||||
if any(part in {'.git','.venv','node_modules','vendor','output'} for part in file.parts):
|
||||
continue
|
||||
json.loads(file.read_text())
|
||||
json.loads(file.read_text(encoding='utf-8'))
|
||||
for required in ('README.md','COURSE.md','CONTRIBUTING.md','REPORT.md','contracts/README.md','tests/smoke.py','tests/README.md'):
|
||||
assert (ROOT/required).is_file(), 'missing '+required
|
||||
print('PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.')
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
@echo off
|
||||
setlocal
|
||||
cd /d "%~dp0.."
|
||||
if "%COMPOSE_ENV_FILE%"=="" set COMPOSE_ENV_FILE=.env
|
||||
echo restart: stopping api, postgres and valkey (volumes preserved)
|
||||
docker compose --env-file %COMPOSE_ENV_FILE% down --timeout 15
|
||||
if errorlevel 1 exit /b 1
|
||||
echo restart: starting api, postgres and valkey
|
||||
docker compose --env-file %COMPOSE_ENV_FILE% up -d --wait --wait-timeout 120
|
||||
if errorlevel 1 exit /b 1
|
||||
docker compose --env-file %COMPOSE_ENV_FILE% ps
|
||||
echo stack_restarted volumes_preserved
|
||||
exit /b 0
|
||||
@@ -0,0 +1,17 @@
|
||||
#!/bin/sh
|
||||
# Restart hook for `make test-persistence ACTION_SCRIPT=scripts/restart.sh`.
|
||||
# Recreates the API and BOTH state stores from the same images, keeping the
|
||||
# named volumes (pgdata, valkeydata): no `down -v`, no seed, no data reset.
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
ENV_FILE="${COMPOSE_ENV_FILE:-.env}"
|
||||
|
||||
printf '%s\n' "restart: stopping api, postgres and valkey (volumes preserved)"
|
||||
docker compose --env-file "$ENV_FILE" down --timeout 15
|
||||
|
||||
printf '%s\n' "restart: starting api, postgres and valkey"
|
||||
docker compose --env-file "$ENV_FILE" up -d --wait --wait-timeout 120
|
||||
|
||||
docker compose --env-file "$ENV_FILE" ps
|
||||
printf '%s\n' "stack_restarted volumes_preserved"
|
||||
@@ -0,0 +1,17 @@
|
||||
@echo off
|
||||
setlocal
|
||||
cd /d "%~dp0.."
|
||||
if "%COMPOSE_ENV_FILE%"=="" set COMPOSE_ENV_FILE=.env
|
||||
if "%TARGET_INSTANCE_ID%"=="" (
|
||||
echo TARGET_INSTANCE_ID is required >&2
|
||||
exit /b 2
|
||||
)
|
||||
if not "%TARGET_INSTANCE_ID%"=="api-1" if not "%TARGET_INSTANCE_ID%"=="api-2" (
|
||||
echo Invalid TARGET_INSTANCE_ID: %TARGET_INSTANCE_ID% >&2
|
||||
exit /b 2
|
||||
)
|
||||
echo stop-one: stopping API replica %TARGET_INSTANCE_ID%
|
||||
docker compose --env-file %COMPOSE_ENV_FILE% stop --timeout 10 %TARGET_INSTANCE_ID%
|
||||
if errorlevel 1 exit /b 1
|
||||
echo stop-one: %TARGET_INSTANCE_ID% stopped
|
||||
exit /b 0
|
||||
@@ -0,0 +1,26 @@
|
||||
#!/bin/sh
|
||||
# Failover hook for `make test-failover ACTION_SCRIPT=scripts/stop-one.sh`.
|
||||
# Stops ONE API replica (selected by TARGET_INSTANCE_ID = api-1 or api-2) and
|
||||
# leaves it stopped; the load balancer keeps serving via the remaining replica.
|
||||
# Does not touch postgres, valkey, nginx or any volumes. Restore the replica
|
||||
# explicitly after the test (docker compose start <service>).
|
||||
set -eu
|
||||
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
ENV_FILE="${COMPOSE_ENV_FILE:-.env}"
|
||||
|
||||
: "${TARGET_INSTANCE_ID:?test provides target API instance alias (api-1 or api-2)}"
|
||||
|
||||
case "$TARGET_INSTANCE_ID" in
|
||||
api-1|api-2) ;;
|
||||
*)
|
||||
printf '%s\n' "stop-one: invalid TARGET_INSTANCE_ID='$TARGET_INSTANCE_ID' (expected api-1 or api-2)" >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
|
||||
printf '%s\n' "stop-one: stopping API replica $TARGET_INSTANCE_ID (nginx keeps serving via the other replica)"
|
||||
docker compose --env-file "$ENV_FILE" stop --timeout 10 "$TARGET_INSTANCE_ID"
|
||||
|
||||
printf '%s\n' "stop-one: $TARGET_INSTANCE_ID stopped"
|
||||
@@ -6,17 +6,17 @@ from openapi_spec_validator import validate
|
||||
from jsonschema import Draft202012Validator
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
spec = json.loads((root/'contracts/openapi.json').read_text())
|
||||
spec = json.loads((root/'contracts/openapi.json').read_text(encoding='utf-8'))
|
||||
validate(spec)
|
||||
from referencing import Registry, Resource
|
||||
# The base URI resolves local #/components references inside examples.
|
||||
resource = dict(spec, **{'$schema': 'https://json-schema.org/draft/2020-12/schema'})
|
||||
registry = Registry().with_resource('urn:mtusi:openapi', Resource.from_contents(resource))
|
||||
for name, example in json.loads((root/'contracts/examples.json').read_text()).items():
|
||||
for name, example in json.loads((root/'contracts/examples.json').read_text(encoding='utf-8')).items():
|
||||
schema = {'$ref': 'urn:mtusi:openapi#/components/schemas/' + name}
|
||||
Draft202012Validator(schema, registry=registry).validate(example)
|
||||
for path in (root/'contracts').glob('*.schema.json'):
|
||||
schema = json.loads(path.read_text())
|
||||
schema = json.loads(path.read_text(encoding='utf-8'))
|
||||
Draft202012Validator.check_schema(schema)
|
||||
for example in schema.get('examples',[]):
|
||||
Draft202012Validator(schema).validate(example)
|
||||
|
||||
Reference in New Issue
Block a user