mirror of
https://github.com/shinyobjectz/raea.git
synced 2026-12-06 05:33:38 -10:00
Farm: a volume reload goes next, shared and limited, so a busy farm never loses a finished render
With a dozen cuts in flight, blob uploads and other requests kept the farm API's volume in use without a break. A reload waited for none of them to be in it, and GET out sat behind it past Modal's 150 s web limit (408), losing a finished cut. Now a waiting reload holds new users back, a reload that starts after a request asked serves it too, and the wait is held to 100 s and answered with a 503 the box asks again on. test/farm_gate.py runs the class from farm.py: the old gate never reloads behind a stream of users; this one does in 0.16 s.
This commit is contained in:
1 parent
3dd0c05375
commit
e4453a23d6
2 files changed
+140
-6
No files matched your search
@@ -574,15 +574,22 @@ def api():
|
||||
# (POST jobs: 500) whenever two agents rendered together. So requests that touch the volume share it, and a
|
||||
# reload waits until none of them is in it.
|
||||
class Gate:
|
||||
# 12-04 (one-check proof, 12 cuts in flight): blob uploads and other requests kept the volume in use without a
|
||||
# break, a reload waited for none of them to be in it, and GET out sat behind it past Modal's 150 s web limit:
|
||||
# 408, and Elfster's finished cut was lost. So a waiting reload holds new users back (it goes next), a reload
|
||||
# that starts after a request asked for one serves that request too, and a wait has a limit (a 503, asked again)
|
||||
def __init__(self):
|
||||
self.users = 0
|
||||
self.reloading = False
|
||||
self.waiting = 0
|
||||
self.begun = 0 # reloads started
|
||||
self.fresh = 0 # the latest reload that worked, by its start
|
||||
self.cond = asyncio.Condition()
|
||||
|
||||
@contextlib.asynccontextmanager
|
||||
async def use(self):
|
||||
async with self.cond:
|
||||
await self.cond.wait_for(lambda: not self.reloading)
|
||||
await self.cond.wait_for(lambda: not self.reloading and not self.waiting)
|
||||
self.users += 1
|
||||
try:
|
||||
yield
|
||||
@@ -591,17 +598,36 @@ def api():
|
||||
self.users -= 1
|
||||
self.cond.notify_all()
|
||||
|
||||
async def reload(self, wait: bool = True) -> bool:
|
||||
"""the volume as other containers committed it; with wait=False skipped (False) while the volume is in use"""
|
||||
async def reload(self, wait: bool = True, within: "float | None" = None) -> bool:
|
||||
"""the volume as other containers committed it; with wait=False skipped (False) while the volume is in use,
|
||||
and False when `within` seconds pass first"""
|
||||
async with self.cond:
|
||||
if not wait and (self.users or self.reloading):
|
||||
if not wait and (self.users or self.reloading or self.waiting):
|
||||
return False
|
||||
await self.cond.wait_for(lambda: not self.users and not self.reloading)
|
||||
want = self.begun + 1
|
||||
self.waiting += 1
|
||||
try:
|
||||
ready = lambda: self.fresh >= want or (not self.users and not self.reloading)
|
||||
if within is None:
|
||||
await self.cond.wait_for(ready)
|
||||
else:
|
||||
await asyncio.wait_for(self.cond.wait_for(ready), within)
|
||||
except asyncio.TimeoutError:
|
||||
return False
|
||||
finally:
|
||||
self.waiting -= 1
|
||||
self.cond.notify_all()
|
||||
if self.fresh >= want:
|
||||
return True
|
||||
self.reloading = True
|
||||
self.begun += 1
|
||||
mine = self.begun
|
||||
ok = False
|
||||
try:
|
||||
for pause in (0.5, 1, 2, 4, 0):
|
||||
try:
|
||||
await vol.reload.aio()
|
||||
ok = True
|
||||
return True
|
||||
except Exception as e: # modal.exception.ConflictError: a file open elsewhere in this container
|
||||
if not pause:
|
||||
@@ -612,13 +638,17 @@ def api():
|
||||
finally:
|
||||
async with self.cond:
|
||||
self.reloading = False
|
||||
if ok:
|
||||
self.fresh = mine
|
||||
self.cond.notify_all()
|
||||
|
||||
gate = Gate()
|
||||
# under Modal's 150 s web request limit, past which the request is lost (408) and a box can't ask again
|
||||
RELOAD_WAIT_S = 100
|
||||
|
||||
async def reloaded():
|
||||
"""the coordinator's commits seen here, or a 503 the box tries again on (the job is still all here)"""
|
||||
if not await gate.reload():
|
||||
if not await gate.reload(within=RELOAD_WAIT_S):
|
||||
raise HTTPException(503, "the farm's volume is busy: try again")
|
||||
|
||||
def check(req: Request):
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
# The farm API's volume gate (deploy/modal/farm.py's Gate, cut from the file as it is) against a stand-in volume:
|
||||
# python3 test/farm_gate.py from sdk/cloud. 12-04: a reload starved behind uploads lost a finished cut (GET out: 408).
|
||||
import asyncio
|
||||
import contextlib
|
||||
import os
|
||||
import sys
|
||||
import textwrap
|
||||
|
||||
src = open(sys.argv[1] if len(sys.argv) > 1 else os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "deploy", "modal", "farm.py")).read()
|
||||
a = src.index(" class Gate:\n")
|
||||
b = src.index(" gate = Gate()\n")
|
||||
code = textwrap.dedent(src[a:b])
|
||||
|
||||
|
||||
class Vol:
|
||||
def __init__(self):
|
||||
self.reloads = 0
|
||||
|
||||
async def _reload(self):
|
||||
await asyncio.sleep(0.05)
|
||||
self.reloads += 1
|
||||
|
||||
class _R:
|
||||
def __init__(self, v):
|
||||
self.v = v
|
||||
|
||||
async def aio(self):
|
||||
await self.v._reload()
|
||||
|
||||
@property
|
||||
def reload(self):
|
||||
return Vol._R(self)
|
||||
|
||||
|
||||
async def main():
|
||||
vol = Vol()
|
||||
ns = {"asyncio": asyncio, "contextlib": contextlib, "vol": vol}
|
||||
exec(code, ns)
|
||||
Gate = ns["Gate"]
|
||||
|
||||
# 1. users that overlap without a break (each 0.2 s, a new one every 0.1 s) for 3 s: a reload still happens quickly
|
||||
g = Gate()
|
||||
stop = False
|
||||
|
||||
async def user():
|
||||
async with g.use():
|
||||
await asyncio.sleep(0.2)
|
||||
|
||||
async def stream():
|
||||
tasks = []
|
||||
while not stop:
|
||||
tasks.append(asyncio.create_task(user()))
|
||||
await asyncio.sleep(0.1)
|
||||
await asyncio.gather(*tasks)
|
||||
|
||||
s = asyncio.create_task(stream())
|
||||
await asyncio.sleep(0.3)
|
||||
t0 = asyncio.get_running_loop().time()
|
||||
ok = await g.reload(within=2)
|
||||
took = asyncio.get_running_loop().time() - t0
|
||||
stop = True
|
||||
await s
|
||||
assert ok, "a reload behind a stream of users gave up"
|
||||
assert took < 0.6, f"a reload behind a stream of users took {took:.2f} s"
|
||||
print(f"ok: a reload behind a stream of users in {took:.2f} s")
|
||||
|
||||
# 2. ten reloads asked at once share: fewer volume reloads than asks, and all True
|
||||
g = Gate()
|
||||
before = vol.reloads
|
||||
rs = await asyncio.gather(*[g.reload() for _ in range(10)])
|
||||
assert all(rs)
|
||||
n = vol.reloads - before
|
||||
assert n <= 2, f"10 asks made {n} reloads"
|
||||
print(f"ok: 10 reloads asked together made {n} volume reload(s)")
|
||||
|
||||
# 3. a user that holds the volume past the limit: the reload gives up (False), and the gate still works after
|
||||
g = Gate()
|
||||
|
||||
async def long_user():
|
||||
async with g.use():
|
||||
await asyncio.sleep(0.5)
|
||||
|
||||
lu = asyncio.create_task(long_user())
|
||||
await asyncio.sleep(0.05)
|
||||
assert await g.reload(within=0.2) is False
|
||||
await lu
|
||||
async with g.use():
|
||||
pass
|
||||
assert await g.reload(within=1) is True
|
||||
print("ok: a wait past its limit gives up, and the gate is usable after")
|
||||
|
||||
# 4. wait=False while a reload is waiting: skipped
|
||||
g = Gate()
|
||||
lu = asyncio.create_task(long_user())
|
||||
await asyncio.sleep(0.05)
|
||||
w = asyncio.create_task(g.reload())
|
||||
await asyncio.sleep(0.05)
|
||||
assert await g.reload(wait=False) is False
|
||||
await lu
|
||||
assert await w is True
|
||||
print("ok: wait=False skips while a reload waits")
|
||||
|
||||
|
||||
asyncio.run(main())
|
||||
Reference in new issue
Block a user