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: session status-history handler #1116

Closed
wants to merge 5 commits into from
Closed
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/1116.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement session status-history handler
27 changes: 23 additions & 4 deletions src/ai/backend/client/cli/session/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

from ai.backend.cli.main import main
from ai.backend.cli.types import ExitCode
from ai.backend.client.cli.extensions import pass_ctx_obj
from ai.backend.client.cli.types import CLIContext
from ai.backend.common.arch import DEFAULT_IMAGE_ARCH

from ...compat import asyncio_run
Expand Down Expand Up @@ -712,8 +714,9 @@ def logs(session_id):


@session.command("status-history")
@pass_ctx_obj
@click.argument("session_id", metavar="SESSID")
def status_history(session_id):
def status_history(ctx: CLIContext, session_id):
"""
Shows the status transition history of the compute session.

Expand All @@ -725,15 +728,31 @@ def status_history(session_id):
kernel = session.ComputeSession(session_id)
try:
status_history = kernel.get_status_history().get("result")
print_info(f"status_history: {status_history}")
if (preparing := status_history.get("preparing")) is None:

print()
ctx.output.print_item(
status_history,
[
FieldSpec("PENDING"),
FieldSpec("SCHEDULED"),
FieldSpec("PREPARING"),
FieldSpec("RUNNING"),
FieldSpec("RUNNING_DEGRADED"),
FieldSpec("RESTARTING"),
FieldSpec("TERMINATING"),
FieldSpec("TERMINATED"),
FieldSpec("ERROR"),
],
)

if (preparing := status_history.get("PREPARING")) is None:
result = {
"result": {
"seconds": 0,
"microseconds": 0,
},
}
elif (terminated := status_history.get("terminated")) is None:
elif (terminated := status_history.get("TERMINATED")) is None:
alloc_time_until_now: timedelta = datetime.now(tzutc()) - isoparse(preparing)
result = {
"result": {
Expand Down
31 changes: 31 additions & 0 deletions src/ai/backend/manager/api/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1926,6 +1926,36 @@ async def get_container_logs(request: web.Request, params: Any) -> web.Response:
return web.json_response(resp, status=200)


@server_status_required(READ_ALLOWED)
@auth_required
@check_api_params(
t.Dict(
{
t.Key("owner_access_key", default=None): t.Null | t.String,
}
)
)
async def get_status_history(request: web.Request, params: Any) -> web.Response:
root_ctx: RootContext = request.app["_root.context"]
session_name: str = request.match_info["session_name"]
requester_access_key, owner_access_key = await get_access_key_scopes(request, params)
log.info(
"GET_STATUS_HISTORY (ak:{}/{}, s:{})", requester_access_key, owner_access_key, session_name
)
resp: dict[str, Mapping] = {"result": {}}

async with root_ctx.db.begin_readonly_session() as db_sess:
compute_session = await SessionRow.get_session(
db_sess,
session_name,
owner_access_key,
kernel_loading_strategy=KernelLoadingStrategy.MAIN_KERNEL_ONLY,
)
resp["result"] = compute_session.status_history

return web.json_response(resp, status=200)


@server_status_required(READ_ALLOWED)
@auth_required
@check_api_params(
Expand Down Expand Up @@ -2064,6 +2094,7 @@ def create_app(
app.router.add_route("GET", "/{session_name}/direct-access-info", get_direct_access_info)
)
cors.add(app.router.add_route("GET", "/{session_name}/logs", get_container_logs))
cors.add(app.router.add_route("GET", "/{session_name}/status-history", get_status_history))
cors.add(app.router.add_route("POST", "/{session_name}/rename", rename_session))
cors.add(app.router.add_route("POST", "/{session_name}/interrupt", interrupt))
cors.add(app.router.add_route("POST", "/{session_name}/complete", complete))
Expand Down
Loading