lang: New function engine

This mega patch primarily introduces a new function engine. The main
reasons for this new engine are:

1) Massively improved performance with lock-contended graphs.

Certain large function graphs could have very high lock-contention which
turned out to be much slower than I would have liked. This new algorithm
happens to be basically lock-free, so that's another helpful
improvement.

2) Glitch-free function graphs.

The function graphs could "glitch" (an FRP term) which could be
undesirable in theory. In practice this was never really an issue, and
I've not explicitly guaranteed that the new graphs are provably
glitch-free, but in practice things are a lot more consistent.

3) Simpler graph shape.

The new graphs don't require the private channels. This makes
understanding the graphs a lot easier.

4) Branched graphs only run half.

Previously we would run two pure side of an if statement, and while this
was mostly meant as an early experiment, it stayed in for far too long
and now was the right time to remove this. This also means our graphs
are much smaller and more efficient too.

Note that this changed the function API slightly. Everything has been
ported. It's possible that we introduce a new API in the future, but it
is unexpected to cause removal of the two current APIs.

In addition, we finally split out the "schedule" aspect from
world.schedule(). The "pick me" aspects now happen in a separate
resource, rather than as a yucky side-effect in the function. This also
lets us more precisely choose when we're scheduled, and we can observe
without being chosen too.

As usual many thanks to Sam for helping through some of the algorithmic
graph shape issues!
This commit is contained in:
James Shubin
2025-09-09 02:46:59 -04:00
parent 1e2db5b8c5
commit 790b7199ca
109 changed files with 3632 additions and 6904 deletions

View File

@@ -1,20 +0,0 @@
# run this example with these commands
# watch -n 0.1 'tail *' # run this in /tmp/mgmt/
# time ./mgmt run --hostname h1 --tmp-prefix --no-pgp empty
# time ./mgmt run --hostname h2 --seeds=http://127.0.0.1:2379 --client-urls=http://127.0.0.1:2381 --server-urls=http://127.0.0.1:2382 --tmp-prefix --no-pgp empty
# time ./mgmt run --hostname h3 --seeds=http://127.0.0.1:2379 --client-urls=http://127.0.0.1:2383 --server-urls=http://127.0.0.1:2384 --tmp-prefix --no-pgp empty
# time ./mgmt run --hostname h4 --seeds=http://127.0.0.1:2379 --client-urls=http://127.0.0.1:2385 --server-urls=http://127.0.0.1:2386 --tmp-prefix --no-pgp empty
# time ./mgmt deploy --no-git --seeds=http://127.0.0.1:2379 lang examples/lang/exchange0.mcl
import "golang"
import "sys"
import "world"
$rand = random1(8)
$exchanged = world.exchange("keyns", $rand)
$host = sys.hostname()
file "/tmp/mgmt/exchange-${host}" {
state => $const.res.file.state.exists,
content => golang.template("Found: {{ . }}\n", $exchanged),
}

View File

@@ -3,9 +3,9 @@ import "golang"
$dt = datetime.now()
$hystvalues = {"ix0" => $dt, "ix1" => history($dt, 1), "ix2" => history($dt, 2), "ix3" => history($dt, 3),}
$hystvalues = {"now" => $dt, "ix0" => history($dt, 0), "ix1" => history($dt, 1000), "ix2" => history($dt, 2000), "ix3" => history($dt, 3000),}
file "/tmp/mgmt/history" {
state => $const.res.file.state.exists,
content => golang.template("Index(0) {{.ix0}}: {{ datetime_print .ix0 }}\nIndex(1) {{.ix1}}: {{ datetime_print .ix1 }}\nIndex(2) {{.ix2}}: {{ datetime_print .ix2 }}\nIndex(3) {{.ix3}}: {{ datetime_print .ix3 }}\n", $hystvalues),
content => golang.template("Index($) {{.ix0}}: {{ datetime_print .now }}\nIndex(0) {{.ix0}}: {{ datetime_print .ix0 }}\nIndex(1) {{.ix1}}: {{ datetime_print .ix1 }}\nIndex(2) {{.ix2}}: {{ datetime_print .ix2 }}\nIndex(3) {{.ix3}}: {{ datetime_print .ix3 }}\n", $hystvalues),
}

View File

@@ -13,8 +13,8 @@ $threshold = 1.5 # change me if you like
# simple hysteresis implementation
$h1 = $theload > $threshold
$h2 = history($theload, 1) > $threshold
$h3 = history($theload, 2) > $threshold
$h2 = history($theload, 1000) > $threshold
$h3 = history($theload, 2000) > $threshold
$unload = $h1 or $h2 or $h3
virt "mgmt1" {

View File

@@ -1,14 +1,22 @@
import "golang"
import "iter"
#import "fmt"
#import "datetime"
#$str_now = fmt.printf("%d", datetime.now())
$fn = func($x) { # notable because concrete type is fn(t1) t2, where t1 != t2
len($x)
#len($x)
#$x + $str_now
#$x + fmt.printf("%d:%d", len($x), datetime.now())
$x
}
$in1 = ["a", "bb", "ccc", "dddd", "eeeee",]
$out1 = iter.map($in1, $fn)
$t1 = golang.template("out1: {{ . }}", $out1)
#$t1 = golang.template("out1: {{ . }}", $out1)
test [$t1,] {}
print $out1 {}
#print [$t1,] {}

View File

@@ -1,24 +1,32 @@
# test with:
# time ./mgmt run --hostname h1 --tmp-prefix --no-pgp lang examples/lang/schedule0.mcl
# time ./mgmt run --hostname h2 --seeds=http://127.0.0.1:2379 --no-server --no-magic --tmp-prefix --no-pgp lang examples/lang/schedule0.mcl
# time ./mgmt run --hostname h3 --seeds=http://127.0.0.1:2379 --no-server --no-magic --tmp-prefix --no-pgp lang examples/lang/schedule0.mcl
# kill h2 (should see h1 and h3 pick [h1, h3] instead)
# restart h2 (should see [h1, h3] as before)
# kill h3 (should see h1 and h2 pick [h1, h2] instead)
# restart h3 (should see [h1, h2] as before)
# kill h3
# kill h2
# kill h1... all done!
import "golang"
import "sys"
import "world"
# here are all the possible options:
#$opts = struct{strategy => "rr", max => 3, reuse => false, ttl => 10,}
$ns = "xsched" # some unique string
# although an empty struct is valid too:
#$opts = struct{}
# This node wishes to be included in the scheduled set. These are the options...
schedule "${ns}" {
strategy => "rr",
max => 2,
#reuse => false,
ttl => 10,
}
# we'll just use a smaller subset today:
$opts = struct{strategy => "rr", max => 2, ttl => 10,}
# See the scheduled selection for a particular namespace:
$set = world.schedule($ns)
# schedule in a particular namespace with options:
$set = world.schedule("xsched", $opts)
# and if you want, you can omit the options entirely:
#$set = world.schedule("xsched")
$host = sys.hostname()
file "/tmp/mgmt/scheduled-${host}" {
file "/tmp/mgmt/scheduled-${hostname}" {
state => $const.res.file.state.exists,
content => golang.template("set: {{ . }}\n", $set),
}

11
examples/lang/system1.mcl Normal file
View File

@@ -0,0 +1,11 @@
import "fmt"
import "os"
$i = os.system(fmt.printf("for x in `seq 3`; do echo \"hello \$x\"; sleep 1s; done; echo done; echo double done"))
# The msg field is updated several times in quick succession, the resource is
# only guaranteed to be ran for the last value. Thus, it is likely that the
# single "done" values will not be printed.
print "out" {
msg => $i,
}