-
Notifications
You must be signed in to change notification settings - Fork 178
feat(BA-6581): wire IdleCheckerRepository into idle-check reconciler source #12398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d1f2c3e
feat(BA-6581): wire IdleCheckerRepository into idle-check reconciler …
seedspirit a0daf9b
changelog: add news fragment for PR #12398
seedspirit 9e3b5fc
refactor(BA-6581): provide IdleCheckerRepository via repositories con…
seedspirit cee60bd
refactor(BA-6581): store idle-checker spec via PydanticColumn
seedspirit 3d1e71e
refactor(BA-6581): expose idle-check source as batch targets
seedspirit 942a15d
refactor: suffix idle-check repository data types
seedspirit 370071e
refactor: use ops provider for idle-check reads
seedspirit 288f792
test: cover idle-check repository batch fetch
seedspirit db79915
refactor(BA-6581): drop joins and stage-own session_type in idle-chec…
seedspirit 81cefd3
refactor(BA-6581): target session types per checker in idle-check read
seedspirit 2db2582
fix: Remove system session from default idlechcker session type
seedspirit ad1ae43
refactor(BA-6581): dedupe idle-check candidate conditions
seedspirit 0d87441
refactor: Split checker target data building func
seedspirit fe0b1cb
refactor(BA-6581): extract idle-check target assembly into a typed he…
seedspirit 13d7f42
refactor: Fix types
seedspirit 391d711
Address idle checker repository review feedback
seedspirit c8d75bf
Address idle checker scope review feedback
seedspirit 30d81cb
Move idle checker target sessions out of spec
seedspirit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Wire IdleCheckerRepository into the idle-check reconciler source to fetch a normalized session/scope/checker snapshot |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import enum | ||
| from typing import Self | ||
|
|
||
| from pydantic import Field, model_validator | ||
|
|
||
| from ai.backend.common.types import BackendAISchema | ||
|
|
||
|
|
||
| class CheckerType(enum.StrEnum): | ||
| """Discriminator for the kind of idle checker; selects the concrete spec.""" | ||
|
|
||
| SESSION_LIFETIME = "session_lifetime" | ||
| NETWORK_TIMEOUT = "network_timeout" | ||
| UTILIZATION = "utilization" | ||
|
|
||
|
|
||
| class SessionLifetimeSpec(BackendAISchema): | ||
| """Config for ``CheckerType.SESSION_LIFETIME``. | ||
|
|
||
| Concrete fields (e.g. max lifetime) land with the checker-logic stories. | ||
| """ | ||
|
|
||
|
|
||
| class NetworkTimeoutSpec(BackendAISchema): | ||
| """Config for ``CheckerType.NETWORK_TIMEOUT``. | ||
|
|
||
| Concrete fields land with the checker-logic stories. | ||
| """ | ||
|
|
||
|
|
||
| class UtilizationSpec(BackendAISchema): | ||
| """Config for ``CheckerType.UTILIZATION``. | ||
|
|
||
| Concrete fields land with the checker-logic stories. | ||
| """ | ||
|
|
||
|
|
||
| class IdleCheckerSpec(BackendAISchema): | ||
| """Config payload stored in ``idle_checkers.spec`` as a single JSONB document. | ||
|
|
||
| ``type`` selects which sub-config is valid; exactly one sub-config field must | ||
| be present for the chosen type (validated below). Stored via ``PydanticColumn``. | ||
| """ | ||
|
|
||
| type: CheckerType = Field(description="Idle checker kind; selects the sub-config.") | ||
| session_lifetime: SessionLifetimeSpec | None = Field( | ||
| default=None, description="session_lifetime config." | ||
| ) | ||
| network: NetworkTimeoutSpec | None = Field(default=None, description="network_timeout config.") | ||
| utilization: UtilizationSpec | None = Field(default=None, description="utilization config.") | ||
|
|
||
| @model_validator(mode="after") | ||
| def validate_spec_matches_type(self) -> Self: | ||
| match self.type: | ||
| case CheckerType.SESSION_LIFETIME: | ||
| if self.session_lifetime is None: | ||
| raise ValueError("session_lifetime is required for type=session_lifetime.") | ||
| if self.network or self.utilization: | ||
| raise ValueError("Only session_lifetime is allowed for type=session_lifetime.") | ||
| case CheckerType.NETWORK_TIMEOUT: | ||
| if self.network is None: | ||
| raise ValueError("network is required for type=network_timeout.") | ||
| if self.session_lifetime or self.utilization: | ||
| raise ValueError("Only network is allowed for type=network_timeout.") | ||
| case CheckerType.UTILIZATION: | ||
| if self.utilization is None: | ||
| raise ValueError("utilization is required for type=utilization.") | ||
| if self.session_lifetime or self.network: | ||
| raise ValueError("Only utilization is allowed for type=utilization.") | ||
| return self |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,15 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import enum | ||
| from dataclasses import dataclass | ||
| from datetime import datetime | ||
|
|
||
| from ai.backend.common.types import SessionId | ||
|
|
||
| class CheckerType(enum.StrEnum): | ||
| """Discriminator for the kind of idle checker; selects the concrete spec.""" | ||
|
|
||
| SESSION_LIFETIME = "session_lifetime" | ||
| NETWORK_TIMEOUT = "network_timeout" | ||
| UTILIZATION = "utilization" | ||
| @dataclass(frozen=True) | ||
| class IdleCheckSession: | ||
| """Session fields needed to evaluate idle checkers.""" | ||
|
|
||
| session_id: SessionId | ||
| created_at: datetime | ||
| starts_at: datetime | None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import sqlalchemy as sa | ||
|
|
||
| from ai.backend.manager.models.clauses import QueryCondition | ||
|
|
||
| from .row import IdleCheckerBindingRow | ||
|
|
||
|
|
||
| class IdleCheckerBindingConditions: | ||
| @staticmethod | ||
| def enabled() -> QueryCondition: | ||
| def inner() -> sa.sql.expression.ColumnElement[bool]: | ||
| return IdleCheckerBindingRow.enabled == sa.true() | ||
|
|
||
| return inner |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
102 changes: 102 additions & 0 deletions
102
src/ai/backend/manager/repositories/idle_checker/db_source/db_source.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| """DB reads backing the idle-check Source: the per-session checker batch.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Sequence | ||
| from typing import cast | ||
|
|
||
| import sqlalchemy as sa | ||
|
|
||
| from ai.backend.common.data.idle_checker.types import CheckerType, IdleCheckerSpec | ||
| from ai.backend.common.data.permission.types import ScopeType | ||
| from ai.backend.common.identifier.idle_checker import IdleCheckerID | ||
| from ai.backend.common.types import SessionId, SessionTypes | ||
| from ai.backend.manager.data.idle_checker.types import IdleCheckSession | ||
| from ai.backend.manager.data.permission.id import ScopeId | ||
| from ai.backend.manager.models.idle_checker.row import IdleCheckerBindingRow, IdleCheckerRow | ||
| from ai.backend.manager.models.session.row import SessionRow | ||
| from ai.backend.manager.repositories.base import BatchQuerier | ||
| from ai.backend.manager.repositories.idle_checker.types import ( | ||
| BoundCheckerData, | ||
| IdleCheckerDefinitionData, | ||
| IdleCheckSessionData, | ||
| ) | ||
| from ai.backend.manager.repositories.ops import DBOpsProvider | ||
|
|
||
|
|
||
| class IdleCheckerDBSource: | ||
| _ops: DBOpsProvider | ||
|
|
||
| def __init__(self, ops_provider: DBOpsProvider) -> None: | ||
| self._ops = ops_provider | ||
|
|
||
| async def fetch_bound_checkers( | ||
| self, | ||
| querier: BatchQuerier, | ||
| ) -> Sequence[BoundCheckerData]: | ||
| binding_query = sa.select( | ||
| IdleCheckerBindingRow.scope_type, | ||
| IdleCheckerBindingRow.scope_id, | ||
| IdleCheckerBindingRow.created_at, | ||
| IdleCheckerBindingRow.idle_checker_id, | ||
| IdleCheckerRow.checker_type, | ||
| IdleCheckerRow.target_session_types, | ||
| IdleCheckerRow.spec, | ||
| ).join(IdleCheckerRow, IdleCheckerBindingRow.idle_checker_id == IdleCheckerRow.id) | ||
|
|
||
| async with self._ops.read_ops() as r: | ||
| binding_rows = (await r.batch_query_in_global(binding_query, querier)).rows | ||
| bound_checkers: list[BoundCheckerData] = [] | ||
| for row in binding_rows: | ||
| scope = ScopeId( | ||
| scope_type=ScopeType(row.scope_type), | ||
| scope_id=str(row.scope_id), | ||
| ) | ||
| checker = IdleCheckerDefinitionData( | ||
| checker_id=cast(IdleCheckerID, row.idle_checker_id), | ||
| checker_type=cast(CheckerType, row.checker_type), | ||
| target_session_types=frozenset( | ||
| cast(Sequence[SessionTypes], row.target_session_types) | ||
| ), | ||
| spec=cast(IdleCheckerSpec, row.spec), | ||
| ) | ||
| bound_checkers.append( | ||
| BoundCheckerData( | ||
| scope=scope, | ||
| binding_created_at=row.created_at, | ||
| checker=checker, | ||
| ) | ||
| ) | ||
| return bound_checkers | ||
|
|
||
| async def fetch_candidate_sessions( | ||
| self, | ||
| querier: BatchQuerier, | ||
| ) -> Sequence[IdleCheckSessionData]: | ||
| session_query = sa.select( | ||
| SessionRow.id, | ||
| SessionRow.created_at, | ||
| SessionRow.starts_at, | ||
| SessionRow.session_type, | ||
| SessionRow.resource_group_id, | ||
| SessionRow.group_id, | ||
| SessionRow.domain_id, | ||
| ) | ||
| async with self._ops.read_ops() as r: | ||
| session_rows = (await r.batch_query_in_global(session_query, querier)).rows | ||
| return tuple( | ||
| IdleCheckSessionData( | ||
| session=IdleCheckSession( | ||
| session_id=SessionId(row.id), | ||
| created_at=row.created_at, | ||
| starts_at=row.starts_at, | ||
| ), | ||
| session_type=row.session_type, | ||
| scopes=( | ||
| ScopeId(ScopeType.RESOURCE_GROUP, str(row.resource_group_id)), | ||
| ScopeId(ScopeType.PROJECT, str(row.group_id)), | ||
| ScopeId(ScopeType.DOMAIN, str(row.domain_id)), | ||
| ), | ||
| ) | ||
| for row in session_rows | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.