|
| 1 | +import logging |
| 2 | +import uuid |
| 3 | +from collections.abc import Awaitable, Callable, Sequence |
| 4 | +from dataclasses import dataclass |
| 5 | +from datetime import UTC, datetime |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +from ai.backend.logging.utils import BraceStyleAdapter |
| 9 | +from ai.backend.manager.actions.action import ( |
| 10 | + BaseActionTriggerMeta, |
| 11 | +) |
| 12 | +from ai.backend.manager.actions.action.bulk import ( |
| 13 | + BaseBulkAction, |
| 14 | + BaseBulkActionResult, |
| 15 | +) |
| 16 | +from ai.backend.manager.actions.monitors.monitor import ActionMonitor |
| 17 | +from ai.backend.manager.actions.validator.bulk import ( |
| 18 | + BulkActionValidator, |
| 19 | + BulkValidationResult, |
| 20 | +) |
| 21 | + |
| 22 | +from .base import ActionRunner |
| 23 | + |
| 24 | +log = BraceStyleAdapter(logging.getLogger(__spec__.name)) |
| 25 | + |
| 26 | + |
| 27 | +@dataclass(frozen=True) |
| 28 | +class ValidatorDecision: |
| 29 | + """One validator's per-entity verdict observed during bulk processing. |
| 30 | +
|
| 31 | + Mirrors the ``SubStepResult`` pattern used by the scheduler history so |
| 32 | + callers can trace where in the validator chain each ID was filtered and |
| 33 | + *why*. ``results`` carries the validator's classification unchanged. |
| 34 | + """ |
| 35 | + |
| 36 | + validator_name: str |
| 37 | + results: BulkValidationResult |
| 38 | + |
| 39 | + |
| 40 | +@dataclass(frozen=True) |
| 41 | +class BulkProcessResult[TBulkActionResult: BaseBulkActionResult]: |
| 42 | + """Outcome of a ``BulkActionProcessor`` run. |
| 43 | +
|
| 44 | + ``result`` is what the service function returned for the permitted subset |
| 45 | + of entity IDs. ``validator_decisions`` keeps the per-validator trace in |
| 46 | + iteration order; callers assemble the partial-success response by |
| 47 | + walking it (each decision carries the denied IDs and their reasons). |
| 48 | + """ |
| 49 | + |
| 50 | + result: TBulkActionResult |
| 51 | + validator_decisions: list[ValidatorDecision] |
| 52 | + |
| 53 | + |
| 54 | +class BulkActionProcessor[ |
| 55 | + TBulkAction: BaseBulkAction[Any], |
| 56 | + TBulkActionResult: BaseBulkActionResult, |
| 57 | +]: |
| 58 | + _validators: Sequence[BulkActionValidator] |
| 59 | + |
| 60 | + _runner: ActionRunner[TBulkAction, TBulkActionResult] |
| 61 | + |
| 62 | + def __init__( |
| 63 | + self, |
| 64 | + func: Callable[[TBulkAction], Awaitable[TBulkActionResult]], |
| 65 | + monitors: Sequence[ActionMonitor] | None = None, |
| 66 | + validators: Sequence[BulkActionValidator] | None = None, |
| 67 | + ) -> None: |
| 68 | + self._runner = ActionRunner(func, monitors) |
| 69 | + |
| 70 | + self._validators = validators or [] |
| 71 | + |
| 72 | + def _filter_by_validation( |
| 73 | + self, |
| 74 | + action: TBulkAction, |
| 75 | + validation: BulkValidationResult, |
| 76 | + ) -> TBulkAction: |
| 77 | + """Return a new action narrowed to the IDs this validator permitted. |
| 78 | +
|
| 79 | + Returns the incoming action unchanged when the validator denied |
| 80 | + nothing; otherwise constructs a fresh instance of the same class |
| 81 | + via its ``entity_ids``-only constructor so the original stays |
| 82 | + immutable. |
| 83 | + """ |
| 84 | + if not validation.denied_entities: |
| 85 | + return action |
| 86 | + allowed_set = set(validation.allowed_entity_ids) |
| 87 | + filtered_ids = [eid for eid in action.entity_ids if eid in allowed_set] |
| 88 | + return type(action)(entity_ids=filtered_ids) |
| 89 | + |
| 90 | + async def _run(self, action: TBulkAction) -> BulkProcessResult[TBulkActionResult]: |
| 91 | + started_at = datetime.now(UTC) |
| 92 | + action_id = uuid.uuid4() |
| 93 | + action_trigger_meta = BaseActionTriggerMeta(action_id=action_id, started_at=started_at) |
| 94 | + |
| 95 | + filtered_action: TBulkAction = action |
| 96 | + decisions: list[ValidatorDecision] = [] |
| 97 | + |
| 98 | + for validator in self._validators: |
| 99 | + validation = await validator.validate(filtered_action, action_trigger_meta) |
| 100 | + decisions.append( |
| 101 | + ValidatorDecision( |
| 102 | + validator_name=validator.name(), |
| 103 | + results=validation, |
| 104 | + ) |
| 105 | + ) |
| 106 | + filtered_action = self._filter_by_validation(filtered_action, validation) |
| 107 | + |
| 108 | + action_result = await self._runner.run(filtered_action, action_trigger_meta) |
| 109 | + return BulkProcessResult(result=action_result, validator_decisions=decisions) |
| 110 | + |
| 111 | + async def wait_for_complete(self, action: TBulkAction) -> BulkProcessResult[TBulkActionResult]: |
| 112 | + return await self._run(action) |
0 commit comments