completed lab6

This commit is contained in:
2026-11-20 17:52:56 +03:00
parent 470d9d6f1e
commit 06c8c643c3
29 changed files with 1663 additions and 54 deletions
+27
View File
@@ -0,0 +1,27 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
python3 - <<'PY'
from pathlib import Path
import secrets
import json
p = Path('.env')
if not p.exists():
p.write_text('\n'.join(k+'='+secrets.token_hex(20) for k in (
'POSTGRES_PASSWORD','BROKER_PASSWORD','S3_ACCESS_KEY_ID','S3_SECRET_ACCESS_KEY','GRAFANA_PASSWORD'))+'\n')
p.chmod(0o600)
print('Created local .env')
else:
print('Keeping existing .env')
values = dict(line.split('=', 1) for line in p.read_text().splitlines()
if line.strip() and not line.lstrip().startswith('#') and '=' in line)
folder = Path('secrets')
folder.mkdir(exist_ok=True, mode=0o700)
config = folder / 'seaweed-s3.json'
config.write_text(json.dumps({'identities': [{'name': 'messenger',
'credentials': [{'accessKey': values['S3_ACCESS_KEY_ID'].strip(),
'secretKey': values['S3_SECRET_ACCESS_KEY'].strip()}],
'actions': ['Admin', 'Read', 'Write', 'List', 'Tagging']}]}))
config.chmod(0o600)
print('Configured authenticated SeaweedFS storage')
PY
+4
View File
@@ -0,0 +1,4 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
docker compose restart api1 api2 worker postgres s3 broker otel-collector prometheus loki tempo grafana lb
+7
View File
@@ -0,0 +1,7 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
case "${TARGET_INSTANCE_ID:-}" in
api1|api2) docker compose stop "$TARGET_INSTANCE_ID" ;;
*) exit 2 ;;
esac
+34
View File
@@ -0,0 +1,34 @@
#!/bin/sh
# Restore both replicas before and after the unchanged public failure test.
set -eu
cd "$(dirname "$0")/.."
restore() {
docker compose start api1 api2
}
trap restore EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
restore
"${PYTHON:-python3}" - <<'PY'
import os
import time
import urllib.request
base = os.environ.get('BASE_URL', 'http://localhost:8080').rstrip('/')
seen = set()
deadline = time.monotonic() + 90
while time.monotonic() < deadline:
try:
with urllib.request.urlopen(base + '/health/ready', timeout=3) as response:
instance = response.headers.get('X-Instance-Id')
if response.status == 200 and instance:
seen.add(instance)
if {'api1', 'api2'} <= seen:
print('Both API replicas are ready through the load balancer')
break
except OSError:
pass
time.sleep(0.3)
else:
raise SystemExit('Both API replicas did not become ready within 90 seconds')
PY
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
+49
View File
@@ -0,0 +1,49 @@
#!/usr/bin/env python3
"""Check that an upload survives a temporary worker outage."""
from pathlib import Path
import subprocess
import sys
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests'))
from client import Client, arguments, check, run
def compose(action):
try:
subprocess.run(['docker', 'compose', action, 'worker'], cwd=ROOT, check=True)
except subprocess.CalledProcessError as exc:
raise SystemExit(f'docker compose {action} worker failed ({exc.returncode})') from None
def main():
args = arguments(__doc__)
client = Client(args.base_url, args.ca_file)
alice, _, _, chat = client.bootstrap()
content = (ROOT / 'tests/fixtures/sample.png').read_bytes()
try:
compose('stop')
upload = client.call(
'POST', f'/api/v1/chats/{chat["id"]}/attachments?filename=sample.png',
expected=202, actor=alice, raw=content,
headers={'Content-Type': 'image/png'}, schema='Attachment',
)
trace_id = upload['headers'].get('X-Trace-Id')
check(bool(trace_id), 'missing upload trace ID')
attachment = upload['json']
path = f'/api/v1/attachments/{attachment["id"]}'
metadata = client.call('GET', path, actor=alice, schema='Attachment')['json']
check(metadata['status'] == 'queued', 'expected queued while worker is stopped')
print(f'worker stopped, attachment queued, trace_id={trace_id}', flush=True)
finally:
compose('start')
client.wait_ready(alice, attachment)
original = client.call('GET', f'{path}/content', actor=alice)['bytes']
check(original == content, 'downloaded original differs from the upload')
print(f'PASS: attachment ready, original verified, trace_id={trace_id}')
if __name__ == '__main__':
run(main)