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