worker: без лишней копии непрозрачного RGBA; тесты: anon-память и OOM, ожидание лидера и отметки outbox
This commit is contained in:
@@ -76,6 +76,11 @@ func Decode(data []byte) (*image.RGBA, error) {
|
||||
return nil, fmt.Errorf("%w: %v", ErrDecode, err)
|
||||
}
|
||||
b := src.Bounds()
|
||||
// Непрозрачный RGBA (типичный PNG RGB) уже готов: вторая полноразмерная копия
|
||||
// (4 байта × пиксель) не нужна — для 36 Мпикс это 137 MiB памяти worker.
|
||||
if rgba, ok := src.(*image.RGBA); ok && b.Min == (image.Point{}) && rgba.Opaque() {
|
||||
return Orient(rgba, ExifOrientation(data)), nil
|
||||
}
|
||||
out := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
|
||||
draw.Draw(out, out.Bounds(), image.NewUniform(color.White), image.Point{}, draw.Src)
|
||||
draw.Draw(out, out.Bounds(), src, b.Min, draw.Over) // alpha → на белый
|
||||
|
||||
@@ -166,7 +166,14 @@ def scenario_demo(c):
|
||||
sh('sh', 'scripts/restore.sh')
|
||||
wait_ready_api(c)
|
||||
timeline.append('api and rabbitmq restarted (volumes kept), outbox published=%s' % published)
|
||||
timeline.append('rabbitmq queues after restart: %s' % queues())
|
||||
q = {}
|
||||
for _ in range(30):
|
||||
q = queues()
|
||||
if q.get('attachment.process', 0) >= 1:
|
||||
break
|
||||
time.sleep(1)
|
||||
timeline.append('rabbitmq queues after restart: %s' % q)
|
||||
check(q.get('attachment.process', 0) >= 1, 'accepted job not in broker after restart: %s' % q)
|
||||
t0 = time.time()
|
||||
compose('start', 'worker')
|
||||
meta = wait_status(c, bob, att['id'], ('ready',), timeout=90)
|
||||
@@ -200,6 +207,10 @@ def scenario_outbox_gap(c):
|
||||
att2 = upload(c, alice, chat, sample(), name='dup.png')
|
||||
meta2 = wait_status(c, bob, att2['id'], ('ready',), timeout=120)
|
||||
jid2, _, attempt2 = job_of(att2['id'])
|
||||
for _ in range(60):
|
||||
if psql("SELECT count(*) FROM outbox WHERE job_id='%s' AND published_at IS NOT NULL" % jid2) == '1':
|
||||
break
|
||||
time.sleep(0.5)
|
||||
attempts = psql("SELECT publish_attempts FROM outbox WHERE job_id='%s'" % jid2)
|
||||
restarts = sh('docker', 'inspect', '-f', '{{.RestartCount}}', compose('ps', '-q', 'relay').stdout.strip()).stdout.strip()
|
||||
recreate('relay')
|
||||
@@ -365,20 +376,44 @@ def scenario_limits(c):
|
||||
alice, bob, _, chat = c.bootstrap()
|
||||
big = solid_png(6000, 6000) # 36 Мпикс, сжатый файл маленький
|
||||
check(len(big) < 10 * 1024 * 1024, 'test image too big')
|
||||
compose('restart', 'worker') # новый cgroup → memory.peak с нуля
|
||||
import threading
|
||||
compose('restart', 'worker') # новый cgroup → счётчики с нуля
|
||||
time.sleep(3)
|
||||
cid = compose('ps', '-q', 'worker').stdout.split()[0]
|
||||
|
||||
def cg(name):
|
||||
return sh('docker', 'exec', cid, 'cat', '/sys/fs/cgroup/' + name, check=False).stdout
|
||||
|
||||
anon_max = [0]
|
||||
stop = threading.Event()
|
||||
|
||||
def sampler():
|
||||
# memory.peak включает page cache (временные файлы cwebp и т. п.), поэтому
|
||||
# отдельно снимаем анонимную память процесса — её и ограничивает пиксельный бюджет.
|
||||
while not stop.is_set():
|
||||
for line in cg('memory.stat').splitlines():
|
||||
if line.startswith('anon '):
|
||||
anon_max[0] = max(anon_max[0], int(line.split()[1]))
|
||||
time.sleep(0.3)
|
||||
th = threading.Thread(target=sampler, daemon=True)
|
||||
th.start()
|
||||
t0 = time.time()
|
||||
ids = [upload(c, alice, chat, big, name='big%d.png' % i)['id'] for i in range(3)]
|
||||
done = [wait_status(c, bob, i, ('ready', 'failed'), timeout=240) for i in ids]
|
||||
took = time.time() - t0
|
||||
peak = sh('docker', 'exec', cid, 'cat', '/sys/fs/cgroup/memory.peak', check=False).stdout.strip()
|
||||
limit = sh('docker', 'exec', cid, 'cat', '/sys/fs/cgroup/memory.max', check=False).stdout.strip()
|
||||
stop.set()
|
||||
th.join()
|
||||
peak = cg('memory.peak').strip()
|
||||
limit = cg('memory.max').strip()
|
||||
oom = {l.split()[0]: int(l.split()[1]) for l in cg('memory.events').splitlines() if len(l.split()) == 2}
|
||||
check(all(d['status'] == 'ready' for d in done), 'big images failed: %s' % [d['error_code'] for d in done])
|
||||
check(oom.get('oom_kill', 0) == 0, 'worker was OOM-killed')
|
||||
th = {v['name']: (v['width'], v['height']) for v in done[0]['variants']}
|
||||
check(th['thumbnail'] == (256, 256) and th['optimized'] == (1280, 1280), 'variant sizes %s' % th)
|
||||
return {'image': '6000x6000 PNG (%d bytes)' % len(big), 'parallel_uploads': 3, 'seconds_all_ready': round(took, 1),
|
||||
'worker_memory_peak_mib': round(int(peak) / 1048576, 1) if peak.isdigit() else peak,
|
||||
'worker_anon_memory_max_mib': round(anon_max[0] / 1048576, 1),
|
||||
'worker_memory_peak_incl_page_cache_mib': round(int(peak) / 1048576, 1) if peak.isdigit() else peak,
|
||||
'oom_kill_events': oom.get('oom_kill', 0),
|
||||
'worker_memory_limit_mib': round(int(limit) / 1048576, 1) if limit.isdigit() else limit,
|
||||
'pixel_budget': 60_000_000, 'decoded_rgba_per_image_mib': round(6000 * 6000 * 4 / 1048576, 1),
|
||||
'variant_sizes': th}
|
||||
|
||||
Reference in New Issue
Block a user