From ce3238e62d1e69d1b42ce44cd8e9516a13373102 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Fri, 8 Mar 2024 18:52:48 +0100 Subject: [PATCH] Begin implementation of WASM - host API --- Cargo.lock | 877 ++++++++++++++++++++++++-------------- Cargo.toml | 15 +- bindings/Cargo.toml | 9 + bindings/src/lib.rs | 116 +++++ bindings/src/main.rs | 3 + coordinator/src/main.rs | 21 +- rust-example/Cargo.lock | 192 +++++++++ rust-example/Cargo.toml | 12 + rust-example/server.js | 1 + rust-example/src/lib.rs | 11 + utils/Cargo.toml | 2 + utils/src/lib.rs | 20 + utils/src/services/mod.rs | 21 +- wasm/Cargo.toml | 5 + wasm/src/lib.rs | 260 +++++++++-- wasm/src/main.rs | 148 +++++++ worker/Cargo.toml | 2 + worker/src/main.rs | 192 ++++++--- 18 files changed, 1467 insertions(+), 440 deletions(-) create mode 100644 bindings/Cargo.toml create mode 100644 bindings/src/lib.rs create mode 100644 bindings/src/main.rs create mode 100644 rust-example/Cargo.lock create mode 100644 rust-example/Cargo.toml create mode 100644 rust-example/server.js create mode 100644 rust-example/src/lib.rs create mode 100644 wasm/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index de8be9d..8cc6736 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -45,6 +45,15 @@ version = "0.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e9d4ee0d472d1cd2e28c97dfa124b3d8d992e10eb0a035f33f5d12e3a177ba3b" +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.5.0" @@ -80,7 +89,7 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" dependencies = [ - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -90,7 +99,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "58f54d10c6dfa51283a066ceab3ec1ab78d13fae00aa49243a45e4571fb79dfd" dependencies = [ "anstyle", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -163,9 +172,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.4.0" +version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" +checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" [[package]] name = "block-buffer" @@ -176,6 +185,30 @@ dependencies = [ "generic-array", ] +[[package]] +name = "borsh" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f58b559fd6448c6e2fd0adb5720cd98a2506594cafa4737ff98c396f3e82f667" +dependencies = [ + "borsh-derive", + "cfg_aliases", +] + +[[package]] +name = "borsh-derive" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7aadb5b6ccbd078890f6d7003694e33816e6b784358f18e15e7e6d9f065a57cd" +dependencies = [ + "once_cell", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.37", + "syn_derive", +] + [[package]] name = "bumpalo" version = "3.14.0" @@ -196,21 +229,21 @@ checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" [[package]] name = "cap-fs-ext" -version = "2.0.0" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b779b2d0a001c125b4584ad586268fb4b92d957bff8d26d7fe0dd78283faa814" +checksum = "769f8cd02eb04d57f14e2e371ebb533f96817f9b2525d73a5c72b61ca7973747" dependencies = [ "cap-primitives", "cap-std", "io-lifetimes", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] name = "cap-net-ext" -version = "2.0.0" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ffc30dee200c20b4dcb80572226f42658e1d9c4b668656d7cc59c33d50e396e" +checksum = "59ff6d3fb274292a9af283417e383afe6ded1fe66f6472d2c781216d3d80c218" dependencies = [ "cap-primitives", "cap-std", @@ -220,9 +253,9 @@ dependencies = [ [[package]] name = "cap-primitives" -version = "2.0.0" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bf30c373a3bee22c292b1b6a7a26736a38376840f1af3d2d806455edf8c3899" +checksum = "90a0b44fc796b1a84535a63753d50ba3972c4db55c7255c186f79140e63d56d0" dependencies = [ "ambient-authority", "fs-set-times", @@ -231,15 +264,15 @@ dependencies = [ "ipnet", "maybe-owned", "rustix", - "windows-sys", + "windows-sys 0.52.0", "winx", ] [[package]] name = "cap-rand" -version = "2.0.0" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "577de6cff7c2a47d6b13efe5dd28bf116bd7f8f7db164ea95b7cc2640711f522" +checksum = "4327f08daac33a99bb03c54ae18c8f32c3ba31c728a33ddf683c6c6a5043de68" dependencies = [ "ambient-authority", "rand", @@ -247,9 +280,9 @@ dependencies = [ [[package]] name = "cap-std" -version = "2.0.0" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84bade423fa6403efeebeafe568fdb230e8c590a275fba2ba978dd112efcf6e9" +checksum = "266626ce180cf9709f317d0bf9754e3a5006359d87f4bf792f06c9c5f1b63c0f" dependencies = [ "cap-primitives", "io-extras", @@ -259,11 +292,13 @@ dependencies = [ [[package]] name = "cap-time-ext" -version = "2.0.0" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8f52b3c8f4abfe3252fd0a071f3004aaa3b18936ec97bdbd8763ce03aff6247" +checksum = "e1353421ba83c19da60726e35db0a89abef984b3be183ff6f58c5b8084fcd0c5" dependencies = [ + "ambient-authority", "cap-primitives", + "iana-time-zone", "once_cell", "rustix", "winx", @@ -285,6 +320,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cfg_aliases" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" + [[package]] name = "clap" version = "4.4.5" @@ -340,11 +381,17 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "core-foundation-sys" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" + [[package]] name = "cpp_demangle" -version = "0.3.5" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eeaa953eaad386a53111e47172c2fedba671e5684c8dd601a5f474f4f118710f" +checksum = "7e8227005286ec39567949b33df9896bcadfa6051bccca2488129f108ca23119" dependencies = [ "cfg-if", ] @@ -360,18 +407,16 @@ dependencies = [ [[package]] name = "cranelift-bforest" -version = "0.102.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76eb38f2af690b5a4411d9a8782b6d77dabff3ca939e0518453ab9f9a4392d41" +version = "0.106.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "cranelift-entity", ] [[package]] name = "cranelift-codegen" -version = "0.102.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39526c036b92912417e8931f52c1e235796688068d3efdbbd8b164f299d19156" +version = "0.106.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "bumpalo", "cranelift-bforest", @@ -390,33 +435,29 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" -version = "0.102.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdb0deedc9fccf2db53a5a3c9c9d0163e44143b0d004dca9bf6ab6a0024cd79a" +version = "0.106.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "cranelift-codegen-shared", ] [[package]] name = "cranelift-codegen-shared" -version = "0.102.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cea2d1b274e45aa8e61e9103efa1ba82d4b5a19d12bd1fd10744c3b7380ba3ff" +version = "0.106.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" [[package]] name = "cranelift-control" -version = "0.102.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ea5977559a71e63db79a263f0e81a89b996e8a38212c4281e37dd1dbaa8b65c" +version = "0.106.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "arbitrary", ] [[package]] name = "cranelift-entity" -version = "0.102.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f871ada808b58158d84dfc43a6a2e2d2756baaf4ed1c51fd969ca8330e6ca5c" +version = "0.106.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "serde", "serde_derive", @@ -424,9 +465,8 @@ dependencies = [ [[package]] name = "cranelift-frontend" -version = "0.102.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8e6890f587ef59824b3debe577e68fdf9b307b3808c54b8d93a18fd0b70941b" +version = "0.106.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "cranelift-codegen", "log", @@ -436,15 +476,13 @@ dependencies = [ [[package]] name = "cranelift-isle" -version = "0.102.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8d5fc6d5d3b52d1917002b17a8ecce448c2621b5bf394bb4e77e2f676893537" +version = "0.106.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" [[package]] name = "cranelift-native" -version = "0.102.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e10c2e7faa65d4ae7de9a83b44f2c31aca7dc638e17d0a79572fdf8103d720b" +version = "0.106.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "cranelift-codegen", "libc", @@ -453,9 +491,8 @@ dependencies = [ [[package]] name = "cranelift-wasm" -version = "0.102.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2755807efc7ec80d1cc0b6815e70f10cedf968889f0469091dbff9c5c0741c48" +version = "0.106.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "cranelift-codegen", "cranelift-entity", @@ -463,7 +500,7 @@ dependencies = [ "itertools", "log", "smallvec", - "wasmparser 0.116.1", + "wasmparser", "wasmtime-types", ] @@ -509,6 +546,13 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crows-bindings" +version = "0.1.0" +dependencies = [ + "borsh", +] + [[package]] name = "crows-cli" version = "0.1.0" @@ -558,8 +602,10 @@ dependencies = [ "byteorder", "crows-service", "futures", + "num-rational", "serde", "serde_json", + "sha256", "syn 2.0.37", "thiserror", "tokio", @@ -573,6 +619,10 @@ name = "crows-wasm" version = "0.1.0" dependencies = [ "anyhow", + "borsh", + "crows-bindings", + "crows-utils", + "slab", "thiserror", "tokio", "wasi-common", @@ -591,6 +641,8 @@ dependencies = [ "crows-utils", "crows-wasm", "futures", + "num-rational", + "num_cpus", "serde", "serde_json", "thiserror", @@ -673,14 +725,14 @@ dependencies = [ [[package]] name = "educe" -version = "0.4.19" +version = "0.5.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c07b7cc9cd8c08d10db74fca3b20949b9b6199725c04a0cce6d543496098fcac" +checksum = "e4bd92664bf78c4d3dba9b7cdafce6fa15b13ed3ed16175218196942e99168a8" dependencies = [ "enum-ordinalize", "proc-macro2", "quote", - "syn 1.0.99", + "syn 2.0.37", ] [[package]] @@ -700,16 +752,22 @@ dependencies = [ [[package]] name = "enum-ordinalize" -version = "3.1.11" +version = "4.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2170fc0efee383079a8bdd05d6ea2a184d2a0f07a1c1dcabdb2fd5e9f24bc36c" +checksum = "fea0dcfa4e54eeb516fe454635a95753ddd39acda650ce703031c6973e315dd5" +dependencies = [ + "enum-ordinalize-derive", +] + +[[package]] +name = "enum-ordinalize-derive" +version = "4.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d28318a75d4aead5c4db25382e8ef717932d0346600cacae6357eb5941bc5ff" dependencies = [ - "num-bigint", - "num-traits", "proc-macro2", "quote", - "rustc_version", - "syn 1.0.99", + "syn 2.0.37", ] [[package]] @@ -720,23 +778,12 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "errno" -version = "0.3.4" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "add4f07d43996f76ef320709726a556a9d4f965d9410d8d0271132d2f8293480" +checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" dependencies = [ - "errno-dragonfly", - "libc", - "windows-sys", -] - -[[package]] -name = "errno-dragonfly" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" -dependencies = [ - "cc", "libc", + "windows-sys 0.52.0", ] [[package]] @@ -753,7 +800,7 @@ checksum = "0b0377f1edc77dbd1118507bc7a66e4ab64d2b90c66f90726dc801e73a8c68f9" dependencies = [ "cfg-if", "rustix", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -767,13 +814,13 @@ dependencies = [ [[package]] name = "fs-set-times" -version = "0.20.0" +version = "0.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd738b84894214045e8414eaded76359b4a5773f0a0a56b16575110739cdcf39" +checksum = "033b337d725b97690d86893f9de22b67b80dcc4e9ad815f348254c38119db8fb" dependencies = [ "io-lifetimes", "rustix", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -880,7 +927,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "27d12c0aed7f1e24276a241aadc4cb8ea9f83000f34bc062b7cc2d51e3b0fabd" dependencies = [ - "bitflags 2.4.0", + "bitflags 2.4.2", "debugid", "fxhash", "serde", @@ -954,11 +1001,37 @@ checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] name = "hermit-abi" -version = "0.1.19" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" + +[[package]] +name = "hex" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "iana-time-zone" +version = "0.1.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" dependencies = [ - "libc", + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", ] [[package]] @@ -990,19 +1063,19 @@ dependencies = [ [[package]] name = "io-extras" -version = "0.18.0" +version = "0.18.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d3c230ee517ee76b1cc593b52939ff68deda3fae9e41eca426c6b4993df51c4" +checksum = "c301e73fb90e8a29e600a9f402d095765f74310d582916a952f618836a1bd1ed" dependencies = [ "io-lifetimes", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] name = "io-lifetimes" -version = "2.0.2" +version = "2.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bffb4def18c48926ccac55c1223e02865ce1a821751a95920448662696e7472c" +checksum = "5a611371471e98973dbcab4e0ec66c31a10bc356eeb4d54a0e05eac8158fe38c" [[package]] name = "ipnet" @@ -1012,9 +1085,9 @@ checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" [[package]] name = "itertools" -version = "0.10.5" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" dependencies = [ "either", ] @@ -1054,6 +1127,15 @@ dependencies = [ "libc", ] +[[package]] +name = "js-sys" +version = "0.3.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "406cda4b368d531c842222cf9d2600a9a4acce8d29423695379c6868a143a9ee" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "leb128" version = "0.2.5" @@ -1062,15 +1144,15 @@ checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" [[package]] name = "libc" -version = "0.2.150" +version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "linux-raw-sys" -version = "0.4.11" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "969488b55f8ac402214f3f5fd243ebb7206cf82de60d3172994707a4bcc2b829" +checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" [[package]] name = "lock_api" @@ -1144,7 +1226,7 @@ checksum = "927a765cd3fc26206e66b296465fa9d3e5ab003e651c1b3c060e7956d96b19d2" dependencies = [ "libc", "wasi", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -1168,6 +1250,19 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-rational" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" +dependencies = [ + "autocfg", + "num-bigint", + "num-integer", + "num-traits", + "serde", +] + [[package]] name = "num-traits" version = "0.2.15" @@ -1179,9 +1274,9 @@ dependencies = [ [[package]] name = "num_cpus" -version = "1.13.1" +version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ "hermit-abi", "libc", @@ -1225,7 +1320,7 @@ dependencies = [ "libc", "redox_syscall 0.3.5", "smallvec", - "windows-targets", + "windows-targets 0.48.5", ] [[package]] @@ -1284,6 +1379,38 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "proc-macro-crate" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284" +dependencies = [ + "toml_edit 0.21.1", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.67" @@ -1415,28 +1542,19 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" -[[package]] -name = "rustc_version" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" -dependencies = [ - "semver", -] - [[package]] name = "rustix" -version = "0.38.25" +version = "0.38.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc99bc2d4f1fed22595588a013687477aedf3cdcfb26558c559edb67b4d9b22e" +checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" dependencies = [ - "bitflags 2.4.0", + "bitflags 2.4.2", "errno", "itoa", "libc", "linux-raw-sys", "once_cell", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] @@ -1453,9 +1571,9 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "semver" -version = "1.0.13" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93f6841e709003d68bb2deee8c343572bf446003ec20a583e76f7b15cebf3711" +checksum = "92d43fe69e652f3df9bdc2b85b2854a0825b86e4fb76bc44d945137d053639ca" [[package]] name = "serde" @@ -1488,6 +1606,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" +dependencies = [ + "serde", +] + [[package]] name = "sha2" version = "0.10.8" @@ -1499,6 +1626,19 @@ dependencies = [ "digest", ] +[[package]] +name = "sha256" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7895c8ae88588ccead14ff438b939b0c569cd619116f14b4d13fdff7b8333386" +dependencies = [ + "async-trait", + "bytes", + "hex", + "sha2", + "tokio", +] + [[package]] name = "shellexpand" version = "2.1.2" @@ -1545,7 +1685,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4031e820eb552adee9295814c0ced9e5cf38ddf1e8b7d566d6de8e2538ea989e" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -1588,27 +1728,39 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn_derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1329189c02ff984e9736652b1631330da25eaa6bc639089ed4915d25446cbe7b" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.37", +] + [[package]] name = "system-interface" -version = "0.26.0" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "27ce32341b2c0b70c144bbf35627fdc1ef18c76ced5e5e7b3ee8b5ba6b2ab6a0" +checksum = "9aef1f9d4c1dbdd1cb3a63be9efd2f04d8ddbc919d46112982c76818ffc2f1a7" dependencies = [ - "bitflags 2.4.0", + "bitflags 2.4.2", "cap-fs-ext", "cap-std", "fd-lock", "io-lifetimes", "rustix", - "windows-sys", + "windows-sys 0.52.0", "winx", ] [[package]] name = "target-lexicon" -version = "0.12.12" +version = "0.12.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14c39fd04924ca3a864207c66fc2cd7d22d7c016007f9ce846cbb9326331930a" +checksum = "e1fc403891a21bcfb7c37834ba66a547a8f402146eba7265b5a6d88059c9ff2f" [[package]] name = "thiserror" @@ -1661,7 +1813,7 @@ dependencies = [ "signal-hook-registry", "socket2", "tokio-macros", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -1677,9 +1829,9 @@ dependencies = [ [[package]] name = "tokio-serde" -version = "0.8.0" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "911a61637386b789af998ee23f50aa30d5fd7edcec8d6d3dedae5e5815205466" +checksum = "caf600e7036b17782571dd44fa0a5cea3c82f60db5137f774a325a76a0d6852b" dependencies = [ "bytes", "educe", @@ -1710,11 +1862,47 @@ dependencies = [ [[package]] name = "toml" -version = "0.5.11" +version = "0.8.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a9aad4a3066010876e8dcf5a8a06e70a558751117a145c6ce2b82c2e2054290" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit 0.22.6", +] + +[[package]] +name = "toml_datetime" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" +dependencies = [ + "indexmap", + "toml_datetime", + "winnow 0.5.40", +] + +[[package]] +name = "toml_edit" +version = "0.22.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +checksum = "2c1b5fd4128cc8d3e0cb74d4ed9a9cc7c7284becd4df68f5f940e1ad123606f6" dependencies = [ + "indexmap", "serde", + "serde_spanned", + "toml_datetime", + "winnow 0.6.5", ] [[package]] @@ -1835,13 +2023,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] -name = "wasi-cap-std-sync" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3542b8d238a3de6c9986218af842f1e8f950ca7c4707aee9d0dd83002577a759" +name = "wasi-common" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "anyhow", - "async-trait", + "bitflags 2.4.2", "cap-fs-ext", "cap-rand", "cap-std", @@ -1849,105 +2036,107 @@ dependencies = [ "fs-set-times", "io-extras", "io-lifetimes", + "log", "once_cell", "rustix", "system-interface", + "thiserror", "tracing", - "wasi-common", - "windows-sys", + "wasmtime", + "wiggle", + "windows-sys 0.52.0", ] [[package]] -name = "wasi-common" -version = "15.0.0" +name = "wasm-bindgen" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a362c9dbdc5eb0809ce9db09e7b76805fea3ddaf2b8ff41a0e5c805935736205" +checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f" dependencies = [ - "anyhow", - "bitflags 2.4.0", - "cap-rand", - "cap-std", - "io-extras", - "log", - "rustix", - "thiserror", - "tracing", - "wasmtime", - "wiggle", - "windows-sys", + "cfg-if", + "wasm-bindgen-macro", ] [[package]] -name = "wasi-tokio" -version = "15.0.0" +name = "wasm-bindgen-backend" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "385503c0e4502dc5cbd7fdfe25f2d87a5dd19dd1cdd7e3bc5184146713bba554" +checksum = "c9e7e1900c352b609c8488ad12639a311045f40a35491fb69ba8c12f758af70b" dependencies = [ - "anyhow", - "cap-std", - "io-extras", - "io-lifetimes", - "rustix", - "tokio", - "wasi-cap-std-sync", - "wasi-common", - "wiggle", + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn 2.0.37", + "wasm-bindgen-shared", ] [[package]] -name = "wasm-encoder" -version = "0.36.2" +name = "wasm-bindgen-macro" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "822b645bf4f2446b949776ffca47e2af60b167209ffb70814ef8779d299cd421" +checksum = "b30af9e2d358182b5c7449424f017eba305ed32a7010509ede96cdc4696c46ed" dependencies = [ - "leb128", + "quote", + "wasm-bindgen-macro-support", ] [[package]] -name = "wasm-encoder" -version = "0.38.0" +name = "wasm-bindgen-macro-support" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b09bc5df933a3dabbdb72ae4b6b71be8ae07f58774d5aa41bd20adcd41a235a" +checksum = "642f325be6301eb8107a83d12a8ac6c1e1c54345a7ef1a9261962dfefda09e66" dependencies = [ - "leb128", + "proc-macro2", + "quote", + "syn 2.0.37", + "wasm-bindgen-backend", + "wasm-bindgen-shared", ] [[package]] -name = "wasmparser" -version = "0.116.1" +name = "wasm-bindgen-shared" +version = "0.2.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838" + +[[package]] +name = "wasm-encoder" +version = "0.201.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a58e28b80dd8340cb07b8242ae654756161f6fc8d0038123d679b7b99964fa50" +checksum = "b9c7d2731df60006819b013f64ccc2019691deccf6e11a1804bc850cd6748f1a" dependencies = [ - "indexmap", - "semver", + "leb128", ] [[package]] name = "wasmparser" -version = "0.118.0" +version = "0.201.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebbb91574de0011ded32b14db12777e7dd5e9ea2f9d7317a1ab51a9495c75924" +checksum = "84e5df6dba6c0d7fafc63a450f1738451ed7a0b52295d83e868218fa286bf708" dependencies = [ + "bitflags 2.4.2", "indexmap", "semver", ] [[package]] name = "wasmprinter" -version = "0.2.74" +version = "0.201.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61a7a046e6636d25c06a5df00bdc34e02f9e6e0e8a356d738299b961a6126114" +checksum = "a67e66da702706ba08729a78e3c0079085f6bfcb1a62e4799e97bbf728c2c265" dependencies = [ "anyhow", - "wasmparser 0.118.0", + "wasmparser", ] [[package]] name = "wasmtime" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae4b1702ef55144d6f594085f4989dc71fb71a791be1c8354ecc8e489b81199b" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ + "addr2line", "anyhow", "async-trait", "bincode", @@ -1955,47 +2144,50 @@ dependencies = [ "cfg-if", "encoding_rs", "fxprof-processed-profile", + "gimli", "indexmap", + "ittapi", "libc", "log", "object", "once_cell", "paste", - "psm", "rayon", + "rustix", + "semver", "serde", "serde_derive", "serde_json", "target-lexicon", - "wasm-encoder 0.36.2", - "wasmparser 0.116.1", + "wasm-encoder", + "wasmparser", "wasmtime-cache", "wasmtime-component-macro", "wasmtime-component-util", "wasmtime-cranelift", "wasmtime-environ", "wasmtime-fiber", - "wasmtime-jit", + "wasmtime-jit-debug", + "wasmtime-jit-icache-coherence", "wasmtime-runtime", + "wasmtime-slab", "wasmtime-winch", "wat", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] name = "wasmtime-asm-macros" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c981d0e87bb3e98e08e76644e7ae5dfdef7f1d4105145853f3d677bb4535d65f" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "cfg-if", ] [[package]] name = "wasmtime-cache" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d7ba8adaa84fdb9dd659275edcf7fc5282c44b9c9f829986c71d44fd52ea80a" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "anyhow", "base64", @@ -2007,15 +2199,14 @@ dependencies = [ "serde_derive", "sha2", "toml", - "windows-sys", + "windows-sys 0.52.0", "zstd", ] [[package]] name = "wasmtime-component-macro" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c91dcbbd0e1f094351d1ae0e53463c63ba53ec8f8e0e21d17567c1979a8c3758" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "anyhow", "proc-macro2", @@ -2028,15 +2219,13 @@ dependencies = [ [[package]] name = "wasmtime-component-util" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e85f1319a7ed36aa59446ab7e967d0c2fb0cd179bf56913633190b44572023e" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" [[package]] name = "wasmtime-cranelift" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1453665878e16245b9a25405e550c4a36c6731c6e34ea804edc002a38c3e6741" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "anyhow", "cfg-if", @@ -2051,7 +2240,7 @@ dependencies = [ "object", "target-lexicon", "thiserror", - "wasmparser 0.116.1", + "wasmparser", "wasmtime-cranelift-shared", "wasmtime-environ", "wasmtime-versioned-export-macros", @@ -2059,9 +2248,8 @@ dependencies = [ [[package]] name = "wasmtime-cranelift-shared" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3dface3d9b72b4670781ff72675eabb291e2836b5dded6bb312b577d2bb561f" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "anyhow", "cranelift-codegen", @@ -2075,22 +2263,24 @@ dependencies = [ [[package]] name = "wasmtime-environ" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0116108e7d231cce15fe7dd642c66c3abb14dbcf169b0130e11f223ce8d1ad7" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "anyhow", + "bincode", + "cpp_demangle", "cranelift-entity", "gimli", "indexmap", "log", "object", + "rustc-demangle", "serde", "serde_derive", "target-lexicon", "thiserror", - "wasm-encoder 0.36.2", - "wasmparser 0.116.1", + "wasm-encoder", + "wasmparser", "wasmprinter", "wasmtime-component-util", "wasmtime-types", @@ -2098,9 +2288,8 @@ dependencies = [ [[package]] name = "wasmtime-fiber" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8a5896355c37bf0f9feb4f1299142ef4bed8c92576aa3a41d150fed0cafa056" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "anyhow", "cc", @@ -2108,41 +2297,13 @@ dependencies = [ "rustix", "wasmtime-asm-macros", "wasmtime-versioned-export-macros", - "windows-sys", -] - -[[package]] -name = "wasmtime-jit" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e32b210767452f6b20157bb7c7d98295b92cc47aaad2a8aa31652f4469813a5d" -dependencies = [ - "addr2line", - "anyhow", - "bincode", - "cfg-if", - "cpp_demangle", - "gimli", - "ittapi", - "log", - "object", - "rustc-demangle", - "rustix", - "serde", - "serde_derive", - "target-lexicon", - "wasmtime-environ", - "wasmtime-jit-debug", - "wasmtime-jit-icache-coherence", - "wasmtime-runtime", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] name = "wasmtime-jit-debug" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bffd2785a16c55ac77565613ebda625f5850d4014af0499df750e8de97c04547" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "object", "once_cell", @@ -2152,20 +2313,18 @@ dependencies = [ [[package]] name = "wasmtime-jit-icache-coherence" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b73ad1395eda136baec5ece7e079e0536a82ef73488e345456cc9b89858ad0ec" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "cfg-if", "libc", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] name = "wasmtime-runtime" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77b50f7f3c1a8dabb2607f32a81242917bd77cee75f3dec66e04b02ccbb8ba07" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "anyhow", "cc", @@ -2178,37 +2337,40 @@ dependencies = [ "memfd", "memoffset", "paste", - "rand", + "psm", "rustix", "sptr", - "wasm-encoder 0.36.2", + "wasm-encoder", "wasmtime-asm-macros", "wasmtime-environ", "wasmtime-fiber", "wasmtime-jit-debug", "wasmtime-versioned-export-macros", "wasmtime-wmemcheck", - "windows-sys", + "windows-sys 0.52.0", ] +[[package]] +name = "wasmtime-slab" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" + [[package]] name = "wasmtime-types" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "447973db3dc5c24db14130ab0922795c58790aec296d198ad9d253b82ec67471" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "cranelift-entity", "serde", "serde_derive", "thiserror", - "wasmparser 0.116.1", + "wasmparser", ] [[package]] name = "wasmtime-versioned-export-macros" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a347bb8ecf12275fb180afb1b1c85c9e186553c43109737bffed4f54c2aa365" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "proc-macro2", "quote", @@ -2217,13 +2379,12 @@ dependencies = [ [[package]] name = "wasmtime-wasi" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77f94342fc932695027cdfa0500a62a680879bdad495b36490887b1564124e53" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "anyhow", "async-trait", - "bitflags 2.4.0", + "bitflags 2.4.2", "bytes", "cap-fs-ext", "cap-net-ext", @@ -2234,8 +2395,6 @@ dependencies = [ "futures", "io-extras", "io-lifetimes", - "libc", - "log", "once_cell", "rustix", "system-interface", @@ -2243,26 +2402,22 @@ dependencies = [ "tokio", "tracing", "url", - "wasi-cap-std-sync", - "wasi-common", - "wasi-tokio", "wasmtime", "wiggle", - "windows-sys", + "windows-sys 0.52.0", ] [[package]] name = "wasmtime-winch" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc8c602f026526d754c33b750f67d754234c6ec29595865916693c3306ca6a3b" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "anyhow", "cranelift-codegen", "gimli", "object", "target-lexicon", - "wasmparser 0.116.1", + "wasmparser", "wasmtime-cranelift-shared", "wasmtime-environ", "winch-codegen", @@ -2270,9 +2425,8 @@ dependencies = [ [[package]] name = "wasmtime-wit-bindgen" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41786c7bbbf250c0e685b291323b50c6bb65f0505a2c0b4f0b598c740f13f185" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "anyhow", "heck", @@ -2282,9 +2436,8 @@ dependencies = [ [[package]] name = "wasmtime-wmemcheck" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47907bdd67500c66fa308acbce7387c7bfb63b5505ef81be7fc897709afcca60" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" [[package]] name = "wast" @@ -2297,34 +2450,34 @@ dependencies = [ [[package]] name = "wast" -version = "69.0.0" +version = "201.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efa51b5ad1391943d1bfad537e50f28fe938199ee76b115be6bae83802cd5185" +checksum = "1ef6e1ef34d7da3e2b374fd2b1a9c0227aff6cad596e1b24df9b58d0f6222faa" dependencies = [ + "bumpalo", "leb128", "memchr", "unicode-width", - "wasm-encoder 0.38.0", + "wasm-encoder", ] [[package]] name = "wat" -version = "1.0.81" +version = "1.201.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74a4c2488d058326466e086a43f5d4ea448241a8d0975e3eb0642c0828be1eb3" +checksum = "453d5b37a45b98dee4f4cb68015fc73634d7883bbef1c65e6e9c78d454cf3f32" dependencies = [ - "wast 69.0.0", + "wast 201.0.0", ] [[package]] name = "wiggle" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35b5a36af7e0a7d68fd6c080e78803b34c3105caa3f743dff2fc8db2fac4ab71" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "anyhow", "async-trait", - "bitflags 2.4.0", + "bitflags 2.4.2", "thiserror", "tracing", "wasmtime", @@ -2334,9 +2487,8 @@ dependencies = [ [[package]] name = "wiggle-generate" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09f5a763e4801e83c438e7fa6abdd5c38d735194c2a94e2f2ccdcc66456cefee" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "anyhow", "heck", @@ -2349,9 +2501,8 @@ dependencies = [ [[package]] name = "wiggle-macro" -version = "15.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "58262f5ac3a8ea686d4b940aa9f976f26c7e4e980aa8ac378f29274cb8638e33" +version = "19.0.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "proc-macro2", "quote", @@ -2383,9 +2534,8 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "winch-codegen" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9057ea325cac1ec02b28418da975a9f3a3634611812dc6150401347f1774844e" +version = "0.17.0" +source = "git+https://github.com/bytecodealliance/wasmtime.git#0f7b1759d64382b57dc7f886a9862e3a6f6b97ca" dependencies = [ "anyhow", "cranelift-codegen", @@ -2393,17 +2543,35 @@ dependencies = [ "regalloc2", "smallvec", "target-lexicon", - "wasmparser 0.116.1", + "wasmparser", "wasmtime-environ", ] +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.4", +] + [[package]] name = "windows-sys" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets", + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.4", ] [[package]] @@ -2412,13 +2580,28 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" +dependencies = [ + "windows_aarch64_gnullvm 0.52.4", + "windows_aarch64_msvc 0.52.4", + "windows_i686_gnu 0.52.4", + "windows_i686_msvc 0.52.4", + "windows_x86_64_gnu 0.52.4", + "windows_x86_64_gnullvm 0.52.4", + "windows_x86_64_msvc 0.52.4", ] [[package]] @@ -2427,57 +2610,117 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" + [[package]] name = "windows_aarch64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" + [[package]] name = "windows_i686_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" +[[package]] +name = "windows_i686_gnu" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" + [[package]] name = "windows_i686_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" +[[package]] +name = "windows_i686_msvc" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" + [[package]] name = "windows_x86_64_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" + [[package]] name = "windows_x86_64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" + +[[package]] +name = "winnow" +version = "0.5.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876" +dependencies = [ + "memchr", +] + +[[package]] +name = "winnow" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dffa400e67ed5a4dd237983829e66475f0a4a26938c4b04c21baede6262215b8" +dependencies = [ + "memchr", +] + [[package]] name = "winx" version = "0.36.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "357bb8e2932df531f83b052264b050b81ba0df90ee5a59b2d1d3949f344f81e5" dependencies = [ - "bitflags 2.4.0", - "windows-sys", + "bitflags 2.4.2", + "windows-sys 0.48.0", ] [[package]] name = "wit-parser" -version = "0.13.0" +version = "0.201.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15df6b7b28ce94b8be39d8df5cb21a08a4f3b9f33b631aedb4aa5776f785ead3" +checksum = "196d3ecfc4b759a8573bf86a9b3f8996b304b3732e4c7de81655f875f6efdca6" dependencies = [ "anyhow", "id-arena", @@ -2488,6 +2731,7 @@ dependencies = [ "serde_derive", "serde_json", "unicode-xid", + "wasmparser", ] [[package]] @@ -2504,20 +2748,19 @@ dependencies = [ [[package]] name = "zstd" -version = "0.11.2+zstd.1.5.2" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20cc960326ece64f010d2d2107537f26dc589a6573a316bd5b1dba685fa5fde4" +checksum = "bffb3309596d527cfcba7dfc6ed6052f1d39dfbd7c867aa2e865e4a449c10110" dependencies = [ "zstd-safe", ] [[package]] name = "zstd-safe" -version = "5.0.2+zstd.1.5.2" +version = "7.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d2a5585e04f9eea4b2a3d1eca508c4dee9592a89ef6f450c11719da0726f4db" +checksum = "43747c7422e2924c11144d5229878b98180ef8b06cca4ab5af37afc8a8d8ea3e" dependencies = [ - "libc", "zstd-sys", ] diff --git a/Cargo.toml b/Cargo.toml index 43ffced..9c1d23d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,21 +8,22 @@ members = [ "service", "worker", "wasm", + "bindings", ] [workspace.dependencies] tokio = { version = "1.20", features = ["full"] } anyhow = { version = "1", features = ["backtrace"] } thiserror = "1" -tokio-serde = { version = "0.8.0", features = ["json"] } +tokio-serde = { version = "0.9.0", features = ["json"] } tokio-util = { version = "0.7.3", features = ["full"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.108" futures = "0.3.21" uuid = { version = "1.4.1", features = ["serde", "v4"] } -wasmtime = "15" -wasmtime-wasi = { version = "15", features = ["tokio"] } -wasi-common = "15" -wiggle = "15" - - +wasmtime = { git = "https://github.com/bytecodealliance/wasmtime.git" } +wasmtime-wasi = { git = "https://github.com/bytecodealliance/wasmtime.git" } +wasi-common = { git = "https://github.com/bytecodealliance/wasmtime.git" } +wiggle = { git = "https://github.com/bytecodealliance/wasmtime.git" } +num-rational = { version = "0.4", features = ["serde"]} +borsh = { version = "1.3", features = ["unstable__schema", "derive"] } diff --git a/bindings/Cargo.toml b/bindings/Cargo.toml new file mode 100644 index 0000000..e839436 --- /dev/null +++ b/bindings/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "crows-bindings" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +borsh.workspace = true diff --git a/bindings/src/lib.rs b/bindings/src/lib.rs new file mode 100644 index 0000000..81d0f0a --- /dev/null +++ b/bindings/src/lib.rs @@ -0,0 +1,116 @@ +use borsh::{from_slice, to_vec, BorshDeserialize, BorshSchema, BorshSerialize}; +use std::{cell::RefCell, collections::HashMap, mem::MaybeUninit}; + +#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)] +pub enum HTTPMethod { + HEAD, + GET, + POST, + PUT, + DELETE, + OPTIONS, +} + +#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)] +pub struct HTTPRequest { + url: String, + method: HTTPMethod, + headers: HashMap, + body: Option, +} + +#[derive(Debug, BorshDeserialize, BorshSerialize)] +pub struct HTTPError {} + +#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)] +pub struct HTTPResponse { + // TODO: these should not be public I think, I'd prefer to do a public interface for them + pub headers: HashMap, + pub body: String, + pub status: u16, +} + +fn extract_from_return_value(value: u64) -> (u8, u32, u32) { + let status = ((value >> 56) & 0xFF) as u8; + let length = ((value >> 32) & 0x00FFFFFF) as u32; + let ptr = (value & 0xFFFFFFFF) as u32; + (status, length, ptr) +} + +mod bindings { + #[link(wasm_import_module = "crows")] + extern "C" { + pub fn log(content: *mut u8, content_len: usize); + pub fn http(content: *mut u8, content_len: usize) -> u64; + pub fn consume_buffer(index: u32, content: *mut u8, content_len: usize); + + } +} + +fn with_buffer(f: impl FnOnce(&mut Vec) -> R) -> R { + thread_local! { + static BUFFER: RefCell> = RefCell::new(Vec::new()); + } + + BUFFER.with(|r| { + let mut buf = r.borrow_mut(); + buf.clear(); + f(&mut buf) + }) +} + +pub fn http_request( + url: String, + method: HTTPMethod, + headers: HashMap, + body: String, +) -> Result { + let body = Some(body); + let request = HTTPRequest { + method, + url, + headers, + body, + }; + + call_host_function(&request, |buf| unsafe { + bindings::http(buf.as_mut_ptr(), buf.len()) + }) +} + +fn call_host_function(arguments: &T, f: impl FnOnce(&mut Vec) -> u64) -> Result +where + T: BorshSerialize, + R: BorshDeserialize, + E: BorshDeserialize, +{ + let mut encoded = to_vec(arguments).unwrap(); + + println!("encoded length: {}", encoded.len()); + let (status, length, index) = with_buffer(|mut buf| { + buf.append(&mut encoded); + let response = f(&mut buf); + + extract_from_return_value(response) + }); + + println!("response: {status}, {length}, {index}"); + with_buffer(|buf| { + let capacity = buf.capacity(); + if capacity < length as usize { + let additional = length as usize - buf.capacity(); + buf.reserve_exact(additional); + } + + unsafe { + bindings::consume_buffer(index, buf.as_mut_ptr(), length as usize); + buf.set_len(length as usize); + } + + if status == 0 { + Ok(from_slice(buf).expect("Couldn't decode message from the host")) + } else { + Err(from_slice(buf).expect("Couldn't decode message from the host")) + } + }) +} diff --git a/bindings/src/main.rs b/bindings/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/bindings/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/coordinator/src/main.rs b/coordinator/src/main.rs index e7bd984..35f6135 100644 --- a/coordinator/src/main.rs +++ b/coordinator/src/main.rs @@ -13,14 +13,16 @@ use crows_utils::services::{ WorkerStatus, }; use crows_utils::services::{Coordinator, WorkerToCoordinator}; +use crows_utils::ModuleId; use uuid::Uuid; // TODO: I don't like the fact that we have to wrap the client in Mutex and option. It should // be easier to match the client object with the request to the service. I should probably // add a context object at some point. +// TODO: Client should probably be thread safe for easier handling #[derive(Default)] struct WorkerToCoordinatorService { - scenarios: Arc>>>, + scenarios: Arc>>>, workers: Arc>>, client: Arc>>, } @@ -39,7 +41,7 @@ impl WorkerToCoordinator for WorkerToCoordinatorService { #[derive(Clone, Default)] struct CoordinatorService { - scenarios: Arc>>>, + scenarios: Arc>>>, workers: Arc>>, } @@ -56,19 +58,24 @@ impl Coordinator for CoordinatorService { name: String, content: Vec, ) -> Result<(), CoordinatorError> { + let id = ModuleId::new(name.clone(), &content); + + // TODO: to send bandwidth maybe it will be worth it to gzip the data? we would be gzipping + // once and sending to N clients // send each uploaded scenario to all of the workers for (_, worker_entry) in self.workers.lock().await.iter() { let locked = worker_entry.client.lock(); let mut futures = Vec::new(); futures.push(async { if let Some(client) = locked.await.as_mut() { - client.upload_scenario(name.clone(), content.clone()).await; + // TODO: handle Result + client.upload_scenario(id.clone(), content.clone()).await; } }); join_all(futures).await; } - self.scenarios.lock().await.insert(name, content); + self.scenarios.lock().await.insert(id, content); Ok(()) } @@ -103,7 +110,7 @@ pub async fn main() { .parse() .unwrap(); - let original_scenarios: Arc>>> = Default::default(); + let original_scenarios: Arc>>> = Default::default(); let original_workers: Arc>> = Default::default(); let scenarios = original_scenarios.clone(); @@ -130,8 +137,8 @@ pub async fn main() { // sent all the current scenarios to a new worker node let locked = scenarios.lock().await; - for (name, content) in locked.iter() { - let _ = client.upload_scenario(name.clone(), content.clone()).await; + for (id, content) in locked.iter() { + let _ = client.upload_scenario(id.clone(), content.clone()).await; } drop(locked); diff --git a/rust-example/Cargo.lock b/rust-example/Cargo.lock new file mode 100644 index 0000000..ea7a499 --- /dev/null +++ b/rust-example/Cargo.lock @@ -0,0 +1,192 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "borsh" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f58b559fd6448c6e2fd0adb5720cd98a2506594cafa4737ff98c396f3e82f667" +dependencies = [ + "borsh-derive", + "cfg_aliases", +] + +[[package]] +name = "borsh-derive" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7aadb5b6ccbd078890f6d7003694e33816e6b784358f18e15e7e6d9f065a57cd" +dependencies = [ + "once_cell", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn", + "syn_derive", +] + +[[package]] +name = "cfg_aliases" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" + +[[package]] +name = "crows-bindings" +version = "0.1.0" +dependencies = [ + "borsh", +] + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "hashbrown" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" + +[[package]] +name = "indexmap" +version = "2.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b0b929d511467233429c45a44ac1dcaa21ba0f5ba11e4879e6ed28ddb4f9df4" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "memchr" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "proc-macro-crate" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284" +dependencies = [ + "toml_edit", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.52" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn_derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1329189c02ff984e9736652b1631330da25eaa6bc639089ed4915d25446cbe7b" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "toml_datetime" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" + +[[package]] +name = "toml_edit" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" +dependencies = [ + "indexmap", + "toml_datetime", + "winnow", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "wasm_example" +version = "0.1.0" +dependencies = [ + "crows-bindings", +] + +[[package]] +name = "winnow" +version = "0.5.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876" +dependencies = [ + "memchr", +] diff --git a/rust-example/Cargo.toml b/rust-example/Cargo.toml new file mode 100644 index 0000000..f794927 --- /dev/null +++ b/rust-example/Cargo.toml @@ -0,0 +1,12 @@ +[workspace] +[package] +name = "wasm_example" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +crate-type = ["cdylib"] + +[dependencies] +crows-bindings = { path = "../bindings" } diff --git a/rust-example/server.js b/rust-example/server.js new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/rust-example/server.js @@ -0,0 +1 @@ + diff --git a/rust-example/src/lib.rs b/rust-example/src/lib.rs new file mode 100644 index 0000000..bf717b2 --- /dev/null +++ b/rust-example/src/lib.rs @@ -0,0 +1,11 @@ +use std::cell::RefCell; +use std::collections::HashMap; +use crows_bindings::{http_request, HTTPMethod::*}; + +#[export_name="test"] +pub fn test() { + let response = http_request("foo".into(), GET, HashMap::new(), "".into()); + println!("response: {response:?}"); +} + + diff --git a/utils/Cargo.toml b/utils/Cargo.toml index 2ba5483..b5db35f 100644 --- a/utils/Cargo.toml +++ b/utils/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" crows-service = { path = "../service" } syn = { version = "2.0.37", features = ["full", "extra-traits", "fold"] } byteorder = "1.4" +sha256 = "1.4" tokio.workspace = true thiserror.workspace = true @@ -19,3 +20,4 @@ uuid.workspace = true tokio-util.workspace = true tokio-serde.workspace = true futures.workspace = true +num-rational.workspace = true diff --git a/utils/src/lib.rs b/utils/src/lib.rs index 2033231..9afc9f3 100644 --- a/utils/src/lib.rs +++ b/utils/src/lib.rs @@ -28,6 +28,26 @@ pub use tokio; pub mod services; pub use crows_service; +// ModuleId will be used to distinguish between different versions of modules +// TODO: I don't have time to check the properties of the default Hash implementation +// it should be checked at some point. It's probably good enough, but I would like to confirm +#[derive(Debug, Serialize, Deserialize, Clone, Hash, PartialEq, Eq)] +pub struct ModuleId { + name: String, + version: String, // version will be a sha256 hash of the module contents +} + +impl ModuleId { + pub fn new(name: String, content: &[u8]) -> Self { + let version = sha256::digest(content); + + Self { + name, + version, + } + } +} + pub struct Server { listener: TcpListener, } diff --git a/utils/src/services/mod.rs b/utils/src/services/mod.rs index 5c5f7db..caeda4d 100644 --- a/utils/src/services/mod.rs +++ b/utils/src/services/mod.rs @@ -1,8 +1,9 @@ -use crate as utils; +use crate::{self as utils, ModuleId}; use serde::{Deserialize, Serialize}; use crows_service::service; use thiserror::Error; use uuid::Uuid; +use num_rational::Rational64; #[derive(Error, Debug, Serialize, Deserialize, Clone)] pub enum CoordinatorError { @@ -18,6 +19,21 @@ pub enum WorkerError { ScenarioNotFound, } +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)] +pub struct RunId(Uuid); + +impl RunId { + pub fn new() -> Self { + Self(Uuid::new_v4()) + } +} + +impl Into for RunId { + fn into(self) -> Uuid { + self.0 + } +} + // TODO: I don't like the fact that I need to specify the "other_side" // here. It would be better if it was only needed when connecting, then // all the trait definitions wouldn't have to be here @@ -48,8 +64,9 @@ pub struct WorkerData { #[service(variant = "client", other_side = WorkerToCoordinator)] pub trait Worker { - async fn upload_scenario(&mut self, name: String, content: Vec); + async fn upload_scenario(&mut self, id: ModuleId, content: Vec); async fn ping(&self) -> String; + async fn prepare(&mut self, id: ModuleId, concurrency: usize, rate: Rational64) -> Result; async fn start(&self, name: String, concurrency: usize) -> Result<(), WorkerError>; async fn get_data(&self) -> WorkerData; } diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml index 2cc6a80..10251e6 100644 --- a/wasm/Cargo.toml +++ b/wasm/Cargo.toml @@ -4,6 +4,11 @@ version = "0.1.0" edition = "2021" [dependencies] +crows-utils = { path = "../utils" } +slab = "0.4" +crows-bindings = { path = "../bindings" } + +borsh.workspace = true tokio.workspace = true anyhow.workspace = true thiserror.workspace = true diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index feca170..991681e 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -1,43 +1,54 @@ -use std::{any::Any, io::IoSlice, sync::Arc, collections::HashMap}; +use borsh::to_vec; +use crows_bindings::{HTTPResponse, HTTPError}; +use crows_utils::{services::RunId, ModuleId}; +use std::mem::MaybeUninit; +use std::{any::Any, collections::HashMap, io::IoSlice, sync::Arc}; use tokio::time::{Duration, Instant}; +use wasi_common::WasiFile; use wasi_common::{ file::{FdFlags, FileType}, pipe::WritePipe, Table, }; -use wasmtime::{Config, Engine, Linker, Module, Store, Val}; -use wasmtime_wasi::{tokio::WasiCtxBuilder, WasiCtx, WasiFile}; +use wasmtime::{ + Caller, Config, Engine, Linker, Memory, MemoryType, Module, Store, StoreContextMut, Val, + ValType, +}; +use wasmtime_wasi::{StdoutStream, WasiCtxBuilder}; #[derive(thiserror::Error, Debug)] pub enum Error { #[error("the module with a given name couldn't be found")] - NoSuchModule(String), + NoSuchRun(RunId), } // A runtime should be run in a single async runtime. Ideally also in a single // thread as we want a share-nothing architecture for performance and simplicity -struct Runtime { - // we index instances with a name of the module, cause technically we can run +#[derive(Default)] +pub struct Runtime { + // we index instances with the run id, cause technically we can run // scenarios from multiple modules on a single runtime // TODO: might be simpler to assume only one module? I'm not sure yet if it's worth // it. If the overhead is too big I'd probably refactor it to allow only one module // at any point in time. I would like to start with multiple modules, though, to first - // see if it's actually problematic, maybe it's not - instances: HashMap>, + // see if it's actually problematic, maybe it's not and it seems to give more flexibility + instances: HashMap>, } impl Runtime { - // TODO: it looks like the name/module pair should be in a separate data type, might - // be worth to extract it, although I'm not sure yet - // TODO: if modules are indexed by name, how do we distinguish between module versions? - // maybe it should be a hash - fn create_instances( + pub fn new() -> Self { + Self::default() + } + + // TODO: it looks like the id/module pair should be in a separate data type, might + // be worth to extract it in the future + pub fn create_instances( &mut self, - name: String, + id: RunId, count: usize, - instance: Instance, + instance: &Instance, ) -> Result<(), Error> { - let mut instances = self.instances.get_mut(&name).ok_or(Error::NoSuchModule(name))?; + let mut instances = self.instances.get_mut(&id).ok_or(Error::NoSuchRun(id))?; for _ in (0..count).into_iter() { instances.push(instance.clone()); } @@ -46,11 +57,108 @@ impl Runtime { } } +struct WasiHostCtx { + preview2_ctx: wasmtime_wasi::WasiCtx, + preview2_table: wasmtime::component::ResourceTable, + preview1_adapter: wasmtime_wasi::preview1::WasiPreview1Adapter, + memory: Option, + buffers: slab::Slab>, +} + +fn create_return_value(status: u8, length: u32, ptr: u32) -> u64 { + assert!( + length <= 0x00FFFFFF, + "Length must be no larger than 3 bytes" + ); + ((status as u64) << 56) | ((length as u64) << 32) | (ptr as u64) +} + +impl WasiHostCtx { + pub fn instantiate(&mut self, mem: Memory) { + self.memory = Some(mem); + } + + pub fn log(mut caller: Caller<'_, Self>, ptr: u32, len: u32) -> anyhow::Result<()> { + let memory = get_memory(&mut caller)?; + + let row_str = memory + .data(&caller) + .get(ptr as usize..(ptr + len) as usize) + .unwrap(); + + Ok(()) + } + + pub fn http(mut caller: Caller<'_, Self>, ptr: u32, len: u32) -> u64 { + let memory = get_memory(&mut caller).unwrap(); + + let str = memory + .data(&caller) + .get(ptr as usize..(ptr + len) as usize) + .unwrap(); + + let response = HTTPResponse { + headers: HashMap::new(), + body: "foo bar".into(), + status: 200, + }; + let err = HTTPError { + + }; + let encoded = to_vec(&err).unwrap(); + + let length = encoded.len(); + println!("return len: {length}, {:?}", encoded.to_vec()); + let (_, store) = memory.data_and_store_mut(&mut caller); + let index = store.buffers.insert(encoded.into_boxed_slice()); + + println!("returning 0, {length}, {index}"); + create_return_value(1, length as u32, index as u32) + } + + pub fn consume_buffer(mut caller: Caller<'_, Self>, index: u32, ptr: u32, len: u32) -> anyhow::Result<()> { + let memory = get_memory(&mut caller).unwrap(); + let (mut slice, store) = memory.data_and_store_mut(&mut caller); + let buffer = store.buffers.try_remove(index as usize).unwrap(); + anyhow::ensure!( + len as usize == buffer.len(), + "bad length passed to consume_buffer" + ); + slice.get_mut((ptr as usize)..((ptr+len) as usize)).unwrap().copy_from_slice(&buffer); + + Ok(()) + } +} + +impl wasmtime_wasi::WasiView for WasiHostCtx { + fn table(&mut self) -> &mut wasmtime::component::ResourceTable { + &mut self.preview2_table + } + + fn ctx(&mut self) -> &mut wasmtime_wasi::WasiCtx { + &mut self.preview2_ctx + } +} + +impl wasmtime_wasi::preview1::WasiPreview1View for WasiHostCtx { + fn adapter(&self) -> &wasmtime_wasi::preview1::WasiPreview1Adapter { + &self.preview1_adapter + } + + fn adapter_mut(&mut self) -> &mut wasmtime_wasi::preview1::WasiPreview1Adapter { + &mut self.preview1_adapter + } +} + #[derive(Clone)] -struct Instance { +pub struct Instance { engine: Engine, module: Module, - linker: Linker, + linker: Linker, +} + +pub fn get_memory(caller: &mut Caller<'_, T>) -> anyhow::Result { + Ok(caller.get_export("memory").unwrap().into_memory().unwrap()) } impl Instance { @@ -64,36 +172,98 @@ impl Instance { let module = Module::from_binary(&engine, &raw_module)?; let mut linker = Linker::new(&engine); - let get_row_count_type = wasmtime::FuncType::new(None, Some(wasmtime::ValType::I32)); - - let _ = linker.func_new_async( - "crows", - "hello", - get_row_count_type, - |_, _params, results| { - Box::new(async { - println!("Waiting 5s in rust async code"); - tokio::time::sleep(Duration::from_secs(5)).await; - println!("Finished waiting"); - results[0] = Val::I32(1111 as i32); - Ok(()) - }) - }, - )?; - linker.func_wrap("crows", "print", |param: i32| { - println!("Got value: {param}") - })?; - - wasmtime_wasi::tokio::add_to_linker(&mut linker, |cx| cx)?; + + linker.func_wrap("crows", "log", WasiHostCtx::log).unwrap(); + linker.func_wrap("crows", "consume_buffer", WasiHostCtx::consume_buffer).unwrap(); + linker + .func_wrap("crows", "http", WasiHostCtx::http) + .unwrap(); + // let _ = linker.func_new_async( + // "crows", + // "http_request", + // http_request_type, + // |_, _params, results| { + // Box::new(async { + // println!("Waiting 5s in rust async code"); + // tokio::time::sleep(Duration::from_secs(5)).await; + // println!("Finished waiting"); + // results[0] = Val::I32(1111 as i32); + // Ok(()) + // }) + // }, + // )?; + // linker.func_wrap("crows", "print", |param: i32| { + // println!("Got value: {param}") + // })?; + + wasmtime_wasi::preview1::add_to_linker_async(&mut linker)?; Ok(Self { engine, module, - linker, + linker: linker, }) } } +pub async fn run_wasm(instance: &Instance) -> anyhow::Result<()> { + let (sender, receiver) = std::sync::mpsc::channel(); + tokio::spawn(async move { + tokio::task::spawn_blocking(move || { + while let Ok(message) = receiver.recv() { + println!("stdout: {}", String::from_utf8(message).unwrap()); + } + }) + .await; + }); + + let stdout = RemoteStdout { sender }; + // let wasi = WasiCtxBuilder::new() + // . + // .stdout(stdout.clone()) + // Set an environment variable so the wasm knows its name. + // .env("NAME", &inputs.name)? + // .build(); + let wasi_ctx = wasmtime_wasi::WasiCtxBuilder::new() + .stdout(stdout.clone()) + .inherit_stdio() + .build(); + + let host_ctx = WasiHostCtx { + preview2_ctx: wasi_ctx, + preview2_table: wasmtime::component::ResourceTable::new(), + preview1_adapter: wasmtime_wasi::preview1::WasiPreview1Adapter::new(), + buffers: slab::Slab::default(), + memory: None, + }; + let mut store: Store = Store::new(&instance.engine, host_ctx); + + let memory = Memory::new(&mut store, MemoryType::new(1, None)).unwrap(); + store.data_mut().memory = Some(memory); + + // WebAssembly execution will be paused for an async yield every time it + // consumes 10000 fuel. Fuel will be refilled u64::MAX times. + store.fuel_async_yield_interval(Some(10000))?; + store.set_fuel(u64::MAX).unwrap(); + + // Instantiate into our own unique store using the shared linker, afterwards + // acquiring the `_start` function for the module and executing it. + let instance = instance + .linker + .instantiate_async(&mut store, &instance.module) + .await?; + + let func = instance + .clone() + .get_typed_func::<(), ()>(&mut store, "test")?; + + func.call_async(&mut store, ()).await?; + + drop(store); + + Ok(()) +} + #[derive(Clone)] struct RemoteStdout { sender: std::sync::mpsc::Sender>, @@ -120,3 +290,13 @@ impl WasiFile for RemoteStdout { Ok(size) } } + +impl StdoutStream for RemoteStdout { + fn stream(&self) -> Box { + todo!() + } + + fn isatty(&self) -> bool { + todo!() + } +} diff --git a/wasm/src/main.rs b/wasm/src/main.rs new file mode 100644 index 0000000..5c7101f --- /dev/null +++ b/wasm/src/main.rs @@ -0,0 +1,148 @@ +// use std::{any::Any, io::IoSlice, sync::Arc}; +// use tokio::time::{Duration, Instant}; +// use wasi_common::{ +// file::{FdFlags, FileType}, +// pipe::WritePipe, +// Table, +// }; +// use wasmtime::{Config, Engine, Linker, Module, Store, Val}; +// use wasmtime_wasi::{tokio::WasiCtxBuilder, WasiCtx, WasiFile}; + +use std::time::Instant; + +use crows_wasm::{run_wasm, Instance}; + +#[tokio::main] +async fn main() -> Result<(), anyhow::Error> { + let path = std::env::var("MODULE_PATH").expect("MODULE_PATH env var is not set"); + let content = std::fs::read(path).unwrap(); + + // let mut config = Config::new(); + // config.async_support(true); + // config.consume_fuel(true); + + // let engine = Engine::new(&config)?; + + // let module = Module::from_binary(&engine, &content)?; + + let env = Instance::new(content.clone())?; + let instant = Instant::now(); + for _ in 0..1 { + + run_wasm(&env).await?; + } + + println!("elapsed: {}ms", instant.elapsed().as_millis()); + + Ok(()) +} + +// struct Environment { +// engine: Engine, +// module: Module, +// linker: Arc>, +// } +// +// impl Environment { +// pub fn new(module: Module, engine: Engine) -> Result { +// let mut linker = Linker::new(&engine); +// let get_row_count_type = wasmtime::FuncType::new(None, Some(wasmtime::ValType::I32)); +// +// let _ = linker.func_new_async( +// "crows", +// "hello", +// get_row_count_type, +// |_, _params, results| { +// Box::new(async { +// println!("Waiting 5s in rust async code"); +// tokio::time::sleep(Duration::from_secs(5)).await; +// println!("Finished waiting"); +// results[0] = Val::I32(1111 as i32); +// Ok(()) +// }) +// }, +// )?; +// linker.func_wrap("crows", "print", |param: i32| { +// println!("Got value: {param}") +// })?; +// +// wasmtime_wasi::tokio::add_to_linker(&mut linker, |cx| cx)?; +// +// Ok(Self { +// engine, +// module, +// linker: Arc::new(linker), +// }) +// } +// } +// +// #[derive(Clone)] +// struct RemoteStdout { +// sender: std::sync::mpsc::Sender>, +// } +// +// #[wiggle::async_trait] +// impl WasiFile for RemoteStdout { +// fn as_any(&self) -> &dyn Any { +// self +// } +// async fn get_filetype(&self) -> Result { +// Ok(FileType::Pipe) +// } +// async fn get_fdflags(&self) -> Result { +// Ok(FdFlags::APPEND) +// } +// async fn write_vectored<'a>(&self, bufs: &[IoSlice<'a>]) -> Result { +// let mut size: u64 = 0; +// for slice in bufs { +// let slice = slice.to_vec(); +// size += slice.len() as u64; +// self.sender.send(slice); +// } +// Ok(size) +// } +// } +// +// async fn run_wasm(env: &Environment) -> Result<(), anyhow::Error> { +// let (sender, receiver) = std::sync::mpsc::channel(); +// tokio::spawn(async move { +// tokio::task::spawn_blocking(move || { +// while let Ok(message) = receiver.recv() { +// println!("stdout: {}", String::from_utf8(message).unwrap()); +// } +// }) +// .await; +// }); +// +// let stdout = RemoteStdout { sender }; +// let wasi = WasiCtxBuilder::new() +// .stdout(Box::new(stdout.clone())) +// // Set an environment variable so the wasm knows its name. +// // .env("NAME", &inputs.name)? +// .build(); +// let mut store = Store::new(&env.engine, wasi); +// +// // WebAssembly execution will be paused for an async yield every time it +// // consumes 10000 fuel. Fuel will be refilled u64::MAX times. +// store.fuel_async_yield_interval(Some(10000))?; +// +// // Instantiate into our own unique store using the shared linker, afterwards +// // acquiring the `_start` function for the module and executing it. +// let instance = env +// .linker +// .instantiate_async(&mut store, &env.module) +// .await?; +// +// let func = instance +// .clone() +// .get_typed_func::<(), ()>(&mut store, "test")?; +// +// func.call_async(&mut store, ()).await?; +// +// drop(store); +// +// Ok(()) +// } + + + diff --git a/worker/Cargo.toml b/worker/Cargo.toml index b84541d..856acac 100644 --- a/worker/Cargo.toml +++ b/worker/Cargo.toml @@ -10,6 +10,7 @@ crows-utils = { path = "../utils" } byteorder = "1.4" crows-service = { path = "../service" } crows-wasm = { path = "../wasm" } +num_cpus = "1.16" uuid.workspace = true serde.workspace = true @@ -21,3 +22,4 @@ tokio-serde.workspace = true tokio-util.workspace = true futures.workspace = true wasmtime.workspace = true +num-rational.workspace = true diff --git a/worker/src/main.rs b/worker/src/main.rs index 433c5de..087dfe8 100644 --- a/worker/src/main.rs +++ b/worker/src/main.rs @@ -1,74 +1,78 @@ #![feature(async_fn_in_trait)] #![feature(return_position_impl_trait_in_trait)] -use std::{collections::HashMap, env::args_os, time::Duration}; +use crows_wasm::Runtime; use serde::{Deserialize, Serialize}; +use std::sync::Arc; +use std::{collections::HashMap, env::args_os, time::Duration}; +use tokio::sync::{RwLock, Mutex}; use tokio::time::sleep; use uuid::Uuid; -use crows_utils::services::{connect_to_worker_to_coordinator, Worker, WorkerData, WorkerError}; +use crows_utils::services::{connect_to_worker_to_coordinator, Worker, WorkerData, WorkerError, RunId}; +use crows_utils::ModuleId; +use num_rational::Rational64; + +type ScenariosList = Arc>>>; + +// TODO: in the future we should probably share it with the coordinator, ie. +// coordinator should prepare the defaults based on the default module settings +// by examining the module +struct RunInfo { + run_id: RunId, + concurrency: usize, + rate: Rational64, + module_id: ModuleId, +} + +impl RunInfo { + fn new(run_id: RunId, concurrency: usize, rate: Rational64, module_id: ModuleId) -> Self { + Self { run_id, concurrency, rate, module_id } + } +} -#[derive(Serialize, Deserialize, Clone)] +#[derive(Clone)] struct WorkerService { - scenarios: HashMap>, + scenarios: ScenariosList, hostname: String, + wasm_handles: Arc>>, + runs: HashMap } impl Worker for WorkerService { - async fn upload_scenario(&mut self, name: String, content: Vec) { - todo!() - // self.scenarios.insert(name, content); + async fn upload_scenario(&mut self, id: ModuleId, content: Vec) { + self.scenarios.write().await.insert(id, content); } async fn ping(&self) -> String { todo!() } - async fn start(&self, name: String, concurrency: usize) -> Result<(), WorkerError> { - todo!() - // let scenario = self - // .scenarios - // .get(&name) - // .ok_or(WorkerError::ScenarioNotFound)? - // .clone(); - // - // let args = (scenario, name, concurrency); - // let _process = spawn!(|args, mailbox: Mailbox<()>| { - // let (scenario, name, concurrency) = args; - // - // println!("Running {name} scenario with {concurrency} concurrency."); - // - // let module = WasmModule::new(&scenario).unwrap(); - // let monitorable = mailbox.monitorable(); - // let mut processes = Vec::new(); - // for _ in 0..concurrency { - // match module.spawn::<(), Bincode>("_start", &[]) { - // Ok(process) => { - // processes.push(process.id()); - // monitorable.monitor(process); - // } - // Err(e) => { - // println!("Could not start process {name}: {e:?}"); - // } - // } - // } - // - // loop { - // match monitorable.receive() { - // MessageSignal::Signal(ProcessDiedSignal(id)) => { - // if let Some(index) = processes.iter().position(|x| *x == id) { - // processes.remove(index); - // if processes.is_empty() { - // break; - // } - // } - // } - // MessageSignal::Message(_) => {} - // } - // } - // }); - // - // Ok(()) + async fn prepare(&mut self, id: ModuleId, concurrency: usize, rate: Rational64) -> Result { + let run_id: RunId = RunId::new(); + + // TODO: we should check if we have a given module available and if not ask coordinator + // to send it. For now let's assume we have the module id + let info = RunInfo::new(run_id.clone(), concurrency, rate, id); + self.runs.insert(run_id, info); + + Ok(run_id) + } + + async fn start(&self, run_id: RunId) -> Result<(), WorkerError> { + let locked = self + .scenarios + .read() + .await; + let scenario = locked + .get(&id) + .ok_or(WorkerError::ScenarioNotFound)? + .clone(); + drop(locked); + + // spawn modules + + Ok(()) } async fn get_data(&self) -> WorkerData { @@ -79,27 +83,81 @@ impl Worker for WorkerService { } } -#[tokio::main] -async fn main() { - let coordinator_address: String = std::env::var("COORDINATOR_ADDRESS") - .unwrap(); - let hostname: String = std::env::var("WORKER_NAME") - .unwrap(); +#[derive(Clone)] +struct RuntimeHandle { + runtime: Arc>, +} + +impl RuntimeHandle { + pub fn new(runtime: Runtime) -> Self { + Self { runtime: Arc::new(Mutex::new(runtime)) } + } +} + +fn main() -> Result<(), Box> { + // TODO: allow to set the number of CPUs + let cpus = num_cpus::get(); + let coordinator_address: String = std::env::var("COORDINATOR_ADDRESS").unwrap(); + let hostname: String = std::env::var("WORKER_NAME").unwrap(); println!("Starting with hostname: {hostname}"); - let scenarios: HashMap> = Default::default(); + let handles: Arc>> = Default::default(); + let scenarios: ScenariosList = Default::default(); let service = WorkerService { - scenarios, + scenarios: scenarios.clone(), hostname, + wasm_handles: handles.clone(), + runs: Default::default() }; - println!("Connecting to {coordinator_address}"); - let mut client = - connect_to_worker_to_coordinator(coordinator_address, service).await.unwrap(); + std::thread::scope(|s| { + let mut threads = Vec::new(); + + let thread = s.spawn(move || { + let rt = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + let runtime = RuntimeHandle::new(Runtime::new()); + + rt.spawn(async move { + println!("Connecting to {coordinator_address}"); + let mut client = connect_to_worker_to_coordinator(coordinator_address, service) + .await + .unwrap(); + + loop { + // TODO: pinging should also work as an indicator of connection being alive + client.ping(); + sleep(Duration::from_secs(1)); + } + }); + }); + + threads.push(thread); + + for _ in (0..cpus).into_iter() { + let scenarios = scenarios.clone(); + let handles = handles.clone(); + let thread = s.spawn(move || { + let rt = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + let wasm_runtime = RuntimeHandle::new(Runtime::new()); + + rt.spawn(async move { + handles.lock().await.push(wasm_runtime.clone()); + }); + }); + threads.push(thread); + } + + for thread in threads { + thread.join(); + } + }); - loop { - client.ping(); - sleep(Duration::from_secs(1)); - } + Ok(()) }