gunk-dev/the-valley

queue.go

source8bf5a934d291main's published tip history
8.2 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.
93func operatorQueue(repos []listed, h handleMap, l listing, c *costs, p *pairing, stderr io.Writer, each func(listed, jsonRepo)) (waiting, moving []*entry) {
94 for _, r := range repos {
95 if r.err != nil {
96 continue
97 }
98 end := phase("changes")
99 view := r.pendingView(h)
100 r.addCarried(&view, stderr)
101 end()
102 end = phase("checks")
103 r.addChecks(&view, l.drafts && l.checks, c, stderr)
104 end()
105 r.addConflicts(&view, p, stderr)
106 wa, mo := r.queue(view, h, l, l.verbose)
107 waiting, moving = append(waiting, wa...), append(moving, mo...)
108 if each != nil {
109 each(r, view)
110 }
111 }
112 byLanding(waiting)
113 byLanding(moving)
114 return waiting, moving
115}
116
117// byLanding orders rows as they would land: by when the top's active
118// version became ready, oldest first, then by project and change ID.
119func byLanding(rows []*entry) {
120 sort.SliceStable(rows, func(i, j int) bool {
121 a, b := rows[i], rows[j]
122 if a.since != b.since {
123 return a.since < b.since
124 }
125 if a.repo != b.repo {
126 return a.repo < b.repo
127 }
128 return a.id < b.id
129 })
130}
131
132// checkCount is a version's checks as a row says them: passed out of
133// required when every nix check passed, and otherwise how many are
134// pending or failed. A check of another runner is not counted, since the
135// host worker does not run it. It is "" when the checks were not read.
136func checkCount(c *jsonVersionChecks) string {
137 if c == nil {
138 return ""
139 }
140 count := map[string]int{}
141 for _, q := range c.Required {
142 count[q.State]++
143 }
144 built := len(c.Required) - count[checkOther]
145 switch {
146 case built == 0:
147 return "no checks required"
148 case count[checkFailed] > 0:
149 return counted(count[checkFailed], "check") + " failed"
150 case count[checkPending] > 0:
151 return counted(count[checkPending], "check") + " pending"
152 }
153 return fmt.Sprintf("checks %d/%d", count[checkPassed], built)
154}
155
156// failedNames names the checks that failed, up to three.
157func failedNames(c *jsonVersionChecks) string {
158 var names []string
159 for _, q := range c.Required {
160 if q.State == checkFailed {
161 names = append(names, q.Name)
162 }
163 }
164 return firstFew(names)
165}
166
167// states is the state of every change a repository records, by its
168// handle key.
169func (r listed) states() map[handleKey]string {
170 states := map[handleKey]string{}
171 for _, n := range r.snap.all {
172 states[handleKey{r.name, n.record.Target, n.id}] = n.record.State
173 }
174 return states
175}
176
177// changeEntry is a recorded change as an entry of section.
178func (r listed) changeEntry(section string, c jsonChange, h handleMap, l listing, states map[handleKey]string) *entry {
179 v := c.Versions[c.Active-1]
180 e := &entry{section: section, repo: r.name, id: c.ChangeID, version: v.N, record: c.Record,
181 name: fmt.Sprintf("%s v%d", c.Handle, v.N), ref: c.Handle, head: v.Head, title: readable(v.Title)}
182 if len(c.Knots) > 0 {
183 e.knot = readable(c.Knots[0].ID)
184 }
185 if section == "drafts" && v.Branch != "" {
186 e.name += " on " + branchName(v.Branch)
187 }
188 for _, id := range c.Includes {
189 k := handleKey{r.name, c.Target, id}
190 if st := states[k]; st != change.Ready && st != change.Draft {
191 e.includes = append(e.includes, fmt.Sprintf("includes %s (%s)", h[k], st))
192 }
193 }
194 if v.Landing == uncertain {
195 e.notes = append(e.notes, "uncertain: it may have landed without its evidence being stored")
196 }
197 if c.Restack != nil && c.Restack.Current {
198 name := func(id string) string { return h[handleKey{r.name, c.Target, id}] }
199 e.notes = append(e.notes, "restack: "+readable(restackSays(c.Restack.Report, name)))
200 }
201 if l.checks {
202 e.requires = r.snap.checks(v.Head)
203 }
204 if v.Checks != nil {
205 e.state, e.checks, e.see = v.Checks, checksLine(v.Checks), workerSee(v.Checks)
206 }
207 e.conflicts = conflictLines(c, l.checks || l.verbose)
208 if c.ConflictsUnknown != nil {
209 e.unknown = *c.ConflictsUnknown
210 }
211 return e
212}
213
214// dated gives each entry its head's commit time, age and author, and a
215// change's entry the commit time of its record.
216func (r listed) dated(all []*entry) {
217 for _, e := range all {
218 out, _ := r.g.output(true, "log", "-1", "--format=%ct%x00%cr%x00%an", e.head)
219 if fields := strings.SplitN(out, "\x00", 3); len(fields) == 3 {
220 e.when = parseUnix(fields[0])
221 e.age, e.author = fields[1], readable(fields[2])
222 }
223 if e.record != "" {
224 out, _ := r.g.output(true, "log", "-1", "--format=%ct", e.record)
225 e.since = parseUnix(out)
226 }
227 }
228}