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.
Give the instance database one write policy
8f6548a6249e86bfb4741a85cc6ca7efe8b3a064 · Keenan Nemetz · 3d ago · parent 1eac8552
Give the instance database one write policy instance.db has one writer per file, and the server, the push hook, and sshd's key command all open it. Each store picked its own pragmas, so a transaction that read and then wrote could be refused with SQLITE_BUSY without the busy timeout applying. And the push hook opened every store with the writer open, which ran each schema inside a push: runs.Open dropped and rebuilt the runs_once index on every call, over a run table that is never pruned, while the server might be committing a lease. Open every file through internal/sqlstore instead: WAL, synchronous=NORMAL, and immediate write transactions, with a read-only mode for the processes that only read. The hook and sshd's key lookup use that read-only mode, so a push can no longer write a schema. The queue's shape is applied once per database and recorded in a schema_versions row, so reopening it does no DDL and an old unversioned queue still upgrades. Write transactions that only touch the database retry a lock conflict instead of failing the operation. Refs #398
internal/sqlstore/sqlstore_test.go +108
| @@ -0,0 +1,108 @@ | |||
| 1 | package sqlstore | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "database/sql" | ||
| 5 | "path/filepath" | ||
| 6 | "testing" | ||
| 7 | ) | ||
| 8 | |||
| 9 | func TestOpenAppliesTheWritePolicy(t *testing.T) { | ||
| 10 | t.Parallel() | ||
| 11 | d, err := Open(filepath.Join(t.TempDir(), "instance.db"), Options{}) | ||
| 12 | if err != nil { | ||
| 13 | t.Fatal(err) | ||
| 14 | } | ||
| 15 | defer d.Close() | ||
| 16 | |||
| 17 | for _, c := range []struct { | ||
| 18 | pragma string | ||
| 19 | want any | ||
| 20 | }{ | ||
| 21 | {"journal_mode", "wal"}, | ||
| 22 | {"synchronous", int64(1)}, // NORMAL | ||
| 23 | {"busy_timeout", int64(5000)}, | ||
| 24 | } { | ||
| 25 | var got any | ||
| 26 | if err := d.QueryRow(`PRAGMA ` + c.pragma).Scan(&got); err != nil { | ||
| 27 | t.Fatalf("PRAGMA %s: %v", c.pragma, err) | ||
| 28 | } | ||
| 29 | if got != c.want { | ||
| 30 | t.Errorf("%s = %v, want %v", c.pragma, got, c.want) | ||
| 31 | } | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | // A read-only handle is what the push hook uses: it must refuse a write rather | ||
| 36 | // than let a migration run inside somebody's push. | ||
| 37 | func TestReadOnlyRefusesAWrite(t *testing.T) { | ||
| 38 | t.Parallel() | ||
| 39 | path := filepath.Join(t.TempDir(), "instance.db") | ||
| 40 | |||
| 41 | writer, err := Open(path, Options{MaxConns: 1}) | ||
| 42 | if err != nil { | ||
| 43 | t.Fatal(err) | ||
| 44 | } | ||
| 45 | if _, err := writer.Exec(`CREATE TABLE t (x INTEGER)`); err != nil { | ||
| 46 | t.Fatal(err) | ||
| 47 | } | ||
| 48 | if _, err := writer.Exec(`INSERT INTO t VALUES (1)`); err != nil { | ||
| 49 | t.Fatal(err) | ||
| 50 | } | ||
| 51 | if err := writer.Close(); err != nil { | ||
| 52 | t.Fatal(err) | ||
| 53 | } | ||
| 54 | |||
| 55 | reader, err := Open(path, Options{MaxConns: 1, ReadOnly: true}) | ||
| 56 | if err != nil { | ||
| 57 | t.Fatal(err) | ||
| 58 | } | ||
| 59 | defer reader.Close() | ||
| 60 | |||
| 61 | var n int | ||
| 62 | if err := reader.QueryRow(`SELECT count(*) FROM t`).Scan(&n); err != nil || n != 1 { | ||
| 63 | t.Fatalf("read = %d (%v), want 1", n, err) | ||
| 64 | } | ||
| 65 | if _, err := reader.Exec(`INSERT INTO t VALUES (2)`); err == nil { | ||
| 66 | t.Fatal("a read-only handle accepted a write") | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | // IsBusy is what makes the retry helper retry, so it has to recognise the error | ||
| 71 | // SQLite actually returns when a second writer's turn never comes. | ||
| 72 | func TestIsBusyRecognisesALockConflict(t *testing.T) { | ||
| 73 | t.Parallel() | ||
| 74 | if IsBusy(nil) || IsBusy(sql.ErrNoRows) { | ||
| 75 | t.Fatal("an error that is not a lock conflict was reported as one") | ||
| 76 | } | ||
| 77 | |||
| 78 | path := filepath.Join(t.TempDir(), "instance.db") | ||
| 79 | writer, err := Open(path, Options{MaxConns: 1}) | ||
| 80 | if err != nil { | ||
| 81 | t.Fatal(err) | ||
| 82 | } | ||
| 83 | defer writer.Close() | ||
| 84 | if _, err := writer.Exec(`CREATE TABLE t (x INTEGER)`); err != nil { | ||
| 85 | t.Fatal(err) | ||
| 86 | } | ||
| 87 | // Hold the write lock for as long as the second writer is willing to wait. | ||
| 88 | tx, err := writer.Begin() | ||
| 89 | if err != nil { | ||
| 90 | t.Fatal(err) | ||
| 91 | } | ||
| 92 | defer tx.Rollback() | ||
| 93 | if _, err := tx.Exec(`INSERT INTO t VALUES (1)`); err != nil { | ||
| 94 | t.Fatal(err) | ||
| 95 | } | ||
| 96 | second, err := sql.Open("sqlite", path+"?_busy_timeout=10&_txlock=immediate") | ||
| 97 | if err != nil { | ||
| 98 | t.Fatal(err) | ||
| 99 | } | ||
| 100 | defer second.Close() | ||
| 101 | _, err = second.Exec(`INSERT INTO t VALUES (2)`) | ||
| 102 | if err == nil { | ||
| 103 | t.Fatal("two writers committed at once; the lock was not held") | ||
| 104 | } | ||
| 105 | if !IsBusy(err) { | ||
| 106 | t.Fatalf("%v was not recognised as a lock conflict", err) | ||
| 107 | } | ||
| 108 | } | ||