-
Notifications
You must be signed in to change notification settings - Fork 659
/
jobs.go
415 lines (362 loc) · 12.2 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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package queue
import (
"fmt"
"time"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/versiondb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/avalanchego/utils/wrappers"
)
const (
// StatusUpdateFrequency is how many containers should be processed between
// logs
StatusUpdateFrequency = 2500
)
// Jobs tracks a series of jobs that form a DAG of dependencies.
type Jobs struct {
// db ensures that database updates are atomically updated.
db *versiondb.Database
// state writes the job queue to [db].
state *state
// Measures the ETA until bootstrapping finishes in nanoseconds.
etaMetric prometheus.Gauge
}
// New attempts to create a new job queue from the provided database.
func New(
db database.Database,
metricsNamespace string,
metricsRegisterer prometheus.Registerer,
) (*Jobs, error) {
vdb := versiondb.New(db)
state, err := newState(vdb, metricsNamespace, metricsRegisterer)
if err != nil {
return nil, fmt.Errorf("couldn't create new jobs state: %w", err)
}
etaMetric := prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: metricsNamespace,
Name: "eta_execution_complete",
Help: "ETA in nanoseconds until execution phase of bootstrapping finishes",
})
return &Jobs{
db: vdb,
state: state,
etaMetric: etaMetric,
}, metricsRegisterer.Register(etaMetric)
}
// SetParser tells this job queue how to parse jobs from the database.
func (j *Jobs) SetParser(parser Parser) error { j.state.parser = parser; return nil }
func (j *Jobs) Has(jobID ids.ID) (bool, error) { return j.state.HasJob(jobID) }
// Returns how many pending jobs are waiting in the queue.
func (j *Jobs) PendingJobs() uint64 { return j.state.numJobs }
// Push adds a new job to the queue. Returns true if [job] was added to the queue and false
// if [job] was already in the queue.
func (j *Jobs) Push(job Job) (bool, error) {
jobID := job.ID()
if has, err := j.state.HasJob(jobID); err != nil {
return false, fmt.Errorf("failed to check for existing job %s due to %w", jobID, err)
} else if has {
return false, nil
}
deps, err := job.MissingDependencies()
if err != nil {
return false, err
}
// Store this job into the database.
if err := j.state.PutJob(job); err != nil {
return false, fmt.Errorf("failed to write job due to %w", err)
}
if deps.Len() != 0 {
// This job needs to block on a set of dependencies.
for depID := range deps {
if err := j.state.AddDependency(depID, jobID); err != nil {
return false, fmt.Errorf("failed to add blocking for depID %s, jobID %s", depID, jobID)
}
}
return true, nil
}
// This job doesn't have any dependencies, so it should be placed onto the
// executable stack.
if err := j.state.AddRunnableJob(jobID); err != nil {
return false, fmt.Errorf("failed to add %s as a runnable job due to %w", jobID, err)
}
return true, nil
}
func (j *Jobs) ExecuteAll(ctx *snow.ConsensusContext, halter common.Haltable, restarted bool, acceptors ...snow.Acceptor) (int, error) {
ctx.Executing(true)
defer ctx.Executing(false)
numExecuted := 0
numToExecute := j.state.numJobs
startTime := time.Now()
// Disable and clear state caches to prevent us from attempting to execute
// a vertex that was previously parsed, but not saved to the VM. Some VMs
// may only persist containers when they are accepted. This is a stop-gap
// measure to ensure the job will be re-parsed before executing until the VM
// provides a more explicit interface for freeing parsed blocks.
// TODO remove DisableCaching when VM provides better interface for freeing
// blocks.
j.state.DisableCaching()
for {
if halter.Halted() {
ctx.Log.Info("interrupted execution",
zap.Int("numExecuted", numExecuted),
)
return numExecuted, nil
}
job, err := j.state.RemoveRunnableJob()
if err == database.ErrNotFound {
break
}
if err != nil {
return 0, fmt.Errorf("failed to removing runnable job with %w", err)
}
jobID := job.ID()
ctx.Log.Debug("executing",
zap.Stringer("jobID", jobID),
)
jobBytes := job.Bytes()
// Note that acceptor.Accept must be called before executing [job] to
// honor Acceptor.Accept's invariant.
for _, acceptor := range acceptors {
if err := acceptor.Accept(ctx, jobID, jobBytes); err != nil {
return numExecuted, err
}
}
if err := job.Execute(); err != nil {
return 0, fmt.Errorf("failed to execute job %s due to %w", jobID, err)
}
dependentIDs, err := j.state.RemoveDependencies(jobID)
if err != nil {
return 0, fmt.Errorf("failed to remove blocking jobs for %s due to %w", jobID, err)
}
for _, dependentID := range dependentIDs {
job, err := j.state.GetJob(dependentID)
if err != nil {
return 0, fmt.Errorf("failed to get job %s from blocking jobs due to %w", dependentID, err)
}
hasMissingDeps, err := job.HasMissingDependencies()
if err != nil {
return 0, fmt.Errorf("failed to get missing dependencies for %s due to %w", dependentID, err)
}
if hasMissingDeps {
continue
}
if err := j.state.AddRunnableJob(dependentID); err != nil {
return 0, fmt.Errorf("failed to add %s as a runnable job due to %w", dependentID, err)
}
}
if err := j.Commit(); err != nil {
return 0, err
}
numExecuted++
if numExecuted%StatusUpdateFrequency == 0 { // Periodically print progress
eta := timer.EstimateETA(
startTime,
uint64(numExecuted),
numToExecute,
)
j.etaMetric.Set(float64(eta))
if !restarted {
ctx.Log.Info("executing operations",
zap.Int("numExecuted", numExecuted),
zap.Uint64("numToExecute", numToExecute),
zap.Duration("eta", eta),
)
} else {
ctx.Log.Debug("executing operations",
zap.Int("numExecuted", numExecuted),
zap.Uint64("numToExecute", numToExecute),
zap.Duration("eta", eta),
)
}
}
j.etaMetric.Set(0)
}
if !restarted {
ctx.Log.Info("executed operations",
zap.Int("numExecuted", numExecuted),
)
} else {
ctx.Log.Debug("executed operations",
zap.Int("numExecuted", numExecuted),
)
}
return numExecuted, nil
}
func (j *Jobs) Clear() error {
return j.state.Clear()
}
// Commit the versionDB to the underlying database.
func (j *Jobs) Commit() error {
return j.db.Commit()
}
type JobsWithMissing struct {
*Jobs
// keep the missing ID set in memory to avoid unnecessary database reads and
// writes.
missingIDs set.Set[ids.ID]
removeFromMissingIDs, addToMissingIDs set.Set[ids.ID]
}
func NewWithMissing(
db database.Database,
metricsNamespace string,
metricsRegisterer prometheus.Registerer,
) (*JobsWithMissing, error) {
innerJobs, err := New(db, metricsNamespace, metricsRegisterer)
if err != nil {
return nil, err
}
jobs := &JobsWithMissing{
Jobs: innerJobs,
}
missingIDs, err := jobs.state.MissingJobIDs()
jobs.missingIDs.Add(missingIDs...)
return jobs, err
}
// SetParser tells this job queue how to parse jobs from the database.
func (jm *JobsWithMissing) SetParser(parser Parser) error {
jm.state.parser = parser
return jm.cleanRunnableStack()
}
func (jm *JobsWithMissing) Clear() error {
if err := jm.state.RemoveMissingJobIDs(jm.missingIDs); err != nil {
return err
}
jm.missingIDs.Clear()
jm.addToMissingIDs.Clear()
jm.removeFromMissingIDs.Clear()
return jm.Jobs.Clear()
}
func (jm *JobsWithMissing) Has(jobID ids.ID) (bool, error) {
if jm.missingIDs.Contains(jobID) {
return false, nil
}
return jm.Jobs.Has(jobID)
}
// Push adds a new job to the queue. Returns true if [job] was added to the queue and false
// if [job] was already in the queue.
func (jm *JobsWithMissing) Push(job Job) (bool, error) {
jobID := job.ID()
if has, err := jm.Has(jobID); err != nil {
return false, fmt.Errorf("failed to check for existing job %s due to %w", jobID, err)
} else if has {
return false, nil
}
deps, err := job.MissingDependencies()
if err != nil {
return false, err
}
// Store this job into the database.
if err := jm.state.PutJob(job); err != nil {
return false, fmt.Errorf("failed to write job due to %w", err)
}
if deps.Len() != 0 {
// This job needs to block on a set of dependencies.
for depID := range deps {
if err := jm.state.AddDependency(depID, jobID); err != nil {
return false, fmt.Errorf("failed to add blocking for depID %s, jobID %s", depID, jobID)
}
}
return true, nil
}
// This job doesn't have any dependencies, so it should be placed onto the
// executable stack.
if err := jm.state.AddRunnableJob(jobID); err != nil {
return false, fmt.Errorf("failed to add %s as a runnable job due to %w", jobID, err)
}
return true, nil
}
// AddMissingID adds [jobID] to missingIDs
func (jm *JobsWithMissing) AddMissingID(jobIDs ...ids.ID) {
for _, jobID := range jobIDs {
if !jm.missingIDs.Contains(jobID) {
jm.missingIDs.Add(jobID)
jm.addToMissingIDs.Add(jobID)
jm.removeFromMissingIDs.Remove(jobID)
}
}
}
// RemoveMissingID removes [jobID] from missingIDs
func (jm *JobsWithMissing) RemoveMissingID(jobIDs ...ids.ID) {
for _, jobID := range jobIDs {
if jm.missingIDs.Contains(jobID) {
jm.missingIDs.Remove(jobID)
jm.addToMissingIDs.Remove(jobID)
jm.removeFromMissingIDs.Add(jobID)
}
}
}
func (jm *JobsWithMissing) MissingIDs() []ids.ID { return jm.missingIDs.List() }
func (jm *JobsWithMissing) NumMissingIDs() int { return jm.missingIDs.Len() }
// Commit the versionDB to the underlying database.
func (jm *JobsWithMissing) Commit() error {
if jm.addToMissingIDs.Len() != 0 {
if err := jm.state.AddMissingJobIDs(jm.addToMissingIDs); err != nil {
return err
}
jm.addToMissingIDs.Clear()
}
if jm.removeFromMissingIDs.Len() != 0 {
if err := jm.state.RemoveMissingJobIDs(jm.removeFromMissingIDs); err != nil {
return err
}
jm.removeFromMissingIDs.Clear()
}
return jm.Jobs.Commit()
}
// cleanRunnableStack iterates over the jobs on the runnable stack and resets any job
// that has missing dependencies to block on those dependencies.
// Note: the jobs queue ensures that no job with missing dependencies will be placed
// on the runnable stack in the first place.
// However, for specific VM implementations blocks may be committed via a two stage commit
// (ex. platformvm Proposal and Commit/Abort blocks). This can cause an issue where if the first stage
// is executed immediately before the node dies, it will be removed from the runnable stack
// without writing the state transition to the VM's database. When the node restarts, the
// VM will not have marked the first block (the proposal block as accepted), but it could
// have already been removed from the jobs queue. cleanRunnableStack handles this case.
func (jm *JobsWithMissing) cleanRunnableStack() error {
runnableJobsIter := jm.state.runnableJobIDs.NewIterator()
defer runnableJobsIter.Release()
for runnableJobsIter.Next() {
jobIDBytes := runnableJobsIter.Key()
jobID, err := ids.ToID(jobIDBytes)
if err != nil {
return fmt.Errorf("failed to convert jobID bytes into ID due to: %w", err)
}
job, err := jm.state.GetJob(jobID)
if err != nil {
return fmt.Errorf("failed to retrieve job on runnnable stack due to: %w", err)
}
deps, err := job.MissingDependencies()
if err != nil {
return fmt.Errorf("failed to retrieve missing dependencies of job on runnable stack due to: %w", err)
}
if deps.Len() == 0 {
continue
}
// If the job has missing dependencies, remove it from the runnable stack
if err := jm.state.runnableJobIDs.Delete(jobIDBytes); err != nil {
return fmt.Errorf("failed to delete jobID from runnable stack due to: %w", err)
}
// Add the missing dependencies to the set that needs to be fetched.
jm.AddMissingID(deps.List()...)
for depID := range deps {
if err := jm.state.AddDependency(depID, jobID); err != nil {
return fmt.Errorf("failed to add blocking for depID %s, jobID %s while cleaning the runnable stack", depID, jobID)
}
}
}
errs := wrappers.Errs{}
errs.Add(
runnableJobsIter.Error(),
jm.Commit(),
)
return errs.Err
}