Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions internal/pipeline/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
package pipeline

import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -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)
Expand Down
53 changes: 52 additions & 1 deletion internal/pipeline/rechecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) &&
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}

Expand Down
28 changes: 28 additions & 0 deletions internal/state/availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
30 changes: 30 additions & 0 deletions internal/state/availability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion internal/state/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
29 changes: 29 additions & 0 deletions internal/state/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions internal/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading