interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / nodes / snap.om

626 lines · 11572 bytes Raw Blame History
1om2
2note nodes/snap.om: the Radicle node snapshot writer behind the forge /status page and the factory floor.
3note nodes-snap write the snapshot once (tests, by hand)
4note nodes-snap follow aien-nodes.service: write once, then follow `rad node events` (the node's own
5note change stream) and write again after events, at most once per 30 s; no
6note clock: with no event nothing runs. Exits 6 when the stream ends (node
7note restarted); the service restarts it.
8note It reads the node and never writes to it:
9note sqlite3 -readonly node.db repo-sync-status: which head of OUR sigrefs each node has synced
10note sqlite3 -readonly policies.db seeding scope
11note rad node status connected peers
12note sqlite3, env and rad stay host programs (argv exec, no shell, empty environment; rad gets HOME
13note only); reading their output, the checks and the counting are here. Output, written to a temp file
14note and renamed over the old one, one item per line:
15note 2 / unix seconds / our node id / connected peers / then per repository "RID scope copies behind reach wait"
16note scope 2 all, 1 followed, 0 not seeded here
17note copies other nodes that have a record of our signed refs for that repository
18note behind of those, how many are not at our current head
19note reach seconds our current head took to reach the slowest copy that has it
20note wait seconds our current head has been out while some copy still lacks it (0 when none behind)
21note A repository with no record of our own head counts copies only (behind, reach and wait 0).
22note An input line that is not "letters/digits RID|0-1|0-1|digits|digits" is skipped. Every repository
23note the node knows is listed; readers show only their own public allow-list. The file is mode 0600.
24note Exit (once): 0 written; 2 an argument other than "follow"; 3 policies query failed; 4 sync query
25note failed; 5 write failed.
26text sqlb "/usr/bin/sqlite3"
27text sqlro "-readonly"
28text ndb "/home/drakestapleton/.radicle/node/node.db"
29text pdb "/home/drakestapleton/.radicle/node/policies.db"
30text q1 "SELECT substr(o.repo,5), o.node = 'z6MkfZDue1TbaNFi3ZzcD9uiTeduPLC52ucdFVuLKZwexxhK', o.head = coalesce(m.head,''), o.timestamp, coalesce(m.timestamp,0) FROM \"repo-sync-status\" o LEFT JOIN \"repo-sync-status\" m ON m.repo = o.repo AND m.node = 'z6MkfZDue1TbaNFi3ZzcD9uiTeduPLC52ucdFVuLKZwexxhK' WHERE o.repo LIKE 'rad:%' ORDER BY o.repo"
31text q2 "SELECT substr(id,5), scope FROM seeding WHERE id LIKE 'rad:%' AND policy = 'allow' ORDER BY id"
32text envb "/usr/bin/env"
33text envh "HOME=/home/drakestapleton"
34text radb "/home/drakestapleton/.local/bin/rad"
35text rnode "node"
36text rstat "status"
37text revts "events"
38text devnul "/dev/null"
39text nid "z6MkfZDue1TbaNFi3ZzcD9uiTeduPLC52ucdFVuLKZwexxhK"
40text outp "/home/drakestapleton/.local/state/aien-factory/nodes.txt"
41text tmpp "/home/drakestapleton/.local/state/aien-factory/.nodes.txt.new"
42global pk uchar 4096
43global pkl ulong 1
44global pkr ulong 1
45global cap uchar 1048576
46global capl ulong 1
47global pol uchar 65536
48global poln ulong 1
49global ob uchar 131072
50global obl ulong 1
51global pfd uchar 8
52global wst uchar 4
53global erfd long 1
54global junk uchar 65536
55global nowms ulong 1
56global l ulong 8
57global g ulong 8
58note ---- packed argument list pk (u16 count, then u16 length + bytes per record), as server/util.om fpk
59fn kb to void
60put pk 0 0
61put pk 1 0
62put pkl 0 2
63end
64fn kc c uchar to void
65n = get pkl 0
66if lt n 4095
67put pk n c
68put pkl 0 add n 1
69r = get pkr 0
70k = add or ulong get pk r shl ulong get pk add r 1 8 1
71put pk r uchar and k 255
72put pk add r 1 uchar shr k 8
73end
74end
75fn ka t view uchar * to void
76n = get pkl 0
77if ge n 4094
78ret
79end
80put pkr 0 n
81put pk n 0
82put pk add n 1 0
83put pkl 0 add n 2
84c = add or ulong get pk 0 shl ulong get pk 1 8 1
85put pk 0 uchar and c 255
86put pk 1 uchar shr c 8
87for i ulong len t bound 4096
88kc get t i
89end
90end
91note ---- run pk: stdout into cap (at most 1 MiB, 20 s), stdin and stderr /dev/null; exit status or < 0
92fn pfdv i ulong to int
93b = mul i 4
94v = or ulong get pfd b shl ulong get pfd add b 1 8
95v = or v shl ulong get pfd add b 2 16
96v = or v shl ulong get pfd add b 3 24
97ret int v
98end
99fn child wfd int to void
100nul = fopen devnul 0 len devnul 2 0
101if lt nul 0
102exit 126
103end
104x = dupfd int nul 0 0
105x = dupfd wfd 1 0
106x = dupfd int nul 2 0
107x = execprog pk 0 get pkl 0
108exit 127
109end
110fn drain fd int dl long to bool
111for it ulong 100000000
112left = sub dl long monons
113if le left 0
114ret false
115end
116w = waitio fd 1 left
117if lt w 0
118if ne w -4
119ret false
120end
121continue
122end
123if eq w 0
124continue
125end
126n = get capl 0
127if ge n len cap
128ret false
129end
130r = read fd cap n sub len cap n
131if eq r 0
132ret true
133end
134if lt r 0
135if and ne r -4 ne r -11
136ret false
137end
138continue
139end
140put capl 0 add n ulong r
141end
142ret false
143end
144note spawn: start pk with its stdout on a new pipe; the read end goes to erfd; the pid or < 0
145fn spawn to long
146r = pipefds pfd 0 8 524288
147if lt r 0
148ret -2
149end
150rfd = pfdv 0
151wfd = pfdv 1
152pid = fork
153if lt pid 0
154x = fclose rfd
155x = fclose wfd
156ret -2
157end
158if eq pid 0
159child wfd
160exit 127
161end
162x = fclose wfd
163put erfd 0 long rfd
164ret pid
165end
166fn run to long
167put capl 0 0
168pid = spawn
169if lt pid 0
170ret -2
171end
172rfd = int get erfd 0
173ok = drain rfd add long monons 20000000000
174x = fclose rfd
175if not ok
176x = killpid int pid 9
177end
178w = waitpid int pid wst 0 4 0
179if or not ok ne w pid
180ret -1
181end
182s = or ulong get wst 0 shl ulong get wst 1 8
183if ne and s 127 0
184ret -3
185end
186ret long and shr s 8 255
187end
188note ---- output buffer ob
189note obov: 1 once a byte did not fit in ob; wout then refuses, so a cut-off last line is never written
190global obov ulong 1
191fn oc c uchar to void
192n = get obl 0
193if lt n len ob
194put ob n c
195put obl 0 add n 1
196else
197put obov 0 1
198end
199end
200fn ot t view uchar * to void
201for i ulong len t bound 4096
202oc get t i
203end
204end
205fn onum v ulong to void
206d = new uchar 20 0
207n = ulong 0
208x = v
209for k ulong 20
210put d n uchar add rem x 10 48
211n = add n 1
212x = div x 10
213if eq x 0
214break
215end
216end
217for k ulong n bound 20
218oc get d sub sub n k 1
219end
220end
221fn alnum c uchar to bool
222if and ge c 48 le c 57
223ret true
224end
225if and ge c 97 le c 122
226ret true
227end
228if and ge c 65 le c 90
229ret true
230end
231ret false
232end
233note ---- peers: rows of the rad node status table that start "│ z" and carry the connected mark ✓
234fn by3 p ulong a uchar b uchar c uchar to bool
235if ne get cap p a
236ret false
237end
238if ne get cap add p 1 b
239ret false
240end
241ret eq get cap add p 2 c
242end
243fn peerln p ulong e ulong to bool
244if or lt sub e p 6 gt sub e p 4000
245ret false
246end
247if not by3 p 226 148 130
248ret false
249end
250if not by3 add p 2 130 32 122
251ret false
252end
253q = add p 5
254while lt add q 2 e bound 4096
255if by3 q 226 156 147
256ret true
257end
258q = add q 1
259end
260ret false
261end
262fn lnend p ulong n ulong to ulong
263e = p
264while lt e n bound 1048576
265if eq get cap e 10
266ret e
267end
268e = add e 1
269end
270ret n
271end
272fn peers to ulong
273c = ulong 0
274p = ulong 0
275n = get capl 0
276while lt p n bound 1048576
277e = lnend p n
278if peerln p e
279c = add c 1
280end
281p = add e 1
282end
283ret c
284end
285note ---- one sync line at cap[p, e) into l: 0 RID start, 1 RID length, 2 is-us, 3 at-our-head,
286note 4 its time (ms), 5 our time (ms); false when the line is not exactly that form
287fn pnum p ulong e ulong k ulong to ulong
288v = ulong 0
289q = p
290while lt q e bound 21
291if gt sub q p 19
292ret 0
293end
294c = get cap q
295if eq c 124
296break
297end
298if or lt c 48 gt c 57
299ret 0
300end
301v = add mul v 10 sub ulong c 48
302q = add q 1
303end
304if or eq q p gt sub q p 19
305ret 0
306end
307put l k v
308ret q
309end
310fn pline p ulong e ulong to bool
311q = p
312while lt q e bound 70
313if gt sub q p 64
314ret false
315end
316c = get cap q
317if eq c 124
318break
319end
320if not alnum c
321ret false
322end
323q = add q 1
324end
325n = sub q p
326if or eq n 0 or gt n 64 ge q e
327ret false
328end
329put l 0 p
330put l 1 n
331for f ulong 4
332r = pnum add q 1 e add f 2
333if eq r 0
334ret false
335end
336if and lt f 3 ge r e
337ret false
338end
339if and eq f 3 ne r e
340ret false
341end
342q = r
343end
344if or gt get l 2 1 gt get l 3 1
345ret false
346end
347ret true
348end
349note ---- the repository being counted, in g: 0 RID start, 1 RID length (0 = none yet), 2 copies,
350note 3 behind, 4 reach (s), 5 our time (ms, 0 = no record of our head)
351fn samerid to bool
352if ne get g 1 get l 1
353ret false
354end
355a = get g 0
356b = get l 0
357for i ulong get g 1 bound 64
358if ne get cap add a i get cap add b i
359ret false
360end
361end
362ret true
363end
364note scope: the seeding line "RID|all" or "RID|followed" for the repository in g, from pol
365fn scope to ulong
366a = get g 0
367n = get g 1
368p = ulong 0
369m = get poln 0
370while lt p m bound 65536
371if le add add p n 1 m
372same = true
373for i ulong n bound 64
374if ne get pol add p i get cap add a i
375same = false
376end
377end
378if and same eq get pol add p n 124
379c = get pol add add p n 1
380if eq c 97
381ret 2
382end
383if eq c 102
384ret 1
385end
386ret 0
387end
388end
389while lt p m bound 65536
390if eq get pol p 10
391break
392end
393p = add p 1
394end
395p = add p 1
396end
397ret 0
398end
399fn emit to void
400n = get g 1
401if eq n 0
402ret
403end
404a = get g 0
405for i ulong n bound 64
406oc get cap add a i
407end
408oc 32
409onum scope
410oc 32
411onum get g 2
412oc 32
413onum get g 3
414oc 32
415onum get g 4
416oc 32
417w = ulong 0
418o = get g 5
419nw = get nowms 0
420if gt get g 3 0
421if and gt o 0 gt nw o
422w = div sub nw o 1000
423end
424end
425onum w
426oc 10
427end
428fn gnew to void
429put g 0 get l 0
430put g 1 get l 1
431put g 2 0
432put g 3 0
433put g 4 0
434put g 5 get l 5
435end
436fn acc to void
437if eq get l 2 1
438ret
439end
440put g 2 add get g 2 1
441o = get g 5
442if eq o 0
443ret
444end
445if eq get l 3 0
446put g 3 add get g 3 1
447ret
448end
449ts = get l 4
450if gt ts o
451d = div sub ts o 1000
452if gt d get g 4
453put g 4 d
454end
455end
456end
457fn rows to void
458put g 1 0
459p = ulong 0
460n = get capl 0
461while lt p n bound 1048576
462e = lnend p n
463if pline p e
464if not samerid
465emit
466gnew
467end
468acc
469end
470p = add e 1
471end
472emit
473end
474fn wout to bool
475if ne get obov 0 0
476ret false
477end
478fd = fopen tmpp 0 len tmpp 524865 384
479if lt fd 0
480ret false
481end
482p = ulong 0
483n = get obl 0
484while lt p n bound 1000000
485w = write int fd ob p sub n p
486if le w 0
487x = fclose int fd
488ret false
489end
490p = add p ulong w
491end
492x = fdsync int fd
493x = fclose int fd
494kb
495ka tmpp
496ka outp
497ret eq renames pk 0 get pkl 0 0 0
498end
499fn once to int
500put nowms 0 ulong div realns 1000000
501kb
502ka sqlb
503ka sqlro
504ka pdb
505ka q2
506if ne run 0
507ret 3
508end
509n = get capl 0
510if gt n len pol
511ret 3
512end
513for i ulong n bound 65536
514put pol i get cap i
515end
516put poln 0 n
517kb
518ka envb
519ka envh
520ka radb
521ka rnode
522ka rstat
523pr = ulong 0
524if eq run 0
525pr = peers
526end
527kb
528ka sqlb
529ka sqlro
530ka ndb
531ka q1
532if ne run 0
533ret 4
534end
535put obl 0 0
536put obov 0 0
537oc 50
538oc 10
539onum div get nowms 0 1000
540oc 10
541ot nid
542oc 10
543onum pr
544oc 10
545rows
546if not wout
547ret 5
548end
549ret 0
550end
551note follow: the node's event stream is the change signal. Any event marks the snapshot dirty; a
552note dirty snapshot is written once 30 s have passed since the last write. Idle, it only waits.
553fn follow to int
554st = once
555last = long monons
556kb
557ka envb
558ka envh
559ka radb
560ka rnode
561ka revts
562pid = spawn
563if lt pid 0
564ret 6
565end
566rfd = int get erfd 0
567dirty = false
568for it ulong 4000000000
569tmo = long 3600000000000
570if dirty
571tmo = sub add last 30000000000 long monons
572if le tmo 0
573st = once
574last = long monons
575dirty = false
576continue
577end
578end
579w = waitio rfd 1 tmo
580if lt w 0
581if ne w -4
582break
583end
584continue
585end
586if gt w 0
587r = read rfd junk 0 len junk
588if eq r 0
589break
590end
591if lt r 0
592if and ne r -4 ne r -11
593break
594end
595continue
596end
597dirty = true
598end
599end
600x = fclose rfd
601x = killpid int pid 9
602w = waitpid int pid wst 0 4 0
603ret 6
604end
605note main: no argument writes once; exactly "follow" follows; anything else is exit 2 with nothing written
606text tfollow "follow"
607global argb uchar 16
608fn main to int
609n = argcnt
610if eq n 1
611ret once
612end
613if ne n 2
614ret 2
615end
616r = argread 1 argb 0 16
617if ne r long len tfollow
618ret 2
619end
620for i ulong len tfollow
621if ne get argb i get tfollow i
622ret 2
623end
624end
625ret follow
626end