Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Copy a prepared instance per test, and act on a push or a finish at once

484548cd70c4ac42e4210132bcadaf8b1a8af3bc · Keenan Nemetz · 9d ago · parent 4489805d

Copy a prepared instance per test, and act on a push or a finish at once

Every test in the server package built an instance from empty files, and
creating those schemas costs about 400 ms: index.Open on a new file is 226 ms
and runs.Open 152 ms, nearly all of it DDL through the pure-Go SQLite driver,
while opening the same files again is 3 ms and 17 ms. With 253 fixtures that
was most of the package's runtime. TestMain now builds one data directory with
every schema and no rows, and a fixture copies it — a handful of small files —
so the per-test cost is the copy, not the schema.

The queue also waited on timers rather than on events, which is latency a
person feels, not just tests:

  - the push spool was read once a second, so a push's first job appeared up
    to a second later. It is read every quarter second now, and once at
    startup, so a restart also picks up what landed while the instance was
    down.
  - the lease janitor swept every five seconds whatever the lease was, so a
    two-second lease still looked dead for five. The sweep is a quarter of the
    lease, capped at five seconds, which leaves a default minute-long lease
    exactly as it was.
  - a merge-when-ready request waited for the landing sweep after its combined
    result turned green, and for the same sweep after being armed. Arming now
    notifies the landing worker through the run store, and a stored result
    notifies it from the SSH session that reports it; both still run the full
    gate, and the sweep remains the safety net.

The waits that only tests can shorten are shorter too: three spool ticks
instead of one long sleep where a test proves nothing was queued, 25 ms polls
instead of 100 ms for state the server has already changed, and the two
workflow steps that sleep only to stay running are half as long.

Measured over `go test -json`, summing per-test elapsed so parallelism cannot
hide it: the server package goes from 650 s of accumulated test time to about
275 s across the same 332 tests. In the verify container the server package's
-race run goes from 47.7 s to about 35 s, and the whole job is roughly 75 s with
the image cached.

The spool contract is unchanged: one file per ref update, consumed idempotently,
cleared when queued. leaseSweep and the run store's arming notification have
unit tests; the rest is covered by the integration tests that depended on those
timers.

Verified with `go test ./... -count=1`, `go test ./server -shuffle=on`, and
`vole workflow run verify` (which includes the race suite).

Closes: 01M87KDS5D5GCB5XGG4MW1NDQC

server/ci.go +45−17

@@ -16,6 +16,12 @@ import (
16 16 "rodent.software/vole/metadata"
17 17 )
18 18
19 // spoolInterval is how often the push spool is read. A spool entry is a file, and
20 // an idle instance pays one directory read for it; a quarter of a second is the
21 // difference between a push landing and its first job appearing for a reader who
22 // just pushed.
23 const spoolInterval = 250 * time.Millisecond
24
19 25 // watchSpool turns accepted pushes into queued CI runs.
20 26 //
21 27 // The hook cannot write to the database — it runs inside a push and must not hold
@@ -24,28 +30,35 @@ import (
24 30 // same (repo, ref, sha, workflow, job) once, because the queue has a unique index
25 31 // on exactly that tuple.
26 32 func (s *Server) watchSpool(ctx context.Context) {
27 t := time.NewTicker(time.Second)
33 t := time.NewTicker(spoolInterval)
28 34 defer t.Stop()
29 35 for {
36 s.drainSpool()
30 37 select {
31 38 case <-ctx.Done():
32 39 return
33 40 case <-t.C:
34 entries, err := runs.Next(s.Cfg.SpoolDir())
35 if err != nil {
36 s.Log.Warn("ci: cannot read the push spool", "err", err)
37 continue
38 }
39 for _, e := range entries {
40 if e.Err != nil {
41 s.Log.Warn("ci: dropping an unreadable spool entry", "path", e.Path, "err", e.Err)
42 } else {
43 s.enqueue(e.Event)
44 }
45 if err := runs.Done(e.Path); err != nil {
46 s.Log.Warn("ci: cannot clear a spool entry", "path", e.Path, "err", err)
47 }
48 }
41 }
42 }
43 }
44
45 // drainSpool enqueues every entry in the spool and clears it. Startup runs it
46 // before the first tick, so a restart picks up a push that landed while the
47 // instance was stopped without waiting for the loop.
48 func (s *Server) drainSpool() {
49 entries, err := runs.Next(s.Cfg.SpoolDir())
50 if err != nil {
51 s.Log.Warn("ci: cannot read the push spool", "err", err)
52 return
53 }
54 for _, e := range entries {
55 if e.Err != nil {
56 s.Log.Warn("ci: dropping an unreadable spool entry", "path", e.Path, "err", e.Err)
57 } else {
58 s.enqueue(e.Event)
59 }
60 if err := runs.Done(e.Path); err != nil {
61 s.Log.Warn("ci: cannot clear a spool entry", "path", e.Path, "err", err)
49 62 }
50 63 }
51 64 }
@@ -500,9 +513,24 @@ func pagesPath(job ci.Job) string {
500 513 return job.Pages.Path
501 514 }
502 515
516 // leaseSweep is how often expired leases are looked for. A runner that died is
517 // only visible as an expiry, so the sweep is the lag before its job is requeued:
518 // a quarter of the lease keeps that in proportion, and the default minute-long
519 // lease stays on the five seconds the janitor has always used.
520 func leaseSweep(lease time.Duration) time.Duration {
521 interval := lease / 4
522 if interval > 5*time.Second {
523 return 5 * time.Second
524 }
525 if interval < 100*time.Millisecond {
526 return 100 * time.Millisecond
527 }
528 return interval
529 }
530
503 531 // watchLeases hands jobs from dead runners back to the queue.
504 532 func (s *Server) watchLeases(ctx context.Context) {
505 t := time.NewTicker(5 * time.Second)
533 t := time.NewTicker(leaseSweep(s.Cfg.Lease()))
506 534 defer t.Stop()
507 535 for {
508 536 select {