Commit 81428f1
committed
perf: defer FTS triggers, bulk-write audit, index audit on sync
Reduces sync write cost on large catch-ups, primarily for Pi-class
hardware:
- Add composite indexes on the `audit` table so the per-tick last-
generation lookup is served by an index in both V1 (registryId,
generation) + (orgUid, generation) and V2 (registry_id, generation)
+ (org_uid, generation). The V1 mostRecentOrgAuditRecord lookup in
sync-registries.js now orders by generation DESC so the new
composite index serves it (the old createdAt DESC ordering would
not).
- Replace per-row Audit.create with a single Audit.bulkCreate per
generation in V1 and V2 sync. V1 grows a new Audit.bulkCreate
static that threads mirrorTransaction through to the mirror
bulkCreate call. Bulk inserts cap at batchSize 500 to stay under
SQLite's SQLITE_MAX_VARIABLE_NUMBER limit on very large
generations.
- Defer projects_fts / units_fts (V1) and projects_v2_fts /
units_v2_fts (V2) maintenance triggers while any subscribed org
is mid-catch-up. New helpers in src/utils/fts5-deferral.js and
src/utils/fts5-deferral-v2.js snapshot the currently-installed
trigger DDL into `meta` via sqlite_master introspection, drop the
triggers, and recreate them from the snapshot once all orgs are
caught up. Snapshotting at defer time instead of hardcoding DDL
removes a silent schema-drift class entirely. Persist the
deferral state in `meta` so a crash mid-catch-up is recovered on
the next boot. The post-loop restore decision runs in a `finally`
block so a thrown sync error doesn't leave triggers permanently
dropped, and boot recovery is non-fatal so a transient FTS
rebuild failure can't brick startup.
The deferral path is gated on NODE_ENV !== 'test' so the existing
integration-test fixtures (which seed many subscribed-but-unsynced
orgs alongside live FTS-dependent specs) keep their FTS triggers
installed; the new sync-write-perf integration specs exercise the
deferral helpers directly.
New integration tests cover:
- audit composite indexes + EXPLAIN-plan checks for both registry
and org-uid lookups
- per-generation Audit.bulkCreate row shape, including a real-DB
test of the V1 mirror branch and isolation between source and
mirror transactions
- FTS5 trigger drop / recreate / rebuild helpers, the
ensure/restore state machine, and the boot-recovery path1 parent e88ac04 commit 81428f1
13 files changed
Lines changed: 2418 additions & 161 deletions
File tree
- src
- database
- migrations
- v2
- migrations
- models/audit
- tasks
- utils
- tests
- integration
- v2/integration
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
25 | 29 | | |
26 | 30 | | |
27 | 31 | | |
28 | 32 | | |
29 | 33 | | |
30 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
31 | 38 | | |
32 | 39 | | |
33 | 40 | | |
| |||
206 | 213 | | |
207 | 214 | | |
208 | 215 | | |
209 | | - | |
210 | | - | |
211 | | - | |
| 216 | + | |
212 | 217 | | |
213 | 218 | | |
214 | 219 | | |
| |||
318 | 323 | | |
319 | 324 | | |
320 | 325 | | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
| 326 | + | |
325 | 327 | | |
326 | 328 | | |
327 | 329 | | |
| |||
523 | 525 | | |
524 | 526 | | |
525 | 527 | | |
526 | | - | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
527 | 533 | | |
528 | 534 | | |
529 | | - | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
530 | 540 | | |
531 | 541 | | |
532 | | - | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
533 | 547 | | |
534 | 548 | | |
535 | 549 | | |
| |||
539 | 553 | | |
540 | 554 | | |
541 | 555 | | |
542 | | - | |
543 | | - | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
544 | 563 | | |
545 | 564 | | |
546 | 565 | | |
| |||
581 | 600 | | |
582 | 601 | | |
583 | 602 | | |
584 | | - | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
585 | 606 | | |
586 | 607 | | |
587 | 608 | | |
| |||
746 | 767 | | |
747 | 768 | | |
748 | 769 | | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
749 | 795 | | |
750 | 796 | | |
751 | 797 | | |
| |||
Lines changed: 56 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
| |||
184 | 185 | | |
185 | 186 | | |
186 | 187 | | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
187 | 192 | | |
0 commit comments