gunk-dev/the-valley

publish.go

source75d050a4e31bmain's published tip history
12.8 KiB
1package main
2
3import (
4 "context"
5 "fmt"
6 "strings"
7
8 "the-valley/change"
9 "the-valley/rebase"
10)
11
12// Publishing. A write pass makes the writes its journal names the way
13// recovery would make them after a crash: it reads origin, decides every
14// write (recover.go), and pushes the ones still to finish, one phase of
15// each family at a time. A family's branch push comes first. Origin is
16// read again, so its versions are decided against the branches as origin
17// holds them and against the requests filed meanwhile. Its versions come
18// next, lower layers first, and its reports and replacements last, each
19// after another read. So a pass that ends anywhere leaves a journal the
20// next pass finishes in the same way, and a push whose answer was lost is
21// decided by what origin shows on the next read.
22//
23// A family whose branches moved gets reports that say what did happen.
24// Its journaled reports say complete, which holds when every version was
25// added. When a version was not, because it stopped or is paused, the
26// pass builds a report in place of each that says partial, names each
27// version origin holds, and says why each other version was not added.
28// Such a report is a replacement. A family with a paused version is open,
29// and its replacements are replaced again once it is not. A replacement
30// is built from the journal's observation and origin as read, and kept in
31// the journal before it is pushed, so a push that fails or whose answer
32// is lost leaves it there for the next read to decide. A replacement that
33// would say what origin's current report says is not built.
34//
35// What a read shows origin took of a family's writes, its branch push and
36// each version, the journal keeps as accepted before anything more is
37// pushed or built (confirm). Recovery decides those writes done from then
38// on, so a replacement still to push stays in the journal even when an
39// author rewrites or deletes every branch the family moved.
40//
41// Once a read finds nothing left to push and no replacement to build, the
42// pass lets go of each family that is not open (retire). A family is open
43// while a write of it is still to finish, or while origin took its branch
44// push and a version of it is neither added nor stopped for good, as a
45// version paused by a request that might still land is. An open family
46// stays in the journal, and the planner has each family that shares a
47// change or a branch with it wait (waits). A later pass adds the families
48// it builds to the same journal. A family whose branch push stopped, and
49// so moved nothing, is left for the next plan. Once no family is left,
50// the journal goes too.
51
52// maxSteps bounds how many times one pass reads origin and pushes what is
53// still to finish. A family takes three: its branches, its versions and its
54// reports. A family with a replacement takes two more: one to build and
55// keep it, and one to push it. A read that confirms the last push comes
56// after these. A write still to finish after the last read is left to the
57// next pass.
58const maxSteps = 8
59
60// Action is one write a pass pushed, and what became of it.
61type Action struct {
62 Operation string `json:"operation"`
63 Family int `json:"family"` // the family's place in the journal
64 Write string `json:"write"` // branches, version, report or replacement
65 ID string `json:"change_id,omitempty"` // the change a version or a report is about
66 Commit string `json:"commit,omitempty"` // the record or report commit pushed
67 Result string `json:"result"` // pushed, refused, unknown or unpushed
68 Why string `json:"why,omitempty"` // why origin did not take it, when it did not
69}
70
71// settled is what settling a journal did.
72type settled struct {
73 actions []Action
74 last *Recovery // what recovery decided of the journal at the last read
75 closed bool // the journal was let go
76}
77
78// settle makes the journal's writes as recovery decides them. Once a read
79// finds nothing left to push and no replacement to build, it lets go of
80// each family that is not open, and of the journal once none is left.
81func settle(ctx context.Context, g rebase.Git, c config, j *Journal) (*settled, error) {
82 out := &settled{actions: []Action{}}
83 for step := 0; step < maxSteps; step++ {
84 rd, err := readOrigin(ctx, g, c)
85 if err != nil {
86 return out, err
87 }
88 out.last = decide(j, rd)
89 if confirm(j, out.last) {
90 if err := keep(ctx, g, c.state, j); err != nil {
91 return out, err
92 }
93 }
94 if pushed(ctx, g, j, out.last, &out.actions) {
95 continue
96 }
97 if err := ctx.Err(); err != nil {
98 return out, err
99 }
100 built, err := replace(ctx, g, c, j, out.last, rd.s)
101 if err != nil || built {
102 if err == nil {
103 continue
104 }
105 return out, err
106 }
107 if err := retire(ctx, g, c, j, out.last); err != nil {
108 return out, err
109 }
110 if out.closed = len(j.Families) == 0; !out.closed {
111 out.last = decide(j, rd)
112 }
113 return out, nil
114 }
115 return out, nil
116}
117
118// confirm adds to each family's accepted writes what recovery decided
119// origin took at its last read: the branch push, and each version, once
120// the branch push is accepted. It reports whether it added anything,
121// which the journal is then kept with before anything more is pushed or
122// built.
123func confirm(j *Journal, r *Recovery) bool {
124 by := decisionsOf(r)
125 added := false
126 for i := range j.Families {
127 f, ds := &j.Families[i], by[i]
128 if f.Branches == nil {
129 continue
130 }
131 if !f.Accepted.Branches && ds["branches "].Action == writeDone {
132 f.Accepted.Branches, added = true, true
133 }
134 if !f.Accepted.Branches {
135 continue
136 }
137 versions := []string{}
138 for _, v := range f.Versions {
139 if f.Accepted.has(v.ID) || ds["version "+v.ID].Action == writeDone {
140 versions = append(versions, v.ID)
141 }
142 }
143 if len(versions) != len(f.Accepted.Versions) {
144 f.Accepted.Versions, added = versions, true
145 }
146 }
147 return added
148}
149
150// readOrigin reads one snapshot of origin, and what recovery reads it
151// through.
152func readOrigin(ctx context.Context, g rebase.Git, c config) (reading, error) {
153 s, err := rebase.ReadSnapshot(ctx, g, c.verify)
154 if err != nil {
155 return reading{}, err
156 }
157 requests, evidence := requestsOf(ctx, g, s)
158 return reading{
159 s: s, requests: requests, evidence: evidence, in: incoming(ctx, g, s.Tip), is: ancestry(ctx, g),
160 have: func(oid string) bool { return run(ctx, g, "cat-file", "-e", oid) == nil },
161 landed: s.IsLanded,
162 }, nil
163}
164
165// decisionsOf is each family's decisions, by write and change ID.
166func decisionsOf(r *Recovery) map[int]map[string]Decision {
167 by := map[int]map[string]Decision{}
168 for _, d := range r.Decisions {
169 if by[d.Family] == nil {
170 by[d.Family] = map[string]Decision{}
171 }
172 by[d.Family][d.Write+" "+d.ID] = d
173 }
174 return by
175}
176
177// pushed pushes, for each family, the first phase that has a write still
178// to finish: its branch push, then its versions, then its reports and its
179// replacements. It reports whether it tried any push.
180func pushed(ctx context.Context, g rebase.Git, j *Journal, r *Recovery, actions *[]Action) bool {
181 tried := false
182 by := decisionsOf(r)
183 for i, f := range j.Families {
184 ds := by[i]
185 record := func(a Action) {
186 a.Operation, a.Family = f.Operation, i
187 *actions = append(*actions, a)
188 tried = true
189 }
190 if ctx.Err() != nil {
191 return tried
192 }
193 if f.Branches != nil && ds["branches "].Action == writeFinish {
194 push, refused := f.Branches.Push(ctx, g)
195 a := Action{Write: "branches", Result: push.String()}
196 var why []string
197 for _, ref := range refused {
198 why = append(why, ref.Ref+": "+ref.Why)
199 }
200 a.Why = strings.Join(why, "; ")
201 record(a)
202 continue
203 }
204 versions := false
205 for _, v := range f.Versions {
206 if ds["version "+v.ID].Action != writeFinish {
207 continue
208 }
209 ap := v.Push(ctx, g)
210 a := Action{Write: "version", ID: v.ID, Commit: v.Commit, Result: ap.Write.Push.String(), Why: ap.Write.Why}
211 if ap.Err != nil && a.Why == "" {
212 a.Why = ap.Err.Error()
213 }
214 record(a)
215 versions = true
216 }
217 if versions {
218 continue
219 }
220 for _, write := range []struct {
221 name string
222 reports []JournalReport
223 }{{"report", f.Reports}, {"replacement", f.Replacements}} {
224 for _, rep := range write.reports {
225 p := rep.Pending
226 if ds[write.name+" "+p.ID].Action != writeFinish {
227 continue
228 }
229 w := p.Push(ctx, g)
230 record(Action{Write: write.name, ID: p.ID, Commit: p.Commit, Result: w.Push.String(), Why: w.Why})
231 }
232 }
233 }
234 return tried
235}
236
237// moved reports whether origin took a family's branch push.
238func moved(f JournalFamily, ds map[string]Decision) bool {
239 return f.Branches != nil && ds["branches "].Action == writeDone
240}
241
242// isOpen reports whether a family is open: a write of it still to finish,
243// or its branches moved and a version neither added nor stopped for good.
244func isOpen(f JournalFamily, ds map[string]Decision) bool {
245 for _, d := range ds {
246 if d.Action == writeFinish {
247 return true
248 }
249 }
250 if !moved(f, ds) {
251 return false
252 }
253 for _, v := range f.Versions {
254 if !ds["version "+v.ID].settled() {
255 return true
256 }
257 }
258 return false
259}
260
261// retire lets go of each family of the journal that is not open, and of
262// the journal once no family is left. It runs once a read finds nothing
263// left to push and no replacement to build, so each report of a family
264// whose branches moved is on origin, as that read showed.
265func retire(ctx context.Context, g rebase.Git, c config, j *Journal, r *Recovery) error {
266 by := decisionsOf(r)
267 open := []JournalFamily{}
268 for i, f := range j.Families {
269 if isOpen(f, by[i]) {
270 open = append(open, f)
271 }
272 }
273 switch {
274 case len(open) == 0:
275 j.Families = open
276 return forget(ctx, g, c.state)
277 case len(open) < len(j.Families):
278 j.Families = open
279 return keep(ctx, g, c.state, j)
280 }
281 return nil
282}
283
284// whatHappened is the observation of a family whose branches moved, as
285// what did happen to it: complete when every version was added, and
286// partial, saying why each other version was not, otherwise. version is
287// what recovery decided of each change's version. It returns too what
288// was published of each change: its prepared head, with the version when
289// it was added.
290func whatHappened(f JournalFamily, version func(id string) Decision) (Observation, map[string]*change.RestackPublished) {
291 heads := map[string]string{}
292 for _, u := range f.Branches.Updates {
293 heads[u.Branch] = u.New
294 }
295 o := f.Observed
296 o.Result, o.Refusal = change.RestackComplete, ""
297 published := map[string]*change.RestackPublished{}
298 for _, l := range o.Layers {
299 if l.ID != "" {
300 published[l.ID] = &change.RestackPublished{Head: change.HeadPrefix + heads[l.Branch]}
301 }
302 }
303 var why []string
304 for _, v := range f.Versions {
305 d := version(v.ID)
306 if d.Action == writeDone {
307 n, record := v.Version, change.HeadPrefix+v.Commit
308 published[v.ID].Version, published[v.ID].Record = &n, &record
309 continue
310 }
311 o.Result = change.RestackPartial
312 why = append(why, fmt.Sprintf("%s v%d was not added: %s", v.Handle, v.Version, d.Why))
313 }
314 o.Refusal = strings.Join(why, "; ")
315 return o, published
316}
317
318// replace builds, for each family whose branches moved, a report of what
319// did happen for each of its changes whose report on origin says
320// otherwise, in place of any replacement of that change the family held.
321// It archives the observation a replacement is cut from when the
322// replacement leaves something out of it, and keeps the journal with them
323// before any is pushed. It reports whether it built any. A change origin
324// no longer records gets none, since the hook takes no report about it,
325// and so does a change no report of which fits, as when the family's
326// writes were built.
327func replace(ctx context.Context, g rebase.Git, c config, j *Journal, r *Recovery, s *rebase.Snapshot) (bool, error) {
328 by := decisionsOf(r)
329 built := false
330 for i := range j.Families {
331 f, ds := &j.Families[i], by[i]
332 if !moved(*f, ds) {
333 continue
334 }
335 o, published := whatHappened(*f, func(id string) Decision { return ds["version "+id] })
336 digest := o.Digest()
337 for _, l := range o.Layers {
338 if l.ID == "" || s.Refs[change.Ref(rebase.Target, l.ID)] == "" {
339 continue
340 }
341 rep, omitted, err := bound(fullReport(f.Operation, f.Tip, l.ID, o, published), digest)
342 if err != nil {
343 continue
344 }
345 if current, found := s.Report(l.ID); found && sameReport(current.Report, rep) {
346 continue
347 }
348 if len(omitted) > 0 {
349 if err := archive(c.state, o); err != nil {
350 return built, err
351 }
352 }
353 p, err := rebase.BuildReport(ctx, g, rep, rebase.ReportMessage(l.Name, rep), s.Refs[rebase.RestackPrefix+l.ID])
354 if err != nil {
355 return built, err
356 }
357 kept := JournalReport{Pending: p, Omitted: append([]string{}, omitted...)}
358 replaced := false
359 for k := range f.Replacements {
360 if f.Replacements[k].Pending.ID == l.ID {
361 f.Replacements[k], replaced = kept, true
362 }
363 }
364 if !replaced {
365 f.Replacements = append(f.Replacements, kept)
366 }
367 built = true
368 }
369 }
370 if !built {
371 return false, ctx.Err()
372 }
373 return true, keep(ctx, g, c.state, j)
374}