Repository navigation
fix(orchestration): retire settled Runs a page at a time so the main thread never stalls #43550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/runtime/orchestration/db/maintenance/orchestration-pass-checkpointer.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { spawnWorkerThread } from '../../../../worker-threads/worker-thread-spawn' | ||
| import type { OrchestrationDb } from '../orchestration-db' | ||
|
|
||
| const CHECKPOINT_INTERVAL_MS = 250 | ||
|
|
||
| // Evaluated source, as the chat journal's checkpoint worker: a passive checkpoint never blocks the writer. | ||
| const checkpointWorkerSource = ` | ||
| const { workerData } = require('node:worker_threads'); | ||
| const { DatabaseSync } = require('node:sqlite'); | ||
| const db = new DatabaseSync(workerData.filename); | ||
| if (db.prepare('PRAGMA journal_mode').get().journal_mode !== 'wal') { | ||
| throw new Error('orchestration database is not in WAL mode'); | ||
| } | ||
| db.exec('PRAGMA busy_timeout = 1000;'); | ||
| const checkpoint = db.prepare('PRAGMA wal_checkpoint(PASSIVE)'); | ||
| let failures = 0; | ||
| setInterval(() => { | ||
| try { | ||
| checkpoint.get(); | ||
| failures = 0; | ||
| } catch (error) { | ||
| // Exiting hands checkpoints back to main rather than letting the WAL grow. | ||
| if (++failures >= 3) throw error; | ||
| } | ||
| }, workerData.intervalMs); | ||
| ` | ||
|
|
||
| /** | ||
| * While a maintenance pass writes, checkpoints the WAL on a worker thread instead of in main's commits: | ||
| * an auto-checkpointing commit fsyncs the whole file and blocked main for up to 934 ms on a busy disk. | ||
| * Returns the undo; a worker that fails or exits hands checkpoints back to main at once. | ||
| */ | ||
| export function checkpointOffMainThread({ db }: OrchestrationDb): () => void { | ||
| const main = db | ||
| .prepare('PRAGMA database_list') | ||
| .all() | ||
| .find((row) => row.name === 'main') | ||
| const filename = typeof main?.file === 'string' ? main.file : '' | ||
| if (!filename) { | ||
| return () => {} | ||
| } | ||
| const previous = Number(db.pragma('wal_autocheckpoint', { simple: true })) | ||
| let worker: ReturnType<typeof spawnWorkerThread> | ||
| try { | ||
| worker = spawnWorkerThread(checkpointWorkerSource, { | ||
| name: 'orca-orchestration-checkpoint', | ||
| eval: true, | ||
| workerData: { filename, intervalMs: CHECKPOINT_INTERVAL_MS } | ||
| }) | ||
| } catch { | ||
| return () => {} | ||
| } | ||
| let restored = false | ||
| const restore = (): void => { | ||
| if (!restored) { | ||
| restored = true | ||
| try { | ||
| db.pragma(`wal_autocheckpoint = ${previous}`) | ||
| } catch { | ||
| // A closed database has no threshold left to restore. | ||
| } | ||
| } | ||
| } | ||
| worker.on('error', restore) | ||
| worker.on('exit', restore) | ||
| worker.unref() | ||
| db.pragma('wal_autocheckpoint = 0') | ||
| return () => { | ||
| worker.terminate().catch(() => {}) | ||
| restore() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.