-
Notifications
You must be signed in to change notification settings - Fork 168
feat(BA-3947): Add sessions resolver to strawberry AgentV2 schema
#9620
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add `sessions` resolver to strawberry `AgentV2` schema |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,11 @@ | |
| KernelV2FilterGQL, | ||
| KernelV2OrderByGQL, | ||
| ) | ||
| from ai.backend.manager.api.gql.session.types import ( | ||
| SessionV2ConnectionGQL, | ||
| SessionV2FilterGQL, | ||
| SessionV2OrderByGQL, | ||
| ) | ||
| from ai.backend.manager.api.gql.utils import dedent_strip | ||
| from ai.backend.manager.data.agent.types import AgentDetailData, AgentStatus | ||
| from ai.backend.manager.models.rbac.permission_defs import AgentPermission | ||
|
|
@@ -32,6 +37,7 @@ | |
| ) | ||
| from ai.backend.manager.repositories.scheduler.options import ( | ||
| KernelConditions, | ||
| SessionConditions, | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -439,7 +445,48 @@ async def kernels( | |
| last=last, | ||
| limit=limit, | ||
| offset=offset, | ||
| base_conditions=[KernelConditions.by_agent_id(str(self._agent_id))], | ||
| base_conditions=[KernelConditions.by_agent_id(self._agent_id)], | ||
| ) | ||
|
|
||
| @strawberry.field( # type: ignore[misc] | ||
| description="Added in 26.3.0. List of sessions running on this agent with pagination support." | ||
| ) | ||
| async def sessions( | ||
| self, | ||
| info: Info[StrawberryGQLContext], | ||
| filter: Annotated[ | ||
| SessionV2FilterGQL, strawberry.lazy("ai.backend.manager.api.gql.session.types") | ||
| ] | ||
| | None = None, | ||
| order_by: list[ | ||
| Annotated[ | ||
| SessionV2OrderByGQL, strawberry.lazy("ai.backend.manager.api.gql.session.types") | ||
| ] | ||
| ] | ||
| | None = None, | ||
| before: str | None = None, | ||
| after: str | None = None, | ||
| first: int | None = None, | ||
| last: int | None = None, | ||
| limit: int | None = None, | ||
| offset: int | None = None, | ||
| ) -> Annotated[ | ||
| SessionV2ConnectionGQL, strawberry.lazy("ai.backend.manager.api.gql.session.types") | ||
| ]: | ||
| """Fetch sessions associated with this agent.""" | ||
| from ai.backend.manager.api.gql.session.fetcher.session import fetch_sessions | ||
|
|
||
| return await fetch_sessions( | ||
| info=info, | ||
| filter=filter, | ||
| order_by=order_by, | ||
| before=before, | ||
| after=after, | ||
| first=first, | ||
| last=last, | ||
| limit=limit, | ||
| offset=offset, | ||
| base_conditions=[SessionConditions.by_agent_id(self._agent_id)], | ||
| ) | ||
|
Comment on lines
+451
to
490
|
||
|
|
||
| @classmethod | ||
|
|
||
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.
AgentV2GQL.sessionscallsfetch_sessions()directly per agent instance. In queries that return a list/connection of agents and requestsessionsfor each node, this will trigger one session-search per agent (N+1 pattern) and can become expensive. Consider introducing a batching approach (e.g., a DataLoader keyed by (agent_id, pagination/filter args) with internal grouping, or a dedicated processor/repository method that can fetch sessions for multiple agent IDs in one call when args match) to keep the number of DB/service calls bounded per GraphQL request.