-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathrun.py
More file actions
277 lines (240 loc) · 11.7 KB
/
Copy pathrun.py
File metadata and controls
277 lines (240 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""Use case: run (enqueue a job for) a Qiskit Function."""
import json
import logging
from django.conf import settings
from django.contrib.auth.models import AbstractUser, Group
from django.db import transaction
from api.access_policies.jobs import JobAccessPolicies
from api.domain.exceptions.active_job_limit_exceeded_exception import ActiveJobLimitExceeded
from api.domain.exceptions.function_configuration_exception import FunctionConfigurationException
from api.domain.exceptions.function_disabled_exception import FunctionDisabledException
from api.domain.exceptions.function_not_found_exception import FunctionNotFoundException
from api.use_cases.programs.run_input import RunFunctionInput
from api.use_cases.programs.runner_config import RunnerConfig
from api.use_cases.programs.validate_arguments import validate_arguments
from api.utils import active_jobs_limit_reached, build_env_variables
from core.domain.authorization.function_access_result import FunctionAccessResult
from core.domain.business_models import BusinessModel
from core.model_managers.job_events import JobEventContext, JobEventOrigin
from core.models import (
ComputeProfile,
FunctionSize,
Job,
JobConfig,
JobEvent,
Program as Function,
PLATFORM_PERMISSION_RUN,
RUN_PROGRAM_PERMISSION,
)
from core.services.storage import get_arguments_storage
from core.utils import encrypt_env_vars
logger = logging.getLogger("api.api.use_cases.programs.run")
def _is_trial(function: Function, user) -> bool:
# Single EXISTS query instead of N+1: iterating two unevaluated QuerySets
# triggers one query per group membership check.
user_run_groups = Group.objects.filter(user=user, permissions__codename=RUN_PROGRAM_PERMISSION)
return function.trial_instances.filter(pk__in=user_run_groups).exists()
def _config_for_profile_id(compute_profile: str, *, size_source: str) -> RunnerConfig:
"""Build a Fleets RunnerConfig from a bare compute profile id.
The id must name a registered ``ComputeProfile`` row; a missing row is a
deployment misconfiguration and we reject the job rather than store a null FK.
No ``FunctionSize`` row backs a profile resolved this way (the deprecated
``compute_profile`` input or the deployment default), so ``function_size``
is null; ``size_source`` records which of those it was.
"""
compute_profile_fk = ComputeProfile.objects.get_by_id(compute_profile)
if compute_profile_fk is None:
raise FunctionConfigurationException(
f"Compute profile '{compute_profile}' is not registered. Contact administrator."
)
return RunnerConfig(
compute_profile=compute_profile,
gpu=False,
compute_profile_fk=compute_profile_fk,
size_source=size_source,
function_size=None,
)
def _get_runner_config(
function: Function,
compute_profile_requested: str | None,
function_size_requested: str | None,
) -> RunnerConfig:
"""Resolve the compute profile and sizing provenance for a run.
``compute_profile_fk`` is the source of truth for the profile a job runs at
and is what gets stored on the job. A Fleets job always resolves to a profile;
if no ``ComputeProfile`` row is registered for it, that is a deployment
misconfiguration and we reject the job rather than store a null FK. Ray is not
profiled (profiles are a Fleets concept), so its FK stays null.
Because the size determines the compute profile (and not the reverse -- two
sizes can map to one profile), the returned :class:`RunnerConfig` also records
``size_source`` (how sizing was chosen) and, where one applies, the
``FunctionSize`` row itself, so a stored job stays distinguishable.
Sizing precedence (Fleets):
1. Both ``function_size`` and ``compute_profile`` -> rejected as ambiguous.
2. ``function_size`` -> resolved through the function's ``FunctionSize``
catalog (source REQUESTED); an undeclared size is rejected.
3. ``compute_profile`` (deprecated) -> used as-is (source COMPUTE_PROFILE).
4. Neither -> the function's ``default_size`` (source DEFAULT_SIZE), else
``settings.DEFAULT_COMPUTE_PROFILE`` (source SETTINGS_DEFAULT).
Both requested values are expected already normalized by the view:
``compute_profile`` to bare (prefix-less) form, ``function_size`` to its
canonical (strip+casefold) label.
Raises:
FunctionConfigurationException: on ambiguous input, an undeclared size, or
a resolved profile with no registered row.
"""
# Ambiguous input is always a 400, whatever the runner, so check before the
# Ray short-circuit.
if compute_profile_requested and function_size_requested:
raise FunctionConfigurationException(
"Provide either 'function_size' or 'compute_profile', not both. "
"'compute_profile' is deprecated; prefer 'function_size'."
)
if function.runner != Function.FLEETS:
# Ray / GPU: sizes and profiles do not apply; both requested values are ignored.
gpu = bool(function.provider and function.gpu)
return RunnerConfig(
compute_profile=None,
gpu=gpu,
compute_profile_fk=None,
size_source=Job.SIZE_SOURCE_NONE,
function_size=None,
)
# (2) An explicitly requested size resolves through the function's catalog.
# Fetch the FunctionSize row itself so we can record it (billing keys off the
# size tier); the compute profile comes from that same row.
if function_size_requested:
function_size = FunctionSize.objects.get_function_size(function, function_size_requested)
if function_size is None:
available = sorted(FunctionSize.objects.function_sizes(function).values_list("function_size", flat=True))
available_msg = ", ".join(available) if available else "this function declares no sizes."
raise FunctionConfigurationException(
f"Unknown function size '{function_size_requested}' for this function. "
f"Available sizes: {available_msg}"
)
profile = function_size.compute_profile
return RunnerConfig(
compute_profile=profile.compute_profile_id,
gpu=False,
compute_profile_fk=profile,
size_source=Job.SIZE_SOURCE_REQUESTED,
function_size=function_size,
)
# (3) Deprecated explicit compute profile.
if compute_profile_requested:
logger.warning(
"program=%s | 'compute_profile' is deprecated; use 'function_size'.",
function.title,
)
return _config_for_profile_id(compute_profile_requested, size_source=Job.SIZE_SOURCE_COMPUTE_PROFILE)
# (4a) Nothing requested: the function's default size.
if function.default_size_id:
function_size = function.default_size
profile = function_size.compute_profile
return RunnerConfig(
compute_profile=profile.compute_profile_id,
gpu=False,
compute_profile_fk=profile,
size_source=Job.SIZE_SOURCE_DEFAULT_SIZE,
function_size=function_size,
)
# (4b) No default size either: the deployment-wide default profile.
return _config_for_profile_id(settings.DEFAULT_COMPUTE_PROFILE, size_source=Job.SIZE_SOURCE_SETTINGS_DEFAULT)
class RunFunctionUseCase:
"""Use case for running (enqueueing a job for) a Qiskit Function."""
def execute( # pylint: disable=too-many-locals, too-many-branches
self,
user: AbstractUser,
accessible_functions: FunctionAccessResult,
data: RunFunctionInput,
) -> Job:
"""Enqueue a job for the specified Qiskit Function.
Raises FunctionNotFoundException or FunctionDisabledException as appropriate.
"""
function = None
if data.provider_name:
function = Function.objects.get_function_by_permission(
user=user,
function_title=data.title,
provider_name=data.provider_name,
accessible_functions=accessible_functions,
permission=PLATFORM_PERMISSION_RUN,
legacy_permission_name=RUN_PROGRAM_PERMISSION,
)
else:
if JobAccessPolicies.can_create(user=user, accessible_functions=accessible_functions):
function = Function.objects.get_user_function(user, data.title)
if function is None:
raise FunctionNotFoundException(function=data.title, provider=data.provider_name)
if function.disabled:
message = function.disabled_message if function.disabled_message else Function.DEFAULT_DISABLED_MESSAGE
raise FunctionDisabledException(message=message)
if active_jobs_limit_reached(user):
raise ActiveJobLimitExceeded()
if function.runner == Function.FLEETS:
message = None
if not function.code_engine_project:
message = "Program has no Code Engine project assigned. Contact administrator."
elif not function.code_engine_project.active:
message = (
f"Code Engine project '{function.code_engine_project.project_name}' assigned to "
"this function is not active. Contact administrator."
)
if message:
logger.warning("user_id=%s program=%s | %s", user.id, function.title, message)
raise FunctionConfigurationException(message)
validate_arguments(function, data.arguments)
logger.info("user_id=%s program=%s | Creating job", user.id, function.title)
business_model = None
if data.provider_name and not accessible_functions.use_legacy_authorization:
business_model = accessible_functions.get_function(data.provider_name, data.title).business_model
if business_model is None:
trial = _is_trial(function, user)
business_model = BusinessModel.TRIAL if trial else BusinessModel.LICENSED
else:
trial = business_model == BusinessModel.TRIAL
runner_config = _get_runner_config(function, data.compute_profile, data.function_size)
job = Job(
trial=trial,
business_model=business_model,
status=Job.QUEUED,
program=function,
author=user,
gpu=runner_config.gpu,
runner=function.runner,
compute_profile_fk=runner_config.compute_profile_fk,
size_source=runner_config.size_source,
function_size=runner_config.function_size,
instance_crn=data.instance,
account_id=data.account_id,
ce_project_name=function.code_engine_project.project_name if function.code_engine_project else None,
ce_region=function.code_engine_project.region if function.code_engine_project else None,
)
env = encrypt_env_vars(
build_env_variables(
channel=data.channel,
token=data.token,
job=job,
trial_mode=trial,
instance=data.instance,
)
)
try:
env["traceparent"] = data.carrier["traceparent"]
except KeyError:
pass
if function.env_vars:
env.update(json.loads(function.env_vars))
job.env_vars = json.dumps(env)
get_arguments_storage(job).save(data.arguments)
with transaction.atomic():
if data.config_data:
job.config = JobConfig.objects.create(**data.config_data)
job.save()
JobEvent.objects.add_status_event(
job_id=job.id,
origin=JobEventOrigin.API,
context=JobEventContext.RUN_PROGRAM,
status=job.status,
)
return job