-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmission_gate.py
More file actions
167 lines (130 loc) · 5.35 KB
/
admission_gate.py
File metadata and controls
167 lines (130 loc) · 5.35 KB
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
# -*- coding: utf-8 -*-
# admission_gate.py
"""
Admission and execution-queue base contracts.
This module defines the admission gate that transfers eligible tokens from the
token pool into the execution queue, along with the minimal abstract interface
implemented by concrete worker-queue backends.
"""
import asyncio
from typing import Optional
from .token_system import TaskToken, TokenPool, TokenState
class AdmissionGate:
"""Pass-through admission layer between the token pool and worker queue.
The gate retrieves tokens from the pool, filters out killed tokens,
performs admission-state transitions, and forwards tokens into the
execution queue.
"""
def __init__(
self,
token_pool: TokenPool,
worker_queue: 'WorkerTaskQueue',
worker_pool,
policy: Optional = None
):
self.token_pool = token_pool
self.worker_queue = worker_queue
self.worker_pool = worker_pool
self.policy = policy
# Control
self._active = False
self._loop_task: Optional[asyncio.Task] = None
# Metrics
self.total_admitted = 0
async def start(self):
"""Start the background admission loop."""
if self._active:
return
self._active = True
self._loop_task = asyncio.create_task(self._admission_loop())
print("[GATE] Admission gate started - full saturation mode")
async def stop(self):
"""Stop the admission loop and await task cancellation."""
self._active = False
if self._loop_task:
self._loop_task.cancel()
try:
await self._loop_task
except asyncio.CancelledError:
pass
print("[GATE] Admission gate stopped")
async def _admission_loop(self):
"""Continuously move eligible tokens from the pool into the worker queue.
This loop does not apply throughput throttling. It performs killed-token
filtering, forwards admitted tokens, and briefly backs off only after
unexpected loop errors.
"""
print("[GATE] Admission loop started - unrestricted flow")
while self._active:
try:
# Get next token from pool
token = await self.token_pool.get_next_token()
# Skip killed tokens
if token.is_killed():
continue
# Route directly to execution
await self._admit_token(token)
except asyncio.CancelledError:
break
except Exception as e:
print(f"[GATE] Error in admission loop: {e}")
await asyncio.sleep(0.1) # Brief pause on error only
async def _admit_token(self, token: TaskToken):
"""Transition a token into the admitted state and enqueue it for execution."""
# Transition state
if not token.transition_state(TokenState.ADMITTED):
print(f"[GATE] Failed to admit token {token.token_id} (state: {token.state})")
return
if token.state == TokenState.CREATED:
token.transition_state(TokenState.WAITING)
# Route to worker queue
await self.worker_queue.put(token)
# Update metrics
self.total_admitted += 1
self.token_pool.total_admitted += 1
def get_stats(self) -> dict:
"""Return admission-gate state and throughput counters."""
return {
'active': self._active,
'total_admitted': self.total_admitted,
'mode': 'full_saturation'
}
# ============================================================================
# Worker Task Queue - Minimal Base Class
# ============================================================================
class WorkerTaskQueue:
"""Abstract base class for execution-queue backends.
Concrete subclasses are responsible for mailbox creation, token placement,
worker startup, execution, and queue-specific metrics.
"""
def __init__(self):
"""Initialize shared worker-queue state and counters."""
super().__init__()
self._active = False
self._execution_tasks = []
# Metrics
self.total_executed = 0
self.total_failed = 0
async def start(self, num_executors: int = 4):
"""Subclass-specific startup logic to initialize worker mailboxes and execution tasks."""
raise NotImplementedError("Subclass must implement start()")
async def stop(self):
"""Stop all execution tasks and await their cancellation."""
self._active = False
# Cancel all executor tasks
for task in self._execution_tasks:
task.cancel()
# Wait for them to finish
await asyncio.gather(*self._execution_tasks, return_exceptions=True)
self._execution_tasks = []
print("[WORKER_QUEUE] Stopped all executors")
async def put(self, token: TaskToken):
"""Enqueue one admitted token for backend-specific execution routing."""
raise NotImplementedError("Subclass must implement put()")
def get_stats(self) -> dict:
"""Return base execution counters and active-task count."""
return {
'num_executors': len(self._execution_tasks),
'total_executed': self.total_executed,
'total_failed': self.total_failed
}