gunk-dev/the-valley

publish.go

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