-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs.go
750 lines (690 loc) · 24.1 KB
/
jobs.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package jobs
import (
gosql "database/sql"
"fmt"
"strings"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/retry"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
)
// Job manages logging the progress of long-running system processes, like
// backups and restores, to the system.jobs table.
//
// The Record field can be directly modified before Created is called. Updates
// to the Record field after the job has been created will not be written to the
// database, however, even when calling e.g. Started or Succeeded.
type Job struct {
// TODO(benesch): avoid giving Job a reference to Registry. This will likely
// require inverting control: rather than having the worker call Created,
// Started, etc., have Registry call a setupFn and a workFn as appropriate.
registry *Registry
id *int64
Record Record
txn *client.Txn
cancelFn func()
mu struct {
syncutil.Mutex
payload Payload
canceled bool
}
}
// Details is a marker interface for job details proto structs.
type Details interface{}
var _ Details = BackupDetails{}
var _ Details = RestoreDetails{}
var _ Details = SchemaChangeDetails{}
// Record stores the job fields that are not automatically managed by Job.
type Record struct {
Description string
Username string
DescriptorIDs sqlbase.IDs
Details Details
}
// Status represents the status of a job in the system.jobs table.
type Status string
const (
// StatusPending is for jobs that have been created but on which work has
// not yet started.
StatusPending Status = "pending"
// StatusRunning is for jobs that are currently in progress.
StatusRunning Status = "running"
// StatusPaused is for jobs that are not currently performing work, but have
// saved their state and can be resumed by the user later.
StatusPaused Status = "paused"
// StatusFailed is for jobs that failed.
StatusFailed Status = "failed"
// StatusSucceeded is for jobs that have successfully completed.
StatusSucceeded Status = "succeeded"
// StatusCanceled is for jobs that were explicitly canceled by the user and
// cannot be resumed.
StatusCanceled Status = "canceled"
)
// Terminal returns whether this status represents a "terminal" state: a state
// after which the job should never be updated again.
func (s Status) Terminal() bool {
return s == StatusFailed || s == StatusSucceeded || s == StatusCanceled
}
// InvalidStatusError is the error returned when the desired operation is
// invalid given the job's current status.
type InvalidStatusError struct {
id int64
status Status
op string
}
func (e *InvalidStatusError) Error() string {
return fmt.Sprintf("cannot %s %s job (id %d)", e.op, e.status, e.id)
}
// ID returns the ID of the job that this Job is currently tracking. This will
// be nil if Created has not yet been called.
func (j *Job) ID() *int64 {
return j.id
}
// WithoutCancel indicates that the job should not have its leasing and
// cancelation managed by Registry. This is only a temporary measure; eventually
// all jobs will use the Registry's leasing and cancelation.
var WithoutCancel func()
// Created records the creation of a new job in the system.jobs table and
// remembers the assigned ID of the job in the Job. The job information is read
// from the Record field at the time Created is called. If cancelFn is not nil,
// the Registry will automatically acquire a lease for this job and invoke
// cancelFn if the lease expires.
func (j *Job) Created(ctx context.Context, cancelFn func()) error {
payload := &Payload{
Description: j.Record.Description,
Username: j.Record.Username,
DescriptorIDs: j.Record.DescriptorIDs,
Details: WrapPayloadDetails(j.Record.Details),
}
if cancelFn != nil {
payload.Lease = j.registry.newLease()
}
if err := j.insert(ctx, payload); err != nil {
return err
}
if cancelFn != nil {
j.cancelFn = cancelFn
if err := j.registry.register(*j.id, j); err != nil {
return err
}
}
return nil
}
// Started marks the tracked job as started.
func (j *Job) Started(ctx context.Context) error {
return j.update(ctx, func(status *Status, payload *Payload) (bool, error) {
if *status != StatusPending {
// Already started - do nothing.
return false, nil
}
*status = StatusRunning
payload.StartedMicros = timeutil.ToUnixMicros(timeutil.Now())
return true, nil
})
}
// ProgressedFn is a callback that allows arbitrary modifications to a job's
// details when updating its progress.
type ProgressedFn func(ctx context.Context, details interface{})
// Noop is a nil ProgressedFn.
var Noop ProgressedFn
// Progressed updates the progress of the tracked job to fractionCompleted. A
// fractionCompleted that is less than the currently-recorded fractionCompleted
// will be silently ignored. If progressedFn is non-nil, it will be invoked with
// a pointer to the job's details to allow for modifications to the details
// before the job is saved. If no such modifications are required, pass Noop
// instead of nil for readability.
func (j *Job) Progressed(
ctx context.Context, fractionCompleted float32, progressedFn ProgressedFn,
) error {
if fractionCompleted < 0.0 || fractionCompleted > 1.0 {
return errors.Errorf(
"Job: fractionCompleted %f is outside allowable range [0.0, 1.0] (job %d)",
fractionCompleted, j.id,
)
}
return j.update(ctx, func(status *Status, payload *Payload) (bool, error) {
if *status != StatusRunning {
return false, &InvalidStatusError{*j.id, *status, "update progress on"}
}
if fractionCompleted > payload.FractionCompleted {
payload.FractionCompleted = fractionCompleted
}
if progressedFn != nil {
progressedFn(ctx, payload.Details)
}
return true, nil
})
}
func isControllable(p *Payload, op string) error {
switch typ := p.Type(); typ {
case TypeSchemaChange:
return pgerror.UnimplementedWithIssueErrorf(
16018, "schema change jobs do not support %s", op)
case TypeImport:
return pgerror.UnimplementedWithIssueErrorf(
18139, "import jobs do not support %s", op)
case TypeBackup:
case TypeRestore:
default:
return fmt.Errorf("%s jobs do not support %s", strings.ToLower(typ.String()), op)
}
if p.Lease == nil {
return fmt.Errorf("job created by node without %s support", op)
}
return nil
}
// Paused sets the status of the tracked job to paused. It does not directly
// pause the job; instead, it expects the job to call job.Progressed soon,
// observe a "job is paused" error, and abort further work.
func (j *Job) Paused(ctx context.Context) error {
return j.update(ctx, func(status *Status, payload *Payload) (bool, error) {
if err := isControllable(payload, "PAUSE"); err != nil {
return false, err
}
if *status == StatusPaused {
// Already paused - do nothing.
return false, nil
}
if status.Terminal() {
return false, &InvalidStatusError{*j.id, *status, "pause"}
}
*status = StatusPaused
return true, nil
})
}
// Resumed sets the status of the tracked job to running iff the job is
// currently paused. It does not directly resume the job; rather, it expires the
// job's lease so that a Registry adoption loop detects it and resumes it.
func (j *Job) Resumed(ctx context.Context) error {
return j.update(ctx, func(status *Status, payload *Payload) (bool, error) {
if *status == StatusRunning {
// Already resumed - do nothing.
return false, nil
}
if *status != StatusPaused {
return false, fmt.Errorf("job with status %s cannot be resumed", *status)
}
*status = StatusRunning
// NB: A nil lease indicates the job is not resumable, whereas an empty
// lease is always considered expired.
payload.Lease = &Lease{}
return true, nil
})
}
// Canceled sets the status of the tracked job to canceled. It does not directly
// cancel the job; like job.Paused, it expects the job to call job.Progressed
// soon, observe a "job is canceled" error, and abort further work.
func (j *Job) Canceled(ctx context.Context) error {
return j.update(ctx, func(status *Status, payload *Payload) (bool, error) {
if err := isControllable(payload, "CANCEL"); err != nil {
return false, err
}
if *status == StatusCanceled {
// Already canceled - do nothing.
return false, nil
}
if *status != StatusPaused && status.Terminal() {
return false, fmt.Errorf("job with status %s cannot be canceled", *status)
}
*status = StatusCanceled
payload.FinishedMicros = timeutil.ToUnixMicros(timeutil.Now())
return true, nil
})
}
// Failed marks the tracked job as having failed with the given error. Any
// errors encountered while updating the jobs table are logged but not returned,
// under the assumption that the the caller is already handling a more important
// error and doesn't care about this one.
func (j *Job) Failed(ctx context.Context, err error) {
// To simplify cleanup routines, it is not an error to call Failed on a job
// that was never Created.
if j.id == nil {
return
}
internalErr := j.update(ctx, func(status *Status, payload *Payload) (bool, error) {
if status.Terminal() {
// Already done - do nothing.
return false, nil
}
*status = StatusFailed
payload.Error = err.Error()
payload.FinishedMicros = timeutil.ToUnixMicros(timeutil.Now())
return true, nil
})
if internalErr != nil {
log.Errorf(ctx, "Job: ignoring error %v while logging failure for job %d: %+v",
err, *j.id, internalErr)
}
j.registry.unregister(*j.id)
}
// Succeeded marks the tracked job as having succeeded and sets its fraction
// completed to 1.0.
func (j *Job) Succeeded(ctx context.Context) error {
defer j.registry.unregister(*j.id)
return j.update(ctx, func(status *Status, payload *Payload) (bool, error) {
if status.Terminal() {
// Already done - do nothing.
return false, nil
}
*status = StatusSucceeded
payload.FinishedMicros = timeutil.ToUnixMicros(timeutil.Now())
payload.FractionCompleted = 1.0
return true, nil
})
}
// FinishedWith is a shortcut for automatically calling Succeeded or Failed
// based on the presence of err. Any non-nil error is taken to mean that the job
// has failed. The error returned, if any, is serious enough that it should not
// be logged and ignored.
//
// TODO(benesch): Fix this wonky API. Once schema change leases are managed by
// this package, replace Succeeded, Failed, and FinishedWith with an API like
//
// func (r *Registry) RunJob(setupFn func() Details, workFn func() error) (result, error)
//
// where RunJob handles writing to system.jobs automatically.
func (j *Job) FinishedWith(ctx context.Context, err error) error {
j.mu.Lock()
canceled := j.mu.canceled
j.mu.Unlock()
if canceled {
// The registry canceled the job because its lease expired. This job will be
// retried, potentially on another node, so we must report an ambiguous
// result to the client because we don't know its fate.
//
// NB: Canceled jobs are automatically unregistered, so no need to call
// j.registry.unregistered.
//
// TODO(benesch): In rare cases, this can return an ambiguous result error
// when the result was not, in fact, ambigious. Specifically, if the job
// succeeds or fails with a non-lease-related error immediately before it
// loses its lease, we'll have knowledge of its true status but will blindly
// report an ambiguous result. Ideally, we'd additionally check for
// `errors.Cause(err) == context.Canceled`, but that yields false negatives
// when using errors.Wrapf, and we'd much rather have false positives (too
// many ambiguous results) than false negatives (too few ambiguous results).
return roachpb.NewAmbiguousResultError("job lease expired")
}
if err, ok := errors.Cause(err).(*InvalidStatusError); ok &&
(err.status == StatusPaused || err.status == StatusCanceled) {
// If we couldn't operate on the job because it was paused or canceled, send
// the more understandable "job paused" or "job canceled" error message to
// the user.
//
// NB: Since we're not calling Succeeded or Failed, we need to manually
// unregister the job.
j.registry.unregister(*j.id)
return fmt.Errorf("job %s", err.status)
}
if err != nil {
j.Failed(ctx, err)
} else if err := j.Succeeded(ctx); err != nil {
// No callers of Succeeded do anything but log the error, so that behavior
// is baked into the API of FinishedWith.
log.Errorf(ctx, "ignoring error while marking job %d as successful: %+v", *j.id, err)
}
return nil
}
// SetDetails sets the details field of the currently running tracked job.
func (j *Job) SetDetails(ctx context.Context, details interface{}) error {
return j.update(ctx, func(_ *Status, payload *Payload) (bool, error) {
payload.Details = WrapPayloadDetails(details)
return true, nil
})
}
// Payload returns the most recently sent Payload for this Job. Will return an
// empty Payload until Created() is called on a new Job.
func (j *Job) Payload() Payload {
j.mu.Lock()
defer j.mu.Unlock()
return j.mu.payload
}
// WithTxn sets the transaction that this Job will use for its next operation.
// If the transaction is nil, the Job will create a one-off transaction instead.
// If you use WithTxn, this Job will no longer be threadsafe.
func (j *Job) WithTxn(txn *client.Txn) *Job {
j.txn = txn
return j
}
// DB returns the *client.DB associated with this job.
func (j *Job) DB() *client.DB {
return j.registry.db
}
// Gossip returns the *gossip.Gossip associated with this job.
func (j *Job) Gossip() *gossip.Gossip {
return j.registry.gossip
}
// NodeID returns the roachpb.NodeID associated with this job.
func (j *Job) NodeID() roachpb.NodeID {
return j.registry.nodeID.Get()
}
// ClusterID returns the uuid.UUID cluster ID associated with this job.
func (j *Job) ClusterID() uuid.UUID {
return j.registry.clusterID()
}
func (j *Job) runInTxn(
ctx context.Context, retryable func(context.Context, *client.Txn) error,
) error {
if j.txn != nil {
defer func() { j.txn = nil }()
return j.txn.Exec(ctx, client.TxnExecOptions{AutoRetry: true},
func(ctx context.Context, txn *client.Txn, _ *client.TxnExecOptions) error {
return retryable(ctx, txn)
})
}
return j.registry.db.Txn(ctx, retryable)
}
func (j *Job) initialize(payload *Payload) (err error) {
j.Record.Description = payload.Description
j.Record.Username = payload.Username
j.Record.DescriptorIDs = payload.DescriptorIDs
if j.Record.Details, err = payload.UnwrapDetails(); err != nil {
return err
}
j.mu.Lock()
defer j.mu.Unlock()
j.mu.payload = *payload
return nil
}
func (j *Job) load(ctx context.Context) error {
var payload *Payload
if err := j.runInTxn(ctx, func(ctx context.Context, txn *client.Txn) error {
const stmt = "SELECT payload FROM system.jobs WHERE id = $1"
row, err := j.registry.ex.QueryRowInTransaction(ctx, "log-job", txn, stmt, *j.id)
if err != nil {
return err
}
if row == nil {
return fmt.Errorf("job with ID %d does not exist", *j.id)
}
payload, err = UnmarshalPayload(row[0])
return err
}); err != nil {
return err
}
return j.initialize(payload)
}
func (j *Job) insert(ctx context.Context, payload *Payload) error {
if j.id != nil {
// Already created - do nothing.
return nil
}
var row parser.Datums
if err := j.runInTxn(ctx, func(ctx context.Context, txn *client.Txn) error {
payload.ModifiedMicros = timeutil.ToUnixMicros(txn.Proto().OrigTimestamp.GoTime())
payloadBytes, err := protoutil.Marshal(payload)
if err != nil {
return err
}
const stmt = "INSERT INTO system.jobs (status, payload) VALUES ($1, $2) RETURNING id"
row, err = j.registry.ex.QueryRowInTransaction(ctx, "job-insert", txn, stmt, StatusPending, payloadBytes)
return err
}); err != nil {
return err
}
j.mu.payload = *payload
j.id = (*int64)(row[0].(*parser.DInt))
return nil
}
func (j *Job) update(
ctx context.Context, updateFn func(*Status, *Payload) (doUpdate bool, err error),
) error {
if j.id == nil {
return errors.New("Job: cannot update: job not created")
}
var payload *Payload
if err := j.runInTxn(ctx, func(ctx context.Context, txn *client.Txn) error {
const selectStmt = "SELECT status, payload FROM system.jobs WHERE id = $1"
row, err := j.registry.ex.QueryRowInTransaction(ctx, "log-job", txn, selectStmt, *j.id)
if err != nil {
return err
}
statusString, ok := row[0].(*parser.DString)
if !ok {
return errors.Errorf("Job: expected string status on job %d, but got %T", *j.id, statusString)
}
status := Status(*statusString)
payload, err = UnmarshalPayload(row[1])
if err != nil {
return err
}
doUpdate, err := updateFn(&status, payload)
if err != nil {
return err
}
if !doUpdate {
return nil
}
payload.ModifiedMicros = timeutil.ToUnixMicros(timeutil.Now())
payloadBytes, err := protoutil.Marshal(payload)
if err != nil {
return err
}
const updateStmt = "UPDATE system.jobs SET status = $1, payload = $2 WHERE id = $3"
updateArgs := []interface{}{status, payloadBytes, *j.id}
n, err := j.registry.ex.ExecuteStatementInTransaction(
ctx, "job-update", txn, updateStmt, updateArgs...)
if err != nil {
return err
}
if n != 1 {
return errors.Errorf("Job: expected exactly one row affected, but %d rows affected by job update", n)
}
return nil
}); err != nil {
return err
}
if payload != nil {
j.mu.Lock()
j.mu.payload = *payload
j.mu.Unlock()
}
return nil
}
func (j *Job) adopt(ctx context.Context, oldLease *Lease) error {
return j.update(ctx, func(status *Status, payload *Payload) (bool, error) {
if *status != StatusRunning {
return false, errors.Errorf("job %d no longer running", *j.id)
}
if !payload.Lease.Equal(oldLease) {
return false, errors.Errorf("current lease %v did not match expected lease %v",
payload.Lease, oldLease)
}
payload.Lease = j.registry.newLease()
if err := j.initialize(payload); err != nil {
return false, err
}
return true, nil
})
}
func (j *Job) cancel() {
j.cancelFn()
j.mu.Lock()
defer j.mu.Unlock()
j.mu.canceled = true
}
// Type returns the payload's job type.
func (p *Payload) Type() Type {
switch p.Details.(type) {
case *Payload_Backup:
return TypeBackup
case *Payload_Restore:
return TypeRestore
case *Payload_SchemaChange:
return TypeSchemaChange
case *Payload_Import:
return TypeImport
default:
panic("Payload.Type called on a payload with an unknown details type")
}
}
// WrapPayloadDetails wraps a Details object in the protobuf wrapper struct
// necessary to make it usable as the Details field of a Payload.
//
// Providing an unknown details type indicates programmer error and so causes a
// panic.
func WrapPayloadDetails(details Details) interface {
isPayload_Details
} {
switch d := details.(type) {
case BackupDetails:
return &Payload_Backup{Backup: &d}
case RestoreDetails:
return &Payload_Restore{Restore: &d}
case SchemaChangeDetails:
return &Payload_SchemaChange{SchemaChange: &d}
case ImportDetails:
return &Payload_Import{Import: &d}
default:
panic(fmt.Sprintf("jobs.WrapPayloadDetails: unknown details type %T", d))
}
}
// UnwrapDetails returns the details object stored within the payload's Details
// field, discarding the protobuf wrapper struct.
//
// Unlike in WrapPayloadDetails, an unknown details type may simply indicate
// that the Payload originated on a node aware of more details types, and so the
// error is returned to the caller.
func (p *Payload) UnwrapDetails() (Details, error) {
switch d := p.Details.(type) {
case *Payload_Backup:
return *d.Backup, nil
case *Payload_Restore:
return *d.Restore, nil
case *Payload_SchemaChange:
return *d.SchemaChange, nil
case *Payload_Import:
return *d.Import, nil
default:
return nil, errors.Errorf("jobs.Payload: unsupported details type %T", d)
}
}
// UnmarshalPayload unmarshals and returns the Payload encoded in the input
// datum, which should be a parser.DBytes.
func UnmarshalPayload(datum parser.Datum) (*Payload, error) {
payload := &Payload{}
bytes, ok := datum.(*parser.DBytes)
if !ok {
return nil, errors.Errorf(
"Job: failed to unmarshal payload as DBytes (was %T)", bytes)
}
if err := proto.Unmarshal([]byte(*bytes), payload); err != nil {
return nil, err
}
return payload, nil
}
func (t Type) String() string {
// Protobufs, by convention, use CAPITAL_SNAKE_CASE for enum identifiers.
// Since Type's string representation is used as a SHOW JOBS output column, we
// simply swap underscores for spaces in the identifier for very SQL-esque
// names, like "BACKUP" and "SCHEMA CHANGE".
return strings.Replace(Type_name[int32(t)], "_", " ", -1)
}
// RunAndWaitForTerminalState runs a closure and potentially tracks its progress
// using the system.jobs table.
//
// If the closure returns before a jobs entry is created, the closure's error is
// passed back with no job information. Otherwise, the first jobs entry created
// after the closure starts is polled until it enters a terminal state and that
// job's id, status, and error are returned.
//
// TODO(dan): Return a *Job instead of just the id and status.
//
// TODO(dan): This assumes that the next entry to the jobs table was made by
// this closure, but this assumption is quite racy. See if we can do something
// better.
func RunAndWaitForTerminalState(
ctx context.Context, sqlDB *gosql.DB, execFn func(context.Context) error,
) (int64, Status, error) {
begin := timeutil.Now()
execErrCh := make(chan error, 1)
go func() {
err := execFn(ctx)
log.Warningf(ctx, "exec returned so attempting to track via jobs: err %+v", err)
execErrCh <- err
}()
var jobID int64
var execErr error
for r := retry.StartWithCtx(ctx, retry.Options{}); ; {
select {
case <-ctx.Done():
return 0, "", ctx.Err()
case execErr = <-execErrCh:
// The closure finished, try to fetch a job id one more time. Close
// and nil out execErrCh so it blocks from now on.
close(execErrCh)
execErrCh = nil
case <-r.NextCh(): // Fallthrough.
}
err := sqlDB.QueryRow(`SELECT id FROM system.jobs WHERE created > $1`, begin).Scan(&jobID)
if err == nil {
break
}
if execDone := execErrCh == nil; err == gosql.ErrNoRows && !execDone {
continue
}
if execErr != nil {
return 0, "", errors.Wrap(execErr, "exec failed before job was created")
}
return 0, "", errors.Wrap(err, "no jobs found")
}
for r := retry.StartWithCtx(ctx, retry.Options{}); ; {
select {
case <-ctx.Done():
return jobID, "", ctx.Err()
case execErr = <-execErrCh:
// The closure finished, this is a nice hint to wake up, but it only
// works once. Close and nil out execErrCh so it blocks from now on.
close(execErrCh)
execErrCh = nil
case <-r.NextCh(): // Fallthrough.
}
var status Status
var jobErr gosql.NullString
var fractionCompleted float64
err := sqlDB.QueryRow(`
SELECT status, error, fraction_completed
FROM [SHOW JOBS]
WHERE id = $1`, jobID).Scan(
&status, &jobErr, &fractionCompleted,
)
if err != nil {
return jobID, "", errors.Wrapf(err, "getting status of job %d", jobID)
}
if !status.Terminal() {
if log.V(1) {
log.Infof(ctx, "job %d: status=%s, progress=%0.3f, created %s ago",
jobID, status, fractionCompleted, timeutil.Since(begin))
}
continue
}
if jobErr.Valid && len(jobErr.String) > 0 {
return jobID, status, errors.New(jobErr.String)
}
return jobID, status, nil
}
}