chadtmiller.com/knotless
git that lives in your Atmosphere account
1
10
The harness PDS commits to SQLite without waiting on the disk
docs/development.md+4−1
| @@ -26,7 +26,10 @@ Tests run against the reference PDS from npm, started by | ||
permissioned-data alpha build of that PDS, for private repositories. The | ||
alpha migrates an account's database every time it opens it, which made | ||
the same tests cost its PDS about a fifth more CPU than the stock one; the | ||
harness has it migrate each database only the first time. Every line and | ||
harness has it migrate each database only the first time. The PDS commits | ||
to SQLite on every write, and a commit waits on the disk (20 ms an fsync on | ||
the runner's), so the harness has its databases commit without waiting | ||
(`KNOTLESS_PDS_FSYNC=1` keeps the PDS as it is). Every line and | ||
function is covered across the two runs, and CI fails below that. | ||
| ||
```sh | ||
harness/pds.mjs+24−0
| @@ -42,6 +42,30 @@ const { TestNetworkNoAppView } = await import( | ||
devEnv ? pathToFileURL(resolve(root, devEnv, 'dist/index.js')).href : '@atproto/dev-env' | ||
) | ||
if (devEnv) await migrateOnce(resolve(root, devEnv)) | ||
if (!process.env.KNOTLESS_PDS_FSYNC) await skipFsync(devEnv ? resolve(root, devEnv) : undefined) | ||
| ||
/// The directory of the PDS's compiled modules that the dev-env at `dir` | ||
/// (the npm package when there is none) runs. | ||
function pdsDist(dir) { | ||
const from = dir | ||
? pathToFileURL(join(dir, 'package.json')) | ||
: import.meta.resolve('@atproto/dev-env') | ||
return dirname(realpathSync(createRequire(from).resolve('@atproto/pds'))) | ||
} | ||
| ||
/// Has the PDS's SQLite databases (an account's, the account manager's, the | ||
/// sequencer's) commit without waiting for the disk: the same modules it | ||
/// loads, patched before it starts. Each commit otherwise waits on an fsync, | ||
/// 20ms on the runner's disk, and a write makes several, which made a push | ||
/// or a pull cost most of a second when the PDS does the rest in a few | ||
/// milliseconds. A test harness has no data to keep through a crash. | ||
/// KNOTLESS_PDS_FSYNC=1 keeps the PDS as it is. | ||
async function skipFsync(dir) { | ||
const { Database } = await import(pathToFileURL(join(pdsDist(dir), 'db/db.js')).href) | ||
const sqlite = Database.sqlite | ||
Database.sqlite = (location, opts) => | ||
sqlite.call(Database, location, { ...opts, pragmas: { ...opts?.pragmas, synchronous: 'OFF' } }) | ||
} | ||
| ||
/// Has the PDS that the dev-env at `dir` runs migrate each account's | ||
/// database on its first opening alone, rather than on every one: the | ||