-
Notifications
You must be signed in to change notification settings - Fork 164
refactor(BA-3117, BA-3118): Introduce source-based structure in user resource policy #6907
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
Open
seedspirit
wants to merge
11
commits into
main
Choose a base branch
from
refactor/BA-3117
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+447
−564
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b665857
feat: Introduce db source in user resource policy
seedspirit eca4da0
test: Update user resource policy test code according to pattern change
seedspirit e8ff5d8
feat: Add custom not found error in user resource policy domain
seedspirit 69081d8
docs: Add news fragment
seedspirit 1023545
misc: Remove unnecessary init file code
seedspirit be8bcdf
test: Remove duplicated integration test as repo test replace it
seedspirit 47a5c7d
refactor: Refactor method params with creator, modifier
seedspirit 0c71376
refactor: Explictly update value through returning query
seedspirit 2f04a01
fix: Change metric policy layer type
seedspirit adadc90
refactor: Use returning in delete query
seedspirit df00c63
chore: update api schema dump
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 @@ | ||
| Introduce source-based structure in user resource policy |
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
File renamed without changes.
124 changes: 124 additions & 0 deletions
124
src/ai/backend/manager/repositories/user_resource_policy/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,124 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Optional | ||
|
|
||
| import sqlalchemy as sa | ||
|
|
||
| from ai.backend.common.exception import BackendAIError, UserResourcePolicyNotFound | ||
| from ai.backend.common.metrics.metric import DomainType, LayerType | ||
| from ai.backend.common.resilience.policies.metrics import MetricArgs, MetricPolicy | ||
| from ai.backend.common.resilience.policies.retry import BackoffStrategy, RetryArgs, RetryPolicy | ||
| from ai.backend.common.resilience.resilience import Resilience | ||
| from ai.backend.manager.data.resource.types import UserResourcePolicyData | ||
| from ai.backend.manager.models.resource_policy import UserResourcePolicyRow | ||
| from ai.backend.manager.services.user_resource_policy.actions.modify_user_resource_policy import ( | ||
| UserResourcePolicyModifier, | ||
| ) | ||
| from ai.backend.manager.services.user_resource_policy.types import UserResourcePolicyCreator | ||
|
|
||
| if TYPE_CHECKING: | ||
| from ai.backend.manager.models.utils import ExtendedAsyncSAEngine | ||
|
|
||
| user_resource_policy_db_source_resilience = Resilience( | ||
| policies=[ | ||
| MetricPolicy( | ||
| MetricArgs(domain=DomainType.DB_SOURCE, layer=LayerType.USER_RESOURCE_POLICY_DB_SOURCE) | ||
| ), | ||
| RetryPolicy( | ||
| RetryArgs( | ||
| max_retries=5, | ||
| retry_delay=0.1, | ||
| backoff_strategy=BackoffStrategy.FIXED, | ||
| non_retryable_exceptions=(BackendAIError,), | ||
| ) | ||
| ), | ||
| ] | ||
| ) | ||
|
|
||
|
|
||
| class UserResourcePolicyDBSource: | ||
| """ | ||
| Database source for user resource policy operations. | ||
| Handles all database operations for user resource policies. | ||
| """ | ||
|
|
||
| _db: ExtendedAsyncSAEngine | ||
|
|
||
| def __init__(self, db: ExtendedAsyncSAEngine) -> None: | ||
| self._db = db | ||
|
|
||
| @user_resource_policy_db_source_resilience.apply() | ||
| async def create(self, creator: UserResourcePolicyCreator) -> UserResourcePolicyData: | ||
| """Creates a new user resource policy.""" | ||
| async with self._db.begin_session() as db_sess: | ||
| db_row = UserResourcePolicyRow.from_creator(creator) | ||
| db_sess.add(db_row) | ||
| await db_sess.flush() | ||
| return db_row.to_dataclass() | ||
|
|
||
| @user_resource_policy_db_source_resilience.apply() | ||
| async def get_by_name(self, name: str) -> UserResourcePolicyData: | ||
| """Retrieves a user resource policy by name.""" | ||
| async with self._db.begin_readonly_session() as db_sess: | ||
| query = sa.select(UserResourcePolicyRow).where(UserResourcePolicyRow.name == name) | ||
| row = await db_sess.scalar(query) | ||
| if row is None: | ||
| raise UserResourcePolicyNotFound( | ||
| f"User resource policy with name {name} not found." | ||
| ) | ||
| return row.to_dataclass() | ||
|
|
||
|
Comment on lines
+59
to
+70
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like this could also be integrated into the querier. |
||
| @user_resource_policy_db_source_resilience.apply() | ||
| async def update( | ||
| self, name: str, modifier: UserResourcePolicyModifier | ||
| ) -> UserResourcePolicyData: | ||
| """Updates an existing user resource policy.""" | ||
| async with self._db.begin_session() as db_sess: | ||
| # Check if the policy exists first | ||
| check_query = sa.select(UserResourcePolicyRow).where(UserResourcePolicyRow.name == name) | ||
| existing_row: Optional[UserResourcePolicyRow] = await db_sess.scalar(check_query) | ||
| if existing_row is None: | ||
| raise UserResourcePolicyNotFound( | ||
| f"User resource policy with name {name} not found." | ||
| ) | ||
|
|
||
| fields = modifier.fields_to_update() | ||
| update_stmt = ( | ||
| sa.update(UserResourcePolicyRow) | ||
| .where(UserResourcePolicyRow.name == name) | ||
| .values(**fields) | ||
| .returning(UserResourcePolicyRow) | ||
| ) | ||
| query_stmt = ( | ||
| sa.select(UserResourcePolicyRow) | ||
| .from_statement(update_stmt) | ||
| .execution_options(populate_existing=True) | ||
| ) | ||
| updated_row: Optional[UserResourcePolicyRow] = await db_sess.scalar(query_stmt) | ||
seedspirit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if updated_row is None: | ||
| raise UserResourcePolicyNotFound( | ||
| f"User resource policy with name {name} not found after update." | ||
| ) | ||
| return updated_row.to_dataclass() | ||
|
|
||
| @user_resource_policy_db_source_resilience.apply() | ||
| async def delete(self, name: str) -> UserResourcePolicyData: | ||
| """Deletes a user resource policy.""" | ||
| async with self._db.begin_session() as db_sess: | ||
| delete_stmt = ( | ||
| sa.delete(UserResourcePolicyRow) | ||
| .where(UserResourcePolicyRow.name == name) | ||
| .returning(UserResourcePolicyRow) | ||
| ) | ||
| query_stms = ( | ||
| sa.select(UserResourcePolicyRow) | ||
| .from_statement(delete_stmt) | ||
| .execution_options(populate_existing=True) | ||
| ) | ||
| row: Optional[UserResourcePolicyRow] = await db_sess.scalar(query_stms) | ||
| if row is None: | ||
| raise UserResourcePolicyNotFound( | ||
| f"User resource policy with name {name} not found." | ||
| ) | ||
| await db_sess.delete(row) | ||
| return row.to_dataclass() | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is serializable applied here? It seems like applying Read Committed this time would be worthwhile, don't you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like applying it in 1.4 would be limited... Let's see after it goes over 2.0 for now.