-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathselection.py
More file actions
334 lines (295 loc) · 11.2 KB
/
Copy pathselection.py
File metadata and controls
334 lines (295 loc) · 11.2 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
from __future__ import annotations
import logging
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import Any, Optional, Sequence, Tuple
from . import lp_rpc_pb2
from .discovery import (
FilterValue,
discover_orchestrator_runners,
discover_orchestrators,
discover_runners,
)
from .errors import (
LivepeerGatewayError,
NoOrchestratorAvailableError,
NoRunnerAvailableError,
OrchestratorRejection,
RunnerRejection,
)
from .live_runner import LiveRunnerCallResult, LiveRunnerInstance, LiveRunnerSession, call_runner
from .orch_info import get_orch_info
_LOG = logging.getLogger(__name__)
_BATCH_SIZE = 5
class SelectionCursor:
"""
Stateful selector that advances through orchestrators in batches.
For each batch, all ``get_orch_info`` calls run in parallel and successful
responses are cached in arrival order. Consumers pop cached successes one at
a time and only trigger the next batch once the current cache is exhausted.
"""
def __init__(
self,
orch_list: Sequence[str],
*,
signer_url: Optional[str] = None,
signer_headers: Optional[dict[str, str]] = None,
capabilities: Optional[lp_rpc_pb2.Capabilities] = None,
use_tofu: bool = True,
) -> None:
self._orch_list = list(orch_list)
self._signer_url = signer_url
self._signer_headers = signer_headers
self._capabilities = capabilities
self._use_tofu = use_tofu
self._batch_start = 0
self._pending_successes: list[Tuple[str, lp_rpc_pb2.OrchestratorInfo]] = []
self.rejections: list[OrchestratorRejection] = []
def next(self) -> Tuple[str, lp_rpc_pb2.OrchestratorInfo]:
while True:
if self._pending_successes:
selected = self._pending_successes.pop(0)
_LOG.debug("select_orchestrator selected: %s", selected[0])
return selected
if self._batch_start >= len(self._orch_list):
_LOG.debug(
"select_orchestrator failed: all %d orchestrators rejected",
len(self._orch_list),
)
raise NoOrchestratorAvailableError(
f"All orchestrators failed ({len(self.rejections)} tried)",
rejections=list(self.rejections),
)
self._populate_next_batch_successes()
def _populate_next_batch_successes(self) -> None:
batch = self._orch_list[self._batch_start : self._batch_start + _BATCH_SIZE]
batch_start = self._batch_start
self._batch_start += _BATCH_SIZE
_LOG.debug(
"select_orchestrator trying batch %d-%d: %s",
batch_start,
batch_start + len(batch) - 1,
batch,
)
with ThreadPoolExecutor(max_workers=len(batch)) as executor:
futures = {
executor.submit(
get_orch_info,
url,
signer_url=self._signer_url,
signer_headers=self._signer_headers,
capabilities=self._capabilities,
use_tofu=self._use_tofu,
): url
for url in batch
}
batch_successes: list[Tuple[str, lp_rpc_pb2.OrchestratorInfo]] = []
for future in as_completed(futures):
url = futures[future]
try:
info = future.result()
except Exception as e:
reason = str(e)
_LOG.debug(
"select_orchestrator candidate failed: %s (%s)",
url,
reason,
)
self.rejections.append(OrchestratorRejection(url=url, reason=reason))
continue
batch_successes.append((url, info))
self._pending_successes.extend(batch_successes)
if not batch_successes:
_LOG.debug("select_orchestrator batch exhausted, trying next batch")
def orchestrator_selector(
orchestrators: Optional[Sequence[str] | str] = None,
*,
signer_url: Optional[str] = None,
signer_headers: Optional[dict[str, str]] = None,
discovery_url: Optional[str] = None,
discovery_headers: Optional[dict[str, str]] = None,
capabilities: Optional[lp_rpc_pb2.Capabilities] = None,
use_tofu: bool = True,
) -> SelectionCursor:
orch_list = discover_orchestrators(
orchestrators,
signer_url=signer_url,
signer_headers=signer_headers,
discovery_url=discovery_url,
discovery_headers=discovery_headers,
capabilities=capabilities,
)
if not orch_list:
_LOG.debug("select_orchestrator failed: empty orchestrator list")
raise NoOrchestratorAvailableError("No orchestrators available to select")
return SelectionCursor(
orch_list,
signer_url=signer_url,
signer_headers=signer_headers,
capabilities=capabilities,
use_tofu=use_tofu,
)
class RunnerSelectionCursor:
"""
Stateful selector that advances through live runners sequentially.
Runner attempts are intentionally not parallelized: selecting a persistent
runner reserves capacity, and selecting a single-shot runner may perform
the caller's actual app operation.
"""
def __init__(
self,
candidates: Sequence[LiveRunnerInstance],
*,
body: Optional[dict[str, Any]] = None,
method: str = "POST",
signer_url: Optional[str] = None,
signer_headers: Optional[dict[str, str]] = None,
timeout: float = 5.0,
) -> None:
self._candidates = list(candidates)
self._body = dict(body or {})
self._method = method
self._signer_url = signer_url
self._signer_headers = signer_headers
self._timeout = timeout
self._next_index = 0
self.rejections: list[RunnerRejection] = []
@property
def candidates(self) -> tuple[LiveRunnerInstance, ...]:
return tuple(self._candidates)
async def next(self) -> LiveRunnerCallResult:
while self._next_index < len(self._candidates):
runner = self._candidates[self._next_index]
self._next_index += 1
try:
kwargs: dict[str, Any] = {
"runner": runner,
"payload": self._body,
"method": self._method,
"timeout": self._timeout,
}
if self._signer_url is not None:
kwargs["signer_url"] = self._signer_url
if self._signer_headers is not None:
kwargs["signer_headers"] = self._signer_headers
result = await call_runner(**kwargs)
except Exception as e:
reason = str(e)
_LOG.debug(
"select_runner candidate failed: %s (%s)",
runner.url,
reason,
)
self.rejections.append(RunnerRejection(url=runner.url, reason=reason))
continue
_LOG.debug("select_runner selected: %s", runner.url)
return result
_LOG.debug(
"select_runner failed: all %d runners rejected",
len(self._candidates),
)
raise NoRunnerAvailableError(
f"All runners failed ({len(self.rejections)} tried)",
rejections=list(self.rejections),
)
async def runner_selector(
*,
body: Optional[dict[str, Any]] = None,
method: str = "POST",
orchestrators: Optional[Sequence[str] | str] = None,
signer_url: Optional[str] = None,
signer_headers: Optional[dict[str, str]] = None,
discovery_url: Optional[str] = None,
discovery_headers: Optional[dict[str, str]] = None,
app: Optional[FilterValue] = None,
gpu: Optional[FilterValue] = None,
timeout: float = 5.0,
) -> RunnerSelectionCursor:
if orchestrators is not None:
entries = await discover_orchestrator_runners(
orchestrators,
app=app,
gpu=gpu,
)
else:
entries = await discover_runners(
signer_url=signer_url,
signer_headers=signer_headers,
discovery_url=discovery_url,
discovery_headers=discovery_headers,
app=app,
gpu=gpu,
)
candidates = _runner_candidates_from_discovery(entries)
if not candidates:
_LOG.debug("select_runner failed: empty runner list")
raise NoRunnerAvailableError("No runners available to select")
return RunnerSelectionCursor(
candidates,
body=body,
method=method,
signer_url=signer_url,
signer_headers=signer_headers,
timeout=timeout,
)
async def reserve_session(
*,
signer_url: Optional[str] = None,
signer_headers: Optional[dict[str, str]] = None,
discovery_url: Optional[str] = None,
discovery_headers: Optional[dict[str, str]] = None,
app: Optional[FilterValue] = None,
gpu: Optional[FilterValue] = None,
timeout: float = 5.0,
payment_interval: float = 3.0,
) -> LiveRunnerSession:
cursor = await runner_selector(
signer_url=signer_url,
signer_headers=signer_headers,
discovery_url=discovery_url,
discovery_headers=discovery_headers,
app=app,
gpu=gpu,
timeout=timeout,
)
result = await cursor.next()
session_id = result.data.get("session_id")
app_url = result.data.get("app_url")
if not isinstance(session_id, str) or not session_id.strip():
raise LivepeerGatewayError("runner session response missing session_id")
if not isinstance(app_url, str) or not app_url.strip():
raise LivepeerGatewayError("runner session response missing app_url")
return LiveRunnerSession(
session_id=session_id.strip(),
app_url=app_url.strip(),
runner_url=result.runner_url,
runner=result.runner,
payment_interval=payment_interval,
payment_session=result.payment_session,
)
def _runner_candidates_from_discovery(entries: Sequence[dict[str, Any]]) -> list[LiveRunnerInstance]:
candidates: list[LiveRunnerInstance] = []
for entry in entries:
orchestrator_url = _string_value(entry.get("address"))
runners = entry.get("runners")
if not isinstance(runners, list):
continue
for runner in runners:
if not isinstance(runner, dict):
continue
url = _string_value(runner.get("url"))
app = _string_value(runner.get("app"))
if not url or not app:
continue
candidates.append(
LiveRunnerInstance(
url=url,
app=app,
runner_id=_string_value(runner.get("runner_id")),
mode=_string_value(runner.get("mode")),
orchestrator_url=orchestrator_url,
raw=dict(runner),
)
)
return candidates
def _string_value(value: object) -> str:
return value.strip() if isinstance(value, str) else ""