code wiki / _hdl_build / nx_shard_view.nx

nx_shard_view.nx source

↩ module page · 70 lines · 3616 B

1// nx_shard_view.nx -- the cross-shard FLEET VIEW builder (CAP-SHARD-VIEW). Serializes the shard registry 2// (nx_shard_registry / shards.conf) to JSON for the control plane's /api/shards -- the "dashboard of all accounts" 3// (AWS Control Tower / k8s fleet console) as a deterministic sovereign endpoint. Kept a standalone module (not inlined 4// in nx_mgmt_api) so it composes cleanly AND is unit-gateable without standing up the daemon. The mgmt API wraps 5// sv_build_shards_json() in an HTTP 200; a gate calls it directly. license_tier: ORIGINAL 6import "nx_shard_registry.nx" 7const K_MAGIC_262144: i64 = 262144 8 9func sv_cat(buf: *u8, off: i64, s: *u8) -> i64 { var i: i64 = 0; while s[i] != (0 as u8) { buf[off + i] = s[i]; i = i + 1 } return off + i } 10func sv_catn(buf: *u8, off: i64, v: i64) -> i64 { 11 var t: i64 = v; if t < 0 { buf[off] = 45 as u8; off = off + 1; t = 0 - t } 12 let tm: *u8 = sys_mmap(24); var k: i64 = 0; if t == 0 { tm[0]=48 as u8; k=1 } 13 while t > 0 { tm[k] = (48 + (t%10)) as u8; t = t/10; k = k+1 } 14 var j: i64 = 0; while j < k { buf[off + j] = tm[k-1-j]; j = j+1 } 15 return off + k 16} 17// leak-free bounded file read into a reused buffer; returns bytes (>=0) or -1. 18func sv_read_file(path: *u8, out: *u8, cap: i64) -> i64 { 19 let fd: i64 = sys_openat_rd(path) 20 if fd < 0 { return 0 - 1 } 21 var total: i64 = 0; var go: i64 = 1 22 while go == 1 { 23 let tail: *u8 = (out as i64 + total) as *u8 24 let nr: i64 = sys_read(fd, tail, cap - total) 25 if nr <= 0 { go = 0 } 26 if nr > 0 { total = total + nr } 27 if total >= cap { go = 0 } 28 } 29 sys_close(fd) 30 return total 31} 32// append a JSON string field "key":"<field f of row [ls,le)>" (no embedded quotes in registry fields). 33func sv_kv(buf: *u8, off: i64, reg: *u8, ls: i64, le: i64, f: i64, key: *u8) -> i64 { 34 let tmp: *u8 = sys_mmap(256) 35 var o: i64 = sv_cat(buf, off, "\"" as *u8) 36 o = sv_cat(buf, o, key); o = sv_cat(buf, o, "\":\"" as *u8) 37 sr_field_at(reg, ls, le, f, tmp, 256); o = sv_cat(buf, o, tmp) 38 o = sv_cat(buf, o, "\"" as *u8) 39 return o 40} 41 42// build {"count":N,"shards":[{id,domain,service,port,realm,health},...]} from the registry file. returns length. 43func sv_build_shards_json(conf_path: *u8, out: *u8, cap: i64) -> i64 { 44 let reg: *u8 = sys_mmap(K_MAGIC_262144) 45 var rn: i64 = sv_read_file(conf_path, reg, K_MAGIC_262144) 46 if rn < 0 { rn = 0 } 47 var b: i64 = sv_cat(out, 0, "{\"count\":" as *u8) 48 b = sv_catn(out, b, sr_count(reg, rn)) 49 b = sv_cat(out, b, ",\"shards\":[" as *u8) 50 var first: i64 = 1 51 var ls: i64 = 0 52 while ls < rn { 53 let le: i64 = slk_line_end(reg, rn, ls) 54 if le > ls { if reg[ls] != (35 as u8) { 55 if first == 0 { b = sv_cat(out, b, "," as *u8) } 56 first = 0 57 b = sv_cat(out, b, "{" as *u8) 58 b = sv_kv(out, b, reg, ls, le, SR_F_ID, "id" as *u8); b = sv_cat(out, b, "," as *u8) 59 b = sv_kv(out, b, reg, ls, le, SR_F_DOMAIN, "domain" as *u8); b = sv_cat(out, b, "," as *u8) 60 b = sv_kv(out, b, reg, ls, le, SR_F_SERVICE, "service" as *u8); b = sv_cat(out, b, "," as *u8) 61 b = sv_cat(out, b, "\"port\":" as *u8); b = sv_catn(out, b, sr_field_int(reg, ls, le, SR_F_PORT)); b = sv_cat(out, b, "," as *u8) 62 b = sv_kv(out, b, reg, ls, le, SR_F_REALM, "realm" as *u8); b = sv_cat(out, b, "," as *u8) 63 b = sv_kv(out, b, reg, ls, le, SR_F_HEALTH, "health" as *u8) 64 b = sv_cat(out, b, "}" as *u8) 65 } } 66 ls = le + 1 67 } 68 b = sv_cat(out, b, "]}" as *u8) 69 return b 70}