Skip to content

Delete dull job and instance models created on no capacity retry#4059

Open
r4victor wants to merge 4 commits into
masterfrom
issue_4054_job_models_limit
Open

Delete dull job and instance models created on no capacity retry#4059
r4victor wants to merge 4 commits into
masterfrom
issue_4054_job_models_limit

Conversation

@r4victor

@r4victor r4victor commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

Fixes #4054

Prevent the accumulation of large number of job and instance models when run retries on no capacity:

  • Hard-delete placeholder instances when terminating jobs that FAILED_TO_START_DUE_TO_NO_CAPACITY.
  • Hard-delete old FAILED_TO_START_DUE_TO_NO_CAPACITY job models when creating new job submissions (for pending runs and retrying failed replicas). Only the latest failed job is preserved to show retrying status correctly.
  • Add runs to events about potentially never-provisioned jobs so that events are displayed with within_runs filter. Event targets now store entity_run_id so that within_runs filter works even for job event when job models are deleted. entity_run_id is backfilled for existing runs and jobs.

Note: the cleanup is for FAILED_TO_START_DUE_TO_NO_CAPACITY jobs that a) carry no useful information and b) created in large quantities on retry. Job models failed due to other reasons are not cleaned up.

@r4victor r4victor changed the title Issue 4054 job models limit Delete dull job and instance models created on no capacity retry Jul 22, 2026
@r4victor
r4victor requested a review from jvstme July 22, 2026 09:56
Comment on lines +27 to +41
op.execute(
"""
UPDATE event_targets SET entity_run_id = entity_id
WHERE entity_type = 'run' AND entity_run_id IS NULL
"""
)
op.execute(
"""
UPDATE event_targets SET entity_run_id = jobs.run_id
FROM jobs
WHERE jobs.id = event_targets.entity_id
AND event_targets.entity_type = 'job'
AND event_targets.entity_run_id IS NULL
"""
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Enums like entity_type hold uppercase values

$ sqlite3 ~/.dstack/server/data/sqlite.db "SELECT DISTINCT entity_type FROM event_targets;"
FLEET
GATEWAY
INSTANCE
JOB
PROJECT
RUN
SECRET
USER
VOLUME

Comment on lines 346 to 363
if within_fleets is not None:
query = select(InstanceModel.id).where(InstanceModel.fleet_id.in_(within_fleets))
res = await session.execute(query)
# In Postgres, fetching instance IDs separately is orders of magnitude faster
# than using a subquery.
instance_ids = list(res.unique().scalars().all())
target_filters.append(
or_(
and_(
EventTargetModel.entity_type == EventTargetType.FLEET,
EventTargetModel.entity_id.in_(within_fleets),
),
and_(
EventTargetModel.entity_type == EventTargetType.INSTANCE,
EventTargetModel.entity_id.in_(instance_ids),
),
)
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Event targets now store entity_run_id so that within_runs filter works even for job event when job models are deleted

Should we also introduce entity_fleet_id to be able to see deleted placeholder instance events when using the within_fleets filter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Slow /api/runs/list due to the large number of job submissions

2 participants