flatsql 3.5.0 → 3.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,7 +12,9 @@ met on Linux. Part V (§37) bounds a reader instance's memory. Part VI (§38)
12
12
  records the memory-safety and race work: the ps-wasm trap, the sanitizers and
13
13
  the first disk-full emergency. Part VII (§39) closes the query gaps T6 found
14
14
  on the host-02 fixture and accepts records stored with their own size
15
- prefix. The design is
15
+ prefix. Part VIII (§40) makes a partition's bookkeeping O(1) or O(log S) per
16
+ commit (the terabyte audit's B4) and adds manifest version 3 (M3). The
17
+ design is
16
18
  the stack's `docs/architecture/flatsql-partition-store.md` (§22 overrides
17
19
  §0–§21; §22.4 holds the owner rulings). This file records what was built, how
18
20
  to run it, the measured acceptance, and every place the build departs from the
@@ -650,11 +652,11 @@ and idle-lane sub-cursors (§21 deviation 1).
650
652
 
651
653
  | Path | Contents |
652
654
  |---|---|
653
- | `ps/compaction.h`, `src/ps/compaction.cpp` | Manifest version 2, the compacted rows format, retire items; the compaction pipeline (plan, build, SWAP, apply, abort). |
654
- | `src/ps/reclaim.cpp` | The per-partition file ledger (`disk_bytes`), the RETIRE and UNLINKED records, reader-gated unlinking, meta-segment retirement (A9), open-time cleanup; the same for the type logs (meta log rotation and retirement, retired catalog runs and manifests, the type ledger). |
655
- | `ps/quota.h`, `src/ps/quota.cpp` | The quota planner, the space emergency, the ballast. |
655
+ | `ps/compaction.h`, `src/ps/compaction.cpp` | Manifest versions 2 and 3 (§40), the compacted rows format, retire items; the compaction pipeline (plan, build, SWAP, apply, abort). |
656
+ | `src/ps/reclaim.cpp` | The per-partition file ledger (`disk_bytes`; a hash table, §40), segment lookups by binary search, the RETIRE and UNLINKED records and the RETIRE valve, reader-gated unlinking, meta-segment retirement (A9), open-time cleanup; the same for the type logs (meta log rotation and retirement, retired catalog runs and manifests, the type ledger). |
657
+ | `ps/quota.h`, `src/ps/quota.cpp` | The quota planner, the space emergency, the ballast; the planner's per-segment summary and the compaction candidate index, kept from a change feed (§40). |
656
658
  | `src/ps/sources-t3.cmake` | Registers the T3 sources and tests on the targets `cpp/CMakeLists.txt` defines. |
657
- | `cpp/test/ps/{compaction,orphan,quota}_test.cpp` | T3 tests. `checkPartitionDir` and `checkTypeDir` (fixtures) walk a partition's or a type's directory against the files its head and manifest name. |
659
+ | `cpp/test/ps/{compaction,orphan,quota,bookkeeping_scale}_test.cpp` | T3 tests (the last: §40). `checkPartitionDir` and `checkTypeDir` (fixtures) walk a partition's or a type's directory against the files its head and manifest name. |
658
660
 
659
661
  ## 24. Compaction (§11, A11, minor 1)
660
662
 
@@ -697,7 +699,9 @@ in the manifest; rows' `seg` is rewritten); after a restart, when the
697
699
  partition's dead share is over the ratio and the per-segment counts are
698
700
  unknown, the oldest
699
701
  segment not yet surveyed (a build that would save under 10% is abandoned and
700
- its count remembered). `Engine::swapSegment` requests one (targeted, or the
702
+ its count remembered). The owner keeps these candidates in an index updated
703
+ from a change feed; a look does not walk the segments (§40).
704
+ `Engine::swapSegment` requests one (targeted, or the
701
705
  oldest-generation compactable segment).
702
706
 
703
707
  Kill bound and readers. A type-level reader's visibility is `V_p =
@@ -737,10 +741,12 @@ appended.
737
741
  133 files in 0.31 s at a 0.3 s grace). BUSY (a handle is open) skips to
738
742
  later items and retries after 50 ms; idle
739
743
  reader lanes close cached handles after 5 s. The next batch carries
740
- `UNLINKED` and the shrunken set; `disk_bytes` drops then. A set past 2,048
741
- items unlinks its oldest regardless of the gate (a statement older than
742
- hours of retirements gets the retryable SNAPSHOT_GONE instead of a writer
743
- waiting on it).
744
+ `UNLINKED` and the shrunken set; `disk_bytes` drops then. The set rides one
745
+ ctl record of one batch, so it is held under a soft cap (2,048 items as the
746
+ record encodes them, less with a large lane table): past it the oldest go at
747
+ once, regardless of the gate and the grace (deviation 37). A statement older
748
+ than them gets the retryable SNAPSHOT_GONE; an open handle still makes the
749
+ unlink BUSY.
744
750
  - **Open** unlinks every file of the persisted set (a file a surviving reader
745
751
  instance still holds stays retired), the outputs of an INTENT_COMPACT
746
752
  without SWAP, and r/a files a first merge created without MERGE_DONE, then
@@ -1841,3 +1847,234 @@ segments per partition, R type catalog runs):
1841
1847
  (arrivals mode), or the tags' matching instances (collected mode, at most
1842
1848
  262,144).
1843
1849
  - Size-prefixed records: nothing (a 4-byte comparison per record).
1850
+
1851
+ # Part VIII: writer bookkeeping at scale
1852
+
1853
+ ## 40. Per-partition bookkeeping at scale (TB audit B4, M3)
1854
+
1855
+ The terabyte audit (stack `docs/architecture/flatsql-ps-terabyte.md`, B4)
1856
+ found a partition's bookkeeping growing as O(S²) in its segment count S. A
1857
+ single dominant partition (the GP archive import, a hot-split partition)
1858
+ reaches S = 1,000 at about 170 GB and S = 6,000 at about 1 TB. With 128 KiB
1859
+ seals, the rate fell from 116,829 rec/s at S = 235 to 465 rec/s at
1860
+ S = 5,188. Commit rounds reached a p99 of 100.7 ms and maintenance steps
1861
+ 100.7 ms. The causes:
1862
+
1863
+ - The ledger (§26) was a vector searched linearly.
1864
+ - `partitionPublishSummary` made three ledger scans per sealed segment on
1865
+ every commit that carried a MERGE_DONE, SEAL, SWAP, RETIRE or UNLINKED.
1866
+ - The compaction look walked every segment every 50 ms, with the same three
1867
+ scans and an L0 scan per segment.
1868
+ - Segment lookups were linear.
1869
+ - Open rebuilt the ledger in O(S²).
1870
+ - A MERGE_DONE and a SWAP recomputed the accelerator bytes by walking every
1871
+ run.
1872
+ - A split partition re-signed its run set on every commit by walking every
1873
+ run, and its helpers matched their previous view run by run in O(runs²).
1874
+
1875
+ What the owner keeps now (`PartitionLedger`, `ps/quota.h`):
1876
+
1877
+ - **The ledger** is an open-addressing table keyed by `(letter, seg, gen)`,
1878
+ with the same matching rules as before: manifests by generation, `d r a m`
1879
+ by segment, the rest by both. The byte total changes with every set and
1880
+ drop. A lookup, change or drop costs O(1).
1881
+ - **A change feed.** A ledger change to a segment's files, a new entry, a
1882
+ kill count, a survey and every input of a SWAP name the segments
1883
+ concerned. The planner's summary and the compaction candidate index update
1884
+ only those segments. The summary does this on the commits that publish
1885
+ it, the index on each look. Open rebuilds both in one walk.
1886
+ - **The compaction candidate index** applies §24's rules unchanged:
1887
+ - segments past the dead ratio, ordered by share (largest first, lowest
1888
+ id among equals);
1889
+ - starts of adjacent small pairs, where the first run of small segments
1890
+ begins at the lowest pair and grows within the coalescing limits;
1891
+ - segments not surveyed since a restart.
1892
+ A look costs O(log S), plus the output's neighbours.
1893
+ - **Segment lookups** binary-search the ascending entry list. Entries are
1894
+ created in order, and a pseq search steps over entries that hold no rows
1895
+ yet. Open walks the unmerged sealed segments alongside the entries.
1896
+ - **Accelerator bytes** change by the runs a MERGE_DONE or SWAP adds and
1897
+ removes.
1898
+ - **The split run set** is signed by the manifest generation, since every
1899
+ change to it commits a new one. A republish for new L0 blocks shares the
1900
+ run list instead of copying it. Helpers find reusable runs by
1901
+ `(seg, gen)`.
1902
+
1903
+ Stage-1 helpers (§32) no longer race the owner or producers (ThreadSanitizer):
1904
+ - A slot's `pos`, `len` and `kind` are relaxed atomics. The owner may take a
1905
+ CLAIMED slot and republish it for the next ordinal while the helper still
1906
+ reads them.
1907
+ - A helper copies an entry with `ringReadShared` (relaxed 8-byte word
1908
+ loads), never through a pointer into the ring. After a steal, the owner may
1909
+ consume and release the entry, and a producer may rewrite its bytes while
1910
+ the helper copies.
1911
+ - `ringWrite` stores relaxed words and edge bytes, which natively cost what
1912
+ plain stores do; the wasm hosts write slabs themselves.
1913
+ - In both cases the helper's CLAIMED to WRITING step fails and its result is
1914
+ dropped, as before. What it parsed is its own copy.
1915
+ - Slabs must be whole 8-byte words.
1916
+
1917
+ Deviations:
1918
+
1919
+ 37. **The RETIRE set is held under what one batch carries** (A12 said: past
1920
+ 2,048 items, the oldest go regardless of the gate once a grace has
1921
+ passed). The set rides one ctl record, whose length is a u16, inside the
1922
+ batch's 64 KiB ctl buffer, alongside UNLINKED (up to 1,024 items) and the
1923
+ lane table. A set that does not fit refuses every batch of the
1924
+ partition. A busy partition retires a manifest and folded runs per
1925
+ merge, and a coalescing SWAP retires a dozen files per input, so within
1926
+ the 60 s grace its set outgrew a batch. Ingest then stopped for the rest
1927
+ of the grace. The audit harness showed 843 rec/s for 50,000 records at a
1928
+ time, about every minute. The valve counts the set as its record
1929
+ encodes it (consecutive manifest generations collapse). The soft cap is
1930
+ 2,048 items, or less when a large lane table leaves less room. Past it,
1931
+ the oldest items are unlinked at once, without waiting for the reader
1932
+ gate or the grace:
1933
+ - A statement older than those items gets the retryable SNAPSHOT_GONE.
1934
+ - A file held open is BUSY and waits as before.
1935
+ - A meta segment still waits for a durable head (A9).
1936
+ - Crash safety never depended on the grace. The batch that retired a
1937
+ file is durable before its retirement counts, and open replays that
1938
+ batch.
1939
+ - While a compaction in flight pins the newest items and the set is past
1940
+ halfway from the soft cap to the hard cap, merges wait. Merges are the
1941
+ steady source of retirements, so the SWAP or abort can land first.
1942
+ 38. **Manifest version 3 (M3)** carries the segment count as a u32 in the
1943
+ header's last word (the u16 then reads 0xFFFF). It is written only when
1944
+ a partition's list outgrows the u16, so an engine that predates it still
1945
+ reads every store it can represent, and refuses a version 3 manifest
1946
+ instead of misreading it. Every version now decodes strictly: the
1947
+ records must end exactly at the CRC trailer. Before, a version 2
1948
+ manifest over 65,535 segments wrapped its count, passed the CRC and lost
1949
+ the rest.
1950
+
1951
+ Measured on the Mac (Darwin 25.3.0, arm64, 28 hardware threads, shared) with
1952
+ the in-memory fault host. One partition, 128 KiB seals, coalescing off (so S
1953
+ is the seal count), 400 B pad records, lockStats on:
1954
+ `bookkeeping_scale_S5k`. The base column is 0df6ef4 (its writer is
1955
+ 14a3075's) with the same test. Each row is the 50,000-record window that ends
1956
+ at that S.
1957
+
1958
+ | | S = 235, base | S = 235 | S = 5,188, base | S = 5,188 |
1959
+ |---|---|---|---|---|
1960
+ | commit round p99 | 0.328 ms | 0.229 ms | 100.7 ms | 0.115 ms |
1961
+ | maintenance step p99 | 0.082 ms | 0.049 ms | 100.7 ms | 0.328 ms |
1962
+ | summary refresh p99 (per commit that publishes it) | | 0.016 ms | | 0.004 ms |
1963
+ | candidate look p99 | | 0.033 ms | | 0.012 ms |
1964
+ | open (half, full: S = 2,594, 5,188) | | | 45.7 ms, 170 ms | 4.0 ms, 4.5 ms |
1965
+ | rec/s | 116,829 | 99,796 | 465 | 5,386 |
1966
+
1967
+ Load was 39–60 for the base run and 22–30 for the new one. The same test on
1968
+ this change over d566293, at load 26–37: commit p99 0.393 → 0.229 ms,
1969
+ maintenance p99 0.066 → 0.786 ms, summary refresh 0.012 → 0.020 ms,
1970
+ candidate look 0.049 → 0.016 ms, open 6.0 ms at S = 5,188, rate 77,556 →
1971
+ 3,363 rec/s.
1972
+
1973
+ Over the same 200,000 records (S = 0 to 940), the process used 5.9–6.9 s of
1974
+ CPU against 9.2–10.3 s on 14a3075 (six alternating runs). Near S = 235 the new
1975
+ build's wall rate was lower than base's by about 7% in alternating runs
1976
+ (82,405 against 94,237 rec/s, load 30–37), while at S ≈ 11 the two were equal.
1977
+ With shorter commit rounds, the writer commits smaller batches (about 940
1978
+ commits per 50,000 records against 700) and waits idle for the test's single
1979
+ producer. The time is lost in the handshake, not in work.
1980
+
1981
+ On ext4, in Docker's Linux VM on the same Mac, the audit's harness
1982
+ (`tb_segments_scaling`: fsync on, one partition, 128 KiB seals, 550,000
1983
+ records) was bound by the VM's shared virtual disk. Other lanes' containers
1984
+ were using it too, so both builds ran near 500 rec/s at S = 235:
1985
+
1986
+ | build | S = 235 | S = 1,886 | S = 2,594 | reopen at S = 2,594 | whole run |
1987
+ |---|---|---|---|---|---|
1988
+ | 14a3075 | 475 rec/s | 211 rec/s | 356 rec/s | 21.0 s | 31.3 min |
1989
+ | this build | 522 rec/s | 795 rec/s | 781 rec/s | 3.6 s | 19.0 min |
1990
+
1991
+ `bookkeeping_scale_S5k` itself on the same ext4 volume (`--dir`) is disk
1992
+ bound: commit rounds wait on fsync (p99 336 ms at S = 235, 470 ms at
1993
+ S = 5,188). The bookkeeping in them stays flat, though: the summary refresh
1994
+ p99 is 0.041 → 0.033 ms and the candidate look p99 0.066 → 0.029 ms, and
1995
+ open at S = 5,188 takes 134 ms. With `--dir` the test reports commit and
1996
+ maintenance p99 without gating them.
1997
+
1998
+ The rate that remains decays with S because of B5: every record's CID dedupe
1999
+ probes the bloom of every L1 run of every segment (`committedPostings`,
2000
+ `partition_log.cpp`). `sample` at S ≈ 4,900 puts 94% of the writer's samples
2001
+ in `cidState` and about 1% in the paths this section changed: maintenance,
2002
+ commit rounds and reclamation together account for 184 of 12,272 samples.
2003
+ Per-partition levelled lookup indexes (B5) are the terabyte design's.
2004
+
2005
+ What still costs O(S), and where it belongs:
2006
+ - A merge snapshots every segment into the manifest it rewrites (M3's delta
2007
+ or two-level manifest). The maintenance p99 above (0.05 → 0.33 ms) is
2008
+ that.
2009
+ - A SWAP writes a whole manifest and rebuilds the entry list.
2010
+ - A split partition's helpers reopen every run file per run set (B2's
2011
+ handle LRU).
2012
+
2013
+ Tests (`cpp/test/ps/bookkeeping_scale_test.cpp`):
2014
+ - `book_manifest_v3_codec`: exact round trips at 0, 1, 65,535, 65,536 and
2015
+ 70,001 segments; a wrapped v2 count, a stray byte, a bad v3 header and
2016
+ version 4 refused; version 1 read.
2017
+ - `book_retire_valve_keeps_ingest_flowing`: 60,000 records at the default
2018
+ 60 s grace. 16,767 files retired, 14,299 unlinked by the valve, the
2019
+ encoded set peaked at 2,113, 0 batches refused. With the valve off, the
2020
+ same test refused 4,397 batches and one 5,000-record chunk took 59.5 s.
2021
+ - The valve test also reports the writer's memory, and none of it grows
2022
+ with records. From 25,000 to 60,000 records:
2023
+ - slab pool: 3.9 → 4.1 MB;
2024
+ - ledger: 2,271 → 2,398 files (67 → 69 KB);
2025
+ - RETIRE set: 2,145 → 2,217 items (160 KB);
2026
+ - segment list: 54 → 68 entries (12 → 15 KB).
2027
+ The process does grow: 1.47 GB at 25,000 records and 3.55 GB at 60,000
2028
+ (3.4 GiB of wasm's 4 GiB). That is the in-memory test host, which keeps
2029
+ every file it ever wrote (7,212 files, 1.45 GB; then 17,281 files,
2030
+ 3.52 GB) so that data calls need no lock. The rest of the heap
2031
+ (13 → 25 MiB) grows by the host's 231 B per file. On wasm the test runs
2032
+ 25,000 records.
2033
+ - `book_equivalence_all_commit_kinds`: runs with `gBookCheck`, which checks
2034
+ every ledger operation against the linear ledger it replaced, and every
2035
+ segment lookup, summary refresh, candidate pick and split run-set
2036
+ signature against a walk. A mismatch aborts. The run covers seals,
2037
+ merges, SWAPs (dead-ratio, coalescing, requested, quota evictions),
2038
+ RETIRE and UNLINKED, meta-segment retirement, a hot split, a clean
2039
+ reopen and a kill -9.
2040
+ - `book_crash_every_op` (every 37th mutating call) and `_full` (every one:
2041
+ 9,416 crash points over seals, merges, SWAPs, retirement, unlinking and
2042
+ A9, all five crash modes, 0 failures): after each reopen, the rebuilt
2043
+ ledger names exactly the files on disk at `disk_bytes`, counters equal a
2044
+ recount, and the engine carries on under `gBookCheck`.
2045
+ - `book_check_fast_suite` (slow): every fast test again with `gBookCheck`
2046
+ (88 of 88 passed).
2047
+ - `bookkeeping_scale_S5k` (slow): the table above. It gates commit and
2048
+ maintenance p99 under 10 ms at S ≥ 5,000, and the summary refresh and
2049
+ candidate look within 2× (+50 µs) of their S = 235 cost. On the base
2050
+ commit it fails both p99 gates.
2051
+
2052
+ Sanitizers, on this build over d566293 (macOS, clang):
2053
+ - ThreadSanitizer reports nothing on eleven tests: the four book tests,
2054
+ `quota_arrival_order_heads_spared_T3_3`,
2055
+ `compaction_cat_supersede_plateau_T3_2`,
2056
+ `orphan_crash_points_during_compaction_T3_4`, and the four fast hot-split
2057
+ tests.
2058
+ - AddressSanitizer with UBSan reports nothing on the book tests,
2059
+ `compaction_removes_dead_rows_keeps_live`,
2060
+ `quota_arrival_order_heads_spared_T3_3`, the orphan harness,
2061
+ `hot_split_equals_unsplit_reference_T3_5` and `crash_faults_T1_1`. It had
2062
+ found one defect in this section, fixed: `std::min` bound a reference to a
2063
+ packed counter.
2064
+ - The stage-1 race, before and after, on Docker linux/arm64 with gcc 13 and
2065
+ on macOS:
2066
+ - 14a3075 reports it in 3 of 5 hot-split runs (`hot_split_throughput_T3_5`,
2067
+ and `hot_split_rebalance_stalled_helpers_A26` at 12 s, twice);
2068
+ - this change reports it in none of 6 runs.
2069
+ - A two-thread program (`ringWrite` against a helper's read of the same
2070
+ bytes) shows it on Linux with a plain reader, and still shows it with
2071
+ atomic loads against a plain `memcpy` write. It is silent only once both
2072
+ sides are atomic.
2073
+ - Under ASan the equivalence test needs more than its default 60 s to settle
2074
+ (it passes with `--eq-settle-s=600`).
2075
+
2076
+ Mutations the checks caught:
2077
+ - a missing kill touch;
2078
+ - a SWAP input not touched;
2079
+ - a ledger update dropped;
2080
+ - the valve off.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flatsql",
3
- "version": "3.5.0",
3
+ "version": "3.5.1",
4
4
  "description": "SQL queries over raw FlatBuffer storage: the SQLite-backed wasm engine and the multithreaded partition store (flatsql-ps-threads.wasm)",
5
5
  "type": "module",
6
6
  "main": "wasm/index.js",
Binary file
@@ -5,9 +5,9 @@
5
5
  "size": 2246620,
6
6
  "files": {
7
7
  "flatsql-ps-threads.wasm": {
8
- "sha256": "787adbcccf52a9e9fe2767d6679b6f2d94a4ead41985bf2a7bb0855f821abc72",
9
- "sri": "sha384-qR37NE4y3sP60hLFJEmxsYz76i2vpTZf9ydfSO0dX1MSD15HKEgEIm6UnkGYS8lo",
10
- "size": 2484927
8
+ "sha256": "3a215da45a53f3a7016257429c392720e728caf850d3a1213dc501bfb5844359",
9
+ "sri": "sha384-HJK5ECR2v2kAKnOHsyvXE+QgSywyuEUa2ipQQMhxR22dg7SZPAYPY985iyy2O7FP",
10
+ "size": 2485719
11
11
  },
12
12
  "flatsql-wasi-noeh.wasm": {
13
13
  "sha256": "8f11fd49ee2e6961b9c1c22dd891d5a6645d0faf481f815408c0f1f5d3a385ab",