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.

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

forge/identity/identity.go +13−12

@@ -45,12 +45,11 @@ import (
45 45 "sync/atomic"
46 46 "time"
47 47
48 _ "modernc.org/sqlite"
49
50 48 "golang.org/x/crypto/ssh"
51 49 "rodent.software/vole/forge/access"
52 50 "rodent.software/vole/internal/audit"
53 51 "rodent.software/vole/internal/secure"
52 "rodent.software/vole/internal/sqlstore"
54 53 )
55 54
56 55 var nameRE = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]{1,31}$`)
@@ -197,16 +196,18 @@ CREATE INDEX IF NOT EXISTS sessions_user ON sessions(user_name);
197 196 CREATE INDEX IF NOT EXISTS sessions_expiry ON sessions(expires);
198 197 `
199 198
199 // openDB opens the roster with the instance's write policy. Foreign keys are on
200 // because the roster's schema declares them: a session or a key must not outlive
201 // the account it belongs to.
200 202 func openDB(path string) (*sql.DB, error) {
201 // _txlock=immediate makes every write transaction take the write lock up
202 // front, so a second `vole admin account` process waits on busy_timeout instead of
203 // failing when it tries to upgrade a read lock mid-transaction.
204 db, err := sql.Open("sqlite", path+"?_busy_timeout=5000&_journal_mode=WAL&_foreign_keys=1&_txlock=immediate")
205 if err != nil {
206 return nil, err
207 }
208 db.SetMaxOpenConns(4)
209 return db, nil
203 return sqlstore.Open(path, sqlstore.Options{MaxConns: 4, ForeignKeys: true})
204 }
205
206 // openDBReader opens the roster for a caller that only reads: sshd runs the key
207 // lookup before it knows who is connecting, and applying a schema there would be
208 // a write on a path that must only read.
209 func openDBReader(path string) (*sql.DB, error) {
210 return sqlstore.Open(path, sqlstore.Options{MaxConns: 4, ReadOnly: true, ForeignKeys: true})
210 211 }
211 212
212 213 // Init creates the roster database if it is missing. Running it on an existing
@@ -656,7 +657,7 @@ func OpenReader(path string) (*Store, error) {
656 657 if _, err := os.Stat(path); err != nil {
657 658 return nil, fmt.Errorf("identity: %s is missing; run vole admin init", path)
658 659 }
659 db, err := openDB(path)
660 db, err := openDBReader(path)
660 661 if err != nil {
661 662 return nil, fmt.Errorf("identity: %w", err)
662 663 }