Skip to content
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

feat: GQL resolvers for querying agent's total resource allocation (#2254) #2725

Merged
merged 2 commits into from
Aug 17, 2024
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/2254.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `scaling_group.agent_count_by_status` and `scaling_group.agent_total_resource_slots_by_status` GQL fields to query the count and the resource allocation of agents that belong to a scaling group.
16 changes: 16 additions & 0 deletions src/ai/backend/manager/api/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,22 @@
scheduler: String
scheduler_opts: JSONString
use_host_network: Boolean

"""Added in 24.03.9."""
agent_count_by_status(

Check notice on line 712 in src/ai/backend/manager/api/schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Field 'agent_count_by_status' was added to object type 'ScalingGroup'

Field 'agent_count_by_status' was added to object type 'ScalingGroup'
"""
Possible states of an agent. Should be one of ['ALIVE', 'LOST', 'RESTARTING', 'TERMINATED']. Default is 'ALIVE'.
"""
status: String = "ALIVE"
): Int

"""Added in 24.03.9."""
agent_total_resource_slots_by_status(

Check notice on line 720 in src/ai/backend/manager/api/schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Field 'agent_total_resource_slots_by_status' was added to object type 'ScalingGroup'

Field 'agent_total_resource_slots_by_status' was added to object type 'ScalingGroup'
"""
Possible states of an agent. Should be one of ['ALIVE', 'LOST', 'RESTARTING', 'TERMINATED']. Default is 'ALIVE'.
"""
status: String = "ALIVE"
): JSONString
}

type StorageVolume implements Item {
Expand Down
3 changes: 2 additions & 1 deletion src/ai/backend/manager/models/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
from .keypair import keypairs
from .minilang.ordering import OrderSpecItem, QueryOrderParser
from .minilang.queryfilter import FieldSpecItem, QueryFilterParser, enum_field_getter
from .scaling_group import query_allowed_sgroups
from .user import UserRole, users

if TYPE_CHECKING:
Expand Down Expand Up @@ -480,6 +479,8 @@ async def _append_sgroup_from_clause(
domain_name: str | None,
scaling_group: str | None = None,
) -> sa.sql.Select:
from .scaling_group import query_allowed_sgroups

if scaling_group is not None:
query = query.where(agents.c.scaling_group == scaling_group)
else:
Expand Down
69 changes: 67 additions & 2 deletions src/ai/backend/manager/models/scaling_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@
from sqlalchemy.dialects import postgresql as pgsql
from sqlalchemy.engine.row import Row
from sqlalchemy.ext.asyncio import AsyncConnection as SAConnection
from sqlalchemy.orm import relationship
from sqlalchemy.orm import load_only, relationship
from sqlalchemy.sql.expression import true

from ai.backend.common import validators as tx
from ai.backend.common.types import AgentSelectionStrategy, JSONSerializableMixin, SessionTypes
from ai.backend.common.types import (
AgentSelectionStrategy,
JSONSerializableMixin,
ResourceSlot,
SessionTypes,
)

from .agent import AgentStatus
from .base import (
Base,
IDColumn,
Expand Down Expand Up @@ -315,6 +321,65 @@ class ScalingGroup(graphene.ObjectType):
scheduler_opts = graphene.JSONString()
use_host_network = graphene.Boolean()

# Dynamic fields.
agent_count_by_status = graphene.Field(
graphene.Int,
description="Added in 24.03.9.",
status=graphene.String(
default_value=AgentStatus.ALIVE.name,
description=f"Possible states of an agent. Should be one of {[s.name for s in AgentStatus]}. Default is 'ALIVE'.",
),
)

agent_total_resource_slots_by_status = graphene.Field(
graphene.JSONString,
description="Added in 24.03.9.",
status=graphene.String(
default_value=AgentStatus.ALIVE.name,
description=f"Possible states of an agent. Should be one of {[s.name for s in AgentStatus]}. Default is 'ALIVE'.",
),
)

async def resolve_agent_count_by_status(
self, info: graphene.ResolveInfo, status: str = AgentStatus.ALIVE.name
) -> int:
from .agent import Agent

return await Agent.load_count(
info.context,
raw_status=status,
scaling_group=self.name,
)

async def resolve_agent_total_resource_slots_by_status(
self, info: graphene.ResolveInfo, status: str = AgentStatus.ALIVE.name
) -> Mapping[str, Any]:
from .agent import AgentRow, AgentStatus

graph_ctx = info.context
async with graph_ctx.db.begin_readonly_session() as db_session:
query_stmt = (
sa.select(AgentRow)
.where(
(AgentRow.scaling_group == self.name) & (AgentRow.status == AgentStatus[status])
)
.options(load_only(AgentRow.occupied_slots, AgentRow.available_slots))
)
result = (await db_session.scalars(query_stmt)).all()
agent_rows = cast(list[AgentRow], result)

total_occupied_slots = ResourceSlot()
total_available_slots = ResourceSlot()

for agent_row in agent_rows:
total_occupied_slots += agent_row.occupied_slots
total_available_slots += agent_row.available_slots

return {
"occupied_slots": total_occupied_slots.to_json(),
"available_slots": total_available_slots.to_json(),
}

@classmethod
def from_row(
cls,
Expand Down
Loading