diff --git a/internal/pipeline/pipeline_test.go b/internal/pipeline/pipeline_test.go index eb2d860a..36440b78 100644 --- a/internal/pipeline/pipeline_test.go +++ b/internal/pipeline/pipeline_test.go @@ -4,8 +4,11 @@ package pipeline import ( + "bytes" "context" "fmt" + "os" + "path/filepath" "reflect" "strings" "testing" @@ -799,6 +802,151 @@ func TestRecheckFromArchive(t *testing.T) { } } +// A dev-list reply that requests a recheck can be matched while +// Patchwork is down, and Patchwork can answer again before it ingests +// that reply as a comment. A comment fetch that returns no comments +// must not consume the request: the archive message is seen only once, +// so nothing else would look at the patch's comments again, and the +// recheck would be lost. The request must also survive the run that +// matched it, and give up after a while when no comment shows up. +func TestRecheckCommentLag(t *testing.T) { + e := newEnv(t, "") + _, failed := testedPatch(t, e, 250) + + // The reply reaches the dev archive while Patchwork is down. + e.reload() + e.dev.add(e.now, 50, "Re: [lorem-dev,1/1] lorem: Patch 1 of series 250", "", "") + e.pw.errs["*"] = errTransient + e.step() + pt := e.series(250).Patches[0] + if !pt.CommentsPending || pt.CommentsSeen != 0 || len(e.gh.reruns) != 0 { + t.Fatalf("after the Patchwork outage: patch = %+v reruns = %v", pt, e.gh.reruns) + } + + // Patchwork answers again, but it has not ingested the reply as a + // comment yet: the fetch succeeds and returns no comments. + delete(e.pw.errs, "*") + e.reload() + if pt = e.series(250).Patches[0]; !pt.CommentsPending { + t.Fatalf("the request did not survive the run: patch = %+v", pt) + } + e.step() + if pt = e.series(250).Patches[0]; !pt.CommentsPending { + t.Errorf("an empty comment fetch consumed the recheck request: patch = %+v", pt) + } + + // The comment shows up only now; the recheck must still happen. + e.pw.comments[25001] = comments("Recheck-request: github-robot\n") + e.step() + if len(e.gh.reruns) != 1 || e.gh.reruns[0] != failed { + t.Fatalf("reruns = %v, want [%d]: the recheck was lost", e.gh.reruns, failed) + } + if pt = e.series(250).Patches[0]; pt.CommentsSeen != 1 { + t.Fatalf("comments seen = %d, want 1", pt.CommentsSeen) + } + + // One more reply matches, but Patchwork never turns it into a + // comment: the request must wait instead of being lost, and give + // up after a while, with a decision instead of a rerun. + e.reload() + e.dev.add(e.now, 51, "Re: [lorem-dev,1/1] lorem: Patch 1 of series 250", "", "") + e.step() + if pt = e.series(250).Patches[0]; !pt.CommentsPending { + t.Errorf("fetching only old comments consumed the recheck request: patch = %+v", pt) + } + // When the wait started must survive the run, too. + e.reload() + e.advance(7 * time.Hour) + e.step() + pt = e.series(250).Patches[0] + if pt.CommentsPending || pt.CommentsPendingSince != "" { + t.Errorf("the comment wait never gave up: patch = %+v", pt) + } + if len(e.gh.reruns) != 1 { + t.Errorf("reruns = %v, want only the recheck that arrived", e.gh.reruns) + } + var gaveUp bool + for _, d := range pt.Decisions { + if d.Action == "comments-skipped" { + gaveUp = true + } + } + if !gaveUp { + t.Errorf("giving up was not recorded in the decisions: patch = %+v", pt) + } +} + +// Patchwork can be unavailable many times in a row for a few minutes +// each, and these periods can add up to more than the comment wait. +// Patchwork cannot ingest a reply while it is down, so only the time it +// was available counts against the wait. Waiting must not change the +// state either: the patch of a finished series lives in a history shard +// of a whole month, which would be rewritten on every run. +func TestRecheckCommentWaitOutages(t *testing.T) { + e := newEnv(t, "") + testedPatch(t, e, 270) + shard := func() []byte { + t.Helper() + paths, err := filepath.Glob(filepath.Join(e.dir, state.HistoryDir, "*.json")) + if err != nil || len(paths) != 1 { + t.Fatalf("history shards = %v, err = %v", paths, err) + } + data, err := os.ReadFile(paths[0]) + if err != nil { + t.Fatal(err) + } + return data + } + + e.reload() + e.dev.add(e.now, 50, "Re: [lorem-dev,1/1] lorem: Patch 1 of series 270", "", "") + e.step() + e.reload() + if pt := e.series(270).Patches[0]; !pt.CommentsPending { + t.Fatalf("the reply did not mark the patch: patch = %+v", pt) + } + waiting := shard() + + // For 9 hours, Patchwork answers for 10 minutes, then is down for + // 50: it was available for only 90 minutes. + for hour := 1; hour <= 9; hour++ { + e.advance(10 * time.Minute) + e.pw.errs["*"] = errTransient + e.step() + e.reload() + e.advance(50 * time.Minute) + delete(e.pw.errs, "*") + e.step() + e.reload() + if pt := e.series(270).Patches[0]; !pt.CommentsPending { + t.Fatalf("hour %d: gave up waiting for the comment while Patchwork was "+ + "mostly down: patch = %+v", hour, pt) + } + if !bytes.Equal(shard(), waiting) { + t.Fatalf("hour %d: waiting for the comment rewrote the history shard", hour) + } + } + + // Patchwork stays available: the wait goes on until it was + // available for the whole wait, then gives up. + e.advance(4 * time.Hour) + e.step() + e.reload() + if pt := e.series(270).Patches[0]; !pt.CommentsPending { + t.Fatalf("gave up after 5.5 hours of Patchwork availability: patch = %+v", pt) + } + e.advance(time.Hour) + e.step() + e.reload() + pt := e.series(270).Patches[0] + if pt.CommentsPending || pt.CommentsPendingSince != "" { + t.Fatalf("the comment wait never gave up: patch = %+v", pt) + } + if len(e.gh.reruns) != 0 { + t.Errorf("reruns = %v, want none", e.gh.reruns) + } +} + func TestDependsOn(t *testing.T) { e := newEnv(t, "") e.pw.addSeries(110, 1, "lorem", e.now) diff --git a/internal/pipeline/rechecks.go b/internal/pipeline/rechecks.go index 4edc52e6..c162a9fb 100644 --- a/internal/pipeline/rechecks.go +++ b/internal/pipeline/rechecks.go @@ -19,6 +19,12 @@ import ( const commentEvents = "patch-comment-created" +// commentWait is how long a patch waits for its comments after +// activity was detected: Patchwork can take a while to ingest a +// matched message, but not every message becomes a comment. Only the +// time Patchwork was available counts: it ingests nothing while down. +const commentWait = 6 * time.Hour + var recheckRe = regexp.MustCompile(`(?m)^Recheck-request:[ \t]*(.+)$`) // ParseRecheck returns whether a comment requests a recheck by label, @@ -111,13 +117,14 @@ func (p *Pipeline) rechecks(ctx context.Context) { p.warnf("%s: cannot fetch comments: %v", patchName(t.patch), err) continue } - t.patch.CommentsPending = false + delivered := false for _, c := range comments { if c.ID <= t.patch.CommentsSeen { p.infof("%s: comment %d already processed; skipping it.", patchName(t.patch), c.ID) continue } + delivered = true t.patch.CommentsSeen = c.ID all, names := ParseRecheck(c.Content, labels) checks := p.matchChecks(t.patch, names) @@ -147,6 +154,43 @@ func (p *Pipeline) rechecks(ctx context.Context) { } } } + // The patch can live in a history shard of a whole month, + // which is only written when marked: mark it when it changes, + // or the request and the seen comments would not survive the + // run, but not while it only waits, or every run would + // rewrite the shard. + if delivered { + t.patch.CommentsPending = false + t.patch.CommentsPendingSince = "" + p.Store.PatchChanged(t.patch) + continue + } + // Patchwork can answer before it ingests a message that + // matched a tracked patch, and the archive shows a message + // only once: an empty fetch must not consume the request, or + // a recheck in that message would be lost. Wait for the + // comment instead, but not forever: not every matched + // message becomes a comment. + if t.patch.CommentsPendingSince == "" { + // Marked before the bot noted since when. + t.patch.CommentsPendingSince = state.FormatTime(now) + p.Store.PatchChanged(t.patch) + } + since := state.ParseTime(t.patch.CommentsPendingSince) + down := p.Store.Availability.Downtime(state.ServicePatchwork, since, now) + if wait := now.Sub(since) - down; wait < commentWait { + p.infof("%s: no new comments yet; waited %s while Patchwork was "+ + "available, and %s while it was not. Still waiting for "+ + "Patchwork to ingest the message.", patchName(t.patch), + wait.Round(time.Minute), down.Round(time.Minute)) + continue + } + p.notePatch(t.patch, "comments-skipped", + "Gave up waiting for comments: none in %s of Patchwork being "+ + "available since the activity was detected at %s.", + commentWait, t.patch.CommentsPendingSince) + t.patch.CommentsPending = false + t.patch.CommentsPendingSince = "" } for _, id := range ids { if t := patches[id]; (t.patch.Recheck || len(t.patch.RecheckChecks) > 0) && @@ -176,6 +220,8 @@ func (p *Pipeline) commentsFromEvents(ctx context.Context, patches map[int]track p.infof("Comment event %d: marking patch %d for a comment scan.", ev.ID, pl.Patch.ID) t.patch.CommentsPending = true + t.patch.CommentsPendingSince = state.FormatTime(p.now()) + p.Store.PatchChanged(t.patch) } else { p.infof("Ignoring event %d: patch %d is not tracked for rechecks.", ev.ID, pl.Patch.ID) @@ -226,6 +272,11 @@ func (p *Pipeline) commentsFromArchive(ctx context.Context, patches map[int]trac for _, id := range archive.MatchPatchSubjects(msgs, names) { p.infof("Dev-list activity matches patch %d; checking comments for rechecks.", id) patches[id].patch.CommentsPending = true + patches[id].patch.CommentsPendingSince = state.FormatTime(p.now()) + // The patch can live in a history shard, which is only + // written when marked: mark it, or the request would not + // survive the run. + p.Store.PatchChanged(patches[id].patch) } } diff --git a/internal/state/availability.go b/internal/state/availability.go index 5741fa45..30bc74ab 100644 --- a/internal/state/availability.go +++ b/internal/state/availability.go @@ -69,6 +69,34 @@ func (a *Availability) Available(service string, now time.Time) *Period { return p } +// Downtime returns how long a service was unavailable between two +// times, as far as the kept periods tell; a period going on lasts until +// the end. +func (a *Availability) Downtime(service string, from, to time.Time) time.Duration { + var d time.Duration + add := func(p *Period, until time.Time) { + since := ParseTime(p.Since) + if since.Before(from) { + since = from + } + if until.After(to) { + until = to + } + if until.After(since) { + d += until.Sub(since) + } + } + for _, p := range a.Periods { + if p.Service == service { + add(p, ParseTime(p.Until)) + } + } + if p := a.Failing[service]; p != nil { + add(p, to) + } + return d +} + // Prune forgets the periods that ended before a time. func (a *Availability) Prune(before time.Time) { keep := a.Periods[:0] diff --git a/internal/state/availability_test.go b/internal/state/availability_test.go index bdbcb5e8..8297657b 100644 --- a/internal/state/availability_test.go +++ b/internal/state/availability_test.go @@ -44,3 +44,33 @@ func TestAvailability(t *testing.T) { t.Fatalf("after pruning: %+v", a) } } + +func TestDowntime(t *testing.T) { + t0 := time.Date(2026, 3, 10, 12, 0, 0, 0, time.UTC) + a := newAvailability() + // Down 11:50-12:10, 12:30-12:40 and 13:00-13:20, and email at 12:30-13:30. + a.Unavailable(ServicePatchwork, "lorem", t0.Add(-10*time.Minute)) + a.Available(ServicePatchwork, t0.Add(10*time.Minute)) + a.Unavailable(ServiceEmail, "ipsum", t0.Add(30*time.Minute)) + a.Unavailable(ServicePatchwork, "dolor", t0.Add(30*time.Minute)) + a.Available(ServicePatchwork, t0.Add(40*time.Minute)) + a.Available(ServiceEmail, t0.Add(90*time.Minute)) + a.Unavailable(ServicePatchwork, "sit", t0.Add(time.Hour)) + + for _, tc := range []struct { + from, to time.Duration + want time.Duration + }{ + // Periods are cut at both ends; the period going on lasts + // until the end. + {0, 75 * time.Minute, 35 * time.Minute}, + {15 * time.Minute, 25 * time.Minute, 0}, + {35 * time.Minute, 2 * time.Hour, 65 * time.Minute}, + {-time.Hour, -30 * time.Minute, 0}, + } { + got := a.Downtime(ServicePatchwork, t0.Add(tc.from), t0.Add(tc.to)) + if got != tc.want { + t.Errorf("downtime from %s to %s = %s, want %s", tc.from, tc.to, got, tc.want) + } + } +} diff --git a/internal/state/schema.go b/internal/state/schema.go index 0e96f25a..1d9ef56b 100644 --- a/internal/state/schema.go +++ b/internal/state/schema.go @@ -17,7 +17,7 @@ import ( // even an added field: older versions refuse unknown fields, and a // version mismatch tells the user what to do. TestSchemaFields fails // when the structure changes without a bump. -const SchemaVersion = 14 +const SchemaVersion = 15 const ( kindMeta = "meta" diff --git a/internal/state/schema_test.go b/internal/state/schema_test.go index 0c3b0ce7..fc98d94b 100644 --- a/internal/state/schema_test.go +++ b/internal/state/schema_test.go @@ -357,6 +357,35 @@ func TestVersionThirteenFixesRewrittenSubmitters(t *testing.T) { } } +func TestVersionFourteenKeepsPendingComments(t *testing.T) { + dir := t.TempDir() + if err := os.MkdirAll(filepath.Join(dir, HistoryDir), 0o755); err != nil { + t.Fatal(err) + } + x := sampleSeries(1) + x.Finish(SeriesDone, "", now) + x.Patches[0].CommentsPending = true + b, err := json.Marshal(queueFile{Schema: 14, Series: []*Series{x}}) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, HistoryDir, "2026-03.json"), b, + 0o644); err != nil { + t.Fatal(err) + } + s, err := Load(dir) + if err != nil { + t.Fatal(err) + } + list, err := s.History("2026-03") + if err != nil { + t.Fatal(err) + } + if pt := list[0].Patches[0]; !pt.CommentsPending || pt.CommentsPendingSince != "" { + t.Errorf("patch = %+v", pt) + } +} + func TestNewerSchemaRefused(t *testing.T) { dir := t.TempDir() os.MkdirAll(filepath.Join(dir, HistoryDir), 0o755) diff --git a/internal/state/state.go b/internal/state/state.go index 25b97919..e3043b02 100644 --- a/internal/state/state.go +++ b/internal/state/state.go @@ -199,9 +199,12 @@ type Patch struct { Pushed string `json:"pushed,omitempty"` // CommentsSeen is the highest comment ID checked for rechecks; - // CommentsPending asks for the comments to be fetched. - CommentsSeen int `json:"comments_seen,omitempty"` - CommentsPending bool `json:"comments_pending,omitempty"` + // CommentsPending asks for the comments to be fetched, and + // CommentsPendingSince is when that was asked for, to give up + // after a while when no comments show up. + CommentsSeen int `json:"comments_seen,omitempty"` + CommentsPending bool `json:"comments_pending,omitempty"` + CommentsPendingSince string `json:"comments_pending_since,omitempty"` // Recheck requests a rerun of the failed runs and RecheckChecks of // the runs with these Patchwork check names, from a Recheck-request diff --git a/internal/state/testdata/schema-15.txt b/internal/state/testdata/schema-15.txt new file mode 100644 index 00000000..6f8fbc73 --- /dev/null +++ b/internal/state/testdata/schema-15.txt @@ -0,0 +1,147 @@ +availability.failing{}.outage bool +availability.failing{}.reason string +availability.failing{}.service string +availability.failing{}.since string +availability.failing{}.until string +availability.periods[].outage bool +availability.periods[].reason string +availability.periods[].service string +availability.periods[].since string +availability.periods[].until string +availability.schema int +availability.since string +meta.archives{}.failing string +meta.archives{}.last_month string +meta.archives{}.months{} int +meta.archives{}.retry{}.attempts int +meta.archives{}.retry{}.cause string +meta.archives{}.retry{}.first string +meta.archives{}.retry{}.last_error string +meta.archives{}.retry{}.next string +meta.archives{}.retry{}.op string +meta.archives{}.retry{}.outage string +meta.discovery_start string +meta.events{} int +meta.instance.patchwork string +meta.instance.project string +meta.instance.test_repo string +meta.outages{}.alert_failing string +meta.outages{}.alerted string +meta.outages{}.checks bool +meta.outages{}.next_probe string +meta.outages{}.reason string +meta.outages{}.since string +meta.schema int +meta.stats.jobs_per_patch float64 +meta.tools_synced string +series.schema int +series.series[].base.branch string +series.series[].base.git_url string +series.series[].base.sha string +series.series[].base.why string +series.series[].branch string +series.series[].checked string +series.series[].cover_id int +series.series[].date string +series.series[].decisions[].action string +series.series[].decisions[].detail string +series.series[].decisions[].run_url string +series.series[].decisions[].time string +series.series[].decisions[].workflow_url string +series.series[].depends_on int +series.series[].discovered string +series.series[].failure.attempts int +series.series[].failure.cause string +series.series[].failure.first string +series.series[].failure.last_error string +series.series[].failure.next string +series.series[].failure.op string +series.series[].failure.outage string +series.series[].finished string +series.series[].hold bool +series.series[].id int +series.series[].name string +series.series[].patches[].comments_pending bool +series.series[].patches[].comments_pending_since string +series.series[].patches[].comments_seen int +series.series[].patches[].decisions[].action string +series.series[].patches[].decisions[].detail string +series.series[].patches[].decisions[].run_url string +series.series[].patches[].decisions[].time string +series.series[].patches[].decisions[].workflow_url string +series.series[].patches[].failure.attempts int +series.series[].patches[].failure.cause string +series.series[].patches[].failure.first string +series.series[].patches[].failure.last_error string +series.series[].patches[].failure.next string +series.series[].patches[].failure.op string +series.series[].patches[].failure.outage string +series.series[].patches[].id int +series.series[].patches[].msgid string +series.series[].patches[].name string +series.series[].patches[].number int +series.series[].patches[].pushed string +series.series[].patches[].recheck bool +series.series[].patches[].recheck_checks[] string +series.series[].patches[].retry bool +series.series[].patches[].runs[].attempt int +series.series[].patches[].runs[].auto_reruns int +series.series[].patches[].runs[].check_failure.attempts int +series.series[].patches[].runs[].check_failure.cause string +series.series[].patches[].runs[].check_failure.first string +series.series[].patches[].runs[].check_failure.last_error string +series.series[].patches[].runs[].check_failure.next string +series.series[].patches[].runs[].check_failure.op string +series.series[].patches[].runs[].check_failure.outage string +series.series[].patches[].runs[].completed string +series.series[].patches[].runs[].conclusion string +series.series[].patches[].runs[].correlation_id string +series.series[].patches[].runs[].details string +series.series[].patches[].runs[].failure.attempts int +series.series[].patches[].runs[].failure.cause string +series.series[].patches[].runs[].failure.first string +series.series[].patches[].runs[].failure.last_error string +series.series[].patches[].runs[].failure.next string +series.series[].patches[].runs[].failure.op string +series.series[].patches[].runs[].failure.outage string +series.series[].patches[].runs[].file string +series.series[].patches[].runs[].final.check string +series.series[].patches[].runs[].final.email string +series.series[].patches[].runs[].final.message_id string +series.series[].patches[].runs[].id int64 +series.series[].patches[].runs[].kind string +series.series[].patches[].runs[].min_attempt int +series.series[].patches[].runs[].pending.check string +series.series[].patches[].runs[].pending.email string +series.series[].patches[].runs[].pending.message_id string +series.series[].patches[].runs[].reply.check string +series.series[].patches[].runs[].reply.email string +series.series[].patches[].runs[].reply.message_id string +series.series[].patches[].runs[].reply_failure.attempts int +series.series[].patches[].runs[].reply_failure.cause string +series.series[].patches[].runs[].reply_failure.first string +series.series[].patches[].runs[].reply_failure.last_error string +series.series[].patches[].runs[].reply_failure.next string +series.series[].patches[].runs[].reply_failure.op string +series.series[].patches[].runs[].reply_failure.outage string +series.series[].patches[].runs[].result string +series.series[].patches[].runs[].status string +series.series[].patches[].runs[].updated string +series.series[].patches[].runs[].url string +series.series[].patches[].runs[].workflow string +series.series[].patches[].sender.email string +series.series[].patches[].sender.name string +series.series[].patches[].sha string +series.series[].patches[].status string +series.series[].patches[].subject string +series.series[].reapply bool +series.series[].reason string +series.series[].retry bool +series.series[].skip string +series.series[].started string +series.series[].status string +series.series[].submitter.email string +series.series[].submitter.name string +series.series[].total int +series.series[].version int +series.series[].web_url string