Реализовать ЛР 7 на оценку 3
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Run the public failure scenario once with each API replica as its target."""
|
||||
|
||||
from functools import partial
|
||||
import os
|
||||
import subprocess
|
||||
from uuid import uuid4
|
||||
|
||||
import resilience
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
|
||||
class ReplicaClient(Client):
|
||||
def __init__(self, *args, target: str, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.target: str | None = target
|
||||
|
||||
def message(self, actor, chat, text, key=None, attachments=None):
|
||||
if self.target is None:
|
||||
return super().message(actor, chat, text, key=key, attachments=attachments)
|
||||
# The first write in the public scenario selects the replica to stop.
|
||||
# Replay it with the same key until that replica handles the request.
|
||||
key = key or uuid4().hex
|
||||
for _ in range(20):
|
||||
response = super().message(actor, chat, text, key=key, attachments=attachments)
|
||||
if response['headers']['X-Instance-Id'] == self.target:
|
||||
self.target = None
|
||||
return response
|
||||
check(False, 'the chosen API replica did not receive the initial write')
|
||||
|
||||
|
||||
def main():
|
||||
def options(parser):
|
||||
parser.add_argument('--mode', choices=['failover'], required=True)
|
||||
parser.add_argument('--action-script', required=True)
|
||||
|
||||
args = arguments(__doc__, options)
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
targets = set()
|
||||
for _ in range(20):
|
||||
response = client.call('GET', '/health/ready', schema='Health')
|
||||
targets.add(response['headers']['X-Instance-Id'])
|
||||
if len(targets) == 2:
|
||||
break
|
||||
check(len(targets) == 2, 'expected both API replicas to be ready')
|
||||
|
||||
for target in sorted(targets):
|
||||
print(f'Checking replica alias {target}', flush=True)
|
||||
resilience.Client = partial(ReplicaClient, target=target)
|
||||
try:
|
||||
resilience.main()
|
||||
finally:
|
||||
resilience.Client = Client
|
||||
subprocess.run([
|
||||
'docker', 'compose', '--env-file', os.getenv('COMPOSE_ENV_FILE', '.env'),
|
||||
'up', '-d', '--wait', '--no-deps', 'api',
|
||||
], check=True)
|
||||
print('PASS: failover confirmed independently for both replicas')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Reference in New Issue
Block a user