Source code for mstar.worker.rid_table

"""Worker-local integer handles for request ids.

Messages carry the request id string; everything inside a worker keys on a
small int instead. The only translations are at the process boundary: a handle
is minted on NEW_REQUEST and looked up on every other inbound message, and the
string goes back on the wire at each send site.

Handles are RECYCLED once a request is removed, so anything keyed by one must
be purged on the same REMOVE_REQUEST that releases it. A leaked string key was
harmless -- the string never recurred -- but a leaked handle silently attaches
to whichever request gets that handle next. Known handle-keyed state:
RequestLifecycle.{failed_rids, arrivals, pending_removes, _removes_awaiting_step,
_unreported} (purged by removed), MicroScheduler.{backlog, last_ran, chunk_started,
cached_prefix} (purged by clear_rid), Admission.{held_until, parked} (purged by clear),
Lockstep.read_failed_rids (purged by clear), Rebuilds.{_kept, _keeps_ids, _prefilling,
_writes, _cursors}, Preemption._preempted, RequestStateManager.{per_request_info,
_fwd_by_partition} and StreamBufferManager._rids (purged by remove_request), the
engine's and tensor manager's per-request state (including ChunkingManagement.prepared,
Glm52LLMSubmodule's MTP maps, cleared by its cleanup_request, and the tensor store's rid
-> uuids ownership, held in the Rust bookkeeper under RustTensorStore; purged by
force_cleanup_request), RecurrentStatePool.{_keyed, _leases, _restores, _snap_writes,
_decode, _unkeyed, _chunk_ends, _admit_restores, _admit_snaps, _admit_taken,
_preplan_taken, _reserved}, KDAManager._verifying and RollingKVManager.{_requests,
_admit_taken, _wants, _reserved}
(purged by remove_request), ResidentPlanner.{_slot_of, _stale, _moved} (purged by
release), SamplerCells._cells and SamplerResource._sampled_rids (purged by the
sampler's remove_request), HostFields._fields (purged by the engine's
remove_request), RopeManager._counters and LongRopeManager._long_rids (purged by their
remove_request), and WorkerProfileInfo.

Lockstep's follow count is the one deliberate exception: a
ScheduleTPNode can arrive before the NEW_REQUEST that mints the handle, so it
stays keyed by the wire string and is purged by clear_wire_rid.
"""


[docs] class RidTable: __slots__ = ("_names", "_handles", "_free") def __init__(self): self._names: list[str | None] = [] self._handles: dict[str, int] = {} self._free: list[int] = []
[docs] def intern(self, request_id: str) -> int: """Handle for ``request_id``, minting one if it has none. A request spanning several partitions on this worker gets one NEW_REQUEST per partition; they all share the first one's handle. """ handle = self._handles.get(request_id) if handle is not None: return handle if self._free: handle = self._free.pop() self._names[handle] = request_id else: handle = len(self._names) self._names.append(request_id) self._handles[request_id] = handle return handle
[docs] def handle(self, request_id: str) -> int | None: """Handle for an inbound message's request id, or None if this worker does not know it (never admitted, or already removed -- a benign race, not an error).""" return self._handles.get(request_id)
[docs] def name(self, handle: int) -> str: """The wire identity, for a message about to leave this process.""" name = self._names[handle] if 0 <= handle < len(self._names) else None if name is None: raise KeyError(f"no live request has handle {handle}") return name
[docs] def release(self, handle: int) -> None: """Free ``handle`` for reuse. Must run after every handle-keyed map has been purged of it.""" name = self._names[handle] if 0 <= handle < len(self._names) else None if name is None: return self._names[handle] = None del self._handles[name] self._free.append(handle)
def __len__(self) -> int: return len(self._handles)