Files
mtusi2026lab4/student-tests/availability.py
T
2026-10-16 22:47:54 +03:00

65 lines
3.3 KiB
Python

"""Каждая реплика отдельно и поочерёдная замена под одним профилем нагрузки."""
import json
import os
from pathlib import Path
import sys
import time
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from scripts.benchmark import context, measure, verify_confirmations
from scripts.stack import containers, stop, wait, command, rolling, save
from scripts.monitor import Monitor
def main():
base = os.getenv('BASE_URL', 'http://localhost:8080')
aliases = wait(2, base)
results = []
for target in aliases:
ctx = context(base)
# Сохраняем подтверждённую запись до отказа вместе с подтверждениями нагрузки.
from scripts.benchmark import Client
original = Client(base).message(ctx['actor'], ctx['chat'], 'До отказа')['json']
attachment, content = Client(base).attachment(ctx['actor'], ctx['chat'])
try:
with Monitor() as monitor:
report, confirmations = measure(base, ctx, 16, 4,
action=lambda: stop(target), retry_writes=True)
event = report['action_events'][0]
successful_times = sorted(int(x) for x, count in report['successful_per_second'].items() if count)
first = next((x for x in successful_times if x > event['end_seconds']), None)
if first is None or first+1-event['start_seconds'] > 10:
raise AssertionError('Recovery exceeded 10 seconds')
stopped_host = target.split(':')[0]
if any(r['running'] for r in containers('api') if r['hostname'] == stopped_host):
raise AssertionError('Target is still running')
report['first_successful_bucket_after_stop_seconds'] = first
report['recovery_upper_bound_seconds'] = round(first+1-event['start_seconds'], 3)
report['verification'] = verify_confirmations(base, ctx, [original, *confirmations])
assert Client(base).call('GET', '/api/v1/attachments/'+attachment['id']+'/content',
actor=ctx['reader'])['bytes'] == content
report['attachment_original_verified'] = True
report['monitor'] = monitor.samples
report['target'] = target
results.append(report)
save('failures.json', results)
finally:
command(['docker', 'compose', 'start', 'api'])
wait(2, base)
ctx = context(base)
attachment, content = Client(base).attachment(ctx['actor'], ctx['chat'])
original = Client(base).message(ctx['actor'], ctx['chat'], '', attachments=[attachment['id']])['json']
with Monitor() as monitor:
report, confirmations = measure(base, ctx, 35, 4, action=rolling, retry_writes=True)
report['verification'] = verify_confirmations(base, ctx, [original, *confirmations])
assert Client(base).call('GET', '/api/v1/attachments/'+attachment['id']+'/content',
actor=ctx['reader'])['bytes'] == content
report['attachment_original_verified'] = True
report['monitor'] = monitor.samples
save('rolling.json', report)
print('PASS: both API failures and rolling replacement, confirmations preserved')
if __name__ == '__main__':
main()