-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
run_manager.go
412 lines (354 loc) · 12 KB
/
run_manager.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
package services
import (
"fmt"
"math/big"
"time"
uuid "github.com/satori/go.uuid"
"github.com/smartcontractkit/chainlink/core/adapters"
"github.com/smartcontractkit/chainlink/core/assets"
"github.com/smartcontractkit/chainlink/core/logger"
clnull "github.com/smartcontractkit/chainlink/core/null"
"github.com/smartcontractkit/chainlink/core/services/synchronization"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/smartcontractkit/chainlink/core/store/orm"
"github.com/smartcontractkit/chainlink/core/utils"
"gorm.io/gorm"
"github.com/pkg/errors"
)
// RecurringScheduleJobError contains the field for the error message.
type RecurringScheduleJobError struct {
msg string
}
// Error returns the error message for the run.
func (err RecurringScheduleJobError) Error() string {
return err.msg
}
//go:generate mockery --name RunManager --output ../internal/mocks/ --case=underscore
// RunManager supplies methods for queueing, resuming and cancelling jobs in
// the RunQueue
type RunManager interface {
Create(
jobSpecID models.JobID,
initiator *models.Initiator,
creationHeight *big.Int,
runRequest *models.RunRequest) (*models.JobRun, error)
CreateErrored(
jobSpecID models.JobID,
initiator models.Initiator,
err error) (*models.JobRun, error)
ResumePendingBridge(
runID uuid.UUID,
input models.BridgeRunResult) error
Cancel(runID uuid.UUID) (*models.JobRun, error)
ResumeAllInProgress() error
ResumeAllPendingNextBlock(currentBlockHeight *big.Int) error
ResumeAllPendingConnection() error
}
// runManager implements RunManager
type runManager struct {
orm *orm.ORM
statsPusher synchronization.StatsPusher
runQueue RunQueue
config orm.ConfigReader
clock utils.AfterNower
}
func runCost(job *models.JobSpec, config orm.ConfigReader, adapters []*adapters.PipelineAdapter) *assets.Link {
minimumRunPayment := assets.NewLink(0)
if job.MinPayment != nil {
minimumRunPayment = job.MinPayment
logger.Debugw("Using job's minimum payment", "required_payment", minimumRunPayment)
} else if config.MinimumContractPayment() != nil {
minimumRunPayment = config.MinimumContractPayment()
logger.Debugw("Using configured minimum payment", "required_payment", minimumRunPayment)
}
for _, adapter := range adapters {
minimumPayment := adapter.MinPayment()
if minimumPayment != nil {
minimumRunPayment = assets.NewLink(0).Add(minimumRunPayment, minimumPayment)
}
}
return minimumRunPayment
}
// NewRun returns a complete run from a JobSpec
func NewRun(
job *models.JobSpec,
initiator *models.Initiator,
currentHeight *big.Int,
runRequest *models.RunRequest,
config orm.ConfigReader,
orm *orm.ORM,
now time.Time) (*models.JobRun, []*adapters.PipelineAdapter) {
run := models.MakeJobRun(job, now, initiator, currentHeight, runRequest)
runAdapters := []*adapters.PipelineAdapter{}
for i, task := range job.Tasks {
adapter, err := adapters.For(task, config, orm)
if err != nil {
run.SetError(err)
break
}
runAdapters = append(runAdapters, adapter)
if currentHeight == nil {
continue
}
run.TaskRuns[i].MinRequiredIncomingConfirmations = clnull.Uint32From(
utils.MaxUint32(
config.MinIncomingConfirmations(),
task.MinRequiredIncomingConfirmations.Uint32,
adapter.MinConfs()),
)
}
return &run, runAdapters
}
// ValidateRun ensures that a run's initial preconditions have been met
func ValidateRun(run *models.JobRun, contractCost *assets.Link) {
// payment is only present for runs triggered by runlogs
if run.Payment != nil && contractCost.Cmp(run.Payment) > 0 {
logger.Debugw("Rejecting run with insufficient payment",
run.ForLogger("required_payment", contractCost.String())...)
err := fmt.Errorf(
"rejecting job %s with payment %s below minimum threshold (%s)",
run.JobSpecID,
run.Payment.Text(10),
contractCost.Text(10))
run.SetError(err)
return
}
}
// NewRunManager returns a new job manager
func NewRunManager(
runQueue RunQueue,
config orm.ConfigReader,
orm *orm.ORM,
statsPusher synchronization.StatsPusher,
clock utils.AfterNower) RunManager {
return &runManager{
orm: orm,
statsPusher: statsPusher,
runQueue: runQueue,
config: config,
clock: clock,
}
}
// CreateErrored creates a run that is in the errored state. This is a
// special case where this job cannot run but we want to create the run record
// so the error is more visible to the node operator.
func (rm *runManager) CreateErrored(
jobSpecID models.JobID,
initiator models.Initiator,
runErr error) (*models.JobRun, error) {
job, err := rm.orm.Unscoped().FindJobSpec(jobSpecID)
if err != nil {
return nil, errors.Wrap(err, "failed to find job spec")
}
now := time.Now()
run := models.JobRun{
ID: uuid.NewV4(),
JobSpecID: job.ID,
CreatedAt: now,
UpdatedAt: now,
InitiatorID: initiator.ID,
}
run.SetError(runErr)
defer rm.statsPusher.PushNow()
return &run, rm.orm.CreateJobRun(&run)
}
// Create immediately persists a JobRun and sends it to the RunQueue for
// execution.
func (rm *runManager) Create(
jobSpecID models.JobID,
initiator *models.Initiator,
creationHeight *big.Int,
runRequest *models.RunRequest,
) (*models.JobRun, error) {
logger.Debugw(fmt.Sprintf("New run triggered by %s", initiator.Type),
"job", jobSpecID.String(),
"creation_height", creationHeight.String(),
)
job, err := rm.orm.Unscoped().FindJobSpec(jobSpecID)
if err != nil {
return nil, errors.Wrap(err, "failed to find job spec")
}
if job.Archived() {
return nil, RecurringScheduleJobError{
msg: fmt.Sprintf("Trying to run archived job %s", job.ID),
}
}
now := rm.clock.Now()
if !job.Started(now) {
return nil, RecurringScheduleJobError{
msg: fmt.Sprintf("Job runner: Job %v unstarted: %v before job's start time %v", job.ID, now, job.EndAt),
}
}
if job.Ended(now) {
return nil, RecurringScheduleJobError{
msg: fmt.Sprintf("Job runner: Job %v ended: %v past job's end time %v", job.ID, now, job.EndAt),
}
}
if len(job.Tasks) == 0 {
return nil, fmt.Errorf("invariant for job %s: no tasks to run in NewRun", job.ID)
}
run, adapters := NewRun(&job, initiator, creationHeight, runRequest, rm.config, rm.orm, now)
runCost := runCost(&job, rm.config, adapters)
ValidateRun(run, runCost)
if err := rm.orm.CreateJobRun(run); err != nil {
return nil, errors.Wrap(err, "CreateJobRun failed")
}
rm.statsPusher.PushNow()
if run.GetStatus().Runnable() {
logger.Debugw(
fmt.Sprintf("Executing run originally initiated by %s", run.Initiator.Type),
run.ForLogger()...,
)
rm.runQueue.Run(run.ID)
}
return run, nil
}
// ResumeAllPendingNextBlock wakes up all jobs that were sleeping because they
// were waiting for the next block
func (rm *runManager) ResumeAllPendingNextBlock(currentBlockHeight *big.Int) error {
logger.Debugw("Resuming all runs pending next block", "currentBlockHeight", currentBlockHeight)
observedHeight := utils.NewBig(currentBlockHeight)
resumableRunStatuses := []models.RunStatus{
models.RunStatusPendingConnection,
models.RunStatusPendingOutgoingConfirmations,
models.RunStatusPendingIncomingConfirmations,
}
resumableTaskStatuses := []models.RunStatus{
models.RunStatusUnstarted,
models.RunStatusPendingConnection,
models.RunStatusPendingOutgoingConfirmations,
models.RunStatusPendingIncomingConfirmations,
}
runIDs := []uuid.UUID{}
err := rm.orm.Transaction(func(tx *gorm.DB) error {
updateTaskRunsQuery := `
UPDATE task_runs
SET status = ?, confirmations = (
CASE
WHEN job_runs.creation_height IS NULL OR task_runs.minimum_confirmations IS NULL OR ?::bigint IS NULL THEN
NULL
ELSE
GREATEST(0, LEAST(task_runs.minimum_confirmations, (? - job_runs.creation_height) + 1))
END
)
FROM job_runs
WHERE job_runs.status IN (?)
AND task_runs.job_run_id = job_runs.id
AND task_runs.status IN (?);`
result := tx.Exec(updateTaskRunsQuery, models.RunStatusInProgress, observedHeight, observedHeight, resumableRunStatuses, resumableTaskStatuses)
if result.Error != nil {
return result.Error
}
updateJobRunsQuery := `
UPDATE job_runs
SET status = ?, observed_height = ?
WHERE status IN (?)
RETURNING id;`
result = tx.Raw(updateJobRunsQuery, models.RunStatusInProgress, observedHeight, resumableRunStatuses)
if result.Error != nil {
return result.Error
}
return result.Scan(&runIDs).Error
})
if err != nil {
return err
}
for _, runID := range runIDs {
rm.runQueue.Run(runID)
}
rm.statsPusher.PushNow()
return nil
}
// ResumeAllPendingConnection wakes up all tasks that have gone to sleep because they
// needed an ethereum client connection.
func (rm *runManager) ResumeAllPendingConnection() error {
return rm.orm.UnscopedJobRunsWithStatus(func(run *models.JobRun) {
logger.Debugw("New connection resuming run", run.ForLogger()...)
currentTaskRun := run.NextTaskRun()
if currentTaskRun == nil {
err := rm.updateWithError(run, "Attempting to resume connecting run with no remaining tasks %s", run.ID)
logger.ErrorIf(err, "failed when run manager updates with error")
return
}
run.SetStatus(models.RunStatusInProgress)
err := rm.saveAndResumeIfInProgress(run)
if err != nil {
logger.Errorw("Error saving run", run.ForLogger("error", err)...)
}
},
models.RunStatusPendingConnection, models.RunStatusPendingOutgoingConfirmations)
}
// ResumePendingBridgeTask wakes up a task that required a response from a bridge adapter.
func (rm *runManager) ResumePendingBridge(
runID uuid.UUID,
input models.BridgeRunResult,
) error {
run, err := rm.orm.Unscoped().FindJobRun(runID)
if err != nil {
return err
}
logger.Debugw("External adapter resuming run", run.ForLogger("input_data", input.Data)...)
if !run.GetStatus().PendingBridge() {
return fmt.Errorf("attempting to resume non pending run %s", run.ID)
}
currentTaskRun := run.NextTaskRun()
if currentTaskRun == nil {
return rm.updateWithError(&run, "Attempting to resume pending run with no remaining tasks %s", run.ID)
}
data, err := models.Merge(run.RunRequest.RequestParams, input.Data)
if err != nil {
return rm.updateWithError(&run, "Error while merging onto RequestParams for run %s", run.ID)
}
run.RunRequest.RequestParams = data
currentTaskRun.ApplyBridgeRunResult(input)
run.ApplyBridgeRunResult(input)
return rm.saveAndResumeIfInProgress(&run)
}
// ResumeAllInProgress queries the db for job runs that should be resumed
// since a previous node shutdown.
//
// As a result of its reliance on the database, it must run before anything
// persists a job RunStatus to the db to ensure that it only captures pending and in progress
// jobs as a result of the last shutdown, and not as a result of what's happening now.
//
// To recap: This must run before anything else writes job run status to the db,
// ie. tries to run a job.
func (rm *runManager) ResumeAllInProgress() error {
queueRun := func(run *models.JobRun) { rm.runQueue.Run(run.ID) }
return rm.orm.UnscopedJobRunsWithStatus(queueRun, models.RunStatusInProgress, models.RunStatusPendingSleep)
}
// Cancel suspends a running task.
func (rm *runManager) Cancel(runID uuid.UUID) (*models.JobRun, error) {
run, err := rm.orm.FindJobRun(runID)
if err != nil {
return nil, err
}
logger.Debugw("Cancelling run", run.ForLogger()...)
if run.GetStatus().Finished() {
return nil, fmt.Errorf("cannot cancel a run that has already finished")
}
run.Cancel()
defer rm.statsPusher.PushNow()
return &run, rm.orm.SaveJobRun(&run)
}
func (rm *runManager) updateWithError(run *models.JobRun, msg string, args ...interface{}) error {
run.SetError(fmt.Errorf(msg, args...))
logger.Error(fmt.Sprintf(msg, args...))
if err := rm.orm.SaveJobRun(run); err != nil {
logger.Errorw("Error saving run", run.ForLogger("error", err)...)
return err
}
rm.statsPusher.PushNow()
return nil
}
func (rm *runManager) saveAndResumeIfInProgress(run *models.JobRun) error {
defer rm.statsPusher.PushNow()
if err := rm.orm.SaveJobRun(run); err != nil {
return err
}
rm.statsPusher.PushNow()
if run.GetStatus() == models.RunStatusInProgress {
rm.runQueue.Run(run.ID)
}
return nil
}