Repository navigation
Description
dstack preset create previews the offers available to the preset. If the preset doesn't set fleets, the preview requests offers for every active fleet in the project, including imported ones, and the server computes them in a way that scales with the number of fleets and blocks the event loop.
How the preview requests offers
Without fleets in the preset configuration, the CLI lists all active fleets:
dstack/src/dstack/_internal/cli/services/presets/create.py
Lines 948 to 956 in 2e2a2a2
| def _get_allowed_fleets(api: Client, configuration: PresetConfiguration) -> tuple[str, ...]: | |
| if configuration.fleets is not None: | |
| return tuple(fleet.format() for fleet in configuration.fleets) | |
| fleets = api.client.fleets.list(api.project, include_imported=True) | |
| return tuple( | |
| fleet.name if fleet.project_name == api.project else f"{fleet.project_name}/{fleet.name}" | |
| for fleet in fleets | |
| if fleet.status == FleetStatus.ACTIVE | |
| ) |
and requests a plan for a broad task with all of them, asking for 10 offers:
dstack/src/dstack/_internal/cli/services/presets/create.py
Lines 918 to 925 in 2e2a2a2
| # nvidia nor pulls image config from a registry (as in `dstack offer`). | |
| offer_configuration = TaskConfiguration(commands=[":"], image="scratch", user="root") | |
| offer_configuration.fleets = list(allowed_fleets) | |
| run_spec = RunSpec(configuration=offer_configuration, profile=None) | |
| with console.status("Getting offers..."): | |
| run_plan = api.client.runs.get_plan( | |
| api.project, run_spec, max_offers=10, for_offers_only=True | |
| ) |
Why it is expensive
With for_offers_only=True and fleets set, the server collects backend offers for each fleet separately and without a cap, so that dstack offer --fleet reports an exact total_offers:
dstack/src/dstack/_internal/server/services/runs/plan.py
Lines 1002 to 1016 in 2e2a2a2
| # TODO: Intentionally pass `max_offers_per_fleet=None` here. `dstack offer --fleet ...` | |
| # is expected to return the exact `total_offers`, so capping backend offers per selected | |
| # fleet would make that total approximate. We already deduplicate identical backend offers | |
| # while merging selected fleets via `_get_backend_offer_identity()`. Revisit adding a cap | |
| # only if this path causes real performance or memory problems. | |
| backend_offers = await get_backend_offers_in_run_candidate_fleets( | |
| session=session, | |
| project=project, | |
| run_spec=run_spec, | |
| job=job, | |
| volumes=volumes, | |
| max_offers_per_fleet=None, | |
| full_offers=full_offers, | |
| unallocated_resources=unallocated_resources, | |
| ) |
max_offers=10 only truncates the final list, since total_offers and max_price still need all matches:
dstack/src/dstack/_internal/server/services/runs/plan.py
Lines 1071 to 1076 in 2e2a2a2
| return JobPlan( | |
| job_spec=job.job_spec, | |
| offers=job_offers[: (max_offers or _DEFAULT_MAX_OFFERS)], | |
| total_offers=len(job_offers), | |
| max_price=max((offer.price for offer in job_offers), default=None), | |
| ) |
For each fleet, the server:
- runs a full pass over the offers of all backends of the fleet's project and sorts the result. With the broad preview task and a fleet without resource constraints, that's about half of the ~55k cached offers.
- deduplicates every offer against the other fleets by freezing its full
model_dump(), which takes ~35µs per offer, so ~1s per such fleet. This loop runs in the coroutine, so it blocks the event loop for that time. - copies the merged list of all offers collected so far.
dstack/src/dstack/_internal/server/services/runs/plan.py
Lines 936 to 955 in 2e2a2a2
| seen_offer_identities = set() | |
| offers: list[InstanceOfferWithAvailability] = [] | |
| for candidate_fleet_model in candidate_fleet_models: | |
| offers_from_fleet = [] | |
| for offer in await _get_backend_offers_in_fleet( | |
| project=project, | |
| fleet_model=candidate_fleet_model, | |
| run_spec=run_spec, | |
| job=job, | |
| volumes=volumes, | |
| max_offers=max_offers_per_fleet, | |
| full_offers=full_offers, | |
| unallocated_resources=unallocated_resources, | |
| ): | |
| offer_identity = _get_backend_offer_identity(offer) | |
| if offer_identity not in seen_offer_identities: | |
| offers_from_fleet.append(offer) | |
| seen_offer_identities.add(offer_identity) | |
| offers = list(heapq.merge(offers, offers_from_fleet, key=lambda o: o.price)) | |
| return offers |
Fleets that share backends produce the same offers, but each fleet's offers are fetched and identified again, so the cost grows linearly with the number of fleets. With 40 fleets without resource constraints, a preview of 10 offers would take an estimated ~40s of server time, most of it blocking the event loop. Fleets with resource constraints match fewer offers and cost less, and SSH fleets produce no backend offers.
The same path serves dstack offer --fleet, dstack offer --group-by with --fleet, and the fleet filter on the UI Offers page, but only for the fleets the user selects.
Proposed changes
- Move the deduplication off the event loop, and compute a cheap offer identity from a few fields instead of the full
model_dump(). - Don't pass every fleet from the preset preview when the preset doesn't restrict fleets, or cap the offers the preview requests.
- Query offers once per distinct set of backends and check each selected fleet's constraints on the result, instead of querying per fleet.
Related: #4519.
Activity