Skip to content

fix(BA-4282): Remove kernel row when purge scaling group preventing fk error#8614

Open
seedspirit wants to merge 2 commits intomainfrom
fix/BA-4282
Open

fix(BA-4282): Remove kernel row when purge scaling group preventing fk error#8614
seedspirit wants to merge 2 commits intomainfrom
fix/BA-4282

Conversation

@seedspirit
Copy link
Contributor

resolves #8613 (BA-4282)

Checklist: (if applicable)

  • Milestone metadata specifying the target backport version
  • Mention to the original issue
  • Installer updates including:
    • Fixtures for db schema changes
    • New mandatory config options
  • Update of end-to-end CLI integration tests in ai.backend.test
  • API server-client counterparts (e.g., manager API -> client SDK)
  • Test case(s) to:
    • Demonstrate the difference of before/after
    • Demonstrate the flow of abstract/conceptual models with a concrete implementation
  • Documentation
    • Contents in the docs directory
    • docstrings in public interfaces and type annotations

@seedspirit seedspirit marked this pull request as ready for review February 5, 2026 09:55
Copilot AI review requested due to automatic review settings February 5, 2026 09:55
@github-actions github-actions bot added size:L 100~500 LoC comp:manager Related to Manager component labels Feb 5, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request fixes a foreign key constraint violation (IntegrityError) that occurs when purging a scaling group that has sessions with associated kernels. The issue was that the cascade deletion sequence was attempting to delete sessions before deleting their associated kernels, which violates the fk_kernels_session_id_sessions foreign key constraint (which uses the default RESTRICT behavior).

Changes:

  • Added kernel deletion step in the cascade deletion sequence for purge_scaling_group
  • Added comprehensive test to verify purging scaling groups with the full FK hierarchy (scaling group → session → kernel + endpoint → route)

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/ai/backend/manager/repositories/scaling_group/db_source/db_source.py Added deletion of KernelRow records between endpoint and session deletion to prevent FK constraint violations
tests/unit/manager/repositories/scaling_group/test_scaling_group_repository.py Added test fixtures and test case to verify purging works correctly with the full entity hierarchy including kernels

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1339 to +1343
async with db_with_cleanup.begin_readonly_session() as db_sess:
sg_result = await db_sess.execute(
sa.select(ScalingGroupRow).where(ScalingGroupRow.name == sgroup_name)
)
assert sg_result.scalar_one_or_none() is None
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test only verifies that the scaling group is deleted, but doesn't explicitly verify that all child records (sessions, kernels, endpoints, routes) are also deleted. While the deletion of the scaling group indirectly proves these were deleted (otherwise there would be FK constraint violations), it would be more comprehensive to explicitly verify that each child record type is deleted. Consider adding assertions to check that no sessions, kernels, endpoints, or routes remain after the purge operation.

Copilot uses AI. Check for mistakes.
Comment on lines 132 to 133
"""Purges a scaling group and all related sessions and routes using a purger.

Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cascade delete order documentation was removed from the docstring. While the implementation steps are now documented inline with comments, it would be helpful to keep a high-level mention of the deletion order in the docstring since this is critical behavior that prevents FK constraint violations. Consider adding back a brief note like "Deletes in order: routes, endpoints, kernels, sessions, then scaling group" to help future developers understand the method's behavior at a glance.

Suggested change
"""Purges a scaling group and all related sessions and routes using a purger.
"""Purges a scaling group and all related sessions, routes, endpoints, and kernels.
Deletes in order: routes, endpoints, kernels, sessions, then scaling group.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:manager Related to Manager component size:L 100~500 LoC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

purge_scaling_group fails with IntegrityError when scaling group has sessions with kernels

1 participant