-
Notifications
You must be signed in to change notification settings - Fork 127
/
schedulerdb.go
290 lines (276 loc) · 8.26 KB
/
schedulerdb.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
package scheduleringester
import (
"context"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/pkg/errors"
"golang.org/x/exp/maps"
"github.com/armadaproject/armada/internal/common/database"
"github.com/armadaproject/armada/internal/common/ingest"
"github.com/armadaproject/armada/internal/common/ingest/metrics"
schedulerdb "github.com/armadaproject/armada/internal/scheduler/database"
)
// SchedulerDb writes DbOperations into postgres.
type SchedulerDb struct {
// Connection to the postgres database.
db *pgxpool.Pool
metrics *metrics.Metrics
initialBackOff time.Duration
maxBackOff time.Duration
lockTimeout time.Duration
}
func NewSchedulerDb(
db *pgxpool.Pool,
metrics *metrics.Metrics,
initialBackOff time.Duration,
maxBackOff time.Duration,
lockTimeout time.Duration,
) ingest.Sink[*DbOperationsWithMessageIds] {
return &SchedulerDb{
db: db,
metrics: metrics,
initialBackOff: initialBackOff,
maxBackOff: maxBackOff,
lockTimeout: lockTimeout,
}
}
// Store persists all operations in the database. Note that:
// - this function will retry until it either succeeds or a terminal error is encountered
// - this function will take out a postgres lock to ensure that other ingesters are not writing to the database
// at the same time (for details, see acquireLock())
func (s *SchedulerDb) Store(ctx context.Context, instructions *DbOperationsWithMessageIds) error {
return ingest.WithRetry(func() (bool, error) {
err := s.db.BeginTxFunc(ctx, pgx.TxOptions{
IsoLevel: pgx.ReadCommitted,
AccessMode: pgx.ReadWrite,
DeferrableMode: pgx.Deferrable,
}, func(tx pgx.Tx) error {
// First acquire the write lock
lockCtx, cancel := context.WithTimeout(ctx, s.lockTimeout)
defer cancel()
err := s.acquireLock(lockCtx, tx)
if err != nil {
return err
}
// Now insert the ops
for _, dbOp := range instructions.Ops {
err := s.WriteDbOp(ctx, tx, dbOp)
if err != nil {
return err
}
}
return err
})
return true, err
}, s.initialBackOff, s.maxBackOff)
}
// acquireLock acquires the armada_scheduleringester_lock, which prevents two ingesters writing to the db at the same
// time. This is necessary because:
// - when rows are inserted into the database they are stamped with a sequence number
// - the scheduler relies on this sequence number increasing to ensure it has fetched all updated rows
// - concurrent transactions will result in sequence numbers being interleaved across transactions.
// - the interleaved sequences may result in the scheduler seeing sequence numbers that do not strictly increase over time.
func (s *SchedulerDb) acquireLock(ctx context.Context, tx pgx.Tx) error {
const lockId = 8741339439634283896
_, err := tx.Exec(ctx, "SELECT pg_advisory_xact_lock($1)", lockId)
return errors.Wrapf(err, "Could not obtain lock")
}
func (s *SchedulerDb) WriteDbOp(ctx context.Context, tx pgx.Tx, op DbOperation) error {
queries := schedulerdb.New(tx)
switch o := op.(type) {
case InsertJobs:
records := make([]any, len(o))
i := 0
for _, v := range o {
records[i] = *v
i++
}
err := database.Upsert(ctx, tx, "jobs", records)
if err != nil {
return err
}
case InsertRuns:
records := make([]any, len(o))
i := 0
for _, v := range o {
records[i] = *v.dbRun
i++
}
err := database.Upsert(ctx, tx, "runs", records)
if err != nil {
return err
}
case UpdateJobSetPriorities:
for jobSetInfo, priority := range o {
err := queries.UpdateJobPriorityByJobSet(
ctx,
schedulerdb.UpdateJobPriorityByJobSetParams{
JobSet: jobSetInfo.jobSet,
Queue: jobSetInfo.queue,
Priority: priority,
},
)
if err != nil {
return errors.WithStack(err)
}
}
case UpdateJobSchedulingInfo:
updateJobInfoSqlStatement := "update jobs set scheduling_info = $1::bytea, scheduling_info_version = $2::int where job_id = $3 and $2::int > scheduling_info_version"
batch := &pgx.Batch{}
for key, value := range o {
batch.Queue(updateJobInfoSqlStatement, value.JobSchedulingInfo, value.JobSchedulingInfoVersion, key)
}
err := execBatch(ctx, tx, batch)
if err != nil {
return errors.WithStack(err)
}
case UpdateJobQueuedState:
updateQueuedStateSqlStatement := "update jobs set queued = $1::bool, queued_version = $2::int where job_id = $3 and $2::int > queued_version"
batch := &pgx.Batch{}
for key, value := range o {
batch.Queue(updateQueuedStateSqlStatement, value.Queued, value.QueuedStateVersion, key)
}
err := execBatch(ctx, tx, batch)
if err != nil {
return errors.WithStack(err)
}
case MarkJobSetsCancelRequested:
for jobSetInfo, cancelDetails := range o {
queuedStatesToCancel := make([]bool, 0, 2)
if cancelDetails.cancelQueued {
// Cancel all jobs in a queued state
queuedStatesToCancel = append(queuedStatesToCancel, true)
}
if cancelDetails.cancelLeased {
// Cancel all jobs in a non-queued state
queuedStatesToCancel = append(queuedStatesToCancel, false)
}
err := queries.MarkJobsCancelRequestedBySetAndQueuedState(
ctx,
schedulerdb.MarkJobsCancelRequestedBySetAndQueuedStateParams{
Queue: jobSetInfo.queue,
JobSet: jobSetInfo.jobSet,
QueuedStates: queuedStatesToCancel,
},
)
if err != nil {
return errors.WithStack(err)
}
}
case MarkJobsCancelRequested:
jobIds := maps.Keys(o)
err := queries.MarkJobsCancelRequestedById(ctx, jobIds)
if err != nil {
return errors.WithStack(err)
}
case MarkJobsCancelled:
jobIds := maps.Keys(o)
err := queries.MarkJobsCancelledById(ctx, jobIds)
if err != nil {
return errors.WithStack(err)
}
err = queries.MarkRunsCancelledByJobId(ctx, jobIds)
if err != nil {
return errors.WithStack(err)
}
case MarkJobsSucceeded:
jobIds := maps.Keys(o)
err := queries.MarkJobsSucceededById(ctx, jobIds)
if err != nil {
return errors.WithStack(err)
}
case MarkJobsFailed:
jobIds := maps.Keys(o)
err := queries.MarkJobsFailedById(ctx, jobIds)
if err != nil {
return errors.WithStack(err)
}
case UpdateJobPriorities:
// TODO: This will be slow if there's a large number of ids.
// Could be addressed by using a separate table for priority + upsert.
for jobId, priority := range o {
err := queries.UpdateJobPriorityById(ctx, schedulerdb.UpdateJobPriorityByIdParams{
JobID: jobId,
Priority: priority,
})
if err != nil {
return errors.WithStack(err)
}
}
case MarkRunsSucceeded:
runIds := maps.Keys(o)
err := queries.MarkJobRunsSucceededById(ctx, runIds)
if err != nil {
return errors.WithStack(err)
}
case MarkRunsFailed:
runIds := maps.Keys(o)
returned := make([]uuid.UUID, 0, len(runIds))
runAttempted := make([]uuid.UUID, 0, len(runIds))
for k, v := range o {
if v.LeaseReturned {
returned = append(returned, k)
}
if v.RunAttempted {
runAttempted = append(runAttempted, k)
}
}
err := queries.MarkJobRunsFailedById(ctx, runIds)
if err != nil {
return errors.WithStack(err)
}
err = queries.MarkJobRunsReturnedById(ctx, returned)
if err != nil {
return errors.WithStack(err)
}
err = queries.MarkJobRunsAttemptedById(ctx, runAttempted)
if err != nil {
return errors.WithStack(err)
}
case MarkRunsRunning:
runIds := maps.Keys(o)
err := queries.MarkJobRunsRunningById(ctx, runIds)
if err != nil {
return errors.WithStack(err)
}
case InsertJobRunErrors:
records := make([]any, len(o))
i := 0
for _, v := range o {
records[i] = *v
i++
}
return database.Upsert(ctx, tx, "job_run_errors", records)
case *InsertPartitionMarker:
for _, marker := range o.markers {
err := queries.InsertMarker(ctx, schedulerdb.InsertMarkerParams{
GroupID: marker.GroupID,
PartitionID: marker.PartitionID,
Created: marker.Created,
})
if err != nil {
return errors.Wrapf(err, "error inserting partition marker")
}
}
return nil
default:
return errors.Errorf("received unexpected op %+v", op)
}
return nil
}
func execBatch(ctx context.Context, tx pgx.Tx, batch *pgx.Batch) error {
result := tx.SendBatch(ctx, batch)
for i := 0; i < batch.Len(); i++ {
_, err := result.Exec()
if err != nil {
return err
}
}
err := result.Close()
if err != nil {
return err
}
return nil
}