replace urgency priority queue with strict FIFO
Drop the urgency score and interrupt/preempt decisions: intake is now idle -> dispatch, busy -> append to a bounded FIFO queue (RequestQueue). The running process always finishes before the next queued request. - queue.py: UrgencyQueue -> RequestQueue (deque, no weight/age/recency) - scheduler.py: remove _priority_score, _interrupt_choice, urgency constants, and the weight threaded through dispatch/authoring/requeue - dashboard/static/gateway: drop weight + preempted handling - tests + docs updated; flow-main.svg regenerated
This commit is contained in:
1 parent
a354958599
commit
1dc0550659
18 files changed
+173
-341
No files matched your search
+16
-55
@@ -1,83 +1,44 @@
|
||||
"""Urgency priority queue.
|
||||
"""FIFO request queue with a maximum depth.
|
||||
|
||||
Items are sorted by descending urgency weight, then FIFO arrival, then most
|
||||
recent first. Queued items age upward so they cannot starve, and the queue has
|
||||
a maximum depth.
|
||||
Requests are dispatched strictly in arrival order: there is no urgency scoring
|
||||
and no preemption, so the running process always finishes before the next queued
|
||||
request is picked up.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import heapq
|
||||
import itertools
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from collections import deque
|
||||
from typing import Iterator
|
||||
|
||||
from .decisions import Request
|
||||
|
||||
MIN_RECENCY = 0.0
|
||||
|
||||
class RequestQueue:
|
||||
"""A bounded first-in-first-out queue of pending requests."""
|
||||
|
||||
@dataclass
|
||||
class QueueItem:
|
||||
weight: float
|
||||
seq: int
|
||||
recency: float
|
||||
request: Request
|
||||
|
||||
def key(self) -> tuple:
|
||||
return (-self.weight, self.seq)
|
||||
|
||||
|
||||
class UrgencyQueue:
|
||||
"""A max-by-urgency priority queue with FIFO/recency tie-breaking."""
|
||||
|
||||
def __init__(self, max_size: int = 100, age_rate: float = 0.0):
|
||||
def __init__(self, max_size: int = 100):
|
||||
self.max_size = max_size
|
||||
self.age_rate = age_rate
|
||||
self._entries: list[tuple] = []
|
||||
self._counter = itertools.count()
|
||||
self._entries: deque[Request] = deque()
|
||||
|
||||
def push(self, request: Request, weight: float, recency: float | None = None) -> bool:
|
||||
"""Insert an item. Returns False if the queue is full."""
|
||||
def push(self, request: Request) -> bool:
|
||||
"""Append an item. Returns False if the queue is full."""
|
||||
if len(self._entries) >= self.max_size:
|
||||
return False
|
||||
item = QueueItem(
|
||||
weight=weight,
|
||||
seq=next(self._counter),
|
||||
recency=recency if recency is not None else time.time(),
|
||||
request=request,
|
||||
)
|
||||
entry = (*item.key(), next(self._counter), item)
|
||||
heapq.heappush(self._entries, entry)
|
||||
self._entries.append(request)
|
||||
return True
|
||||
|
||||
def pop(self) -> Request | None:
|
||||
if not self._entries:
|
||||
return None
|
||||
return heapq.heappop(self._entries)[-1].request
|
||||
return self._entries.popleft()
|
||||
|
||||
def peek(self) -> Request | None:
|
||||
if not self._entries:
|
||||
return None
|
||||
return self._entries[0][-1].request
|
||||
|
||||
def age(self, dt: float = 1.0) -> None:
|
||||
"""Pull queued priorities toward critical so old items catch up."""
|
||||
if self.age_rate <= 0:
|
||||
return
|
||||
rebuilt = []
|
||||
for entry in self._entries:
|
||||
item = entry[-1]
|
||||
item.weight += self.age_rate * dt * (1.0 - item.weight)
|
||||
rebuilt.append((*item.key(), next(self._counter), item))
|
||||
self._entries = rebuilt
|
||||
heapq.heapify(self._entries)
|
||||
return self._entries[0]
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self._entries)
|
||||
|
||||
def items(self) -> Iterator[tuple[float, Request]]:
|
||||
for entry in sorted(self._entries, key=lambda e: e[:-1]):
|
||||
item = entry[-1]
|
||||
yield item.weight, item.request
|
||||
def items(self) -> Iterator[Request]:
|
||||
yield from self._entries
|
||||
Reference in new issue
Block a user