Files
2026-10-23 18:10:07 +03:00

80 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""Rolling update одной реплики: остановить → восстановить под нагрузкой."""
import threading
import time
import uuid
from common import (
BASE_URL, Client, Failure, check, print_pass, restore_api, stop_one,
)
def main():
c = Client(BASE_URL)
alice, bob, eve, chat = c.bootstrap()
path = '/api/v1/chats/' + chat['id'] + '/messages'
stop = threading.Event()
stats = {'ok': 0, 'err': 0, 'instances': {}}
def load():
local = Client(BASE_URL, timeout=5)
while not stop.is_set():
try:
key = uuid.uuid4().hex
r = local.call('POST', path, 201, actor=alice,
body={'text': 'roll-' + key},
headers={'Idempotency-Key': key},
schema='Message')
stats['ok'] += 1
iid = r['headers'].get('X-Instance-Id')
stats['instances'][iid] = stats['instances'].get(iid, 0) + 1
except Failure:
stats['err'] += 1
time.sleep(0.05)
t = threading.Thread(target=load, daemon=True)
t.start()
time.sleep(10)
base_ok = stats['ok']
base_err = stats['err']
print(' base: ok=%d err=%d' % (base_ok, base_err))
# 1) Выбрать одну реплику и остановить.
from common import observe_instances
seen = observe_instances(c, alice, n=30)
target = next(iter(seen))
print(' rolling target:', target)
stop_one(target)
time.sleep(15)
# 2) Восстановить.
restore_api()
time.sleep(10)
stop.set()
t.join(timeout=10)
delta_ok = stats['ok'] - base_ok
delta_err = stats['err'] - base_err
print(' after rolling: ok=+%d err=+%d instances=%r'
% (delta_ok, delta_err, stats['instances']))
check(delta_ok > 0, 'rolling update остановил запись полностью')
check(delta_err <= max(5, delta_ok // 3),
'слишком много ошибок при rolling update: +%d ok / +%d err'
% (delta_ok, delta_err))
seen_final = observe_instances(c, alice, n=40)
check(len(seen_final) >= 2,
'после rolling видна одна реплика: %r' % seen_final)
print(' final instances:', seen_final)
print_pass('rolling update under load')
if __name__ == '__main__':
try:
main()
except Failure as e:
print('FAIL:', e)
raise SystemExit(1)