feat: ЛР 4 с хранением вложений в MinIO
This commit is contained in:
Executable
+25
@@ -0,0 +1,25 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||
project_dir=$(dirname -- "$script_dir")
|
||||
cd "$project_dir"
|
||||
|
||||
status=$(docker compose --env-file .env exec -T api python -c '
|
||||
import os
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
url = "http://s3:9000/" + os.environ["S3_BUCKET"] + "/"
|
||||
try:
|
||||
response = urllib.request.urlopen(url, timeout=5)
|
||||
except urllib.error.HTTPError as error:
|
||||
print(error.code)
|
||||
else:
|
||||
print(response.status)
|
||||
')
|
||||
|
||||
case "$status" in
|
||||
401|403) printf '%s\n' "PASS: private bucket rejected anonymous request with HTTP $status" ;;
|
||||
*) printf '%s\n' "FAIL: anonymous bucket request returned HTTP $status" >&2; exit 1 ;;
|
||||
esac
|
||||
Executable
+29
@@ -0,0 +1,29 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||
project_dir=$(dirname -- "$script_dir")
|
||||
cd "$project_dir"
|
||||
|
||||
restore() {
|
||||
docker compose --env-file .env up -d --wait --wait-timeout 60 s3 api lb >/dev/null
|
||||
}
|
||||
trap restore EXIT HUP INT TERM
|
||||
|
||||
docker compose --env-file .env stop s3 >/dev/null
|
||||
docker compose --env-file .env exec -T api python -c '
|
||||
import json
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
try:
|
||||
urllib.request.urlopen("http://127.0.0.1:8080/health/ready", timeout=25)
|
||||
except urllib.error.HTTPError as error:
|
||||
body = json.loads(error.read())
|
||||
assert error.code == 503, error.code
|
||||
assert body["error"]["code"] == "unavailable", body
|
||||
else:
|
||||
raise SystemExit("readiness unexpectedly succeeded while S3 was stopped")
|
||||
'
|
||||
|
||||
printf '%s\n' 'PASS: API readiness returned structured 503 while S3 was unavailable'
|
||||
Executable
+109
@@ -0,0 +1,109 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Run concurrent uploads while sampling API container memory with docker stats."""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
UNITS = {
|
||||
'B': 1,
|
||||
'kB': 1000,
|
||||
'KB': 1000,
|
||||
'KiB': 1024,
|
||||
'MB': 1000**2,
|
||||
'MiB': 1024**2,
|
||||
'GB': 1000**3,
|
||||
'GiB': 1024**3,
|
||||
}
|
||||
|
||||
|
||||
def bytes_value(value: str) -> int:
|
||||
match = re.fullmatch(r'([0-9.]+)([A-Za-z]+)', value.strip())
|
||||
if match is None or match.group(2) not in UNITS:
|
||||
raise ValueError(f'unsupported docker memory value: {value}')
|
||||
return round(float(match.group(1)) * UNITS[match.group(2)])
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--base-url', default='http://localhost:8080')
|
||||
parser.add_argument('--concurrency', type=int, default=10)
|
||||
parser.add_argument('--uploads', type=int, default=30)
|
||||
args = parser.parse_args()
|
||||
|
||||
container_ids = subprocess.check_output(
|
||||
['docker', 'compose', '--env-file', '.env', 'ps', '-q', 'api'],
|
||||
cwd=ROOT,
|
||||
text=True,
|
||||
).split()
|
||||
if len(container_ids) < 2:
|
||||
raise SystemExit('expected at least two API containers')
|
||||
|
||||
peaks: dict[str, int] = {}
|
||||
stop = threading.Event()
|
||||
ready = threading.Event()
|
||||
|
||||
def collect() -> None:
|
||||
while not stop.is_set():
|
||||
output = subprocess.check_output(
|
||||
['docker', 'stats', '--no-stream', '--format', '{{json .}}', *container_ids],
|
||||
cwd=ROOT,
|
||||
text=True,
|
||||
)
|
||||
for line in output.splitlines():
|
||||
sample = json.loads(line)
|
||||
name, usage = sample['Name'], sample['MemUsage']
|
||||
current = bytes_value(usage.split('/', 1)[0])
|
||||
peaks[name] = max(peaks.get(name, 0), current)
|
||||
ready.set()
|
||||
|
||||
reader = threading.Thread(target=collect, daemon=True)
|
||||
reader.start()
|
||||
if not ready.wait(timeout=15):
|
||||
raise SystemExit('docker stats did not produce an initial sample')
|
||||
try:
|
||||
load = subprocess.run(
|
||||
[
|
||||
sys.executable,
|
||||
'tests/upload_load.py',
|
||||
'--base-url',
|
||||
args.base_url,
|
||||
'--concurrency',
|
||||
str(args.concurrency),
|
||||
'--uploads',
|
||||
str(args.uploads),
|
||||
],
|
||||
cwd=ROOT,
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
finally:
|
||||
stop.set()
|
||||
reader.join(timeout=15)
|
||||
|
||||
if not peaks:
|
||||
raise SystemExit('docker stats produced no samples; increase --uploads')
|
||||
print(load.stdout.strip())
|
||||
print(
|
||||
json.dumps(
|
||||
{
|
||||
'peak_memory_bytes': peaks,
|
||||
'peak_memory_mib': {
|
||||
name: round(value / 1024 / 1024, 2) for name, value in peaks.items()
|
||||
},
|
||||
},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
+2
-2
@@ -7,6 +7,6 @@ cd "$project_dir"
|
||||
|
||||
# Останавливаем публичный маршрут до перезапуска общих хранилищ.
|
||||
docker compose --env-file .env stop lb api >/dev/null
|
||||
docker compose --env-file .env restart postgres redis >/dev/null
|
||||
docker compose --env-file .env up -d --wait --wait-timeout 60 postgres redis >/dev/null
|
||||
docker compose --env-file .env restart postgres redis s3 >/dev/null
|
||||
docker compose --env-file .env up -d --wait --wait-timeout 60 postgres redis s3 >/dev/null
|
||||
docker compose --env-file .env up -d --wait --wait-timeout 60 api lb >/dev/null
|
||||
|
||||
Reference in New Issue
Block a user