forked from mtusi2026/mtusi2026lab3
75 lines
3.6 KiB
Python
75 lines
3.6 KiB
Python
"""Operator scenario: stop each real replica and verify writes, reads and replay via LB."""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import time
|
|
import uuid
|
|
from client import Client, Failure, arguments, check, run
|
|
from resilience import history
|
|
|
|
|
|
def configure(p):
|
|
p.add_argument('--action-script', required=True)
|
|
p.add_argument('--restore-script', required=True)
|
|
|
|
|
|
def main():
|
|
args = arguments('Stop either API replica with the other running', configure)
|
|
c = Client(args.base_url, args.ca_file, timeout=2)
|
|
alice, bob, _, chat = c.bootstrap()
|
|
path = '/api/v1/chats/' + chat['id'] + '/messages'
|
|
for target in ('api1', 'api2'):
|
|
seen = {c.call('GET', '/api/v1/users/me', actor=alice)['headers']['X-Instance-Id'] for _ in range(20)}
|
|
check(seen == {'api1', 'api2'}, 'both replicas required before stop')
|
|
key = uuid.uuid4().hex
|
|
original = c.message(alice, chat, 'Сохранить ' + key, key=key)['json']
|
|
env = dict(os.environ, TARGET_INSTANCE_ID=target)
|
|
action = Path(args.action_script).resolve()
|
|
start = time.monotonic()
|
|
proc = subprocess.Popen([str(action)], env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
errors, confirmed, instances = 0, [], []
|
|
recovery = None
|
|
pending_key = uuid.uuid4().hex
|
|
try:
|
|
while time.monotonic() - start < 6:
|
|
check(proc.poll() in (None, 0), 'stop hook failed')
|
|
try:
|
|
# Same session, same message/key even after losing its original replica.
|
|
repeated = c.message(alice, chat, original['text'], key=key)
|
|
reply = c.message(alice, chat, 'После отказа ' + pending_key, key=pending_key)
|
|
confirmed.append(reply['json'])
|
|
pending_key = uuid.uuid4().hex
|
|
page = history(c, bob, chat)
|
|
except Failure:
|
|
errors += 1
|
|
else:
|
|
check(repeated['json'] == original, 'failover changed replayed Message')
|
|
check(original in page and all(m in page for m in confirmed), 'confirmed data lost')
|
|
if proc.poll() == 0:
|
|
instances.append(reply['headers']['X-Instance-Id'])
|
|
if recovery is None:
|
|
recovery = time.monotonic() - start
|
|
time.sleep(0.1)
|
|
check(proc.wait(timeout=3) == 0, 'stop hook failed')
|
|
check(recovery is not None and recovery <= 10, 'recovery exceeded ten seconds')
|
|
check(len(instances) >= 5 and target not in instances[-5:], 'stopped replica still serves requests')
|
|
page = history(c, bob, chat)
|
|
check(original in page and all(m in page for m in confirmed), 'confirmed data lost')
|
|
for text in {m['text'] for m in confirmed}:
|
|
check(sum(m['text'] == text for m in page) == 1, 'retry duplicate')
|
|
print(json.dumps({'stopped': target, 'observed_before': sorted(seen),
|
|
'survivors': sorted(set(instances[-5:])), 'writes': len(confirmed),
|
|
'errors': errors, 'recovery_seconds': round(recovery, 3)}))
|
|
finally:
|
|
if proc.poll() is None:
|
|
proc.kill()
|
|
proc.wait()
|
|
# Record actual stopped process and intact dependencies before restoring.
|
|
subprocess.run([str(Path(args.restore_script).resolve())], check=True, timeout=60, env=env)
|
|
print('PASS: stopping either replica preserved sessions, reads, writes and idempotency')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|