gunk-dev/the-valley

plan.go

source9d8f53c82aaemain's published tip history
9.5 KiB
1package main
2
3import (
4 "fmt"
5 "sort"
6 "strings"
7
8 "the-valley/change"
9 "the-valley/rebase"
10)
11
12// The restacker's own limits on a record it would change. Each is below
13// the push hook's (change/record.go), so a record the restacker leaves at
14// its limit still has room for its owner's own updates.
15const (
16 maxVersions = 900 // versions of one change
17 maxSeq = 99000 // record updates in a change's life
18 maxBytes = 48 << 10 // bytes of the record's encoded JSON
19)
20
21// What the planner would do with a family.
22const (
23 restack = "restack" // rebase every layer onto main's tip
24 hold = "hold" // a layer, a request or a limit stops it; Reasons say which
25 current = "current" // the family already descends from main's tip
26 leave = "leave" // no layer is a ready change, so the restacker has no business with it
27)
28
29// Plan is what one pass would do, from one snapshot of origin.
30type Plan struct {
31 Target string `json:"target"`
32 Tip string `json:"tip"`
33 Evidence string `json:"evidence"` // change.EvidenceFingerprint of every attestation ref
34 Cap int `json:"maxAutomaticVersions"`
35 Families []FamilyPlan `json:"families"`
36}
37
38// FamilyPlan is one family of layers, bottom first, and what would become
39// of it.
40type FamilyPlan struct {
41 Layers []LayerPlan `json:"layers"`
42 Action string `json:"action"`
43 Reasons []string `json:"reasons"`
44 Notes []string `json:"notes"`
45 Requests []RequestPlan `json:"requests"`
46}
47
48// LayerPlan is one layer as the snapshot shows it, with the capacity of
49// the record a restack would change; Capacity is nil for a plain branch.
50type LayerPlan struct {
51 Name string `json:"name"`
52 ID string `json:"id,omitempty"`
53 Branch string `json:"branch"`
54 Head string `json:"head"`
55 State string `json:"state"`
56 Below string `json:"below,omitempty"`
57 Capacity *Capacity `json:"capacity,omitempty"`
58}
59
60// Capacity is how close a record is to each limit, counted as it would be
61// once the restack added a version.
62type Capacity struct {
63 Uploads int `json:"uploads"` // versions the principal uploaded, the new one included
64 Cap int `json:"maxAutomaticVersions"`
65 Versions int `json:"versions"`
66 MaxVersions int `json:"maxVersions"`
67 Seq int `json:"seq"`
68 MaxSeq int `json:"maxSeq"`
69 Bytes int `json:"bytes"`
70 MaxBytes int `json:"maxBytes"`
71 Over []string `json:"over"` // each limit the record would pass, as a sentence
72}
73
74// RequestPlan is an integration request related to a family: how it is
75// related, and whether its outcome is current.
76type RequestPlan struct {
77 Name string `json:"name"`
78 Head string `json:"head"`
79 By []string `json:"by"` // name, head, range
80 Decision string `json:"decision,omitempty"`
81 Current bool `json:"current"`
82 Releases bool `json:"releases"`
83 Why string `json:"why"`
84}
85
86// Request is an integration request on origin: its name under
87// rebase.RequestPrefix and the head it asks to land.
88type Request struct {
89 Name, Head string
90}
91
92// Members is what a request may be related to a family by: the request
93// names of its changes, the head of every layer, and every head in turn
94// for the range test.
95type Members struct {
96 Names map[string]bool
97 Heads []string
98}
99
100// membersOf collects a family's request names and heads. A change's names
101// are c-<change ID> and the branch-derived name of every version it has
102// ever had, since a request filed under an earlier version's branch is
103// still about the change. A plain branch's name is its own.
104func membersOf(f *rebase.Family) Members {
105 m := Members{Names: map[string]bool{}}
106 for _, l := range f.Layers {
107 m.Heads = append(m.Heads, l.Head)
108 if l.ID == "" {
109 m.Names[rebase.ChangeName(l.Branch)] = true
110 continue
111 }
112 m.Names[change.RequestName(l.ID)] = true
113 for _, v := range l.Record.Versions {
114 if v.Branch != "" {
115 m.Names[rebase.ChangeName(v.Branch)] = true
116 }
117 }
118 }
119 return m
120}
121
122// Ancestry answers whether a is an ancestor of b, or b itself. An error is
123// an answer that could not be had, as for an object that is missing or is
124// not a commit.
125type Ancestry func(a, b string) (bool, error)
126
127// Relate says how a request is related to a family: by name, by an exact
128// head, and by an incoming range — the commits the request's head reaches
129// and the tip does not — that contains a layer's head. No answer and a nil
130// error is unrelated. An error is a relation that could not be determined,
131// which the caller must treat as related.
132func Relate(r Request, m Members, tip string, is Ancestry) ([]string, error) {
133 var by []string
134 if m.Names[r.Name] {
135 by = append(by, "name")
136 }
137 for _, h := range m.Heads {
138 if h == r.Head {
139 by = append(by, "head")
140 break
141 }
142 }
143 if len(by) > 0 {
144 return by, nil
145 }
146 for _, h := range m.Heads {
147 in, err := is(h, r.Head)
148 if err != nil {
149 return nil, err
150 }
151 if !in {
152 continue
153 }
154 onMain, err := is(h, tip)
155 if err != nil {
156 return nil, err
157 }
158 if !onMain {
159 return []string{"range"}, nil
160 }
161 }
162 return nil, nil
163}
164
165// Currency judges a related request's outcome: whether it lets the family
166// proceed, and why. An outcome is current when its head, its target tip
167// and its evidence fingerprint all equal what the planner sees. A current
168// stale releases the family, since the integrator will not land the
169// request at that head on that tip. So does a current reject, which the
170// caller notes. Anything else holds: no outcome, a record that does not
171// read, an outcome about another head, tip or evidence, which the
172// integrator will judge again, and a failure or a refusal.
173func Currency(o rebase.Outcome, found bool, head, tip, evidence string) (current, releases bool, why string) {
174 switch {
175 case !found:
176 return false, false, "no outcome yet, so the integrator has still to judge it"
177 case o.Decision == "unknown":
178 return false, false, "its outcome record does not read"
179 case o.Head != head:
180 return false, false, fmt.Sprintf("its outcome is about head %s, not %s", short(o.Head), short(head))
181 case o.Tip != tip:
182 return false, false, fmt.Sprintf("its outcome was judged on main at %s, not %s", short(o.Tip), short(tip))
183 case o.Evidence != evidence:
184 return false, false, fmt.Sprintf("its outcome was judged on evidence %s, not %s", o.Evidence, evidence)
185 }
186 switch o.Decision {
187 case "stale":
188 return true, true, "a current stale outcome: it cannot land as it stands"
189 case "reject":
190 return true, true, "a current reject outcome: the integrator rejected it"
191 case "failed":
192 return true, false, "the integrator failed on it at " + o.FailedAt
193 case "land", "voided-withdrawn":
194 return true, false, fmt.Sprintf("a current %s outcome with the request still filed", o.Decision)
195 }
196 return true, false, "the integrator refused it (" + o.Decision + ")"
197}
198
199// CapacityOf counts a record against every limit as it would stand with
200// one more version uploaded by principal. size is the length of that
201// record's encoded JSON.
202func CapacityOf(r *change.Record, principal string, cap, size int) *Capacity {
203 c := &Capacity{
204 Uploads: 1, Cap: cap,
205 Versions: len(r.Versions) + 1, MaxVersions: maxVersions,
206 Seq: r.Seq + 1, MaxSeq: maxSeq,
207 Bytes: size, MaxBytes: maxBytes,
208 }
209 for _, v := range r.Versions {
210 if v.Uploader == principal {
211 c.Uploads++
212 }
213 }
214 if c.Uploads > c.Cap {
215 c.Over = append(c.Over, fmt.Sprintf("%s would have %d versions uploaded by %s, over the cap of %d", r.ChangeID, c.Uploads, principal, c.Cap))
216 }
217 if c.Versions > c.MaxVersions {
218 c.Over = append(c.Over, fmt.Sprintf("%s would have %d versions, over the %d the restacker allows", r.ChangeID, c.Versions, c.MaxVersions))
219 }
220 if c.Seq > c.MaxSeq {
221 c.Over = append(c.Over, fmt.Sprintf("%s's record would be at update %d, over the %d the restacker allows", r.ChangeID, c.Seq, c.MaxSeq))
222 }
223 if c.Bytes > c.MaxBytes {
224 c.Over = append(c.Over, fmt.Sprintf("%s's record would be %d bytes, over the %d the restacker allows", r.ChangeID, c.Bytes, c.MaxBytes))
225 }
226 return c
227}
228
229// nextSize is the length of a record's encoded JSON once a restack adds a
230// version. The rebased head is not known yet, so the version names a
231// stand-in of the same length; the title, the branch and the uploader are
232// the ones the restack would write. 0 is a record the engine would not
233// extend at all, which the version and update limits already hold.
234func nextSize(handle string, r *change.Record, principal string) int {
235 active := r.ActiveVersion()
236 stand := change.HeadPrefix + strings.Repeat("0", len(rebase.HeadOf(active)))
237 next, err := rebase.NextRecord(handle, r, change.Version{Head: stand, Title: active.Title, Branch: active.Branch, Uploader: principal})
238 if err != nil {
239 return 0
240 }
241 b, err := change.Encode(next)
242 if err != nil {
243 return 0
244 }
245 return len(b)
246}
247
248func short(oid string) string {
249 if len(oid) > 12 {
250 return oid[:12]
251 }
252 return oid
253}
254
255// stateLine names a layer's state for a reason, as a change is ready,
256// approved or submitted.
257func stateLine(l *rebase.Layer) string {
258 switch s := l.State(); s {
259 case rebase.Plain:
260 return l.Name + " is a plain branch, which no record is on"
261 case rebase.Draft:
262 return l.Name + " is a draft"
263 case rebase.Withdrawn:
264 return l.Name + " is withdrawn"
265 case rebase.Uncertain:
266 return l.Name + " may have landed already, with no evidence either way"
267 default:
268 return fmt.Sprintf("%s is %s", l.Name, s)
269 }
270}
271
272// sortRequests puts requests in name order, so a plan reads the same each
273// time.
274func sortRequests(rs []RequestPlan) {
275 sort.Slice(rs, func(i, j int) bool { return rs[i].Name < rs[j].Name })
276}