gunk-dev/the-valley

publish.go

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