jobs S3 report: per job, one worker at a time, by request kind and key family, and a steady-state second run
TestBackgroundJobsS3OnTheFixture (opt-in, CARLOS_JOBS_S3_REPORT=1) now starts each claim worker on its own with recordS3 on, so every job's requests are broken down into LIST, GET, PUT and DELETE and by key family (perfKeyFamily: account and app ids starred), and the accounting check is per job. It then runs the economics, plan grace, run cleanup and lifetime switch passes a second time, caches warm and the day's work done, since the hourly cost is the second run's.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+149
−77
internal/console/jobs_s3_test.go+149 −77
CI failed — run details
diff --git a/internal/console/jobs_s3_test.go b/internal/console/jobs_s3_test.go| index b10fdbbca..5155bba3c 100644 |
| --- a/internal/console/jobs_s3_test.go |
| +++ b/internal/console/jobs_s3_test.go |
| @@ -8,7 +8,6 @@ import ( |
| "slices" |
| "strconv" |
| "strings" |
| - "sync" |
| "testing" |
| "time" |
| ) |
| @@ -61,89 +60,89 @@ func TestJobRunsChargeTheirS3RequestsToTheJob(t *testing.T) { |
| } |
| // The fixture's background jobs, each run once: every claim worker the |
| -// perf fixture configures is started, each is stopped once its first run |
| -// has finished, and the S3 requests each run made are printed, most first. |
| -// The printout is the measured half of the background S3 report (the |
| -// estimate per hour is the run counts times each job's interval). The |
| -// check is the accounting: every S3 request the fake answered while the |
| -// workers ran is either a job's or in the console's unattributed count, |
| -// never lost and never counted twice. It takes about ten seconds, so it |
| -// runs only for the report: CARLOS_JOBS_S3_REPORT=1. |
| +// perf fixture configures is started on its own, stopped once its first |
| +// run has finished, and the S3 requests that run made are printed, most |
| +// first, by kind (listings, reads, writes and deletes) and by key family |
| +// (perfKeyFamily). The printout is the measured half of the |
| +// background S3 report (the estimate per hour is each run's requests times |
| +// the job's runs an hour). The check is the accounting: every S3 request |
| +// the fake answered while a worker ran is either that job's or in the |
| +// console's unattributed count, never lost and never counted twice. It |
| +// takes about ten seconds, so it runs only for the report: |
| +// CARLOS_JOBS_S3_REPORT=1. |
| func TestBackgroundJobsS3OnTheFixture(t *testing.T) { |
| if os.Getenv("CARLOS_JOBS_S3_REPORT") == "" { |
| t.Skip("set CARLOS_JOBS_S3_REPORT=1 to run every job once on the perf fixture and print its S3 requests") |
| } |
| perfCaptureLogs(t) |
| fx, lat := newPerfFixture(t) |
| - s0, _ := lat.Calls() |
| - b0 := fx.srv.PerfBackground()["s3"] |
| + rec := recordS3(lat, nil) |
| - ctx, cancel := context.WithCancel(context.Background()) |
| - defer cancel() |
| - var wg sync.WaitGroup |
| - names := slices.Sorted(mapKeys(fx.srv.claimWorkers)) |
| - exited := make(map[string]bool) |
| - var mu sync.Mutex |
| - for _, name := range names { |
| - wg.Add(1) |
| - go func() { |
| - defer wg.Done() |
| - fx.srv.RunClaimWorker(ctx, name) |
| - mu.Lock() |
| - exited[name] = true |
| - mu.Unlock() |
| - }() |
| + type line struct { |
| + label, kinds, where string |
| + runs, s3 int64 |
| } |
| - // A job is done when its first run finished or its worker returned |
| - // without one (nothing to do on this deployment). Some first runs wait |
| - // on a timer longer than the test; they are reported as not run. |
| - deadline := time.Now().Add(10 * time.Second) |
| - for time.Now().Before(deadline) { |
| - pending := 0 |
| - mu.Lock() |
| - for _, name := range names { |
| - if exited[name] { |
| - continue |
| + var lines []line |
| + for _, name := range slices.Sorted(mapKeys(fx.srv.claimWorkers)) { |
| + rec.reset() |
| + b0 := fx.srv.PerfBackground()["s3"] |
| + before, _ := jobStatusOf(fx.srv, name) |
| + ctx, cancel := context.WithCancel(context.Background()) |
| + exited := make(chan struct{}) |
| + go func() { defer close(exited); fx.srv.RunClaimWorker(ctx, name) }() |
| + // Done when the first run finished or the worker returned without |
| + // one (nothing to do on this deployment). A first run that waits on |
| + // a timer longer than five seconds is reported as not run. |
| + deadline := time.Now().Add(5 * time.Second) |
| + wait: |
| + for time.Now().Before(deadline) { |
| + select { |
| + case <-exited: |
| + break wait |
| + case <-time.After(10 * time.Millisecond): |
| } |
| - if st, ok := jobStatusOf(fx.srv, name); !ok || st.Runs == 0 { |
| - pending++ |
| + if st, ok := jobStatusOf(fx.srv, name); ok && st.Runs > before.Runs { |
| + break |
| } |
| } |
| - mu.Unlock() |
| - if pending == 0 { |
| - break |
| + cancel() |
| + select { |
| + case <-exited: |
| + case <-time.After(30 * time.Second): |
| + t.Fatalf("%s did not return 30s after its context was cancelled", name) |
| } |
| - time.Sleep(20 * time.Millisecond) |
| - } |
| - cancel() |
| - done := make(chan struct{}) |
| - go func() { wg.Wait(); close(done) }() |
| - select { |
| - case <-done: |
| - case <-time.After(30 * time.Second): |
| - t.Fatal("a claim worker did not return 30s after its context was cancelled") |
| - } |
| - if !perfIdle(perfSeedsOf(fx).inflight, 2*time.Second) { |
| - t.Fatal("a request was still running") |
| - } |
| - |
| - s1, _ := lat.Calls() |
| - bg := fx.srv.PerfBackground()["s3"] - b0 |
| - type line struct { |
| - label string |
| - runs, s3 int64 |
| - } |
| - var lines []line |
| - var jobs int64 |
| - for _, st := range fx.srv.jobs.Snapshot() { |
| - label := jobLabels[st.Name] |
| - lines = append(lines, line{label + " (" + st.Name + ")", st.Runs, st.Calls["s3"]}) |
| - jobs += st.Calls["s3"] |
| - } |
| - for _, name := range names { |
| - if _, ok := jobStatusOf(fx.srv, name); !ok { |
| - lines = append(lines, line{jobLabels[name] + " (" + name + "): not configured on the fixture", 0, 0}) |
| + if !perfIdle(perfSeedsOf(fx).inflight, 2*time.Second) { |
| + t.Fatal("a request was still running") |
| + } |
| + st, ok := jobStatusOf(fx.srv, name) |
| + reqs := rec.all() |
| + bg := fx.srv.PerfBackground()["s3"] - b0 |
| + s3 := st.Calls["s3"] - before.Calls["s3"] |
| + if s3+bg != int64(len(reqs)) { |
| + t.Errorf("%s: job %d + unattributed %d S3 requests, the fake answered %d", name, s3, bg, len(reqs)) |
| + } |
| + label := jobLabels[name] + " (" + name + ")" |
| + if !ok { |
| + label += ": not configured on the fixture" |
| + } |
| + kinds := map[string]int{} |
| + where := map[string]int{} |
| + for _, q := range reqs { |
| + k, path := q.Method, q.Key |
| + if q.Key == "" { |
| + k, path = "LIST", q.Prefix |
| + } |
| + kinds[k]++ |
| + where[perfKeyFamily(path)]++ |
| + } |
| + var ks, ws []string |
| + for _, k := range slices.Sorted(mapKeys(kinds)) { |
| + ks = append(ks, k+" "+strconv.Itoa(kinds[k])) |
| } |
| + for _, w := range slices.Sorted(mapKeys(where)) { |
| + ws = append(ws, w+" "+strconv.Itoa(where[w])) |
| + } |
| + lines = append(lines, line{label, strings.Join(ks, ", "), strings.Join(ws, ", "), st.Runs - before.Runs, s3}) |
| } |
| slices.SortFunc(lines, func(a, b line) int { |
| if a.s3 != b.s3 { |
| @@ -153,13 +152,86 @@ func TestBackgroundJobsS3OnTheFixture(t *testing.T) { |
| }) |
| var b strings.Builder |
| for _, l := range lines { |
| - b.WriteString(l.label + "\t" + strconv.FormatInt(l.runs, 10) + " runs\t" + strconv.FormatInt(l.s3, 10) + " S3\n") |
| + fmt.Fprintf(&b, "%s\t%d runs\t%d S3\t%s\t%s\n", l.label, l.runs, l.s3, l.kinds, l.where) |
| } |
| - t.Logf("background S3 requests by job, on the perf fixture, first run each:\n%s(unattributed: %d; the fake answered %d)", b.String(), bg, s1-s0) |
| - if jobs+bg != s1-s0 { |
| - t.Errorf("jobs %d + unattributed %d = %d S3 requests, the fake answered %d", jobs, bg, jobs+bg, s1-s0) |
| + t.Logf("background S3 requests by job, on the perf fixture, one run each:\n%s", b.String()) |
| + |
| + // A second run of the jobs whose first run of a day (or of a process) |
| + // does more than the rest: the hourly cost is the second run's. |
| + srv := fx.srv |
| + second := []struct { |
| + name string |
| + run func(context.Context) |
| + }{ |
| + {"StartEconomicsPass", func(ctx context.Context) { |
| + _ = srv.jobs.Run(ctx, "StartEconomicsPass", func(ctx context.Context) error { return srv.economicsPass(ctx, time.Now()) }) |
| + }}, |
| + {"StartPlanGracePass", func(ctx context.Context) { |
| + _ = srv.jobs.Run(ctx, "StartPlanGracePass", func(ctx context.Context) error { srv.planGracePass(ctx, time.Now()); return nil }) |
| + }}, |
| + {"StartRunSweep", func(ctx context.Context) { |
| + srv.lifetimesIteration(ctx, "run sweep", true, false, func(ctx context.Context) { |
| + _ = srv.jobs.Run(ctx, "StartRunSweep", func(ctx context.Context) error { srv.runSweepPass(ctx, time.Now(), false); return nil }) |
| + }) |
| + }}, |
| + {"StartLifetimesCutoverWatch", func(ctx context.Context) { |
| + _ = srv.jobs.Run(ctx, "StartLifetimesCutoverWatch", func(ctx context.Context) error { srv.lifetimesWatchOnce(ctx); return nil }) |
| + }}, |
| + } |
| + b.Reset() |
| + for _, j := range second { |
| + rec.reset() |
| + j.run(context.Background()) |
| + if !perfIdle(perfSeedsOf(fx).inflight, 2*time.Second) { |
| + t.Fatal("a request was still running") |
| + } |
| + kinds := map[string]int{} |
| + where := map[string]int{} |
| + for _, q := range rec.all() { |
| + k, path := q.Method, q.Key |
| + if q.Key == "" { |
| + k, path = "LIST", q.Prefix |
| + } |
| + kinds[k]++ |
| + where[perfKeyFamily(path)]++ |
| + } |
| + var ks, ws []string |
| + for _, k := range slices.Sorted(mapKeys(kinds)) { |
| + ks = append(ks, k+" "+strconv.Itoa(kinds[k])) |
| + } |
| + for _, w := range slices.Sorted(mapKeys(where)) { |
| + ws = append(ws, w+" "+strconv.Itoa(where[w])) |
| + } |
| + fmt.Fprintf(&b, "%s (%s)\t%d S3\t%s\t%s\n", jobLabels[j.name], j.name, len(rec.all()), strings.Join(ks, ", "), strings.Join(ws, ", ")) |
| + } |
| + t.Logf("a second run, caches warm and today's daily work done:\n%s", b.String()) |
| +} |
| + |
| +// perfKeyFamily is a key or listing prefix with its account and app ids |
| +// starred, cut to the segment that names what is read: apps/*/*/instances, |
| +// accounts/*/plan.json, usage/accounts/*, metrics/boxes/*. |
| +func perfKeyFamily(key string) string { |
| + seg := strings.Split(key, "/") |
| + keep := 2 |
| + switch seg[0] { |
| + case "apps": |
| + keep = 4 |
| + for i := 1; i < len(seg) && i < 3; i++ { |
| + seg[i] = "*" |
| + } |
| + case "accounts": |
| + keep = 3 |
| + if len(seg) > 1 { |
| + seg[1] = "*" |
| + } |
| + case "usage", "metrics", "status", "alerts", "fleets": |
| + keep = 3 |
| + if len(seg) > 2 { |
| + seg[2] = "*" |
| + } |
| } |
| - if jobs == 0 { |
| - t.Error("no job made an S3 request on the fixture") |
| + if len(seg) > keep { |
| + seg = seg[:keep] |
| } |
| + return strings.Join(seg, "/") |
| } |