gunk-dev/the-valley

recover.go

source19cf531ff6f0main's published tip history
14.9 KiB
1package main
2
3import (
4 "fmt"
5 "strings"
6
7 "the-valley/change"
8 "the-valley/rebase"
9)
10
11// Recovery reads origin and a journal, and decides each write the journal
12// names, in the order a pass makes them: a family's branch push, then its
13// versions, lower layers first, then its reports. Each write is one of:
14//
15// - done: origin holds it, or has moved on from it;
16// - finish: origin does not hold it, and everything the write was built
17// on still holds, so it is still valid to push;
18// - stop: it must not be pushed. A stop is final when no later pass could
19// push it either, and terminal when it is also a local failure whose
20// diagnostics the journal keeps.
21//
22// A branch push origin did not take is finished only while nothing it was
23// prepared from has moved: main is at the tip it was rebased onto, every
24// branch at the tip its lease names, and every record at the record
25// commit read. Otherwise it stops, and the next pass prepares the family
26// again. A version is finished only while its branch is at the prepared
27// head, or the branch push is to be finished first, and its record is
28// still the record commit read, so the change is still ready at the head
29// the capture read. A version stops on its author's movement, a
30// withdrawal, an unready, a submission, or a main that no longer descends
31// from the tip the family was rebased onto. A deleted record is a
32// terminal failure: the version cannot be added, and the hook takes no
33// report about a change it does not record. A family whose writes stop
34// does not stop another's.
35//
36// A related integration request that might still land holds the branch
37// push and every version, which wait for a later pass: requests are read
38// again, by name and by every version head the changes have had, the
39// prepared heads included. A report is finished once what it says
40// published is on origin, or will be once the writes before it are
41// finished. A report whose claims no longer can be, because a version it
42// names stopped, stops, and a report of what did happen is built in its
43// place.
44//
45// In --report-only mode recovery decides and pushes nothing.
46
47// What recovery decides about a write.
48const (
49 writeDone = "done"
50 writeFinish = "finish"
51 writeStop = "stop"
52)
53
54// Recovery is what recovery decided about a journal.
55type Recovery struct {
56 Journal string `json:"journal"` // the journal's path
57 Operation string `json:"operation,omitempty"` // the operation it names
58 Error string `json:"error,omitempty"` // why it was not read, so nothing in it is decided
59 Settled bool `json:"settled"` // every write is done or stopped for good
60 Decisions []Decision `json:"decisions"`
61}
62
63// Decision is what recovery decided about one write.
64type Decision struct {
65 Family int `json:"family"` // the family's place in the journal
66 Write string `json:"write"` // branches, version or report
67 ID string `json:"change_id,omitempty"` // the change a version or a report is about
68 Commit string `json:"commit,omitempty"` // the record or report commit it pushes
69 Action string `json:"action"` // done, finish or stop
70 Final bool `json:"final"` // a stop no later pass can finish
71 Terminal bool `json:"terminal"` // a final stop that is a local failure, kept with its diagnostics
72 Why string `json:"why"`
73}
74
75func (d Decision) settled() bool { return d.Action == writeDone || d.Action == writeStop && d.Final }
76
77// reading is what recovery reads origin through.
78type reading struct {
79 s *rebase.Snapshot
80 requests []Request
81 evidence string // the evidence fingerprint origin shows now
82 in Incoming // against origin's tip now
83 is Ancestry // in the scratch repository
84 have func(oid string) bool // whether the scratch repository holds an object
85 landed func(id string, v change.Version) bool // whether a version has landed
86}
87
88// decide decides every write the journal names.
89func decide(j *Journal, rd reading) *Recovery {
90 rec := &Recovery{Operation: j.Operation, Decisions: []Decision{}, Settled: true}
91 for i, f := range j.Families {
92 rec.Decisions = append(rec.Decisions, rd.family(i, j, f)...)
93 }
94 for _, d := range rec.Decisions {
95 rec.Settled = rec.Settled && d.settled()
96 }
97 return rec
98}
99
100func (rd reading) family(i int, j *Journal, f JournalFamily) []Decision {
101 var out []Decision
102 stopped := func(d *Decision, final bool, why string, a ...any) {
103 d.Action, d.Final, d.Why = writeStop, final, fmt.Sprintf(why, a...)
104 }
105 layers := map[string]ObservedLayer{}
106 for _, l := range f.Observed.Layers {
107 layers[l.ID] = l
108 }
109
110 // Whether main still descends from the tip the family was rebased onto.
111 compatible, final, target := true, true, ""
112 switch descends, err := rd.is(j.Tip, rd.s.Tip); {
113 case rd.s.Tip == "":
114 compatible, target = false, "origin has no "+rebase.Target
115 case err != nil:
116 compatible, final, target = false, false, fmt.Sprintf("whether %s at %s descends from %s cannot be told", rebase.Target, short(rd.s.Tip), short(j.Tip))
117 case !descends:
118 compatible, target = false, fmt.Sprintf("%s at %s no longer descends from %s, the tip the family was rebased onto", rebase.Target, short(rd.s.Tip), short(j.Tip))
119 }
120 held := rd.holding(f)
121 var submitted []string
122 for _, v := range f.Versions {
123 if cv, ok := capturedVersion(v, layers[v.ID]); ok && rd.landed(v.ID, cv) {
124 submitted = append(submitted, fmt.Sprintf("%s v%d has been submitted", v.Handle, cv.N))
125 }
126 }
127
128 // The branch push.
129 branches := Decision{Action: ""}
130 if b := f.Branches; b != nil {
131 d := Decision{Family: i, Write: "branches"}
132 var moved, other, missing []string
133 for _, u := range b.Updates {
134 at := rd.s.Refs["refs/heads/"+u.Branch]
135 switch {
136 case at == u.New || at != "" && at != u.Old && rd.reaches(u.New, at):
137 moved = append(moved, u.Branch)
138 case at == u.Old:
139 case at == "":
140 other = append(other, "origin has no "+u.Branch)
141 default:
142 other = append(other, fmt.Sprintf("origin's %s is at %s, not %s", u.Branch, short(at), short(u.Old)))
143 }
144 if !rd.have(u.New) {
145 missing = append(missing, short(u.New))
146 }
147 }
148 recordMoved := ""
149 for _, v := range f.Versions {
150 if rd.s.Refs[v.Ref] != v.Old {
151 recordMoved = rd.recordNow(v, layers[v.ID].Head)
152 break
153 }
154 }
155 switch {
156 case len(moved) > 0:
157 d.Action, d.Why = writeDone, "origin took the branch push: "+strings.Join(moved, ", ")+" at the prepared heads, or past them"
158 case len(other) > 0:
159 stopped(&d, true, "the branches were not pushed, and %s since the family was prepared", strings.Join(other, "; "))
160 case len(missing) > 0:
161 stopped(&d, true, "the scratch repository no longer holds the prepared head %s", strings.Join(missing, ", "))
162 case !compatible:
163 stopped(&d, final, "the branches were not pushed, and %s", target)
164 case rd.s.Tip != j.Tip:
165 stopped(&d, true, "%s moved from %s to %s before the branches were pushed, so the next pass prepares the family again", rebase.Target, short(j.Tip), short(rd.s.Tip))
166 case recordMoved != "":
167 stopped(&d, true, "the branches were not pushed, and %s", recordMoved)
168 case len(submitted) > 0:
169 stopped(&d, true, "the branches were not pushed, and %s", strings.Join(submitted, "; "))
170 case len(held) > 0:
171 stopped(&d, false, "the branches wait: %s", strings.Join(held, "; "))
172 default:
173 d.Action, d.Why = writeFinish, "origin has not moved since the family was prepared"
174 }
175 branches = d
176 out = append(out, d)
177 }
178
179 // Each version, lower layers first.
180 versions := map[string]Decision{}
181 for _, v := range f.Versions {
182 d := Decision{Family: i, Write: "version", ID: v.ID, Commit: v.Commit}
183 at := rd.s.Refs[v.Ref]
184 branch := rd.s.Refs["refs/heads/"+v.Branch]
185 switch {
186 case at == v.Commit || at != "" && at != v.Old && rd.reaches(v.Commit, at):
187 d.Action, d.Why = writeDone, fmt.Sprintf("origin's record of %s holds v%d", v.Handle, v.Version)
188 case at == "":
189 stopped(&d, true, "origin no longer holds a record of %s, so v%d cannot be added", v.Handle, v.Version)
190 d.Terminal = true
191 case at != v.Old:
192 stopped(&d, true, "%s, so v%d is not added", rd.recordNow(v, layers[v.ID].Head), v.Version)
193 case !rd.have(v.Commit):
194 stopped(&d, true, "the scratch repository no longer holds the record commit %s", short(v.Commit))
195 case branches.Action == writeStop:
196 stopped(&d, branches.Final, "its branch push stopped: %s", branches.Why)
197 case !compatible:
198 stopped(&d, final, "%s", target)
199 case len(submitted) > 0:
200 stopped(&d, true, "%s", strings.Join(submitted, "; "))
201 case branches.Action == writeFinish:
202 d.Action, d.Why = writeFinish, "once the branches are pushed: the record is ready at the head the capture read"
203 case branch == "":
204 stopped(&d, true, "origin no longer has %s, so v%d is not added", v.Branch, v.Version)
205 case branch != v.Head:
206 stopped(&d, true, "origin's %s is at %s, not the prepared head %s: its author moved it, so v%d is not added", v.Branch, short(branch), short(v.Head), v.Version)
207 case len(held) > 0:
208 stopped(&d, false, "paused: %s", strings.Join(held, "; "))
209 default:
210 d.Action, d.Why = writeFinish, fmt.Sprintf("origin's %s is at the prepared head, and the record is ready at the head the capture read", v.Branch)
211 }
212 versions[v.ID] = d
213 out = append(out, d)
214 }
215
216 // Each report.
217 for _, r := range f.Reports {
218 p := r.Pending
219 d := Decision{Family: i, Write: "report", ID: p.ID, Commit: p.Commit}
220 at := rd.s.Refs[p.Ref]
221 current, found := rd.s.Report(p.ID)
222 // What the report says was published, against what became of it.
223 // The journal was checked as it was read (loadJournal), so each
224 // version the report names is one the family adds, decided above.
225 claims, claimFinal, pending := "", false, false
226 for _, l := range p.Report.Layers {
227 if l.Published == nil {
228 continue
229 }
230 switch v := versions[l.ChangeID]; {
231 case branches.Action == writeStop:
232 claims, claimFinal = "its branches were not pushed: "+branches.Why, branches.Final
233 case l.Published.Record != nil && v.Action == writeStop:
234 claims, claimFinal = fmt.Sprintf("%s's version was not added: %s", l.ChangeID, v.Why), v.Final
235 case l.Published.Record != nil && v.Action == writeFinish || branches.Action == writeFinish:
236 pending = true
237 }
238 if claims != "" {
239 break
240 }
241 }
242 switch {
243 case at == p.Commit || at != "" && at != p.Old && rd.reaches(p.Commit, at):
244 d.Action, d.Why = writeDone, "origin holds the report"
245 case rd.s.Refs[change.Ref(rebase.Target, p.ID)] == "":
246 stopped(&d, true, "origin no longer holds a record of %s, and the hook takes no report about a change it does not record. The report is kept here: %s, %s",
247 p.ID, p.Report.Result, oneLine(p.Report))
248 d.Terminal = true
249 case found && sameReport(current.Report, p.Report):
250 d.Action, d.Why = writeDone, "origin's report already says the same"
251 case at != p.Old:
252 stopped(&d, true, "origin's report moved to %s since this one was built on %s", short(at), orNone(p.Old))
253 case !rd.have(p.Commit):
254 stopped(&d, true, "the scratch repository no longer holds the report commit %s", short(p.Commit))
255 case claims != "":
256 stopped(&d, claimFinal, "the attempt did not end as this report says, since %s; a report of what did happen is built in its place", claims)
257 case pending:
258 d.Action, d.Why = writeFinish, "once the writes it reports are pushed"
259 default:
260 d.Action, d.Why = writeFinish, "origin's report is still the one it was built on"
261 }
262 out = append(out, d)
263 }
264 return out
265}
266
267// reaches reports whether a commit is in the history of another: whether
268// a ref moved on from a write origin took.
269func (rd reading) reaches(commit, at string) bool {
270 in, err := rd.is(commit, at)
271 return err == nil && in
272}
273
274// holding is why the requests origin holds now might still land one of
275// the family's heads: each related request whose outcome does not release
276// it, as the planner judges them. A family's heads are every version head
277// its changes have had, and the prepared heads.
278func (rd reading) holding(f JournalFamily) []string {
279 if len(f.Versions) == 0 {
280 return nil
281 }
282 m := Members{Names: map[string]bool{}}
283 seen := map[string]bool{}
284 for _, v := range f.Versions {
285 m.Names[change.RequestName(v.ID)] = true
286 for _, version := range v.Record.Versions {
287 if version.Branch != "" {
288 m.Names[rebase.ChangeName(version.Branch)] = true
289 }
290 if h := rebase.HeadOf(version); !seen[h] {
291 seen[h] = true
292 m.Heads = append(m.Heads, h)
293 }
294 }
295 }
296 var held []string
297 for _, r := range rd.requests {
298 by, err := Relate(r, m, rd.in)
299 if err != nil {
300 held = append(held, fmt.Sprintf("request %s: whether it is related cannot be determined: %v", r.Name, err))
301 continue
302 }
303 if by == nil {
304 continue
305 }
306 o, found := rd.s.OutcomeOf(r.Name)
307 if _, releases, why := Currency(o, found, r.Head, rd.s.Tip, rd.evidence); !releases {
308 held = append(held, fmt.Sprintf("request %s might still land: %s", r.Name, why))
309 }
310 }
311 return held
312}
313
314// recordNow says how the change's record moved from the record commit a
315// version was built on. captured is the head the capture read the change
316// at.
317func (rd reading) recordNow(v *rebase.PendingVersion, captured string) string {
318 at := rd.s.Refs[v.Ref]
319 if at == "" {
320 return "origin no longer holds a record of " + v.Handle
321 }
322 for _, n := range rd.s.Changes {
323 if n.ID != v.ID {
324 continue
325 }
326 r, active := n.Record, n.Record.ActiveVersion()
327 switch {
328 case r.State == change.Withdrawn:
329 return v.Handle + " was withdrawn"
330 case r.State != change.Ready:
331 return fmt.Sprintf("%s is %s, not ready", v.Handle, r.State)
332 case rebase.HeadOf(active) != captured:
333 return fmt.Sprintf("%s is at v%d, %s: its author moved it", v.Handle, active.N, short(rebase.HeadOf(active)))
334 }
335 return fmt.Sprintf("the record of %s moved to %s since it was read, so the lease is gone", v.Handle, short(at))
336 }
337 return fmt.Sprintf("the record of %s at %s does not read", v.Handle, short(at))
338}
339
340// capturedVersion is the version a family's layer was at when the capture
341// read it, from the record the version was built on.
342func capturedVersion(v *rebase.PendingVersion, l ObservedLayer) (change.Version, bool) {
343 for _, version := range v.Record.Versions {
344 if version.N == l.Version {
345 return version, true
346 }
347 }
348 return change.Version{}, false
349}
350
351// oneLine is a report as one line, for a diagnostic.
352func oneLine(r *change.RestackReport) string {
353 var layers []string
354 for _, l := range r.Layers {
355 s := fmt.Sprintf("%s v%d at %s", l.ChangeID, l.Version, short(strings.TrimPrefix(l.Head, change.HeadPrefix)))
356 if l.Published != nil {
357 s += " published at " + short(strings.TrimPrefix(l.Published.Head, change.HeadPrefix))
358 }
359 layers = append(layers, s)
360 }
361 return fmt.Sprintf("on %s at %s, operation %s: %s", r.Target, short(strings.TrimPrefix(r.TargetTip, change.HeadPrefix)), r.Operation, strings.Join(layers, "; "))
362}
363
364func orNone(oid string) string {
365 if oid == "" {
366 return "no report"
367 }
368 return short(oid)
369}