Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/8080.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `kernels` resolver to `AgentV2` GQL type
5 changes: 5 additions & 0 deletions docs/manager/graphql-reference/supergraph.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,11 @@ type AgentV2 implements Node

"""Added in 26.1.0. Load the container count for this agent."""
containerCount: Int!

"""
Added in 26.2.0. List of kernels running on this agent with pagination support.
"""
kernels(filter: KernelFilter = null, orderBy: [KernelOrderBy!] = null, before: String = null, after: String = null, first: Int = null, last: Int = null, limit: Int = null, offset: Int = null): KernelConnectionV2!
}

"""
Expand Down
5 changes: 5 additions & 0 deletions docs/manager/graphql-reference/v2-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ type AgentV2 implements Node {

"""Added in 26.1.0. Load the container count for this agent."""
containerCount: Int!

"""
Added in 26.2.0. List of kernels running on this agent with pagination support.
"""
kernels(filter: KernelFilter = null, orderBy: [KernelOrderBy!] = null, before: String = null, after: String = null, first: Int = null, last: Int = null, limit: Int = null, offset: Int = null): KernelConnectionV2!
}

"""
Expand Down
51 changes: 50 additions & 1 deletion src/ai/backend/manager/api/gql/agent/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections.abc import Mapping
from datetime import datetime
from enum import StrEnum
from typing import Any, Self
from typing import TYPE_CHECKING, Annotated, Any, Self

import strawberry
from strawberry import ID, Info
Expand All @@ -13,6 +13,13 @@
from ai.backend.common.types import AgentId
from ai.backend.manager.api.gql.base import OrderDirection, StringFilter
from ai.backend.manager.api.gql.types import GQLFilter, GQLOrderBy, StrawberryGQLContext

if TYPE_CHECKING:
from ai.backend.manager.api.gql.kernel.types import (
KernelConnectionV2GQL,
KernelFilterGQL,
KernelOrderByGQL,
)
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
Expand All @@ -23,6 +30,9 @@
combine_conditions_or,
negate_conditions,
)
from ai.backend.manager.repositories.scheduler.options import (
KernelConditions,
)


@strawberry.enum(
Expand Down Expand Up @@ -388,6 +398,45 @@ async def container_count(
"""
return await info.context.data_loaders.container_count_loader.load(self._agent_id)

@strawberry.field( # type: ignore[misc]
description="Added in 26.2.0. List of kernels running on this agent with pagination support."
)
async def kernels(
self,
info: Info[StrawberryGQLContext],
filter: Annotated[
KernelFilterGQL, strawberry.lazy("ai.backend.manager.api.gql.kernel.types")
]
| None = None,
order_by: list[
Annotated[KernelOrderByGQL, strawberry.lazy("ai.backend.manager.api.gql.kernel.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[
KernelConnectionV2GQL, strawberry.lazy("ai.backend.manager.api.gql.kernel.types")
]:
"""Fetch kernels associated with this agent."""
from ai.backend.manager.api.gql.kernel.fetcher import fetch_kernels

return await fetch_kernels(
info=info,
filter=filter,
order_by=order_by,
before=before,
after=after,
first=first,
last=last,
limit=limit,
offset=offset,
base_conditions=[KernelConditions.by_agent_id(str(self._agent_id))],
)

@classmethod
def from_agent_detail_data(cls, detail_data: AgentDetailData) -> Self:
data = detail_data.agent
Expand Down
9 changes: 9 additions & 0 deletions src/ai/backend/manager/repositories/scheduler/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,15 @@ def inner() -> sa.sql.expression.ColumnElement[bool]:

return inner

@staticmethod
def by_agent_id(agent_id: str) -> QueryCondition:
"""Filter kernels by agent ID."""

def inner() -> sa.sql.expression.ColumnElement[bool]:
return KernelRow.agent == agent_id

return inner

@staticmethod
def by_cursor_forward(cursor_id: str) -> QueryCondition:
"""Cursor condition for forward pagination (after cursor).
Expand Down
Loading