Lab3: track-1 results, failure/rolling/container evidence, report draft
This commit is contained in:
+78
-23
@@ -7,10 +7,14 @@ liveness, recovery through the LB, the same still-valid session token and
|
||||
untouched named volumes. Never removes volumes. Python 3.9+.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import datetime as dt
|
||||
import json
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
@@ -41,6 +45,41 @@ def service_container(name):
|
||||
return M.containers(name)[0]
|
||||
|
||||
|
||||
def direct(api_container, path, headers=None, body=None):
|
||||
"""Call an API process on its container loopback, bypassing the LB.
|
||||
|
||||
During a shared-dependency outage HAProxy correctly removes every unready
|
||||
backend, so the LB itself answers 503 and cannot show the API's JSON error.
|
||||
The process-level contract is therefore checked directly, while the LB
|
||||
outage status is recorded separately.
|
||||
"""
|
||||
command = ['docker', 'exec', api_container, 'wget', '-S', '-O', '-',
|
||||
'-T', '3']
|
||||
if body is not None:
|
||||
command.extend(['--post-data', body])
|
||||
for key, value in (headers or {}).items():
|
||||
command.extend(['--header', key + ': ' + value])
|
||||
command.append('http://127.0.0.1:8080' + path)
|
||||
result = M.run(command, check=False)
|
||||
match = re.search(r'HTTP/\d\.\d (\d{3})', result.stderr)
|
||||
status = int(match.group(1)) if match else 0
|
||||
payload = None
|
||||
try:
|
||||
payload = json.loads(result.stdout)
|
||||
except ValueError:
|
||||
pass
|
||||
return status, payload
|
||||
|
||||
|
||||
def lb_status(path, headers=None):
|
||||
request = urllib.request.Request('http://127.0.0.1:8083' + path, headers=headers or {})
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=3) as response:
|
||||
return response.status, response.headers.get('X-Instance-Id'), response.read(4096)
|
||||
except urllib.error.HTTPError as error:
|
||||
return error.code, error.headers.get('X-Instance-Id'), error.read(4096)
|
||||
|
||||
|
||||
def volumes():
|
||||
names = set()
|
||||
for name in ('postgres', 'valkey'):
|
||||
@@ -53,6 +92,7 @@ def volumes():
|
||||
|
||||
def outage(c, actor, chat, dependency):
|
||||
container = service_container(dependency)['container_id']
|
||||
api_container = M.containers('api')[0]['container_id']
|
||||
before_volumes = volumes()
|
||||
before_counts = M.db_snapshot()['datasets'] if dependency == 'valkey' else None
|
||||
stop_started = time.monotonic()
|
||||
@@ -61,25 +101,38 @@ def outage(c, actor, chat, dependency):
|
||||
time.sleep(1.0)
|
||||
observed = {'dependency': dependency, 'container': container,
|
||||
'stopped_at': now(), 'stop_seconds': round(stop_seconds, 3)}
|
||||
live = c.call('GET', '/health/live', schema='Health')
|
||||
observed['live_status'] = live['status']
|
||||
observed['live_has_instance'] = bool(live['headers'].get('X-Instance-Id'))
|
||||
ready = c.call('GET', '/health/ready', 503)
|
||||
observed['ready_status'] = ready['status']
|
||||
observed['ready_code'] = ready['json']['error']['code']
|
||||
protected = c.call('GET', '/api/v1/users/me', 503, actor=actor)
|
||||
observed['protected_status'] = protected['status']
|
||||
observed['protected_code'] = protected['json']['error']['code']
|
||||
anonymous = c.call('GET', '/api/v1/users/me', 401)
|
||||
observed['anonymous_status'] = anonymous['status']
|
||||
login_error = c.call('POST', '/api/v1/auth/sessions', 503,
|
||||
headers={'Authorization': 'Basic ' + __import__('base64').b64encode(
|
||||
(actor['registration']['username'] + ':' + actor['registration']['password']).encode()).decode()})
|
||||
observed['login_status'] = login_error['status']
|
||||
write_error = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 503,
|
||||
actor=actor, body={'text': 'во время отказа'},
|
||||
headers={'Idempotency-Key': 'fail-' + dt.datetime.now().strftime('%H%M%S%f')})
|
||||
observed['write_status'] = write_error['status']
|
||||
live_status, live = direct(api_container, '/health/live')
|
||||
observed['direct_live_status'] = live_status
|
||||
observed['direct_live_body'] = live
|
||||
ready_status, ready = direct(api_container, '/health/ready')
|
||||
observed['direct_ready_status'] = ready_status
|
||||
observed['direct_ready_code'] = (ready or {}).get('error', {}).get('code')
|
||||
bearer = {'Authorization': 'Bearer ' + actor['token']}
|
||||
protected_status, protected = direct(api_container, '/api/v1/users/me', bearer)
|
||||
observed['direct_protected_status'] = protected_status
|
||||
observed['direct_protected_code'] = (protected or {}).get('error', {}).get('code')
|
||||
anonymous_status, anonymous = direct(api_container, '/api/v1/users/me')
|
||||
observed['direct_anonymous_status'] = anonymous_status
|
||||
credential = base64.b64encode((actor['registration']['username'] + ':' +
|
||||
actor['registration']['password']).encode()).decode()
|
||||
login_status, login_error = direct(api_container, '/api/v1/auth/sessions',
|
||||
{'Authorization': 'Basic ' + credential}, '')
|
||||
observed['direct_login_status'] = login_status
|
||||
observed['direct_login_code'] = (login_error or {}).get('error', {}).get('code')
|
||||
write_status, write_error = direct(
|
||||
api_container, '/api/v1/chats/' + chat['id'] + '/messages',
|
||||
dict(bearer, **{'Content-Type': 'application/json',
|
||||
'Idempotency-Key': 'fail-' + dt.datetime.now().strftime('%H%M%S%f')}),
|
||||
'{"text":"во время отказа"}')
|
||||
observed['direct_write_status'] = write_status
|
||||
observed['direct_write_code'] = (write_error or {}).get('error', {}).get('code')
|
||||
|
||||
lb_live_status, lb_instance, _ = lb_status('/health/live')
|
||||
observed['lb_live_status_during_outage'] = lb_live_status
|
||||
observed['lb_live_instance_header'] = lb_instance
|
||||
lb_ready_status, lb_ready_instance, _ = lb_status('/health/ready')
|
||||
observed['lb_ready_status_during_outage'] = lb_ready_status
|
||||
observed['lb_ready_instance_header'] = lb_ready_instance
|
||||
|
||||
restore_started = time.monotonic()
|
||||
M.run(['docker', 'start', container])
|
||||
@@ -126,10 +179,12 @@ def main():
|
||||
for dependency in ('postgres', 'valkey'):
|
||||
observed = outage(c, alice, chat, dependency)
|
||||
result['outages'].append(observed)
|
||||
print('%s: ready=%d protected=%d login=%d write=%d, recovery %.3fs, volumes preserved=%s' % (
|
||||
dependency, observed['ready_status'], observed['protected_status'],
|
||||
observed['login_status'], observed['write_status'],
|
||||
observed['recovery_seconds_to_ready'], observed['volumes_preserved']))
|
||||
print('%s: direct live=%d ready=%d protected=%d login=%d write=%d; LB live=%d ready=%d; recovery %.3fs; volumes preserved=%s' % (
|
||||
dependency, observed['direct_live_status'], observed['direct_ready_status'],
|
||||
observed['direct_protected_status'], observed['direct_login_status'],
|
||||
observed['direct_write_status'], observed['lb_live_status_during_outage'],
|
||||
observed['lb_ready_status_during_outage'], observed['recovery_seconds_to_ready'],
|
||||
observed['volumes_preserved']))
|
||||
result['result'] = 'PASS'
|
||||
OUT.parent.mkdir(parents=True, exist_ok=True)
|
||||
OUT.write_text(json.dumps(result, indent=2, ensure_ascii=False) + '\n')
|
||||
|
||||
Reference in New Issue
Block a user