-
Notifications
You must be signed in to change notification settings - Fork 659
/
jobs.go
173 lines (146 loc) · 3.49 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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package queue
import (
"errors"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/versiondb"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/wrappers"
)
var (
errEmpty = errors.New("no available containers")
errDuplicate = errors.New("duplicated container")
)
// Jobs ...
type Jobs struct {
parser Parser
baseDB database.Database
db *versiondb.Database
// Dynamic sized stack of ready to execute items
// Map from itemID to list of itemIDs that are blocked on this item
state prefixedState
}
// New ...
func New(db database.Database) (*Jobs, error) {
jobs := &Jobs{
baseDB: db,
db: versiondb.New(db),
}
jobs.state.jobs = jobs
if _, err := jobs.HasNext(); err == nil {
return jobs, nil
}
return jobs, jobs.state.SetStackSize(jobs.db, 0)
}
// SetParser ...
func (j *Jobs) SetParser(parser Parser) { j.parser = parser }
// Push ...
func (j *Jobs) Push(job Job) error {
deps, err := job.MissingDependencies()
if err != nil {
return err
}
if deps.Len() != 0 {
return j.block(job, deps)
}
return j.push(job)
}
// Pop ...
func (j *Jobs) Pop() (Job, error) {
size, err := j.state.StackSize(j.db)
if err != nil {
return nil, err
}
if size == 0 {
return nil, errEmpty
}
if err := j.state.SetStackSize(j.db, size-1); err != nil {
return nil, err
}
job, err := j.state.StackIndex(j.db, size-1)
if err != nil {
return nil, err
}
return job, j.state.DeleteStackIndex(j.db, size-1)
}
// HasNext ...
func (j *Jobs) HasNext() (bool, error) {
size, err := j.state.StackSize(j.db)
return size > 0, err
}
// Execute ...
func (j *Jobs) Execute(job Job) error {
if err := job.Execute(); err != nil {
return err
}
jobID := job.ID()
blocking, err := j.state.Blocking(j.db, jobID)
if err != nil {
return err
}
if err := j.state.DeleteBlocking(j.db, jobID, blocking); err != nil {
return err
}
for _, blockedID := range blocking {
job, err := j.state.Job(j.db, blockedID)
if err != nil {
return err
}
deps, err := job.MissingDependencies()
if err != nil {
return err
}
if deps.Len() > 0 {
continue
}
// TODO: Calling execute here ensures that double decision blocks are
// always called atomically with their proposal blocks. This is
// only a quick fix and should be handled in a more robust manner.
if err := job.Execute(); err != nil {
return err
}
if err := j.state.DeleteJob(j.db, blockedID); err != nil {
return err
}
if err := j.push(job); err != nil {
return err
}
}
return nil
}
// Commit ...
func (j *Jobs) Commit() error { return j.db.Commit() }
func (j *Jobs) push(job Job) error {
if has, err := j.state.HasJob(j.db, job.ID()); err != nil {
return err
} else if has {
return errDuplicate
}
if err := j.state.SetJob(j.db, job); err != nil {
return err
}
errs := wrappers.Errs{}
size, err := j.state.StackSize(j.db)
errs.Add(err)
errs.Add(j.state.SetStackIndex(j.db, size, job))
errs.Add(j.state.SetStackSize(j.db, size+1))
return errs.Err
}
func (j *Jobs) block(job Job, deps ids.Set) error {
if has, err := j.state.HasJob(j.db, job.ID()); err != nil {
return err
} else if has {
return errDuplicate
}
if err := j.state.SetJob(j.db, job); err != nil {
return err
}
jobID := job.ID()
for _, depID := range deps.List() {
if err := j.state.AddBlocking(j.db, depID, jobID); err != nil {
return err
}
}
return nil
}