forked from mtusi2026/mtusi2026lab3
feat: API replicas over HAProxy (3)
This commit is contained in:
Executable
+56
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys
|
||||
import threading
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
|
||||
from client import Client, check
|
||||
|
||||
WORKERS = 20
|
||||
|
||||
|
||||
def main():
|
||||
c = Client()
|
||||
alice, _, _, chat = c.bootstrap()
|
||||
key = uuid.uuid4().hex
|
||||
text = 'Одновременный повтор ' + key
|
||||
barrier = threading.Barrier(WORKERS)
|
||||
results, errors = [], []
|
||||
lock = threading.Lock()
|
||||
|
||||
def send():
|
||||
barrier.wait()
|
||||
try:
|
||||
r = c.message(alice, chat, text, key=key)
|
||||
with lock:
|
||||
results.append((r['json'], r['headers']['X-Instance-Id']))
|
||||
except AssertionError as exc:
|
||||
with lock:
|
||||
errors.append(str(exc))
|
||||
|
||||
threads = [threading.Thread(target=send) for _ in range(WORKERS)]
|
||||
for t in threads:
|
||||
t.start()
|
||||
for t in threads:
|
||||
t.join()
|
||||
|
||||
history = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages?limit=100', actor=alice, schema='MessagePage')['json']['items']
|
||||
stored = [m for m in history if m['text'] == text]
|
||||
instances = sorted({i for _, i in results})
|
||||
ids = {m['id'] for m, _ in results}
|
||||
print('parallel requests with one key:', WORKERS)
|
||||
print('successful responses (201):', len(results))
|
||||
print('failed requests:', len(errors), errors[:3])
|
||||
print('distinct message ids in responses:', len(ids))
|
||||
print('messages stored in history:', len(stored))
|
||||
print('instances that served requests:', instances)
|
||||
check(not errors, 'some requests failed')
|
||||
check(len(ids) == 1 and len(stored) == 1, 'duplicate message created')
|
||||
check(all(m == results[0][0] for m, _ in results), 'replayed responses differ')
|
||||
check(len(instances) >= 2, 'requests were not spread across replicas')
|
||||
print('OK: exactly one message created')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
COMPOSE=${COMPOSE:-docker compose}
|
||||
COMPOSE=${COMPOSE:-podman compose}
|
||||
|
||||
$COMPOSE stop api postgres redis
|
||||
$COMPOSE start postgres redis api
|
||||
$COMPOSE stop lb api redis postgres
|
||||
$COMPOSE start postgres redis api lb
|
||||
|
||||
Executable
+13
@@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
ENGINE=${ENGINE:-podman}
|
||||
PROJECT=${PROJECT:-messenger-lab3}
|
||||
: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}"
|
||||
|
||||
for id in $($ENGINE ps -q --filter "label=com.docker.compose.project=$PROJECT" --filter "label=com.docker.compose.service=api"); do
|
||||
if [ "$($ENGINE inspect -f '{{.Config.Hostname}}' "$id")" = "$TARGET_INSTANCE_ID" ]; then
|
||||
$ENGINE stop -t 5 "$id" >/dev/null
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
exit 1
|
||||
Reference in New Issue
Block a user