-
Notifications
You must be signed in to change notification settings - Fork 129
/
jobiteration.go
232 lines (206 loc) · 6.16 KB
/
jobiteration.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
package scheduler
import (
"context"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"golang.org/x/sync/errgroup"
"github.com/armadaproject/armada/internal/common/types"
"github.com/armadaproject/armada/internal/scheduler/interfaces"
)
type JobIterator interface {
Next() (interfaces.LegacySchedulerJob, error)
}
type JobRepository interface {
GetQueueJobIds(queueName string) ([]string, error)
GetExistingJobsByIds(ids []string) ([]interfaces.LegacySchedulerJob, error)
}
type InMemoryJobIterator struct {
i int
jobs []interfaces.LegacySchedulerJob
}
func NewInMemoryJobIterator[S ~[]E, E interfaces.LegacySchedulerJob](jobs S) *InMemoryJobIterator {
vs := make([]interfaces.LegacySchedulerJob, len(jobs))
for i, job := range jobs {
vs[i] = job
}
return &InMemoryJobIterator{
jobs: vs,
}
}
func (it *InMemoryJobIterator) Next() (interfaces.LegacySchedulerJob, error) {
if it.i >= len(it.jobs) {
return nil, nil
}
v := it.jobs[it.i]
it.i++
return v, nil
}
type InMemoryJobRepository struct {
jobsByQueue map[string][]interfaces.LegacySchedulerJob
jobsById map[string]interfaces.LegacySchedulerJob
priorityClasses map[string]types.PriorityClass
// If true, jobs are sorted first by priority class priority.
// If false, priority class is ignored when ordering jobs.
sortByPriorityClass bool
}
func NewInMemoryJobRepository(priorityClasses map[string]types.PriorityClass) *InMemoryJobRepository {
return &InMemoryJobRepository{
jobsByQueue: make(map[string][]interfaces.LegacySchedulerJob),
jobsById: make(map[string]interfaces.LegacySchedulerJob),
priorityClasses: maps.Clone(priorityClasses),
sortByPriorityClass: true,
}
}
func (repo *InMemoryJobRepository) EnqueueMany(jobs []interfaces.LegacySchedulerJob) {
updatedQueues := make(map[string]bool)
for _, job := range jobs {
queue := job.GetQueue()
repo.jobsByQueue[queue] = append(repo.jobsByQueue[queue], job)
repo.jobsById[job.GetId()] = job
updatedQueues[queue] = true
}
for queue := range updatedQueues {
repo.sortQueue(queue)
}
}
func (repo *InMemoryJobRepository) Enqueue(job interfaces.LegacySchedulerJob) {
queue := job.GetQueue()
repo.jobsByQueue[queue] = append(repo.jobsByQueue[queue], job)
repo.jobsById[job.GetId()] = job
repo.sortQueue(queue)
}
// Sort jobs queued jobs
// first by priority class priority, with higher values first,
// second by in-queue priority, with smaller values first, and
// finally by submit time, with earlier submit times first.
func (repo *InMemoryJobRepository) sortQueue(queue string) {
slices.SortFunc(repo.jobsByQueue[queue], func(a, b interfaces.LegacySchedulerJob) bool {
if repo.sortByPriorityClass {
pca := repo.priorityClasses[a.GetPriorityClassName()]
pcb := repo.priorityClasses[b.GetPriorityClassName()]
if pca.Priority > pcb.Priority {
return true
} else if pca.Priority < pcb.Priority {
return false
}
}
pa := a.GetPerQueuePriority()
pb := b.GetPerQueuePriority()
if pa < pb {
return true
} else if pa > pb {
return false
}
return a.GetSubmitTime().Before(b.GetSubmitTime())
})
}
func (repo *InMemoryJobRepository) GetQueueJobIds(queue string) ([]string, error) {
jobs := repo.jobsByQueue[queue]
rv := make([]string, len(jobs))
for i, job := range jobs {
rv[i] = job.GetId()
}
return rv, nil
}
func (repo *InMemoryJobRepository) GetExistingJobsByIds(jobIds []string) ([]interfaces.LegacySchedulerJob, error) {
rv := make([]interfaces.LegacySchedulerJob, 0, len(jobIds))
for _, jobId := range jobIds {
if job, ok := repo.jobsById[jobId]; ok {
rv = append(rv, job)
}
}
return rv, nil
}
func (repo *InMemoryJobRepository) GetJobIterator(ctx context.Context, queue string) (JobIterator, error) {
return NewInMemoryJobIterator(slices.Clone(repo.jobsByQueue[queue])), nil
}
// QueuedJobsIterator is an iterator over all jobs in a queue.
// It lazily loads jobs in batches from Redis asynch.
type QueuedJobsIterator struct {
ctx context.Context
err error
c chan interfaces.LegacySchedulerJob
}
func NewQueuedJobsIterator(ctx context.Context, queue string, repo JobRepository) (*QueuedJobsIterator, error) {
batchSize := 16
g, ctx := errgroup.WithContext(ctx)
it := &QueuedJobsIterator{
ctx: ctx,
c: make(chan interfaces.LegacySchedulerJob, 2*batchSize), // 2x batchSize to load one batch async.
}
jobIds, err := repo.GetQueueJobIds(queue)
if err != nil {
it.err = err
return nil, err
}
g.Go(func() error { return queuedJobsIteratorLoader(ctx, jobIds, it.c, batchSize, repo) })
return it, nil
}
func (it *QueuedJobsIterator) Next() (interfaces.LegacySchedulerJob, error) {
// Once this function has returned error,
// it will return this error on every invocation.
if it.err != nil {
return nil, it.err
}
// Get one job that was loaded asynchrounsly.
select {
case <-it.ctx.Done():
it.err = it.ctx.Err() // Return an error if called again.
return nil, it.err
case job, ok := <-it.c:
if !ok {
return nil, nil
}
return job, nil
}
}
// queuedJobsIteratorLoader loads jobs from Redis lazily.
// Used with QueuedJobsIterator.
func queuedJobsIteratorLoader(ctx context.Context, jobIds []string, ch chan interfaces.LegacySchedulerJob, batchSize int, repo JobRepository) error {
defer close(ch)
batch := make([]string, batchSize)
for i, jobId := range jobIds {
batch[i%len(batch)] = jobId
if (i+1)%len(batch) == 0 || i == len(jobIds)-1 {
jobs, err := repo.GetExistingJobsByIds(batch[:i%len(batch)+1])
if err != nil {
return err
}
for _, job := range jobs {
if job == nil {
continue
}
select {
case <-ctx.Done():
return ctx.Err()
case ch <- job:
}
}
}
}
return nil
}
// MultiJobsIterator chains several JobIterators together,
// emptying them in the order provided.
type MultiJobsIterator struct {
i int
its []JobIterator
}
func NewMultiJobsIterator(its ...JobIterator) *MultiJobsIterator {
return &MultiJobsIterator{
its: its,
}
}
func (it *MultiJobsIterator) Next() (interfaces.LegacySchedulerJob, error) {
if it.i >= len(it.its) {
return nil, nil
}
if v, err := it.its[it.i].Next(); err != nil {
return nil, err
} else if v == nil {
it.i++
return it.Next()
} else {
return v, nil
}
}