Artifact 50aa0b15828d68094291abb36516c277948b1f9d82d1b2bebc05dd5a13334de7:
- File
db/db.go
— part of check-in
[18bfbc166c]
at
2026-07-27 23:31:22
on branch trunk
— fix(sync): derive the project code, and write artifact visibility inside the storing transaction (#204)
* fix(sync): derive the project code from the repo instead of panicking (#199)
Supplying User/Password made SyncOpts.ProjectCode mandatory in practice: buildLoginCard passed the caller's raw value straight to computeLogin, which asserts it is non-empty, so an authenticated Sync on a repo whose project code the caller had not repeated took the process down. Nothing in the public API signalled that a password made the field required, and a server embedding the library died with it.
buildRequest already fell back to the repo's stored project-code for the push/pull cards; that fallback was local to one function, which is exactly how the login path drifted out of step with it. Resolve the code once when the session is built — caller value first, the repo's own config second — and let buildRequest and buildLoginCard both read it. A code that genuinely cannot be determined now returns a descriptive error from the site that needs it rather than panicking.
Sync's deferred result-not-nil assertion fired during panic unwinding, where err and result are both nil, replacing the original cause with its own message. The invariant only holds on a normal return, so it no longer asserts while a panic is in flight.
* fix(sync): write received artifacts' visibility inside the storing transaction (#200)
Receiving an artifact cost three write transactions, not one. The blob was stored in a transaction, its dephantomize cascade committed another, and then applyPrivateStatus re-derived the rid on the pool and issued a bare DELETE FROM private -- an implicit transaction of its own, with its own BEGIN IMMEDIATE and fsync, for a row that a transaction microseconds earlier had guaranteed did not exist. Every igot card announcing an artifact already held cost one more. Measured against a real fossil peer: 1509 statements executed outside a transaction to receive 1504 artifacts.
That is minutes of pure commit overhead on a large repository -- roughly 200,000 write transactions for a 67k-artifact pull -- and it is fragile as well as slow. Each of those writes contests the database's single write lock independently, so N artifacts means N chances to lose that contest and fail outright rather than one chance to wait at a single BEGIN.
Visibility now travels with the content it describes: storeReceivedFile takes the visibility a private card announced and records it inside the transaction that stores the blob, using the rid that transaction already has. The status is now atomic with the content rather than a separate write that could be interrupted between the two. The igot path buffers its updates and writes them once per round. The same three-transaction shape on the server's receive path goes with it.
The same real-fossil pull now executes 2 statements outside a transaction for those 1504 artifacts, down from 1509. db.DB.PoolExecs makes that budget assertable, which is how the regression test pins O(rounds) rather than O(artifacts) without needing a large corpus.
On the failure originally reported: the "database is locked" error came from a harness that copied a live SQLite database out from under a running server without its WAL, so that specific lock holder is not a product defect and this does not claim to fix it. The transaction storm above is a real performance and robustness defect on its own evidence, and is what this changes.
* test(sync): pin the transaction budget, not just writes outside a transaction
The budget tests measured PoolExecs, which counts statements issued outside a transaction. That pins one half of the defect and misses the other: moving a per-artifact write from the pool into its own WithTx leaves PoolExecs unchanged while every artifact still pays a BEGIN IMMEDIATE and an fsync, and still contests the single write lock on its own. That is the cost issue #200 actually measured -- roughly three write transactions per received file, about 203,000 for one clone-sized pull.
Verified by mutation: reverting the batched visibility flush to one transaction per update left both budget tests green. They now count transactions too, via a new db.DB.WriteTxns counter, and the same mutation fails with "began 51 write transactions for 50 igot cards over 1 rounds; want <= 4".
Also stop the contention test from starving the pull it is meant to observe. The rival connection re-acquired the write lock immediately on release, so on a loaded machine it could hold the pull's single BEGIN past busy_timeout and fail the test even when the batching under test is perfect -- which is what happened on CI. It now yields between bursts, modelling another process doing occasional writes rather than testing SQLite's fairness. (user: [email protected], size: 11304) [annotate] [blame] [check-ins using]
package db import ( "database/sql" "errors" "fmt" "net/url" "os" "sort" "strings" "sync/atomic" ) // CheckpointMode mirrors SQLite's PRAGMA wal_checkpoint(<mode>) argument. type CheckpointMode int const ( // CheckpointPassive checkpoints frames not held by readers; never blocks. // Appropriate for periodic background checkpoints. CheckpointPassive CheckpointMode = iota // CheckpointFull blocks new writers until all frames are checkpointed. CheckpointFull // CheckpointRestart is FULL plus restarts the WAL file. CheckpointRestart // CheckpointTruncate is RESTART plus truncates the WAL file to zero bytes. // Produces the most compact on-disk file and is what Close uses. CheckpointTruncate ) func (m CheckpointMode) String() string { switch m { case CheckpointPassive: return "PASSIVE" case CheckpointFull: return "FULL" case CheckpointRestart: return "RESTART" case CheckpointTruncate: return "TRUNCATE" default: return fmt.Sprintf("CheckpointMode(%d)", int(m)) } } // DB wraps a SQLite database connection. type DB struct { conn *sql.DB path string driver string stmts stmtCache poolExecs atomic.Int64 writeTxns atomic.Int64 } // Open opens a SQLite database with the registered driver and default pragmas. func Open(path string) (*DB, error) { return OpenWith(path, OpenConfig{}) } // OpenSQL opens a raw *sql.DB using the registered driver and the same DSN // construction as OpenWith. params are appended as URI query parameters after // driver pragmas, so callers can request options such as mode=ro without // constructing driver-specific DSNs. func OpenSQL(path string, cfg OpenConfig, params map[string]string) (*sql.DB, error) { driver, dsn := SQLDriverAndDSN(path, cfg, params) conn, err := sql.Open(driver, dsn) if err != nil { return nil, fmt.Errorf("db.OpenSQL(%s): %w", driver, err) } if err := applyWASMPragmas(conn, mergedPragmas(cfg.Pragmas)); err != nil { conn.Close() return nil, fmt.Errorf("db.OpenSQL: %w", err) } return conn, nil } // SQLDriverAndDSN returns the driver name and DSN OpenWith would use, with // optional URI query params appended after driver-specific options. func SQLDriverAndDSN(path string, cfg OpenConfig, params map[string]string) (string, string) { if path == "" { panic("db.SQLDriverAndDSN: path must not be empty") } if registered == nil { panic("db.SQLDriverAndDSN: no driver registered — import a driver package (e.g., _ \"github.com/danmestas/go-libfossil/db/driver/modernc\")") } driver := cfg.Driver if driver == "" { driver = registered.Name } pragmas := mergedPragmas(cfg.Pragmas) dsn := buildSQLDSN(path, pragmas) return driver, appendURIParams(dsn, params) } func mergedPragmas(overrides map[string]string) map[string]string { pragmas := DefaultPragmas() for k, v := range overrides { pragmas[k] = v } return pragmas } func buildSQLDSN(path string, pragmas map[string]string) string { if wasmClearPragmas { suffix := wasmDSNSuffix() if suffix != "" { return fmt.Sprintf("file:%s?%s", path, suffix) } return path } return appendURIParamString(registered.BuildDSN(path, pragmas), wasmDSNSuffix()) } func appendURIParamString(dsn, params string) string { if params == "" { return dsn } if strings.Contains(dsn, "?") { return dsn + "&" + params } if strings.HasPrefix(dsn, "file:") { return dsn + "?" + params } return "file:" + dsn + "?" + params } func appendURIParams(dsn string, params map[string]string) string { if len(params) == 0 { return dsn } keys := make([]string, 0, len(params)) for k := range params { keys = append(keys, k) } sort.Strings(keys) var b strings.Builder if strings.Contains(dsn, "?") { b.WriteString(dsn) b.WriteByte('&') } else { if strings.HasPrefix(dsn, "file:") { b.WriteString(dsn) } else { b.WriteString("file:") b.WriteString(dsn) } b.WriteByte('?') } for i, k := range keys { if i > 0 { b.WriteByte('&') } b.WriteString(url.QueryEscape(k)) b.WriteByte('=') b.WriteString(url.QueryEscape(params[k])) } return b.String() } func applyWASMPragmas(conn *sql.DB, pragmas map[string]string) error { if !wasmClearPragmas { return nil } // Apply safe pragmas via SQL. Skip journal_mode (WAL not supported on WASI). // Only apply known-safe pragmas to prevent SQL injection. safePragmas := map[string]bool{ "busy_timeout": true, "foreign_keys": true, } for k, v := range pragmas { if !safePragmas[k] { continue } if _, err := conn.Exec(fmt.Sprintf("PRAGMA %s = %s", k, v)); err != nil { return fmt.Errorf("PRAGMA %s: %w", k, err) } } return nil } // OpenWith opens a SQLite database with explicit configuration. func OpenWith(path string, cfg OpenConfig) (*DB, error) { driver, dsn := SQLDriverAndDSN(path, cfg, nil) pragmas := mergedPragmas(cfg.Pragmas) // WASM targets: skip DSN pragmas (ncruces _pragma syntax fails under WASI // file locking). Open with nolock=1, then apply safe pragmas via SQL. conn, err := sql.Open(driver, dsn) if err != nil { return nil, fmt.Errorf("db.Open(%s): %w", driver, err) } if err := applyWASMPragmas(conn, pragmas); err != nil { conn.Close() return nil, fmt.Errorf("db.Open %w", err) } return &DB{conn: conn, path: path, driver: driver, stmts: stmtCache{src: conn}}, nil } // SqlDB returns the underlying *sql.DB connection. func (d *DB) SqlDB() *sql.DB { if d == nil { panic("db.SqlDB: receiver must not be nil") } return d.conn } // Close runs PRAGMA wal_checkpoint(TRUNCATE) before closing the underlying // connection so the on-disk file is readable by external SQLite/fossil // tooling. The WASM/WASI build path does not use WAL, so the checkpoint is // skipped there. Checkpoint errors are joined with the close error so the // connection is always closed regardless of checkpoint outcome. func (d *DB) Close() error { var ckptErr error if !wasmClearPragmas { ckptErr = d.Checkpoint(CheckpointTruncate) } // Release cached prepared statements before closing the pool so no // statement handle outlives the connection it was compiled against. d.stmts.closeAll() closeErr := d.conn.Close() return errors.Join(ckptErr, closeErr) } // Checkpoint runs PRAGMA wal_checkpoint(<mode>) against the database. // Safe to call on a live database. PASSIVE never blocks; TRUNCATE produces // the most compact on-disk layout. On a non-WAL database the underlying // PRAGMA is a no-op and returns nil. func (d *DB) Checkpoint(mode CheckpointMode) error { if d == nil { panic("db.Checkpoint: receiver must not be nil") } if mode < CheckpointPassive || mode > CheckpointTruncate { return fmt.Errorf("db.Checkpoint: invalid mode %d", int(mode)) } var busy, log, ckpt int err := d.conn.QueryRow(fmt.Sprintf("PRAGMA wal_checkpoint(%s)", mode)).Scan(&busy, &log, &ckpt) if err != nil { return fmt.Errorf("db.Checkpoint(%s): %w", mode, err) } return nil } func (d *DB) Path() string { return d.path } func (d *DB) Driver() string { return d.driver } // PoolExecs returns how many statements have been executed on the pool rather // than inside a transaction. Each one runs in its own implicit transaction, so // it costs a BEGIN IMMEDIATE and an fsync of its own and contests the database's // single write lock independently of every other. A path that issues one per // artifact therefore degrades from slow to failing the moment anything else // writes, which is what issue #200 cost a large pull. Tests pin the per-round // budget against this counter. func (d *DB) PoolExecs() int64 { if d == nil { panic("db.PoolExecs: receiver must not be nil") } return d.poolExecs.Load() } // WriteTxns counts transactions begun through WithTx. Each is its own // BEGIN IMMEDIATE and fsync, and each takes the single write lock // independently, so a path that opens one per artifact is both slow and // fragile under contention -- the cost issue #200 measured at roughly three // per received file. Tests pin the per-round transaction budget against this, // which PoolExecs cannot see: moving a per-artifact write from the pool into // its own transaction leaves PoolExecs unchanged while the real cost stays. func (d *DB) WriteTxns() int64 { if d == nil { panic("db.WriteTxns: receiver must not be nil") } return d.writeTxns.Load() } func (d *DB) Exec(query string, args ...any) (sql.Result, error) { d.poolExecs.Add(1) if !cacheableStmt(query) { return d.conn.Exec(query, args...) } st, err := d.stmts.get(query) if err != nil { return nil, err } return st.Exec(args...) } func (d *DB) QueryRow(query string, args ...any) *sql.Row { if !cacheableStmt(query) { return d.conn.QueryRow(query, args...) } st, err := d.stmts.get(query) if err != nil { // Preserve database/sql's deferred-error contract: a prepare failure // must surface from Row.Scan, never here. The raw QueryRow re-attempts // the prepare internally and returns a *sql.Row that yields the error // on Scan — identical to the pre-cache behavior. return d.conn.QueryRow(query, args...) } return st.QueryRow(args...) } func (d *DB) Query(query string, args ...any) (*sql.Rows, error) { if !cacheableStmt(query) { return d.conn.Query(query, args...) } st, err := d.stmts.get(query) if err != nil { return nil, err } return st.Query(args...) } func (d *DB) SetApplicationID(id int32) error { _, err := d.conn.Exec(fmt.Sprintf("PRAGMA application_id=%d", id)) return err } func (d *DB) ApplicationID() (int32, error) { var id int32 err := d.conn.QueryRow("PRAGMA application_id").Scan(&id) return id, err } type Tx struct { tx *sql.Tx stmts stmtCache } func (t *Tx) Exec(query string, args ...any) (sql.Result, error) { if !cacheableStmt(query) { return t.tx.Exec(query, args...) } st, err := t.stmts.get(query) if err != nil { return nil, err } return st.Exec(args...) } func (t *Tx) QueryRow(query string, args ...any) *sql.Row { if !cacheableStmt(query) { return t.tx.QueryRow(query, args...) } st, err := t.stmts.get(query) if err != nil { // Preserve the deferred-error contract; see DB.QueryRow. return t.tx.QueryRow(query, args...) } return st.QueryRow(args...) } func (t *Tx) Query(query string, args ...any) (*sql.Rows, error) { if !cacheableStmt(query) { return t.tx.Query(query, args...) } st, err := t.stmts.get(query) if err != nil { return nil, err } return st.Query(args...) } func (d *DB) WithTx(fn func(tx *Tx) error) error { if d == nil { panic("db.WithTx: receiver must not be nil") } d.writeTxns.Add(1) if fn == nil { panic("db.WithTx: fn must not be nil") } sqlTx, err := d.conn.Begin() if err != nil { return fmt.Errorf("db.WithTx begin: %w", err) } tx := &Tx{tx: sqlTx, stmts: stmtCache{src: sqlTx}} defer func() { // A statement prepared against this transaction cannot outlive it, so // close the whole cache before the transaction ends. This runs on // every path (commit, error, panic); Close is idempotent, so closing // again after a successful Commit is a safe no-op. tx.stmts.closeAll() if rbErr := sqlTx.Rollback(); rbErr != nil && rbErr != sql.ErrTxDone { fmt.Fprintf(os.Stderr, "db.WithTx: rollback failed: %v\n", rbErr) } }() if err := fn(tx); err != nil { return err } return sqlTx.Commit() }