-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathtask_config_policy.go
319 lines (281 loc) · 11.4 KB
/
task_config_policy.go
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package configpolicy
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"github.com/labstack/gommon/log"
"github.com/determined-ai/determined/master/internal/rm"
"github.com/determined-ai/determined/master/pkg/model"
"github.com/determined-ai/determined/master/pkg/schemas"
"github.com/determined-ai/determined/master/pkg/schemas/expconf"
)
// ExperimentConfigPolicies is the invariant config and constraints for an experiment.
// Submitted experiments whose config fields vary from the respective InvariantConfig fields set
// within a given scope are silently overridden.
// Submitted experiments whose constraint fields vary from the respective Constraint fields set
// within a given scope are rejected.
type ExperimentConfigPolicies struct {
InvariantConfig *expconf.ExperimentConfig `json:"invariant_config"`
Constraints *model.Constraints `json:"constraints"`
}
// NTSCConfigPolicies is the invariant config and constraints for an NTSC task.
// Submitted NTSC tasks whose config fields vary from the respective InvariantConfig fields set
// within a given scope are silently overridden.
// Submitted NTSC tasks whose constraint fields vary from the respective Constraint fields set
// within a given scope are rejected.
type NTSCConfigPolicies struct {
InvariantConfig *model.CommandConfig `json:"invariant_config"`
Constraints *model.Constraints `json:"constraints"`
}
var (
errPriorityConstraintFailure = errors.New("submitted workload failed priority constraint")
errResourceConstraintFailure = errors.New("submitted workload failed a resource constraint")
errPriorityImmutable = errors.New("priority cannot be modified")
)
// CheckNTSCConstraints returns an error if the NTSC config fails constraint checks.
func CheckNTSCConstraints(
ctx context.Context,
workspaceID int,
workloadConfig model.CommandConfig,
resourceManager rm.ResourceManager,
) error {
constraints, err := GetMergedConstraints(ctx, workspaceID, model.NTSCType)
if err != nil {
return err
}
if constraints.ResourceConstraints != nil && constraints.ResourceConstraints.MaxSlots != nil {
if err = checkSlotsConstraint(*constraints.ResourceConstraints.MaxSlots, workloadConfig.Resources.Slots,
workloadConfig.Resources.MaxSlots); err != nil {
return err
}
}
// For each submitted constraint, check if the workload config is within allowed values.
// rm.SmallerValueIsHigherPriority only returns an error if task priority is not implemented for that resource manager.
// In that case, there is no need to check if requested priority is within limits.
smallerHigher, err := resourceManager.SmallerValueIsHigherPriority()
if err == nil {
if err = checkPriorityConstraint(smallerHigher, constraints.PriorityLimit,
workloadConfig.Resources.Priority); err != nil {
return err
}
}
return nil
}
// CheckExperimentConstraints returns an error if the NTSC config fails constraint checks.
func CheckExperimentConstraints(
ctx context.Context,
workspaceID int,
workloadConfig expconf.ExperimentConfigV0,
resourceManager rm.ResourceManager,
) error {
constraints, err := GetMergedConstraints(ctx, workspaceID, model.ExperimentType)
if err != nil {
return err
}
if constraints.ResourceConstraints != nil && constraints.ResourceConstraints.MaxSlots != nil {
// users cannot specify number of slots for an experiment
slotsRequest := *constraints.ResourceConstraints.MaxSlots
if err = checkSlotsConstraint(*constraints.ResourceConstraints.MaxSlots, slotsRequest,
workloadConfig.Resources().MaxSlots()); err != nil {
return err
}
}
// For each submitted constraint, check if the workload config is within allowed values.
// rm.SmallerValueIsHigherPriority only returns an error if task priority is not implemented for that resource manager.
// In that case, there is no need to check if requested priority is within limits.
smallerHigher, err := resourceManager.SmallerValueIsHigherPriority()
if err == nil {
if err = checkPriorityConstraint(smallerHigher, constraints.PriorityLimit,
workloadConfig.Resources().Priority()); err != nil {
return err
}
}
return nil
}
func checkPriorityConstraint(smallerHigher bool, priorityLimit *int, priorityRequest *int) error {
if priorityLimit == nil || priorityRequest == nil {
return nil
}
if !priorityWithinLimit(*priorityRequest, *priorityLimit, smallerHigher) {
return fmt.Errorf("requested priority [%d] exceeds limit set by admin [%d]: %w",
*priorityRequest, *priorityLimit, errPriorityConstraintFailure)
}
return nil
}
func checkSlotsConstraint(slotsLimit int, slotsRequest int, maxSlotsRequest *int) error {
if slotsLimit < slotsRequest {
return fmt.Errorf("requested resources.slots [%d] exceeds limit set by admin [%d]: %w",
slotsRequest, slotsLimit, errResourceConstraintFailure)
}
if maxSlotsRequest != nil {
if slotsLimit < *maxSlotsRequest {
return fmt.Errorf("requested resources.max_slots [%d] exceeds limit set by admin [%d]: %w",
*maxSlotsRequest, slotsLimit, errResourceConstraintFailure)
}
}
return nil
}
// GetMergedConstraints retrieves Workspace and Global constraints and returns a merged result.
// workloadType is expected to be model.ExperimentType or model.NTSCType.
func GetMergedConstraints(ctx context.Context, workspaceID int, workloadType string) (*model.Constraints, error) {
// Workspace-level constraints should be over-ridden by global contraints, if set.
var constraints model.Constraints
wkspConfigPolicies, err := GetTaskConfigPolicies(ctx, &workspaceID, workloadType)
if err != nil {
return nil, err
}
if wkspConfigPolicies.Constraints != nil {
if err = json.Unmarshal([]byte(*wkspConfigPolicies.Constraints), &constraints); err != nil {
return nil, fmt.Errorf("unable to merge workspace and global constraints: %w", err)
}
}
globalConfigPolicies, err := GetTaskConfigPolicies(ctx, nil, workloadType)
if err != nil {
return nil, err
}
if globalConfigPolicies.Constraints != nil {
if err = json.Unmarshal([]byte(*globalConfigPolicies.Constraints), &constraints); err != nil {
return nil, fmt.Errorf("unable to merge workspace and global constraints: %w", err)
}
}
return &constraints, nil
}
// MergeWithInvariantExperimentConfigs merges the config with workspace and global invariant
// configs, where a global invariant config takes precedence over a workspace-level invariant
// config.
func MergeWithInvariantExperimentConfigs(ctx context.Context, workspaceID int,
config expconf.ExperimentConfigV0,
) (*expconf.ExperimentConfigV0, error) {
originalConfig := config
var wkspOverride, globalOverride bool
wkspConfigPolicies, err := GetTaskConfigPolicies(ctx, &workspaceID, model.ExperimentType)
if err != nil {
return nil, err
}
if wkspConfigPolicies.InvariantConfig != nil {
var tempConfig expconf.ExperimentConfigV0
if err := json.Unmarshal([]byte(*wkspConfigPolicies.InvariantConfig), &tempConfig); err != nil {
return nil, fmt.Errorf("error unmarshaling workspace invariant config: %w", err)
}
// Merge arrays and maps with those specified in the user-submitted config.
config = schemas.Merge(tempConfig, config)
wkspOverride = true
}
globalConfigPolicies, err := GetTaskConfigPolicies(ctx, nil, model.ExperimentType)
if err != nil {
return nil, err
}
if globalConfigPolicies.InvariantConfig != nil {
var tempConfig expconf.ExperimentConfigV0
err = json.Unmarshal([]byte(*globalConfigPolicies.InvariantConfig), &tempConfig)
if err != nil {
return nil, fmt.Errorf("error unmarshaling global invariant config: %w", err)
}
// Merge arrays and maps with those specified in the current (user-submitted combined with
// optionally set workspace invariant) config.
config = schemas.Merge(tempConfig, config)
globalOverride = true
}
scope := ""
if wkspOverride {
if globalOverride {
scope += "workspace and global"
} else {
scope += "workspace"
}
} else if globalOverride {
scope += "global"
}
if !reflect.DeepEqual(originalConfig, config) {
log.Warnf("some fields were overridden by admin %s config policies", scope)
}
return &config, nil
}
func findAllowedPriority(scope *int, workloadType string) (limit int, exists bool, err error) {
configPolicies, err := GetTaskConfigPolicies(context.TODO(), scope, workloadType)
if err != nil {
return 0, false, fmt.Errorf("unable to fetch task config policies: %w", err)
}
// Cannot update priority if priority set in invariant config.
if configPolicies.InvariantConfig != nil {
switch workloadType {
case model.NTSCType:
var configs model.CommandConfig
err = json.Unmarshal([]byte(*configPolicies.InvariantConfig), &configs)
if err != nil {
return 0, false, fmt.Errorf("unable to unmarshal task config policies: %w", err)
}
if configs.Resources.Priority != nil {
adminPriority := *configs.Resources.Priority
return adminPriority, false,
fmt.Errorf("priority set by invariant config: %w", errPriorityImmutable)
}
case model.ExperimentType:
var configs expconf.ExperimentConfigV0
err = json.Unmarshal([]byte(*configPolicies.InvariantConfig), &configs)
if err != nil {
return 0, false, fmt.Errorf("unable to unmarshal task config policies: %w", err)
}
if configs.Resources().Priority() != nil {
adminPriority := *configs.Resources().Priority()
return adminPriority, false,
fmt.Errorf("priority set by invariant config: %w", errPriorityImmutable)
}
default:
return 0, false, fmt.Errorf("workload type %s not supported", workloadType)
}
}
// Find priority constraint, if set.
var constraints model.Constraints
if configPolicies.Constraints != nil {
if err = json.Unmarshal([]byte(*configPolicies.Constraints), &constraints); err != nil {
return 0, false, fmt.Errorf("unable to unmarshal task config policies: %w", err)
}
if constraints.PriorityLimit != nil {
return *constraints.PriorityLimit, true, nil
}
}
return 0, false, nil
}
// PriorityUpdateAllowed returns true if the desired priority is within the task config policy limit.
func PriorityUpdateAllowed(wkspID int, workloadType string, priority int, smallerHigher bool) (bool, error) {
// Check if a priority limit has been set with a constraint policy.
// Global policies have highest precedence.
globalLimit, globalExists, err := findAllowedPriority(nil, workloadType)
if errors.Is(err, errPriorityImmutable) && globalLimit == priority {
// If task config policies have updated since the workload was originally scheduled, allow users
// to update the priority to the new priority set by invariant config.
return true, nil
}
if err != nil {
return false, err
}
// TODO use COALESCE instead once postgres updates are complete.
// Workspace policies have second precedence.
wkspLimit, wkspExists, err := findAllowedPriority(&wkspID, workloadType)
if errors.Is(err, errPriorityImmutable) && wkspLimit == priority {
// If task config policies have updated since the workload was originally scheduled, allow users
// to update the priority to the new priority set by invariant config.
return true, nil
}
if err != nil {
return false, err
}
// No invariant configs. Check for constraints.
if globalExists {
return priorityWithinLimit(priority, globalLimit, smallerHigher), nil
}
if wkspExists {
return priorityWithinLimit(priority, wkspLimit, smallerHigher), nil
}
// No priority limit has been set.
return true, nil
}
func priorityWithinLimit(userPriority int, adminLimit int, smallerHigher bool) bool {
if smallerHigher {
return userPriority >= adminLimit
}
return userPriority <= adminLimit
}