Spaces:
Running
Running
File size: 10,931 Bytes
af35409 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | """A bounded, single-worker inference queue with cancellable visitor leases."""
import logging
import threading
import time
from collections import OrderedDict, deque
from dataclasses import dataclass, field
class AdmissionError(Exception):
def __init__(self, message, status=429, code="queue_full"):
super().__init__(message)
self.status = status
self.code = code
class RequestCancelled(Exception):
pass
@dataclass(eq=False)
class Ticket:
request_id: str
payload: dict
created: float
lease_until: float | None
is_disconnected: object = None
events: deque = field(default_factory=deque)
status: str = "waiting"
cancelled: bool = False
done: bool = False
class InferenceQueue:
def __init__(self, runner, inference_lock=None, capacity=2, lease_seconds=12,
max_wait=180, buffer_size=8, clock=time.monotonic, autostart=True):
self.runner = runner
self.inference_lock = inference_lock or threading.Lock()
self.capacity = capacity
self.lease_seconds = lease_seconds
self.max_wait = max_wait
self.buffer_size = buffer_size
self.clock = clock
self._condition = threading.Condition()
self._tickets = {}
self._waiting = deque()
self._active = None
self._legacy = False
self._closed = False
self._cancelled_ids = OrderedDict()
self._thread = None
self._autostart = autostart
def _start_locked(self):
if self._autostart and self._thread is None:
self._thread = threading.Thread(target=self._work, name="marvin-inference", daemon=True)
self._thread.start()
def _remember_cancelled_locked(self, request_id):
self._cancelled_ids[request_id] = self.clock() + 30
self._cancelled_ids.move_to_end(request_id)
while len(self._cancelled_ids) > 1024:
self._cancelled_ids.popitem(last=False)
def _positions_locked(self):
for position, ticket in enumerate(self._waiting, 1):
# Positions can change faster than a slow client reads. Only the
# current position matters, and these updates must stay bounded.
ticket.events = deque(event for event in ticket.events if event.get("type") != "queued")
ticket.events.append({"type": "queued", "position": position})
self._condition.notify_all()
def _cancel_locked(self, ticket, message="Request cancelled.", code="cancelled"):
if ticket.done or ticket.cancelled:
return
ticket.cancelled = True
ticket.events.clear()
ticket.events.append({"type": "error", "error": message, "code": code})
self._remember_cancelled_locked(ticket.request_id)
if ticket in self._waiting:
self._waiting.remove(ticket)
ticket.done = True
self._positions_locked()
self._condition.notify_all()
def _expire_locked(self):
now = self.clock()
for request_id, expiry in list(self._cancelled_ids.items()):
if expiry <= now:
del self._cancelled_ids[request_id]
for ticket in list(self._tickets.values()):
expired = ticket.lease_until is not None and ticket.lease_until <= now
if ticket.done:
if expired or now - ticket.created > self.max_wait + 300:
self._tickets.pop(ticket.request_id, None)
continue
if ticket.is_disconnected is not None and ticket.is_disconnected():
self._cancel_locked(ticket)
elif expired:
self._cancel_locked(ticket, "Your turn ended because you left Marvin. Come back and try again.", "lease_expired")
elif ticket.status == "waiting" and now - ticket.created >= self.max_wait:
self._cancel_locked(ticket, "Marvin's taking a while. Please try again in a moment.", "queue_expired")
def submit(self, request_id, payload, leased=True, allow_wait=True, is_disconnected=None):
with self._condition:
self._expire_locked()
if self._closed:
raise AdmissionError("Marvin is restarting. Give him a moment.", 503, "unavailable")
if request_id in self._cancelled_ids:
raise AdmissionError("This request was cancelled. Send a new message when you're ready.", 409, "cancelled")
if request_id in self._tickets:
raise AdmissionError("That request is already with Marvin.", 409, "duplicate_request")
occupied = self._active is not None or self._legacy
if occupied and (not allow_wait or len(self._waiting) >= self.capacity):
raise AdmissionError("Marvin's got a little crowd. Try again shortly.")
now = self.clock()
ticket = Ticket(request_id, payload, now, now + self.lease_seconds if leased else None, is_disconnected)
self._tickets[request_id] = ticket
if occupied:
self._waiting.append(ticket)
self._positions_locked()
else:
ticket.status = "starting"
self._active = ticket
self._start_locked()
self._condition.notify_all()
return ticket
def keepalive(self, request_id):
with self._condition:
self._expire_locked()
ticket = self._tickets.get(request_id)
if ticket is None or ticket.cancelled or ticket.done:
return False
if ticket.lease_until is not None:
ticket.lease_until = self.clock() + self.lease_seconds
return True
def cancel(self, request_id):
with self._condition:
# A pagehide beacon can overtake the original POST on the network.
# Remember it even before admission, so that POST cannot start work.
self._remember_cancelled_locked(request_id)
ticket = self._tickets.get(request_id)
if ticket is not None:
self._cancel_locked(ticket)
self._condition.notify_all()
def checkpoint(self, ticket):
with self._condition:
self._expire_locked()
if ticket.cancelled or ticket.done or self._closed:
raise RequestCancelled()
def emit(self, ticket, event):
with self._condition:
stalled_at = self.clock()
while len(ticket.events) >= self.buffer_size:
self.checkpoint(ticket)
if self.clock() - stalled_at >= self.lease_seconds:
self._cancel_locked(ticket, "The connection to Marvin stopped. Please try again.", "connection_stalled")
raise RequestCancelled()
self._condition.wait(0.25)
self.checkpoint(ticket)
ticket.events.append(event)
self._condition.notify_all()
def events(self, ticket):
try:
while True:
with self._condition:
self._expire_locked()
if not ticket.events and not ticket.done and not ticket.cancelled:
self._condition.wait(2)
self._expire_locked()
if ticket.events:
event = ticket.events.popleft()
self._condition.notify_all()
elif ticket.done or ticket.cancelled:
break
else:
event = None # An SSE heartbeat notices broken sockets.
yield event
finally:
self.cancel(ticket.request_id)
with self._condition:
if ticket.done:
self._tickets.pop(ticket.request_id, None)
def _promote_locked(self):
self._expire_locked()
if self._active is None and not self._legacy and self._waiting:
self._active = self._waiting.popleft()
self._active.events.clear()
self._active.status = "starting"
self._positions_locked()
def run_once(self):
"""Run one reserved job; also permits deterministic tests without threads."""
with self._condition:
self._expire_locked()
ticket = self._active
if ticket is None or ticket.status != "starting":
return False
ticket.status = "running"
locked = False
try:
self.checkpoint(ticket)
locked = self.inference_lock.acquire(blocking=False)
if not locked:
raise RuntimeError("Inference admission lost its exclusive lock")
self.runner(ticket, self)
except RequestCancelled:
pass
except Exception:
logging.exception("Diffusion generation failed")
try:
self.emit(ticket, {"type": "error", "error": "Generation failed. Please try again."})
except RequestCancelled:
pass
finally:
if locked:
self.inference_lock.release()
with self._condition:
ticket.done = True
ticket.payload = None
self._active = None
self._promote_locked()
self._condition.notify_all()
return True
def _work(self):
while True:
with self._condition:
self._expire_locked()
if self._closed:
return
if self._active is None:
self._condition.wait(0.5)
continue
self.run_once()
def acquire_legacy(self):
with self._condition:
self._expire_locked()
if self._closed or self._active is not None or self._waiting or self._legacy:
return False
if not self.inference_lock.acquire(blocking=False):
return False
self._legacy = True
return True
def release_legacy(self):
with self._condition:
self.inference_lock.release()
self._legacy = False
self._promote_locked()
self._condition.notify_all()
def snapshot(self):
with self._condition:
self._expire_locked()
return {"active": self._active is not None or self._legacy,
"waiting": len(self._waiting), "capacity": self.capacity,
"lease_seconds": self.lease_seconds}
def close(self):
with self._condition:
self._closed = True
for ticket in list(self._tickets.values()):
self._cancel_locked(ticket)
self._condition.notify_all()
if self._thread:
self._thread.join(timeout=5)
|