| 1 | package main |
| 2 | |
| 3 | // What the check worker says about its own passes, with --telemetry DIR. |
| 4 | // |
| 5 | // DIR is the worker's diagnostic directory, apart from its state |
| 6 | // directory and its key: the host makes it group-readable by the |
| 7 | // collector, and the worker writes nothing else there. It holds two |
| 8 | // things: |
| 9 | // |
| 10 | // - stream/, the telemetry stream (../telemetry) of the worker's passes |
| 11 | // and of the attest runs a pass starts, whose progress attest records |
| 12 | // itself; |
| 13 | // - state.json, the worker's state now: the pass running and what it is |
| 14 | // doing, the last attempts and how each ended, and every failure |
| 15 | // waiting out its backoff, with the change, version, head and tree it |
| 16 | // was about and why it failed. |
| 17 | // |
| 18 | // failures.json in the state directory stays what decides a backoff, in |
| 19 | // its own format; state.json only describes it. A pass that starts and |
| 20 | // finds state.json naming a pass that never ended records that pass as |
| 21 | // interrupted: a pass that is killed cannot say so itself, so this is an |
| 22 | // inference, and is marked as one. |
| 23 | |
| 24 | import ( |
| 25 | "encoding/json" |
| 26 | "fmt" |
| 27 | "os" |
| 28 | "path/filepath" |
| 29 | "sort" |
| 30 | "time" |
| 31 | |
| 32 | "the-valley/telemetry" |
| 33 | ) |
| 34 | |
| 35 | type workerTelemetry struct { |
| 36 | dir string |
| 37 | tel *telemetry.Emitter |
| 38 | state workerState |
| 39 | } |
| 40 | |
| 41 | type workerState struct { |
| 42 | V int `json:"v"` |
| 43 | Updated string `json:"updated"` |
| 44 | Pass *passState `json:"pass"` |
| 45 | Recent []attempt `json:"recent"` |
| 46 | Failures []failureState `json:"failures"` |
| 47 | } |
| 48 | |
| 49 | type passState struct { |
| 50 | Run string `json:"run"` |
| 51 | PID int `json:"pid"` |
| 52 | Exe string `json:"exe,omitempty"` |
| 53 | Version string `json:"version,omitempty"` |
| 54 | Started string `json:"started"` |
| 55 | Phase string `json:"phase"` |
| 56 | Project string `json:"project,omitempty"` |
| 57 | Change string `json:"change,omitempty"` |
| 58 | CV int `json:"change_version,omitempty"` |
| 59 | Head string `json:"head,omitempty"` |
| 60 | Tree string `json:"tree,omitempty"` |
| 61 | Checks []string `json:"checks,omitempty"` |
| 62 | } |
| 63 | |
| 64 | type attempt struct { |
| 65 | Kind string `json:"kind"` // pass, batch or push |
| 66 | Run string `json:"run"` |
| 67 | Project string `json:"project,omitempty"` |
| 68 | Change string `json:"change,omitempty"` |
| 69 | CV int `json:"change_version,omitempty"` |
| 70 | Head string `json:"head,omitempty"` |
| 71 | Tree string `json:"tree,omitempty"` |
| 72 | Checks []string `json:"checks,omitempty"` |
| 73 | Started string `json:"started,omitempty"` |
| 74 | Ended string `json:"ended,omitempty"` |
| 75 | Result string `json:"result"` // passed, failed or interrupted |
| 76 | Reason string `json:"reason,omitempty"` |
| 77 | Inferred bool `json:"inferred,omitempty"` |
| 78 | } |
| 79 | |
| 80 | type failureState struct { |
| 81 | Project string `json:"project"` |
| 82 | Key string `json:"key"` |
| 83 | Failures int `json:"failures"` |
| 84 | Retry string `json:"retry"` |
| 85 | FailedAt string `json:"failed_at,omitempty"` |
| 86 | Change string `json:"change,omitempty"` |
| 87 | CV int `json:"change_version,omitempty"` |
| 88 | Head string `json:"head,omitempty"` |
| 89 | Tree string `json:"tree,omitempty"` |
| 90 | Reason string `json:"reason,omitempty"` |
| 91 | } |
| 92 | |
| 93 | const recentAttempts = 20 |
| 94 | |
| 95 | // openWorkerTelemetry starts a pass's telemetry in dir, or returns nil |
| 96 | // without a dir. The previous pass, if state.json still names it as |
| 97 | // running, is recorded as interrupted. |
| 98 | func openWorkerTelemetry(dir string) *workerTelemetry { |
| 99 | if dir == "" { |
| 100 | return nil |
| 101 | } |
| 102 | t := &workerTelemetry{dir: dir} |
| 103 | if data, err := os.ReadFile(t.statePath()); err == nil { |
| 104 | json.Unmarshal(data, &t.state) |
| 105 | } |
| 106 | self := telemetry.Self("valley-worker", os.Getenv("VALLEY_VERSION")) |
| 107 | t.tel = telemetry.NewEmitter(filepath.Join(dir, "stream"), self, "") |
| 108 | if prev := t.state.Pass; prev != nil { |
| 109 | reason := fmt.Sprintf("pass %s (pid %d) recorded no end: it was killed, or the host stopped, during its %s", prev.Run, prev.PID, prev.Phase) |
| 110 | t.remember(attempt{Kind: "pass", Run: prev.Run, Project: prev.Project, Change: prev.Change, CV: prev.CV, |
| 111 | Head: prev.Head, Tree: prev.Tree, Checks: prev.Checks, Started: prev.Started, Result: "interrupted", Reason: reason, Inferred: true}) |
| 112 | t.tel.Emit("pass.interrupted", telemetry.Record{Project: prev.Project, Change: prev.Change, Version: prev.CV, Head: prev.Head}, |
| 113 | map[string]any{"previous_run": prev.Run, "previous_pid": prev.PID, "phase": prev.Phase, "inferred": true}) |
| 114 | } |
| 115 | t.state.V = 1 |
| 116 | t.state.Pass = &passState{Run: t.tel.Run(), PID: self.PID, Exe: self.Exe, Version: self.Version, |
| 117 | Started: telemetry.Stamp(time.Now()), Phase: "reading"} |
| 118 | t.save() |
| 119 | t.tel.Emit("pass.start", telemetry.Record{}, nil) |
| 120 | return t |
| 121 | } |
| 122 | |
| 123 | func (t *workerTelemetry) statePath() string { return filepath.Join(t.dir, "state.json") } |
| 124 | |
| 125 | func (t *workerTelemetry) stream() string { return filepath.Join(t.dir, "stream") } |
| 126 | |
| 127 | func (t *workerTelemetry) remember(a attempt) { |
| 128 | t.state.Recent = append([]attempt{a}, t.state.Recent...) |
| 129 | if len(t.state.Recent) > recentAttempts { |
| 130 | t.state.Recent = t.state.Recent[:recentAttempts] |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // change says which change the pass is reading now. |
| 135 | func (t *workerTelemetry) change(project string, c jsonChange, v jsonVersion) { |
| 136 | if t == nil { |
| 137 | return |
| 138 | } |
| 139 | p := t.state.Pass |
| 140 | p.Phase, p.Project, p.Change, p.CV, p.Head, p.Tree, p.Checks = "deriving", project, c.ChangeID, v.N, v.Head, "", nil |
| 141 | t.tel.Correlate(telemetry.Record{Project: project, Change: c.ChangeID, Version: v.N, Head: v.Head}) |
| 142 | t.tel.Emit("change.seen", telemetry.Record{}, map[string]any{"handle": c.Handle}) |
| 143 | t.save() |
| 144 | } |
| 145 | |
| 146 | // begin says the pass is building or publishing, and returns the attempt |
| 147 | // it makes. |
| 148 | func (t *workerTelemetry) begin(kind, tree string, checks []string, data map[string]any) *attempt { |
| 149 | if t == nil { |
| 150 | return nil |
| 151 | } |
| 152 | p := t.state.Pass |
| 153 | p.Phase, p.Tree, p.Checks = kind, tree, checks |
| 154 | a := &attempt{Kind: kind, Run: t.tel.Run(), Project: p.Project, Change: p.Change, CV: p.CV, Head: p.Head, Tree: tree, |
| 155 | Checks: checks, Started: telemetry.Stamp(time.Now())} |
| 156 | if data == nil { |
| 157 | data = map[string]any{} |
| 158 | } |
| 159 | data["checks"] = checks |
| 160 | t.tel.Emit(map[string]string{"build": "batch.start", "push": "publish.start"}[kind], telemetry.Record{Tree: tree}, data) |
| 161 | t.save() |
| 162 | return a |
| 163 | } |
| 164 | |
| 165 | // end records how an attempt ended, and its backoff when it failed. |
| 166 | func (t *workerTelemetry) end(a *attempt, err error, reason string, project, key string, f *backoff) { |
| 167 | if t == nil || a == nil { |
| 168 | return |
| 169 | } |
| 170 | a.Ended, a.Result = telemetry.Stamp(time.Now()), "passed" |
| 171 | data := map[string]any{"result": "passed"} |
| 172 | if err != nil { |
| 173 | a.Result, a.Reason = "failed", reason |
| 174 | data["result"], data["reason"] = "failed", reason |
| 175 | } |
| 176 | t.remember(*a) |
| 177 | t.tel.Emit(map[string]string{"build": "batch.end", "push": "publish.end"}[a.Kind], telemetry.Record{Tree: a.Tree}, data) |
| 178 | if f != nil { |
| 179 | t.tel.Emit("backoff", telemetry.Record{Tree: a.Tree}, map[string]any{"key": key, "failures": f.Failures, |
| 180 | "retry": telemetry.Stamp(f.Retry), "reason": reason}) |
| 181 | t.failure(failureState{Project: project, Key: key, Failures: f.Failures, Retry: telemetry.Stamp(f.Retry), |
| 182 | FailedAt: a.Ended, Change: a.Change, CV: a.CV, Head: a.Head, Tree: a.Tree, Reason: reason}) |
| 183 | } |
| 184 | t.state.Pass.Phase = "reading" |
| 185 | t.save() |
| 186 | } |
| 187 | |
| 188 | // failure keeps what a failure waiting out its backoff was about. |
| 189 | func (t *workerTelemetry) failure(f failureState) { |
| 190 | for i, old := range t.state.Failures { |
| 191 | if old.Project == f.Project && old.Key == f.Key { |
| 192 | t.state.Failures[i] = f |
| 193 | return |
| 194 | } |
| 195 | } |
| 196 | t.state.Failures = append(t.state.Failures, f) |
| 197 | } |
| 198 | |
| 199 | // finish ends the pass, and keeps only the failures the failure record |
| 200 | // still holds, with their waits as it holds them. |
| 201 | func (t *workerTelemetry) finish(r *failureRecord, troubles int, err error) { |
| 202 | if t == nil { |
| 203 | return |
| 204 | } |
| 205 | var kept []failureState |
| 206 | for project, failures := range r.Projects { |
| 207 | for key, b := range failures { |
| 208 | f := failureState{Project: project, Key: key} |
| 209 | for _, old := range t.state.Failures { |
| 210 | if old.Project == project && old.Key == key { |
| 211 | f = old |
| 212 | } |
| 213 | } |
| 214 | f.Failures, f.Retry = b.Failures, telemetry.Stamp(b.Retry) |
| 215 | kept = append(kept, f) |
| 216 | } |
| 217 | } |
| 218 | sort.Slice(kept, func(i, j int) bool { return kept[i].Project+kept[i].Key < kept[j].Project+kept[j].Key }) |
| 219 | t.state.Failures = kept |
| 220 | p := t.state.Pass |
| 221 | result := "passed" |
| 222 | reason := "" |
| 223 | if err != nil { |
| 224 | result, reason = "failed", err.Error() |
| 225 | } |
| 226 | t.remember(attempt{Kind: "pass", Run: p.Run, Started: p.Started, Ended: telemetry.Stamp(time.Now()), Result: result, Reason: reason}) |
| 227 | t.state.Pass = nil |
| 228 | t.tel.Correlate(telemetry.Record{}) |
| 229 | t.tel.Emit("pass.end", telemetry.Record{}, map[string]any{"result": result, "troubles": troubles, "waiting": len(kept)}) |
| 230 | t.save() |
| 231 | } |
| 232 | |
| 233 | // save writes state.json whole, through a file renamed into place, |
| 234 | // readable by the collector's group. A state that cannot be written is |
| 235 | // dropped: it describes the pass and decides nothing. |
| 236 | func (t *workerTelemetry) save() { |
| 237 | t.state.Updated = telemetry.Stamp(time.Now()) |
| 238 | data, err := json.MarshalIndent(t.state, "", " ") |
| 239 | if err != nil { |
| 240 | return |
| 241 | } |
| 242 | tmp, err := os.CreateTemp(t.dir, ".state-*") |
| 243 | if err != nil { |
| 244 | return |
| 245 | } |
| 246 | _, err = tmp.Write(append(data, '\n')) |
| 247 | if err == nil { |
| 248 | err = tmp.Chmod(0o640) |
| 249 | } |
| 250 | if cerr := tmp.Close(); err == nil { |
| 251 | err = cerr |
| 252 | } |
| 253 | if err == nil { |
| 254 | err = os.Rename(tmp.Name(), t.statePath()) |
| 255 | } |
| 256 | if err != nil { |
| 257 | os.Remove(tmp.Name()) |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // failedChecks names the checks an attest run recorded as not passing. |
| 262 | func (t *workerTelemetry) failedChecks(run string, after int64) []string { |
| 263 | if t == nil { |
| 264 | return nil |
| 265 | } |
| 266 | st := &telemetry.Stream{Dir: t.stream()} |
| 267 | recs, _, _ := st.Read(after, 1000, 1<<20) |
| 268 | var failed []string |
| 269 | for _, r := range recs { |
| 270 | if r.Run == run && r.Type == "check.result" && r.Data["result"] != "passed" { |
| 271 | failed = append(failed, fmt.Sprintf("%s %v", r.Check, r.Data["result"])) |
| 272 | } |
| 273 | } |
| 274 | return failed |
| 275 | } |
| 276 | |
| 277 | // streamEnd is the number of the stream's newest record. |
| 278 | func (t *workerTelemetry) streamEnd() int64 { |
| 279 | if t == nil { |
| 280 | return 0 |
| 281 | } |
| 282 | rng, _ := (&telemetry.Stream{Dir: t.stream()}).Range() |
| 283 | return rng.Last |
| 284 | } |