-
Notifications
You must be signed in to change notification settings - Fork 127
/
dbops.go
484 lines (419 loc) · 13.2 KB
/
dbops.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package scheduleringester
import (
"time"
"github.com/apache/pulsar-client-go/pulsar"
"github.com/google/uuid"
"golang.org/x/exp/maps"
schedulerdb "github.com/armadaproject/armada/internal/scheduler/database"
)
// DbOperationsWithMessageIds bundles a sequence of schedulerdb ops with the ids of all Pulsar
// messages that were consumed to produce it.
type DbOperationsWithMessageIds struct {
Ops []DbOperation
MessageIds []pulsar.MessageID
}
func (d *DbOperationsWithMessageIds) GetMessageIDs() []pulsar.MessageID {
return d.MessageIds
}
type JobRunFailed struct {
LeaseReturned bool
RunAttempted bool
FailureTime time.Time
}
type JobSchedulingInfoUpdate struct {
JobSchedulingInfo []byte
JobSchedulingInfoVersion int32
}
type JobSetCancelAction struct {
cancelQueued bool
cancelLeased bool
}
type JobSetKey struct {
queue string
jobSet string
}
type JobReprioritiseKey struct {
JobSetKey
Priority int64
}
type JobRunDetails struct {
Queue string
DbRun *schedulerdb.Run
}
type JobQueuedStateUpdate struct {
Queued bool
QueuedStateVersion int32
}
// DbOperation captures a generic batch database operation.
//
// There are 5 types of operations:
// - Insert jobs (i.e., add new jobs to the schedulerdb).
// - Insert runs (i.e., add new runs to the schedulerdb).
// - Job set operations (i.e., modify all jobs and runs in the schedulerdb part of a given job set).
// - Job operations (i.e., modify particular jobs).
// - Job run operations (i.e., modify particular runs).
//
// To improve performance, several ops can be merged into a single op if of the same type.
// To increase the number of ops that can be merged, ops can sometimes be reordered.
//
// Specifically, an op can be applied before another if:
// - Insert jobs: if prior op doesn't affect the job set.
// - Insert runs: if prior op doesn't affect the job set or defines the corresponding job.
// - Job set operations: if not affecting a job defined in prior op.
// - Job operations: if not affecting a job defined in a prior op.
// - Job run operations: if not affecting a run defined in a prior op.
//
// In addition, UpdateJobPriorities can never be applied beforee UpdateJobSetPriorities
// and vice versa, since one may overwrite values set by the other.
type DbOperation interface {
// a.Merge(b) attempts to merge b into a, creating a single combined op.
// Returns true if merging was successful.
// If successful, modifies a in-place.
// If not successful, neither op is mutated.
Merge(DbOperation) bool
// a.CanBeAppliedBefore(b) returns true if a can be placed before b
// without changing the end result of the overall set of operations.
CanBeAppliedBefore(DbOperation) bool
}
// AppendDbOperation appends a sql operation,
// possibly merging it with a previous operation if that can be done in such a way
// that the end result of applying the entire sequence of operations is unchanged.
func AppendDbOperation(ops []DbOperation, op DbOperation) []DbOperation {
ops = append(ops, op)
for i := len(ops) - 1; i > 0; i-- {
if ops[i-1] == nil || ops[i] == nil {
continue
}
if ops[i-1].Merge(ops[i]) {
ops[i] = nil
break
} else if ops[i].CanBeAppliedBefore(ops[i-1]) {
ops[i-1], ops[i] = ops[i], ops[i-1]
} else {
break
}
}
return discardNilOps(ops)
}
func discardNilOps(ops []DbOperation) []DbOperation {
rv := make([]DbOperation, 0, len(ops))
for _, op := range ops {
if op != nil {
rv = append(rv, op)
}
}
return rv
}
type (
InsertJobs map[string]*schedulerdb.Job
InsertRuns map[uuid.UUID]*JobRunDetails
UpdateJobSetPriorities map[JobSetKey]int64
MarkJobSetsCancelRequested map[JobSetKey]*JobSetCancelAction
MarkJobsCancelRequested map[JobSetKey][]string
MarkJobsCancelled map[string]time.Time
MarkJobsSucceeded map[string]bool
MarkJobsFailed map[string]bool
UpdateJobSchedulingInfo map[string]*JobSchedulingInfoUpdate
UpdateJobQueuedState map[string]*JobQueuedStateUpdate
MarkRunsSucceeded map[uuid.UUID]time.Time
MarkRunsFailed map[uuid.UUID]*JobRunFailed
MarkRunsForJobPreemptRequested map[JobSetKey][]string
MarkRunsRunning map[uuid.UUID]time.Time
MarkRunsPending map[uuid.UUID]time.Time
MarkRunsPreempted map[uuid.UUID]time.Time
InsertJobRunErrors map[uuid.UUID]*schedulerdb.JobRunError
UpdateJobPriorities struct {
key JobReprioritiseKey
jobIds []string
}
MarkJobsValidated map[string][]string
InsertPartitionMarker struct {
markers []*schedulerdb.Marker
}
)
type JobSetOperation interface {
AffectsJobSet(queue string, jobSet string) bool
}
func (a UpdateJobSetPriorities) AffectsJobSet(queue string, jobSet string) bool {
_, ok := a[JobSetKey{queue: queue, jobSet: jobSet}]
return ok
}
func (a MarkJobSetsCancelRequested) AffectsJobSet(queue string, jobSet string) bool {
_, ok := a[JobSetKey{queue: queue, jobSet: jobSet}]
return ok
}
func (a InsertJobs) Merge(b DbOperation) bool {
return mergeInMap(a, b)
}
func (a InsertRuns) Merge(b DbOperation) bool {
return mergeInMap(a, b)
}
func (a UpdateJobSetPriorities) Merge(b DbOperation) bool {
return mergeInMap(a, b)
}
func (a MarkJobSetsCancelRequested) Merge(b DbOperation) bool {
return mergeInMap(a, b)
}
func (a MarkJobsCancelRequested) Merge(b DbOperation) bool {
return mergeListMaps(a, b)
}
func (a MarkRunsForJobPreemptRequested) Merge(b DbOperation) bool {
return mergeListMaps(a, b)
}
func (a UpdateJobSchedulingInfo) Merge(b DbOperation) bool {
switch op := b.(type) {
case UpdateJobSchedulingInfo:
for key, value := range op {
aValue, present := a[key]
if !present {
a[key] = value
} else {
if value.JobSchedulingInfoVersion > aValue.JobSchedulingInfoVersion {
a[key] = value
}
}
}
return true
}
return false
}
func (a UpdateJobQueuedState) Merge(b DbOperation) bool {
switch op := b.(type) {
case UpdateJobQueuedState:
for key, value := range op {
currentValue, present := a[key]
if !present {
a[key] = value
} else {
if value.QueuedStateVersion > currentValue.QueuedStateVersion {
a[key] = value
}
}
}
return true
}
return false
}
func (a MarkJobsCancelled) Merge(b DbOperation) bool {
return mergeInMap(a, b)
}
func (a MarkJobsSucceeded) Merge(b DbOperation) bool {
return mergeInMap(a, b)
}
func (a MarkJobsFailed) Merge(b DbOperation) bool {
return mergeInMap(a, b)
}
func (a *UpdateJobPriorities) Merge(b DbOperation) bool {
switch op := b.(type) {
case *UpdateJobPriorities:
if a.key == op.key {
a.jobIds = append(a.jobIds, op.jobIds...)
return true
}
}
return false
}
func (a MarkRunsSucceeded) Merge(b DbOperation) bool {
return mergeInMap(a, b)
}
func (a MarkRunsFailed) Merge(b DbOperation) bool {
return mergeInMap(a, b)
}
func (a MarkRunsRunning) Merge(b DbOperation) bool {
return mergeInMap(a, b)
}
func (a MarkRunsPending) Merge(b DbOperation) bool {
return mergeInMap(a, b)
}
func (a MarkRunsPreempted) Merge(b DbOperation) bool {
return mergeInMap(a, b)
}
func (a InsertJobRunErrors) Merge(b DbOperation) bool {
return mergeInMap(a, b)
}
func (a MarkJobsValidated) Merge(b DbOperation) bool {
return mergeInMap(a, b)
}
func (a *InsertPartitionMarker) Merge(b DbOperation) bool {
switch op := b.(type) {
case *InsertPartitionMarker:
a.markers = append(a.markers, op.markers...)
return true
}
return false
}
// mergeInMap merges an op b into a, provided that b is of the same type as a.
// For example, if a is of type MarkJobSetsCancelRequested, b is only merged if also of type MarkJobSetsCancelRequested.
// Returns true if the ops were merged and false otherwise.
func mergeInMap[M ~map[K]V, K comparable, V any](a M, b DbOperation) bool {
// Using a type switch here, since using a type assertion
// (which should also work in theory) crashes the go1.19 compiler.
switch op := b.(type) {
case M:
maps.Copy(a, op)
return true
}
return false
}
// mergeListMaps merges an op b into a, provided that b is of the same type as a.
// If merged, the resulting map will contain all keys from a and b
// In case both a and b have the same key, the values for that key will be combined
// Returns true if the ops were merged and false otherwise.
func mergeListMaps[M ~map[K][]V, K comparable, V any](a M, b DbOperation) bool {
// Using a type switch here, since using a type assertion
// (which should also work in theory) crashes the go1.19 compiler.
switch op := b.(type) {
case M:
for k, v := range op {
if _, present := a[k]; present {
a[k] = append(a[k], v...)
} else {
a[k] = v
}
}
return true
}
return false
}
func (a InsertJobs) CanBeAppliedBefore(b DbOperation) bool {
// We don't check for job and run ops here,
// since job and run ops can never appear before the corresponding InsertJobs.
switch op := b.(type) {
case JobSetOperation:
for _, job := range a {
if op.AffectsJobSet(job.Queue, job.JobSet) {
return false
}
}
}
return true
}
func (a InsertRuns) CanBeAppliedBefore(b DbOperation) bool {
// We don't check for run ops here,
// since run ops can never appear before the corresponding InsertRuns.
switch op := b.(type) {
case JobSetOperation:
for _, run := range a {
if op.AffectsJobSet(run.Queue, run.DbRun.JobSet) {
return false
}
}
case InsertJobs:
for _, run := range a {
if _, ok := op[run.DbRun.JobID]; ok {
return false
}
}
}
return true
}
func (a UpdateJobSetPriorities) CanBeAppliedBefore(b DbOperation) bool {
_, isUpdateJobPriorities := b.(*UpdateJobPriorities)
return !isUpdateJobPriorities && !definesJobInSet(a, b)
}
func (a MarkJobSetsCancelRequested) CanBeAppliedBefore(b DbOperation) bool {
return !definesJobInSet(a, b) && !definesRunInSet(a, b)
}
func (a MarkJobsCancelRequested) CanBeAppliedBefore(b DbOperation) bool {
return !definesJobInSet(a, b) && !definesRunInSet(a, b)
}
func (a MarkRunsForJobPreemptRequested) CanBeAppliedBefore(b DbOperation) bool {
return !definesJobInSet(a, b) && !definesRunInSet(a, b)
}
func (a MarkJobsSucceeded) CanBeAppliedBefore(b DbOperation) bool {
return !definesJob(a, b)
}
func (a MarkJobsFailed) CanBeAppliedBefore(b DbOperation) bool {
return !definesJob(a, b)
}
func (a MarkJobsCancelled) CanBeAppliedBefore(b DbOperation) bool {
return !definesJob(a, b)
}
func (a UpdateJobSchedulingInfo) CanBeAppliedBefore(b DbOperation) bool {
return !definesJob(a, b)
}
func (a UpdateJobQueuedState) CanBeAppliedBefore(b DbOperation) bool {
return !definesJob(a, b)
}
func (a *UpdateJobPriorities) CanBeAppliedBefore(b DbOperation) bool {
_, isUpdateJobSetPriorities := b.(UpdateJobSetPriorities)
_, isUpdateJobPriorities := b.(*UpdateJobPriorities)
return !isUpdateJobPriorities && !isUpdateJobSetPriorities &&
!definesJobInSet(map[JobSetKey]bool{a.key.JobSetKey: true}, b) &&
!definesRunInSet(map[JobSetKey]bool{a.key.JobSetKey: true}, b)
}
func (a MarkRunsSucceeded) CanBeAppliedBefore(b DbOperation) bool {
return !definesRun(a, b)
}
func (a MarkRunsFailed) CanBeAppliedBefore(b DbOperation) bool {
return !definesRun(a, b)
}
func (a MarkRunsRunning) CanBeAppliedBefore(b DbOperation) bool {
return !definesRun(a, b)
}
func (a MarkRunsPending) CanBeAppliedBefore(b DbOperation) bool {
return !definesRun(a, b)
}
func (a MarkRunsPreempted) CanBeAppliedBefore(b DbOperation) bool {
return !definesRun(a, b)
}
func (a *InsertPartitionMarker) CanBeAppliedBefore(b DbOperation) bool {
// Partition markers can never be brought forward
return false
}
func (a InsertJobRunErrors) CanBeAppliedBefore(_ DbOperation) bool {
// Inserting errors before a run has been marked as failed is ok.
// We only require that errors are written to the schedulerdb before the run is marked as failed.
return true
}
func (a MarkJobsValidated) CanBeAppliedBefore(b DbOperation) bool {
return !definesJob(a, b)
}
// definesJobInSet returns true if b is an InsertJobs operation
// that inserts at least one job in any of the job sets that make
// up the keys of a.
func definesJobInSet[M ~map[JobSetKey]V, V any](a M, b DbOperation) bool {
if op, ok := b.(InsertJobs); ok {
for _, job := range op {
if _, ok := a[JobSetKey{queue: job.Queue, jobSet: job.JobSet}]; ok {
return true
}
}
}
return false
}
// Like definesJobInSet, but checks if b defines a run.
func definesRunInSet[M ~map[JobSetKey]V, V any](a M, b DbOperation) bool {
if op, ok := b.(InsertRuns); ok {
for _, run := range op {
if _, ok := a[JobSetKey{queue: run.Queue, jobSet: run.DbRun.JobSet}]; ok {
return true
}
}
}
return false
}
// definesJob returns true if b is an InsertJobs operation
// that inserts at least one job with id equal to any of the keys of a.
func definesJob[M ~map[string]V, V any](a M, b DbOperation) bool {
if op, ok := b.(InsertJobs); ok {
for _, job := range op {
if _, ok := a[job.JobID]; ok {
return true
}
}
}
return false
}
// definesRun returns true if b is an InsertRuns operation
// that inserts at least one run with id equal to any of the keys of a.
func definesRun[M ~map[uuid.UUID]V, V any](a M, b DbOperation) bool {
if op, ok := b.(InsertRuns); ok {
for _, run := range op {
if _, ok := a[run.DbRun.RunID]; ok {
return true
}
}
}
return false
}