-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathwait.go
282 lines (263 loc) · 9.05 KB
/
wait.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
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package jobs
import (
"context"
"fmt"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sqlliveness"
"github.com/cockroachdb/cockroach/pkg/sql/sqlutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/retry"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/errors"
)
// NotifyToAdoptJobs notifies the job adoption loop to start claimed jobs.
func (r *Registry) NotifyToAdoptJobs() {
select {
case r.adoptionCh <- resumeClaimedJobs:
default:
}
}
// NotifyToResume is used to notify the registry that it should attempt
// to resume the specified jobs. The assumption is that these jobs were
// created by this registry and thus are pre-claimed by it. This bypasses
// the loop to discover jobs already claimed by this registry. If the jobs
// turn out to not be claimed by this registry, it's not a problem.
func (r *Registry) NotifyToResume(ctx context.Context, jobs ...jobspb.JobID) {
m := newJobIDSet(jobs...)
_ = r.stopper.RunAsyncTask(ctx, "resume-jobs", func(ctx context.Context) {
r.withSession(ctx, func(ctx context.Context, s sqlliveness.Session) {
r.filterAlreadyRunningAndCancelFromPreviousSessions(ctx, s, m)
r.resumeClaimedJobs(ctx, s, m)
})
})
}
// WaitForJobs waits for a given list of jobs to reach some sort
// of terminal state.
func (r *Registry) WaitForJobs(
ctx context.Context, ex sqlutil.InternalExecutor, jobs []jobspb.JobID,
) error {
log.Infof(ctx, "waiting for %d %v queued jobs to complete", len(jobs), jobs)
jobFinishedLocally, cleanup := r.installWaitingSet(jobs...)
defer cleanup()
return r.waitForJobs(ctx, ex, jobs, jobFinishedLocally)
}
func (r *Registry) waitForJobs(
ctx context.Context,
ex sqlutil.InternalExecutor,
jobs []jobspb.JobID,
jobFinishedLocally <-chan struct{},
) error {
if len(jobs) == 0 {
return nil
}
query := makeWaitForJobsQuery(jobs)
start := timeutil.Now()
// Manually retry instead of using SHOW JOBS WHEN COMPLETE so we have greater
// control over retries. Also, avoiding SHOW JOBS prevents us from having to
// populate the crdb_internal.jobs vtable.
ret := retry.StartWithCtx(ctx, retry.Options{
InitialBackoff: 500 * time.Millisecond,
MaxBackoff: 3 * time.Second,
Multiplier: 1.5,
// Setting the closer here will terminate the loop if the job finishes
// before the first InitialBackoff period.
Closer: jobFinishedLocally,
})
ret.Next() // wait at least one InitialBackoff, first Next() doesn't block
for ret.Next() {
// We poll the number of queued jobs that aren't finished. As with SHOW JOBS
// WHEN COMPLETE, if one of the jobs is missing from the jobs table for
// whatever reason, we'll fail later when we try to load the job.
row, err := ex.QueryRowEx(
ctx,
"poll-show-jobs",
nil, /* txn */
sessiondata.InternalExecutorOverride{User: security.RootUserName()},
query,
)
if err != nil {
return errors.Wrap(err, "polling for queued jobs to complete")
}
if row == nil {
return errors.New("polling for queued jobs failed")
}
count := int64(tree.MustBeDInt(row[0]))
if log.V(3) {
log.Infof(ctx, "waiting for %d queued jobs to complete", count)
}
if count == 0 {
break
}
}
defer func() {
log.Infof(ctx, "waited for %d %v queued jobs to complete %v",
len(jobs), jobs, timeutil.Since(start))
}()
for i, id := range jobs {
j, err := r.LoadJob(ctx, id)
if err != nil {
return errors.WithHint(
errors.Wrapf(err, "job %d could not be loaded", jobs[i]),
"The job may not have succeeded.")
}
if j.Payload().FinalResumeError != nil {
decodedErr := errors.DecodeError(ctx, *j.Payload().FinalResumeError)
return decodedErr
}
if j.Status() == StatusPaused {
if reason := j.Payload().PauseReason; reason != "" {
return errors.Newf("job %d was paused before it completed with reason: %s", jobs[i], reason)
}
return errors.Newf("job %d was paused before it completed", jobs[i])
}
if j.Payload().Error != "" {
return errors.Newf("job %d failed with error: %s", jobs[i], j.Payload().Error)
}
}
return nil
}
func makeWaitForJobsQuery(jobs []jobspb.JobID) string {
var buf strings.Builder
buf.WriteString(`SELECT count(*) FROM system.jobs WHERE status NOT IN ( ` +
`'` + string(StatusSucceeded) + `', ` +
`'` + string(StatusFailed) + `',` +
`'` + string(StatusCanceled) + `',` +
`'` + string(StatusRevertFailed) + `',` +
`'` + string(StatusPaused) + `'` +
` ) AND id IN (`)
for i, id := range jobs {
if i > 0 {
buf.WriteString(",")
}
_, _ = fmt.Fprintf(&buf, " %d", id)
}
buf.WriteString(")")
return buf.String()
}
// Run starts previously unstarted jobs from a list of scheduled
// jobs. Canceling ctx interrupts the waiting but doesn't cancel the jobs.
func (r *Registry) Run(
ctx context.Context, ex sqlutil.InternalExecutor, jobs []jobspb.JobID,
) error {
if len(jobs) == 0 {
return nil
}
done, cleanup := r.installWaitingSet(jobs...)
defer cleanup()
r.NotifyToResume(ctx, jobs...)
return r.waitForJobs(ctx, ex, jobs, done)
}
// jobWaitingSets stores the set of waitingSets currently waiting on a job ID.
type jobWaitingSets map[jobspb.JobID]map[*waitingSet]struct{}
// waitingSet is a set of job IDs that a local client is waiting to complete.
// It is an optimization for the Registry.Run() method which allows completion
// when a job is scheduled and run to completion locally to be communicated
// directly, without requiring the waiting goroutine to poll KV. This allows
// the jobs package to be responsive to job termination with much less
// contention on the table itself.
//
// The waitingSet is installed in Registry.installWaitingSet(), which returns
// a cleanup function which will remove the waiter from the relevant
// jobWaitingSets when the caller is no longer waiting. This cleanup makes any
// call to notify the waiter purely an optimization. If no calls to
// Registry.removeFromWaitingSet() were placed anywhere in the package, no
// resources would be wasted. In order to deal with the fact that the waiting
// set is an optimization, the caller still polls the job state to wait for it
// to transition to a terminal status (or paused). This is unavoidable: the job
// may end up running elsewhere.
type waitingSet struct {
// jobDoneCh is closed when the set becomes empty because all
// jobs in the set have entered a terminal state.
jobDoneCh chan struct{}
// Note that the set itself is only ever mutated under the registry's mu.
// This choice is made to remove any concerns regarding lock ordering.
// See Registry.removeFromWaitingSet() for the one place where this is
// mutated. The code in the cleanup function returned from
// Registry.installWaitingSet() uses this set to determine which entries in
// the Registry's jobsWaitingSets which still need to be cleaned up.
set jobIDSet
}
// jobIDSet is a set of job IDs.
type jobIDSet map[jobspb.JobID]struct{}
func newJobIDSet(ids ...jobspb.JobID) jobIDSet {
m := make(map[jobspb.JobID]struct{}, len(ids))
for _, j := range ids {
m[j] = struct{}{}
}
return m
}
// installWaitingSet constructs a waiting set and installs it in the registry.
// If all the jobs execute to a terminal status in this registry, the done
// channel will be closed. The cleanup function must be called to avoid
// leaking memory.
func (r *Registry) installWaitingSet(
ids ...jobspb.JobID,
) (jobDoneCh <-chan struct{}, cleanup func()) {
ws := &waitingSet{
jobDoneCh: make(chan struct{}),
set: newJobIDSet(ids...),
}
r.mu.Lock()
defer r.mu.Unlock()
for _, id := range ids {
sets, ok := r.mu.waiting[id]
if !ok {
sets = make(map[*waitingSet]struct{}, 1)
r.mu.waiting[id] = sets
}
sets[ws] = struct{}{}
}
cleanup = func() {
r.mu.Lock()
defer r.mu.Unlock()
for id := range ws.set {
set, ok := r.mu.waiting[id]
if !ok {
// This should never happen and indicates a programming error.
log.Errorf(
r.ac.AnnotateCtx(context.Background()),
"corruption detected in waiting set for id %d", id,
)
continue
}
delete(set, ws)
delete(ws.set, id)
if len(set) == 0 {
delete(r.mu.waiting, id)
}
}
}
return ws.jobDoneCh, cleanup
}
func (r *Registry) removeFromWaitingSets(id jobspb.JobID) {
r.mu.Lock()
defer r.mu.Unlock()
sets, ok := r.mu.waiting[id]
if !ok {
return
}
for ws := range sets {
// Note that the set is only ever mutated underneath this mutex, so it's
// not possible for any other goroutine to have observed the set become
// empty.
delete(ws.set, id)
if len(ws.set) == 0 {
close(ws.jobDoneCh)
}
}
delete(r.mu.waiting, id)
}