-
Notifications
You must be signed in to change notification settings - Fork 164
feat(BA-1950): Action Processor to handle various types of targets #5744
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
fregataa
wants to merge
1
commit into
main
Choose a base branch
from
feat/add-processors-based-on-action-types
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.
Open
Changes from all commits
Commits
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 @@ | ||
Add Action Processors to handle various types of actions |
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,25 @@ | ||
from .base import ( | ||
BaseAction, | ||
BaseActionResult, | ||
BaseActionResultMeta, | ||
BaseActionTriggerMeta, | ||
ProcessResult, | ||
TAction, | ||
TActionResult, | ||
) | ||
from .batch import ( | ||
BaseBatchAction, | ||
BaseBatchActionResult, | ||
) | ||
|
||
__all__ = ( | ||
"BaseAction", | ||
"BaseActionResult", | ||
"BaseActionResultMeta", | ||
"BaseBatchAction", | ||
"BaseBatchActionResult", | ||
"BaseActionTriggerMeta", | ||
"ProcessResult", | ||
"TAction", | ||
"TActionResult", | ||
) |
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,28 @@ | ||
from abc import abstractmethod | ||
from typing import Optional, TypeVar, override | ||
|
||
from .base import BaseAction, BaseActionResult | ||
|
||
|
||
class BaseBatchAction(BaseAction): | ||
@override | ||
def entity_id(self) -> Optional[str]: | ||
return None | ||
|
||
@abstractmethod | ||
def entity_ids(self) -> list[str]: | ||
raise NotImplementedError | ||
|
||
|
||
class BaseBatchActionResult(BaseActionResult): | ||
@override | ||
def entity_id(self) -> Optional[str]: | ||
return None | ||
|
||
@abstractmethod | ||
def entity_ids(self) -> list[str]: | ||
raise NotImplementedError | ||
|
||
|
||
TBatchAction = TypeVar("TBatchAction", bound=BaseBatchAction) | ||
TBatchActionResult = TypeVar("TBatchActionResult", bound=BaseBatchActionResult) |
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,36 @@ | ||
from abc import abstractmethod | ||
from typing import Optional, TypeVar, override | ||
|
||
from .base import BaseAction, BaseActionResult | ||
|
||
|
||
class BaseScopeAction(BaseAction): | ||
@override | ||
def entity_id(self) -> Optional[str]: | ||
return None | ||
|
||
@abstractmethod | ||
def scope_type(self) -> str: | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def scope_id(self) -> str: | ||
raise NotImplementedError | ||
|
||
|
||
class BaseScopeActionResult(BaseActionResult): | ||
@override | ||
def entity_id(self) -> Optional[str]: | ||
return None | ||
|
||
@abstractmethod | ||
def scope_type(self) -> str: | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def scope_id(self) -> str: | ||
raise NotImplementedError | ||
|
||
|
||
TScopeAction = TypeVar("TScopeAction", bound=BaseScopeAction) | ||
TScopeActionResult = TypeVar("TScopeActionResult", bound=BaseScopeActionResult) |
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,28 @@ | ||
from abc import abstractmethod | ||
from typing import Optional, TypeVar, override | ||
|
||
from .base import BaseAction, BaseActionResult | ||
|
||
|
||
class BaseSingleEntityAction(BaseAction): | ||
@override | ||
def entity_id(self) -> Optional[str]: | ||
return None | ||
|
||
@abstractmethod | ||
def target_entity_id(self) -> str: | ||
raise NotImplementedError | ||
|
||
|
||
class BaseSingleEntityActionResult(BaseActionResult): | ||
@override | ||
def entity_id(self) -> Optional[str]: | ||
return None | ||
|
||
@abstractmethod | ||
def target_entity_id(self) -> str: | ||
raise NotImplementedError | ||
|
||
|
||
TSingleEntityAction = TypeVar("TSingleEntityAction", bound=BaseSingleEntityAction) | ||
TSingleEntityActionResult = TypeVar("TSingleEntityActionResult", bound=BaseSingleEntityActionResult) |
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,3 @@ | ||
from .base import ActionProcessor | ||
|
||
__all__ = ("ActionProcessor",) |
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,44 @@ | ||
import logging | ||
import uuid | ||
from datetime import datetime | ||
from typing import Awaitable, Callable, Generic, Optional | ||
|
||
from ai.backend.logging.utils import BraceStyleAdapter | ||
from ai.backend.manager.actions.validator.batch import BatchActionValidator | ||
|
||
from ..action import ( | ||
BaseActionTriggerMeta, | ||
) | ||
from ..action.batch import TBatchAction, TBatchActionResult | ||
from ..monitors.monitor import ActionMonitor | ||
from .base import ActionRunner | ||
|
||
log = BraceStyleAdapter(logging.getLogger(__spec__.name)) | ||
|
||
|
||
class BatchActionProcessor(Generic[TBatchAction, TBatchActionResult]): | ||
_validators: list[BatchActionValidator] | ||
|
||
_runner: ActionRunner[TBatchAction, TBatchActionResult] | ||
|
||
def __init__( | ||
self, | ||
func: Callable[[TBatchAction], Awaitable[TBatchActionResult]], | ||
monitors: Optional[list[ActionMonitor]] = None, | ||
validators: Optional[list[BatchActionValidator]] = None, | ||
) -> None: | ||
self._runner = ActionRunner(func, monitors) | ||
|
||
self._validators = validators or [] | ||
|
||
async def _run(self, action: TBatchAction) -> TBatchActionResult: | ||
started_at = datetime.now() | ||
action_id = uuid.uuid4() | ||
action_trigger_meta = BaseActionTriggerMeta(action_id=action_id, started_at=started_at) | ||
for validator in self._validators: | ||
await validator.validate(action, action_trigger_meta) | ||
|
||
return await self._runner.run(action, action_trigger_meta) | ||
|
||
async def wait_for_complete(self, action: TBatchAction) -> TBatchActionResult: | ||
return await self._run(action) |
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.
Is the entity_id for backward compatibility?
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.
yes, it overrides
BaseAction
method