gunk-dev/the-valley

publish.go

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