code wiki / (root) / nx_seg_compact_cli.nx

nx_seg_compact_cli.nx source

↩ module page · 65 lines · 3821 B

1// nx_seg_compact_cli.nx -- run seg-store COMPACTION for one shard prefix. Folds all live segments into ONE 2// merged segment (latest entry per key; tombstones kept), archives the retired segment names (additive -- 3// never deletes), atomically swaps the live manifest. This is the PERF fix for the data-driven-search debt: 4// after fixing the 256-segment read cap, search loads ALL live segments per query -- compaction folds a 5// large uncompacted shard (e.g. 936 segs) into ~1 so per-query load stays cheap. Uses the FIXED (data-driven, 6// no 256/260 cap) ss_compact. Runs in nishihost CWD (the dp-<domain>-pub- store resolves there). 7// usage: nx_seg_compact_cli <store-prefix> <new-segid> 8// e.g. nx_seg_compact_cli knowledge/store/dp-nishifamily.com-pub- 9000000000000000001 9// license_tier: ORIGINAL 10import "nx_syscalls.nx" 11import "nx_seg_store.nx" 12 13func scc_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 14func scc_putn(v: i64) -> i64 { 15 let t: *u8 = sys_mmap(32); var m: i64 = v; var k: i64 = 0 16 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 17 if m == 0 { t[0] = 48 as u8; k = 1 } 18 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 19 let b: *u8 = sys_mmap(32); var i: i64 = 0 20 while i < k { b[i] = t[k - 1 - i]; i = i + 1 } 21 sys_write(1, b, k); return 0 22} 23func scc_atoi(s: *u8) -> i64 { 24 var v: i64 = 0; var i: i64 = 0; var go: i64 = 1 25 while go == 1 { let c: i64 = s[i] as i64; if c < 48 { go = 0 } else { if c > 57 { go = 0 } else { v = v * 10 + (c - 48); i = i + 1 } } } 26 return v 27} 28 29func main(argc: i64, argv: *i64) -> i64 { 30 if argc < 3 { scc_puts("usage: nx_seg_compact_cli <store-prefix> <new-segid>\n" as *u8); return 2 } 31 let prefix: *u8 = argv[1] as *u8 32 let segid: i64 = scc_atoi(argv[2] as *u8) 33 // serialize against plane writers via the advisory <prefix>plock -- the SAME lock nx_debt/ 34 // nx_store_put/nx_frontier_put/nx_store_compact take. PROVEN NEEDED 2026-07-20: the 600s 35 // nx_segguard beat ran this CLI LOCKLESS and its atomic manifest swap clobbered a concurrent 36 // writer's manifest append (one committed segment vanished from the live view; .docs stayed 37 // on disk). segguard's before-list-vs-archive repair structurally cannot see an append that 38 // lands AFTER its manifest read, so mutual exclusion here is the only correct fix. 39 // Fail-closed: no lock -> no compaction (a skipped sweep is safe; a raced swap is not). 40 let CLI_LOCK_EX: i64 = 2 41 let CLI_LOCK_MODE: i64 = 0x1a4 42 let lkp: *u8 = sys_mmap(512) 43 var lo: i64 = 0 44 lo = ss_cat(lkp, lo, prefix) 45 lo = ss_cat(lkp, lo, "plock" as *u8) 46 lkp[lo] = 0 as u8 47 let lfd: i64 = sys_openat_append(lkp, CLI_LOCK_MODE) 48 if lfd < 0 { scc_puts("REFUSED: cannot open plane lock\n" as *u8); return 3 } 49 if sys_flock(lfd, CLI_LOCK_EX) != 0 { scc_puts("REFUSED: cannot take plane lock\n" as *u8); return 3 } 50 // lock held for the whole read->merge->swap; released on process exit 51 // count live segments BEFORE 52 let sp: *i64 = sys_mmap(8) as *i64 53 let before: i64 = ss_manifest_dyn(prefix, sp) 54 scc_puts("COMPACT prefix=" as *u8); scc_puts(prefix) 55 scc_puts(" live-segs-before=" as *u8); scc_putn(before) 56 scc_puts(" new-segid=" as *u8); scc_putn(segid); scc_puts("\n" as *u8) 57 let r: i64 = ss_compact(prefix, segid) 58 // count live segments AFTER 59 let sp2: *i64 = sys_mmap(8) as *i64 60 let after: i64 = ss_manifest_dyn(prefix, sp2) 61 scc_puts("RESULT ss_compact=" as *u8); scc_putn(r) 62 scc_puts(" live-segs-after=" as *u8); scc_putn(after) 63 if r > 0 { scc_puts(" OK (retired segs archived to manifest-archive.txt, NOT deleted)\n" as *u8) } else { scc_puts(" FAIL (manifest unchanged -- fail-closed)\n" as *u8) } 64 return 0 65}