gunk-dev/the-valley

queue.go

sourcebc00697b19b5main's published tip history
8.3 KiB
1package main
2
3// The operator's queue: the ready changes that wait on an approval, in the
4// order they would land, and the ready changes that move without one.
5// valley pending prints it (pending.go), with each ready change's
6// conflicts with the others (conflicts.go), and valley review with no
7// change walks it (walk.go), without them.
8//
9// A ready change whose active version has not landed is moving when a
10// current restack report says its restack stopped short of complete, when
11// one of its required checks failed, or when it is approved: its active
12// version has an integration request at its head, or an earlier version's
13// request carries to it (addCarried, pending.go). Every other one waits on
14// an approval. Waiting changes are stacked by ancestry among themselves
15// (stack): a waiting change that another's head contains folds into that
16// change's row, since approving the top lands every layer. A moving change
17// is a row of its own whatever it is stacked on, since it moves on its own.
18//
19// Rows are in landing order. The check worker builds ready versions in
20// the order it first saw them ready, then by project, then by change ID
21// (worker.go), and the integrator lands an approved request once its
22// evidence is complete. So a row's place is when its top's active version
23// became ready, oldest first, then its project and change ID. A record
24// carries no time, so when a version became ready is read as the commit
25// time of the change's record: the last write to the record of a ready
26// change is the one that readied its active version. The worker's own
27// order is the scan in which its pass first saw each version, which only
28// that pass holds, so versions readied between the same two scans may
29// land in another order than their rows.
30
31import (
32 "fmt"
33 "io"
34 "sort"
35 "strings"
36
37 "the-valley/change"
38)
39
40// queue is a repository's rows: the stacks of changes that wait on an
41// approval, each by its top, and the moving changes, each with its glyph
42// and what it says. Neither is in landing order yet (byLanding). With
43// measured, each entry is measured for its block (measure).
44func (r listed) queue(view jsonRepo, h handleMap, l listing, measured bool) (waiting, moving []*entry) {
45 states := r.states()
46 var wait []*entry
47 for _, c := range view.Changes {
48 e := r.changeEntry("ready", c, h, l, states)
49 v := c.Versions[c.Active-1]
50 switch {
51 case c.Restack != nil && c.Restack.Current && c.Restack.Report.Result != change.RestackComplete:
52 name := func(id string) string { return h[handleKey{r.name, c.Target, id}] }
53 e.glyph, e.colour = "◇", colourWaiting
54 if c.Restack.Report.Result == change.RestackConflict {
55 e.glyph, e.colour = "✗", colourRefused
56 }
57 e.says = "restack: " + readable(restackSays(c.Restack.Report, name))
58 case v.Checks != nil && v.Checks.State == checkFailed:
59 e.glyph, e.colour, e.says = "✗", colourRefused, failedNames(v.Checks)+" failed"
60 if c.Approved != nil && *c.Approved {
61 e.says = "approved · " + e.says
62 }
63 case c.Approved != nil && *c.Approved:
64 e.glyph, e.colour, e.says = "◐", colourWorking, "approved"
65 if _, n := rowChecks(v.Checks); n != "" {
66 e.says += " · " + n
67 }
68 default:
69 wait = append(wait, e)
70 continue
71 }
72 moving = append(moving, e)
73 }
74 r.dated(append(append([]*entry{}, wait...), moving...))
75 waiting = r.snap.stack(wait, measured)
76 for _, e := range moving {
77 if measured {
78 r.snap.measure(e)
79 }
80 }
81 return waiting, moving
82}
83
84// operatorQueue is the queue across every readable repository in repos,
85// as valley pending and valley review's walk (walk.go) both read it: the
86// rows that wait on an approval and the moving rows, each in landing
87// order. A change's checks are read before it is classed, so a change
88// whose check failed is moving rather than waiting. With --drafts and
89// --checks, the drafts' checks are read with them, in the same batch.
90// With p, each ready change is marked with its conflicts with the others
91// (conflicts.go). each, when it is not nil, is given every repository's
92// view as it is read. The checks read the host's signers as use says
93// (evidenceUse).
94func operatorQueue(repos []listed, h handleMap, l listing, c *costs, p *pairing, use evidenceUse, stderr io.Writer, each func(listed, jsonRepo)) (waiting, moving []*entry) {
95 for _, r := range repos {
96 if r.err != nil {
97 continue
98 }
99 end := phase("changes")
100 view := r.pendingView(h)
101 r.addCarried(&view, stderr)
102 end()
103 end = phase("checks")
104 r.addChecks(&view, l.drafts && l.checks, c, use, stderr)
105 end()
106 r.addConflicts(&view, p, stderr)
107 wa, mo := r.queue(view, h, l, l.verbose)
108 waiting, moving = append(waiting, wa...), append(moving, mo...)
109 if each != nil {
110 each(r, view)
111 }
112 }
113 byLanding(waiting)
114 byLanding(moving)
115 return waiting, moving
116}
117
118// byLanding orders rows as they would land: by when the top's active
119// version became ready, oldest first, then by project and change ID.
120func byLanding(rows []*entry) {
121 sort.SliceStable(rows, func(i, j int) bool {
122 a, b := rows[i], rows[j]
123 if a.since != b.since {
124 return a.since < b.since
125 }
126 if a.repo != b.repo {
127 return a.repo < b.repo
128 }
129 return a.id < b.id
130 })
131}
132
133// checkCount is a version's checks as a row says them: passed out of
134// required when every nix check passed, and otherwise how many are
135// pending or failed. A check of another runner is not counted, since the
136// host worker does not run it. It is "" when the checks were not read.
137func checkCount(c *jsonVersionChecks) string {
138 if c == nil {
139 return ""
140 }
141 count := map[string]int{}
142 for _, q := range c.Required {
143 count[q.State]++
144 }
145 built := len(c.Required) - count[checkOther]
146 switch {
147 case built == 0:
148 return "no checks required"
149 case count[checkFailed] > 0:
150 return counted(count[checkFailed], "check") + " failed"
151 case count[checkPending] > 0:
152 return counted(count[checkPending], "check") + " pending"
153 }
154 return fmt.Sprintf("checks %d/%d", count[checkPassed], built)
155}
156
157// failedNames names the checks that failed, up to three.
158func failedNames(c *jsonVersionChecks) string {
159 var names []string
160 for _, q := range c.Required {
161 if q.State == checkFailed {
162 names = append(names, q.Name)
163 }
164 }
165 return firstFew(names)
166}
167
168// states is the state of every change a repository records, by its
169// handle key.
170func (r listed) states() map[handleKey]string {
171 states := map[handleKey]string{}
172 for _, n := range r.snap.all {
173 states[handleKey{r.name, n.record.Target, n.id}] = n.record.State
174 }
175 return states
176}
177
178// changeEntry is a recorded change as an entry of section.
179func (r listed) changeEntry(section string, c jsonChange, h handleMap, l listing, states map[handleKey]string) *entry {
180 v := c.Versions[c.Active-1]
181 e := &entry{section: section, repo: r.name, id: c.ChangeID, version: v.N, record: c.Record,
182 name: fmt.Sprintf("%s v%d", c.Handle, v.N), ref: c.Handle, head: v.Head, title: readable(v.Title)}
183 if len(c.Knots) > 0 {
184 e.knot = readable(c.Knots[0].ID)
185 }
186 if section == "drafts" && v.Branch != "" {
187 e.name += " on " + branchName(v.Branch)
188 }
189 for _, id := range c.Includes {
190 k := handleKey{r.name, c.Target, id}
191 if st := states[k]; st != change.Ready && st != change.Draft {
192 e.includes = append(e.includes, fmt.Sprintf("includes %s (%s)", h[k], st))
193 }
194 }
195 if v.Landing == uncertain {
196 e.notes = append(e.notes, "uncertain: it may have landed without its evidence being stored")
197 }
198 if c.Restack != nil && c.Restack.Current {
199 name := func(id string) string { return h[handleKey{r.name, c.Target, id}] }
200 e.notes = append(e.notes, "restack: "+readable(restackSays(c.Restack.Report, name)))
201 }
202 if l.checks {
203 e.requires = r.snap.checks(v.Head)
204 }
205 if v.Checks != nil {
206 e.state, e.checks, e.see = v.Checks, checksLine(v.Checks), workerSee(v.Checks)
207 }
208 e.conflicts = conflictLines(c, l.checks || l.verbose)
209 if c.ConflictsUnknown != nil {
210 e.unknown = *c.ConflictsUnknown
211 }
212 return e
213}
214
215// dated gives each entry its head's commit time, age and author, and a
216// change's entry the commit time of its record.
217func (r listed) dated(all []*entry) {
218 for _, e := range all {
219 out, _ := r.g.output(true, "log", "-1", "--format=%ct%x00%cr%x00%an", e.head)
220 if fields := strings.SplitN(out, "\x00", 3); len(fields) == 3 {
221 e.when = parseUnix(fields[0])
222 e.age, e.author = fields[1], readable(fields[2])
223 }
224 if e.record != "" {
225 out, _ := r.g.output(true, "log", "-1", "--format=%ct", e.record)
226 e.since = parseUnix(out)
227 }
228 }
229}